FileZilla 源代码分析7

系统 Linux
FileZilla是一个免费开源的FTP客户端软件,分为客户端版本和服务器版本,具备所有的FTP软件功能。可控性、有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP客户端工具,而FileZilla Server则是一个小巧并且可靠的支持FTP&SFTP的FTP服务器软件。

FileZilla是一种快速、可信赖的FTP客户端以及服务器端开放源代码程式,具有多种特色、直觉的接口。本文就给大家分析下FileZilla的源代码。

  服务线程苏醒后,调用OnThreadMessage来处理这个WM_FILEZILLA_THREADMSG消息,参数是FTM_NEWSOCKET, sockethandle,接着进入AddNewSocket方法,表示有一个新的客户端需要连接上来。

  void CServerThread::AddNewSocket(SOCKET sockethandle, bool ssl)

  {

  // 首先创建了新的CControlSocket类,这也是继承于CAsyncSocketEx类的,从下面起经过一些初始化之后,就由这个CControlSocket类来接管这个客户连接了。

  CControlSocket *socket = new CControlSocket(this);

  socket->Attach(sockethandle); // 加入到FileZilla消息机制中, 建立与分发线程等之间的关系

  CStdString ip;

  unsigned int port;

  SOCKADDR_IN sockAddr;

  memset(&sockAddr, 0, sizeof(sockAddr));

  int nSockAddrLen = sizeof(sockAddr);

  BOOL bResult = socket->GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen); // 获取socket客户端的信息

  if (bResult)

  {

  port = ntohs(sockAddr.sin_port); // 端口

  ip = inet_ntoa(sockAddr.sin_addr); // IP地址

  }

  else

  {

  socket->m_RemoteIP = _T("ip unknown");

  socket->m_userid = 0;

  socket->SendStatus(_T("Can't get remote IP, disconnected"), 1);

  socket->Close();

  delete socket;

  return;

  }

  socket->m_RemoteIP= ip;

  EnterCritSection(m_GlobalThreadsync);

  int userid = CalcUserID(); // 自动为当前socket连接生成一个客户号:userID

  if (userid == -1)

  {

  LeaveCritSection(m_GlobalThreadsync);

  socket->m_userid = 0;

  socket->SendStatus(_T("Refusing connection, server too busy!"), 1);

  socket->Send(_T("421 Server too busy, closing connection. Please retry later!"));

  socket->Close();

  delete socket;

  return;

  }

  socket->m_userid = userid;

  t_socketdata data;

  data.pSocket = socket;

  data.pThread = this;

  m_userids[userid] = data; // m_userids是static的,定义为static std::map m_userids;

  // hammering这块可以先不管

  // Check if remote IP is blocked due to hammering

  std::map::iterator iter = m_antiHammerInfo.find(sockAddr.sin_addr.s_addr);

  if (iter != m_antiHammerInfo.end())

  {

  if (iter->second > 10)

  socket->AntiHammerIncrease(25); // ~6 secs delay

  }

  LeaveCritSection(m_GlobalThreadsync);

  EnterCritSection(m_threadsync);

  // 下面记录这个服务线程所处理的CControlSocket

  m_LocalUserIDs[userid] = socket;

  LeaveCritSection(m_threadsync);

  t_connectiondata_add *conndata = new t_connectiondata_add;

  t_connop *op = new t_connop;

  op->data = conndata;

  op->op = USERCONTROL_CONNOP_ADD; // 新用户连接即将连接,在CServer的OnServerMessage中要用过

  op->userid = userid;

  conndata->pThread = this;

  memset(&sockAddr, 0, sizeof(sockAddr));

  nSockAddrLen = sizeof(sockAddr);

  bResult = socket->GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen);

  if (bResult)

  {

  conndata->port = ntohs(sockAddr.sin_port);

  #ifdef _UNICODE

  _tcscpy(conndata->ip, ConvFromLocal(inet_ntoa(sockAddr.sin_addr))); // 拷贝字符串

  #else

  _tcscpy(conndata->ip, inet_ntoa(sockAddr.sin_addr));

  #endif

  }

  // 这里往全局的hMainWnd发送消息,

  // 消息的wParam类型为FSM_CONNECTIONDATA, 指示消息是跟connection相关的消息,参数是t_connop

  // 这些在CServer的WindowProc中处理这个消息时用到,这个消息处理结束后,会在admin窗口的下边显示

  // 类似000001 (not logged in) 127.0.0.1 的信息

  SendNotification(FSM_CONNECTIONDATA, (LPARAM)op);

  if (ssl) // SSL相关, 可以先跳过

  if (!socket->InitImplicitSsl())

  return;

  socket->AsyncSelect(FD_READ|FD_WRITE|FD_CLOSE); // 对socket上这些event建立侦听关系

  // SendStatus最终还是调用SendNotification方法,不过发送的参数是FSM_STATUSMESSAGE,

  // 因此在CServer中的处理并不一样

  socket->SendStatus(_T("Connected, sending welcome message..."), 0);

  // 这时,admin窗口的下半部分会显示类似(000003) 2006-8-24 3:26:47 - (not logged in) (127.0.0.1)> Connected, sending welcome message...

  // 下面格式化欢迎信息

  CStdString msg = m_pOptions->GetOption(OPTION_WELCOMEMESSAGE);

  if (m_RawWelcomeMessage != msg)

  {

  m_RawWelcomeMessage = msg;

  m_ParsedWelcomeMessage.clear();

  msg.Replace(_T("%%"), _T("\001"));

  msg.Replace(_T("%v"), GetVersionString());

  msg.Replace(_T("\001"), _T("%"));

  ASSERT(msg != _T(""));

  int oldpos = 0;

  msg.Replace(_T("\r\n"), _T("\n"));

  int pos=msg.Find(_T("\n"));

  CStdString line;

  while (pos!=-1)

  {

  ASSERT(pos);

  m_ParsedWelcomeMessage.push_back(_T("220-") + msg.Mid(oldpos, pos-oldpos) );

  oldpos=pos + 1;

  pos=msg.Find(_T("\n"), oldpos);

  }

  line = msg.Mid(oldpos);

  if (line != _T(""))

  m_ParsedWelcomeMessage.push_back(_T("220 ") + line);

  else

  {

  m_ParsedWelcomeMessage.back()[3] = 0;

  }

  }

  // hideStatus指示这个欢迎消息要不要发给admin port

  bool hideStatus = m_pOptions->GetOptionVal(OPTION_WELCOMEMESSAGE_HIDE) != 0;

  ASSERT(!m_ParsedWelcomeMessage.empty());

  for (std::list::iterator iter = m_ParsedWelcomeMessage.begin(); iter != m_ParsedWelcomeMessage.end(); iter++)

  // 发送给socket客户, 并且发送消息到admin port上,发送的参数是FSM_STATUSMESSAGE

  if (!socket->Send(*iter, !hideStatus))

  break;

  // 运行到这里,客户的登录界面上、admin窗口上半部已经出现了welcome信息,类似:

  // (000003) 2006-8-24 3:27:19 - (not logged in) (127.0.0.1)> 220-FileZilla Server version 0.9.18 beta

  // ((000003) 2006-8-24 3:27:22 - (not logged in) (127.0.0.1)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)

  // ((000003) 2006-8-24 3:27:29 - (not logged in) (127.0.0.1)> 220 Please visit http://sourceforge.net/projects/filezilla/

