/********************************************* Finger Performs the finger for the specified address the information that is returned can be retrieved with the GetReturnLine function. If filename parameter was specified all the received data is saved in the file. In this case you can't access data with GetReturnLine function. Params address - address to perform the finger on. address format is: @domain.com or [email protected] dest - data source for saving received data [fileType] - -1 - don't use UTM_OM_WRITING - open writing UTM_OM_APPEND - open for appending Return UTE_SUCCESS - success UTE_INVALID_ADDRESS_FORMAT - invalid address format UTE_INVALID_ADDRESS - invalid address UTE_CONNECT_FAILED - connection failed UTE_NO_RESPONSE - no response UTE_ABORTED - aborted UTE_CONNECT_TIMEOUT - time out **********************************************/ int CUT_FingerClient::Finger(LPTSTR address, CUT_DataSource & dest, OpenMsgType fileType) { int error = UTE_SUCCESS; int len; char buf[MAX_PATH+1]; char domain[MAX_PATH+1]; //delete prev finger information m_listReturnLines.ClearList(); //split the address apart // v4.2 address splitting into name and domain allows AC macro, hence LPTSTR in interface. if(SplitAddress(buf,domain,AC(address)) != UTE_SUCCESS) return OnError(UTE_INVALID_ADDRESS_FORMAT); //check to see if the domain is a name or address if(IsIPAddress(domain) != TRUE) { //get the name from the address if(GetAddressFromName(domain,domain,sizeof(domain)) != UTE_SUCCESS) return OnError(UTE_INVALID_ADDRESS); } //connect using a timeout if((error=Connect(m_nPort, domain, m_nConnectTimeout)) != UTE_SUCCESS) return OnError(error); if(IsAborted()) // Test abort flag error = UTE_ABORTED; // Aborted if(error == UTE_SUCCESS) { //send the name to finger Send(buf); Send("\r\n"); // Read data into the file if(fileType != -1) { error = Receive(dest, fileType, m_nReceiveTimeout); } // Read data line by line & save it in the string list else { //read in the return lines // v4.2 change to eliminate C4127:conditional expression is constant for(;;) { if(IsAborted()) { // Test abort flag error = UTE_ABORTED; // Aborted break; } //wait for a receive if(WaitForReceive(m_nReceiveTimeout, 0)!= UTE_SUCCESS) break; //get the information len = ReceiveLine(buf, sizeof(buf)-1); if(len <= 0) break; buf[len]=0; CUT_StrMethods::RemoveCRLF(buf); //store the information m_listReturnLines.AddString(buf); } if (error == UTE_SUCCESS && m_listReturnLines.GetCount() == 0) error = UTE_NO_RESPONSE; // No response } } // Close connection CloseConnection(); return OnError(error); }
bool wxTCPServer::Create(const wxString& serverName) { // Destroy previous server, if any if (m_server) { m_server->SetClientData(NULL); m_server->Destroy(); m_server = NULL; } wxSockAddress *addr = GetAddressFromName(serverName); if ( !addr ) return false; #ifdef __UNIX_LIKE__ mode_t umaskOld; if ( addr->Type() == wxSockAddress::UNIX ) { // ensure that the file doesn't exist as otherwise calling socket() would // fail int rc = remove(serverName.fn_str()); if ( rc < 0 && errno != ENOENT ) { delete addr; return false; } // also set the umask to prevent the others from reading our file umaskOld = umask(077); } else { // unused anyhow but shut down the compiler warnings umaskOld = 0; } #endif // __UNIX_LIKE__ // Create a socket listening on the specified port m_server = new wxSocketServer(*addr, SCKIPC_FLAGS); #ifdef __UNIX_LIKE__ if ( addr->Type() == wxSockAddress::UNIX ) { // restore the umask umask(umaskOld); // save the file name to remove it later m_filename = serverName; } #endif // __UNIX_LIKE__ delete addr; if (!m_server->Ok()) { m_server->Destroy(); m_server = NULL; return false; } m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID); m_server->SetClientData(this); m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); m_server->Notify(true); return true; }
/*********************************************** Connect Connects to a specified port Params port - port to connect to address - address to connect to (ex."204.64.75.73") [timeout] - time to wait for connection [family] - protocol family: AF_INET, AF_AF_IPX, etc. Default AF_INET [sockType] - SOCK_STREAM (TCP) or SOCK_DGRAM (UDP) Default is SOCK_STREAM Return UTE_SOCK_ALREADY_OPEN - socket already open or in use UTE_SOCK_CREATE_FAILED - socket creation failed UTE_SOCK_CONNECT_FAILED - socket connection failed UTE_INVALID_ADDRESS - invalid address UTE_SUCCESS - success UTE_CONNECT_TIMEOUT - connect time out ************************************************/ int CUT_WSThread::Connect(unsigned int port, LPCSTR address, long timeout, int family, int sockType) { int nError = UTE_SUCCESS; if(m_clientSocket != INVALID_SOCKET) return OnError(UTE_SOCK_ALREADY_OPEN); //copy the params //check to see if the domain is a name or address if (inet_addr(address) == INADDR_NONE){ if(GetAddressFromName(address, m_szAddress, sizeof(m_szAddress)) != UTE_SUCCESS) return OnError(UTE_INVALID_ADDRESS); } else strncpy(m_szAddress, address, sizeof(m_szAddress)); //m_nFamily = family; m_nSockType = sockType; //Set up the SockAddr structure memset(&m_clientSocketAddr, 0, sizeof(m_clientSocketAddr)); //clear all m_clientSocketAddr.sin_port = (unsigned short) htons((u_short )port); //port m_clientSocketAddr.sin_family = (short) family; //family m_clientSocketAddr.sin_addr.s_addr = inet_addr(m_szAddress); //address //create a socket m_clientSocket = socket(family, sockType, 0); if(m_clientSocket == INVALID_SOCKET) return OnError(UTE_SOCK_CREATE_FAILED); // switch to nonblocking mode to support timeout if(timeout >= 0) SetBlockingMode(CUT_NONBLOCKING); if( connect(m_clientSocket,(LPSOCKADDR)&m_clientSocketAddr,sizeof(m_clientSocketAddr))==SOCKET_ERROR){ if(WSAGetLastError() == WSAEWOULDBLOCK ) { if(timeout >= 0) { if(WaitForSend(timeout, 0) != CUT_SUCCESS) { SocketClose(m_clientSocket); m_clientSocket = INVALID_SOCKET; return OnError(UTE_CONNECT_TIMEOUT); } SetBlockingMode(CUT_BLOCKING); } //set up the default send are receive time-outs // SetReceiveTimeOut(m_lRecvTimeOut); // SetSendTimeOut(m_lSendTimeOut); // save the remote port // m_nRemotePort = ntohs(m_clientSocketAddr.sin_port); // save the local port SOCKADDR_IN sa; int len = sizeof(SOCKADDR_IN); getsockname(m_clientSocket, (SOCKADDR*) &sa, &len); // m_nLocalPort = ntohs(sa.sin_port); // Call socket connection notification if((nError = SocketOnConnected(m_clientSocket, address)) != UTE_SUCCESS) { SocketClose(m_clientSocket); m_clientSocket = INVALID_SOCKET; } return OnError(nError); } else { // did not have these two lines prior to Feb 11 1999 SocketClose(m_clientSocket); m_clientSocket = INVALID_SOCKET; } return OnError(UTE_SOCK_CONNECT_FAILED); // ERROR: connect unsuccessful } // set up the default send are receive time-outs // SetReceiveTimeOut(m_lRecvTimeOut); SetSendTimeOut(m_lSendTimeOut); // Call socket connection notification if((nError = SocketOnConnected(m_clientSocket, address)) != UTE_SUCCESS) { SocketClose(m_clientSocket); m_clientSocket = INVALID_SOCKET; } return OnError(nError); }
wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, const wxString& serverName, const wxString& topic) { wxSockAddress *addr = GetAddressFromName(serverName, host); if ( !addr ) return NULL; wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); wxSocketStream *stream = new wxSocketStream(*client); wxDataInputStream *data_is = new wxDataInputStream(*stream); wxDataOutputStream *data_os = new wxDataOutputStream(*stream); bool ok = client->Connect(*addr); delete addr; if ( ok ) { unsigned char msg; // Send topic name, and enquire whether this has succeeded data_os->Write8(IPC_CONNECT); data_os->WriteString(topic); msg = data_is->Read8(); // OK! Confirmation. if (msg == IPC_CONNECT) { wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection (); if (connection) { if (connection->IsKindOf(CLASSINFO(wxTCPConnection))) { connection->m_topic = topic; connection->m_sock = client; connection->m_sockstrm = stream; connection->m_codeci = data_is; connection->m_codeco = data_os; client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID); client->SetClientData(connection); client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); client->Notify(true); return connection; } else { delete connection; // and fall through to delete everything else } } } } // Something went wrong, delete everything delete data_is; delete data_os; delete stream; client->Destroy(); return NULL; }