通过文章完整的描述,大家应该知道了FileZilla 源代码,希望对大家有帮助!

【编辑推荐】

  1. FileZilla 源代码分析1
  2. FileZilla 源代码分析2
  3. FileZilla 源代码分析3
  4. FileZilla 源代码分析4
  5. FileZilla 源代码分析5
  6. FileZilla 源代码分析6
  7. FileZilla简单介绍
责任编辑:赵鹏 来源: 网络转载
相关推荐

2011-02-23 14:46:21

FileZilla

2011-02-23 14:39:27

FileZilla

2011-02-23 14:16:43

FileZilla

2011-02-23 14:54:58

FileZilla

2011-02-23 15:33:42

FileZilla

2011-02-23 15:21:06

FileZilla

2011-02-23 15:26:01

FileZilla

2011-02-23 13:47:33

FileZilla

2011-02-23 14:26:28

FileZilla

2011-03-01 16:32:58

FileZilla

2011-03-01 16:01:08

FileZilla

2011-03-01 16:19:27

FileZilla

2011-03-01 16:25:37

FileZilla

2015-08-28 09:38:51

Linux源代码分析工具

2018-05-25 14:16:55

NFS源代码线程

2012-07-04 10:18:10

Tomcat调试代码分析

2015-08-26 17:38:47

Linux源代码

2009-07-02 13:59:35

JSP后台

2011-08-24 15:42:38

LUA源代码

2020-07-10 13:58:18

Windows 7微软软件
点赞
收藏

51CTO技术栈公众号