// 密码采用编码结果 // Validate函数的密码采用明文 bool ZSmtp::ValidateBase64(const string Username,const string Password) { ZBase64 base64; //发送"AUTH LOGIN" if(send(m_SOCKET,"AUTH LOGIN\r\n",strlen("AUTH LOGIN\r\n"),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("334")) return false; //发送经base64编码的用户名 string strUserName=base64.Encode((unsigned char *)Username.c_str(),Username.length())+"\r\n"; if(send(m_SOCKET,strUserName.c_str(),strUserName.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("334")) return false; //发送经base64编码的密码 //string strPassword=base64.Encode((unsigned char *)Password.c_str(),Password.length())+"\r\n"; string strPassword = Password + "\r\n"; if(send(m_SOCKET,strPassword.c_str(),strPassword.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("235")) return false; return true; }
BOOL S3PDBSocketServer::Stop() { BOOL bRet = FALSE; if ( NULL == m_hServer ) { bRet = FALSE; return bRet; } if ( NULL != m_pSocketPool ) { if ( FALSE == m_pSocketPool->Stop() ) { bRet = FALSE; return bRet; } } *m_piRunSignal = 0; DWORD dwResult = KPIWaitForSingleObject( m_hServer, 3000 ); if ( 0 == dwResult ) { bRet = FALSE; return bRet; } else if ( 1 == dwResult ) // The thread has ended. { m_hServer = NULL; bRet = ReleaseSocket(); } else if ( 2 == dwResult ) // Time out. { if ( TRUE == KPITerminateThread( m_hServer, 0 ) ) { m_hServer = NULL; bRet = ReleaseSocket(); } else { bRet = FALSE; return bRet; } } if ( TRUE == bRet ) { WSACleanup(); // Needed on Windows OS platforms. } return bRet; }
void CSocketServer::ReleaseSockets() { CCriticalSection::Owner lock( m_listManipulationSection ); Socket *pSocket = m_activeList.Head(); while ( pSocket ) { Socket *pNext = SocketList::Next( pSocket ); pSocket->Close(); pSocket = pNext; } while ( m_activeList.Head() ) { ReleaseSocket( m_activeList.Head() ); } while ( m_freeList.Head() ) { DestroySocket( m_freeList.PopNode() ); } if ( m_freeList.Count() + m_freeList.Count() != 0 ) { /* * call to unqualified virtual function */ OnError( _T("CSocketServer::ReleaseSockets() - Leaked sockets") ); } }
//---------------------------------------------------------------------------- // Sets a data socket to read/write file streams // int CSTATEngine::SetSocket(const char *ip, const int port) { //initialise winsock WSAStartup( MAKEWORD(2,2), &wsaData ); dataSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( dataSocket == INVALID_SOCKET ) { dataSocket=NULL; return E_SOCKETCREATE; } // Connect to a server. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( ip ); clientService.sin_port = htons( port ); if ( connect( dataSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { ReleaseSocket(); return E_SOCKETCONNECT; } else { return ITS_OK; } }
bool ZSmtp::Connect(const string SmtpAddr,const int Port) { if(!CreateSocket()) return false; if((m_HOSTENT=gethostbyname(SmtpAddr.c_str()))==NULL) { ReleaseSocket(); return false; } if(m_HOSTENT->h_addr_list[0] == NULL) { ReleaseSocket(); return false; } memset(&m_SOCKADDR_IN,0,sizeof(m_SOCKADDR_IN)); m_SOCKADDR_IN.sin_family = AF_INET; m_SOCKADDR_IN.sin_port = htons(Port); m_SOCKADDR_IN.sin_addr.S_un.S_addr = *(ULONG *)m_HOSTENT->h_addr_list[0]; //连接服务器 if(connect(m_SOCKET,(sockaddr *)&m_SOCKADDR_IN,sizeof(m_SOCKADDR_IN)) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("220")) return false; //向服务器发送"HELO "+服务器名 string strTmp="HELO "+SmtpAddr+"\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("250")) return false; return true; }
FormosaExit() { int fd = getdtablesize(); while (fd) (void) close(--fd); if (ifPass) user_logout(cutmp, &curuser); ReleaseSocket(); exit(0); }
void TimeOut(int s) { if (ifPass) { #if 1 if (uinfo.active < 1 || uinfo.active > MAXACTIVE) csbbslog("ERR", "%s TimeOut user_logout: active[%d]", uinfo.userid, uinfo.active); #endif user_logout(cutmp, &curuser); } ReleaseSocket(); exit(1); }
bool ZSmtp::CreateSocket() { if(WSAStartup(MAKEWORD(2,2),&m_WSADATA) == SOCKET_ERROR) { ReleaseSocket(); return FALSE; } if((m_SOCKET = socket(AF_INET,SOCK_STREAM,0)) == SOCKET_ERROR) { ReleaseSocket(); return FALSE; } // int time = 10000; // if(setsockopt(m_SOCKET,SOL_SOCKET,SO_RCVTIMEO,(char *)&time,sizeof(int)) == SOCKET_ERROR) // { // ReleaseSocket(); // return FALSE; // } return TRUE; }
void destroy_acceptor(acceptor_t *a) { close((*a)->poller_fd); list_node *_st = link_list_head((*a)->st_listens); while(_st) { struct st_listen *tmp = (struct st_listen *)_st; _st = _st->next; ReleaseSocket(tmp->sock); free(tmp); } LINK_LIST_DESTROY(&((*a)->st_listens)); free(*a); *a = NULL; }
SOCK add_listener(acceptor_t a,const char *ip,uint32_t port,on_accept call_back,void *ud) { if(!a) return INVALID_SOCK; SOCK ListenSocket; struct sockaddr_in servaddr; ListenSocket = Tcp_Listen(ip,port,&servaddr,256); if(ListenSocket != INVALID_SOCK) { struct st_listen *_st = (struct st_listen *)calloc(1,sizeof(*_st)); _st->accept_callback = call_back; _st->ud = ud; _st->sock = ListenSocket; _st->real_fd = GetSocketByHandle(ListenSocket)->fd; int32_t ret = -1; struct epoll_event ev; ev.data.ptr = _st; ev.events = EV_IN; TEMP_FAILURE_RETRY(ret = epoll_ctl(a->poller_fd,EPOLL_CTL_ADD,_st->real_fd,&ev)); if(ret == -1) { ReleaseSocket(ListenSocket); printf("listen %s:%d error\n",ip,port); return INVALID_SOCK; } LINK_LIST_PUSH_BACK(a->st_listens,_st); return ListenSocket; } else { ReleaseSocket(ListenSocket); printf("listen %s:%d error\n",ip,port); return INVALID_SOCK; } }
H_CONNECTION KUVSocket::Listen(LPCSTR szAddress, UINT nPort) { H_CONNECTION ret = INVALID_HANDLER; if (!szAddress) { sprintf_s(g_ErrorMsg, "KUVSocket::Listen Fail, the address is empty\n%s", g_ErrorMsg); return ret; } /*hostent* host = gethostbyname(szAddress); if (!host) { sprintf_s(ErrorMsg, "KUVSocket::Listen Fail, get host Error, the szAddress is %s", szAddress); return ret; } struct sockaddr_in addrBind; addrBind.sin_family = AF_INET; memcpy(&addrBind.sin_addr, host->h_addr_list[0], sizeof(in_addr)); addrBind.sin_port = htons(nPort);*/ struct sockaddr_in addrBind = uv_ip4_addr(szAddress, nPort); uv_tcp_t* pListen = NULL; pListen = CreateSocket(ret); if (!pListen) { sprintf_s(g_ErrorMsg, "KUVSocket::Listen Fail, create socket Error\n%s", g_ErrorMsg); return ret; } m_nError = uv_tcp_bind(pListen, addrBind); if (m_nError) { ReleaseSocket(ret); return ret; } m_nError = uv_listen((uv_stream_t*)pListen, SOMAXCONN, OnConnectionIncoming); if (m_nError) { return ret; } return ret; }
void rem_listener(acceptor_t a,SOCK l) { if(a) { struct epoll_event ev;int32_t ret; TEMP_FAILURE_RETRY(ret = epoll_ctl(a->poller_fd,EPOLL_CTL_DEL,GetSocketByHandle(l)->fd,&ev)); if(ret == 0) { int32_t count = link_list_size(a->st_listens); while(count>0) { struct st_listen *_st = LINK_LIST_POP(struct st_listen *,a->st_listens); if(_st->sock == l) { ReleaseSocket(l); free(_st); break; } else LINK_LIST_PUSH_BACK(a->st_listens,_st); --count; }; } }
bool ZSmtp::SendData(const string SendFrom, // 发送地址 const deque<string> SendToList, // 目地地址列表 const string SenderName, // 发送者姓名 const string ReceiverName, // 接收者姓名 const string Subject, // 邮件主题 const string Content, // 邮件内容 const deque<string> Files, // 附件列表 bool IsHtml) // 是否以HTML格式发送 { if(SendFrom.empty()) return false; if(SendToList.size()<=0) return false; string strTmp; ZBase64 base64; //发送MAIL FROM:<*****@*****.**> strTmp="MAIL FROM:<"+SendFrom+">\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("250")) return false; //发送RCPT To:<*****@*****.**> for(int i=0;i<SendToList.size();i++) { strTmp="RCPT To:<"+SendToList[i]+">\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("250")) return false; } //发送"DATA\r\n" if(send(m_SOCKET,"DATA\r\n",strlen("DATA\r\n"),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("354")) return false; //"Mail From:SenderName<*****@*****.**>\r\n" strTmp="From:"+SenderName+"<"+SendFrom+">\r\n"; //"Subject: 邮件主题\r\n" strTmp+="Subject:"+Subject+"\r\n"; //"MIME_Version:1.0\r\n" strTmp+="MIME_Version:1.0\r\n"; //"X-Mailer:Smtp Client By xxx"//版权信息 strTmp+="X-Mailer:"; strTmp+=COPYRIGHT; strTmp+="\r\n"; //"MIME_Version:1.0\r\n" strTmp+="MIME_Version:1.0\r\n"; //"Content-type:multipart/mixed;Boundary=xxx\r\n\r\n"; strTmp+="Content-type:multipart/mixed;Boundary="; strTmp+=BOUNDARY; strTmp+="\r\n\r\n"; //先将HEADER部分发送过去 if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } //邮件主体 strTmp="--"; strTmp+=BOUNDARY; strTmp+="\r\n"; strTmp+=IsHtml ? "Content-type:text/html;Charset=gb2312\r\n" : "Content-type:text/plain;Charset=gb2312\r\n"; strTmp+="Content-Transfer-Encoding:8bit\r\n\r\n"; //邮件内容 strTmp+=Content+"\r\n\r\n"; //将邮件内容发送出去 if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } /* 附件处理 对文件内容分段进行base64编码,编码一小段,传送一小段 */ for(int i=0;i<Files.size();i++) { strTmp="--"; strTmp+=BOUNDARY; strTmp+="\r\n"; strTmp+="Content-Type:application/octet-stream;Name="; strTmp+=Files[i].substr(Files[i].find_last_of("\\")+1).c_str(); strTmp+="\r\n"; strTmp+="Content-Disposition:attachment;FileName="; strTmp+=Files[i].substr(Files[i].find_last_of("\\")+1).c_str(); strTmp+="\r\n"; strTmp+="Content-Transfer-Encoding:Base64\r\n\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } FILE* fin=fopen(Files[i].c_str(),"rb"); char* Buf=new char[BUFFER_READ+1]; memset(Buf,0,BUFFER_READ+1); int ReadSize=fread(Buf,1,BUFFER_READ,fin); while(ReadSize==BUFFER_READ) { strTmp=base64.Encode((unsigned char*)Buf,ReadSize); if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } memset(Buf,0,BUFFER_READ+1); ReadSize=fread(Buf,1,BUFFER_READ,fin); } if(ReadSize>0) { strTmp=base64.Encode((unsigned char*)Buf,ReadSize); strTmp+="\r\n\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } } delete[] Buf; fclose(fin); } // end for //界尾 strTmp="--"; strTmp+=BOUNDARY; strTmp+="--\r\n.\r\n"; if(send(m_SOCKET,strTmp.c_str(),strTmp.length(),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("250")) return false; //退出 if(send(m_SOCKET,"QUIT\r\n",strlen("QUIT\r\n"),0) == SOCKET_ERROR) { ReleaseSocket(); return false; } if(!CheckResponse("221")) return false; ReleaseSocket(); return true; }
bool TmtSocketServer::Unitial() { ForceEnd(); return ReleaseSocket()!=0; }
//---------------------------------------------------------------------------- // Execute a script file int CSTATEngine::RunScript(ScriptProgressMonitor *const monitor) { int ret = ITS_OK; iCurrentCommand = 0; eStopProcessing = STAT_RUN; iDeviceCode = 0; // anything smaller can cause problems and doesn't make sense anyway! if (iMaxTimeLimit < 1000) iMaxTimeLimit = 1000; // pointers to our command structures CSTATScriptCommand *pSendCommand; CSTATScriptCommand *pRecvCommand; char lastCommand = NULL; receivedData.Empty( ); // get a command from the script while (pDecoder->GetNextCommand(&pSendCommand) && ret == ITS_OK) { iCurrentCommand++; if (StopProcessing()) { pComms->Send(STAT_RESYNCID); ret = E_USERCANCEL; break; } if (lastCommand == STAT_REBOOT) { ret = ITS_OK; break; } switch(pSendCommand->cCommandID) { case 'P': Message(pSendCommand->Command()); Sleep(atol(pSendCommand->Command())); break; case '/': Message(pSendCommand->Command()); break; case '#': { Message(pSendCommand->Command()); cScreenshotDirectory = pSendCommand->Command(); if(cScreenshotDirectory.Right(1) != _T("\\")) cScreenshotDirectory += _T("\\"); CreateAllDirectories(cScreenshotDirectory); } break; default: { // send the command and retrieve a response int iResyncErrors = 0; while ((ret = SendCommand(pSendCommand, &pRecvCommand)) == E_RESYNCCOMMAND) { Sleep(STAT_RETRYDELAY); iResyncErrors++; if (iResyncErrors > STAT_MAXERRORS) { Message("Too many resync errors - stopping"); ret = E_COMMANDFAILED; break; } } if (ret == ITS_OK) { // perform special operations for these commands switch(pSendCommand->cCommandID) { case 'D': StoreData(pRecvCommand->Command(), pRecvCommand->Length(), pDeviceInfo); AppendCommandToSTATLog("*** DEVICE INFORMATION ***", pRecvCommand->Command(), pRecvCommand->Length()); break; case 'S': { // convert and save the data returned in the response CString image = pSendCommand->Command(); ret = ConvertAndSaveScreeenshot(image, pRecvCommand->Command(), pRecvCommand->Length()); // imave verification if (ret == ITS_OK) { if (pImageVerify->IsActive() && pConverter->bWriteToFile) { ret = pImageVerify->VerifyImage(image); if (ret == VERIFICATION_PASS) ret = ITS_OK; } } break; } case 'T': { if(dataSocket==NULL) { // filename has been sent, now send the file itself CSTATScriptCommand oSendCommand; oSendCommand.cCommandID = pRecvCommand->cCommandID; // read and send the file contents if ((ret = ReadTransferFile(pSendCommand->Command(), &oSendCommand)) == ITS_OK) { int iResyncErrors = 0; while ((ret = SendCommand(&oSendCommand, &pRecvCommand)) == E_RESYNCCOMMAND) { Sleep(STAT_RETRYDELAY); iResyncErrors++; if (iResyncErrors > STAT_MAXERRORS) { Message("Too many resync errors - stopping"); ret = E_COMMANDFAILED; break; } } } } else { //release the socket ret = ReleaseSocket(); } break; } case 'R': case 'X': { if(dataSocket==NULL) { // save the file contents ret = SaveTransferFile(pSendCommand->Command(), pRecvCommand->Command(), pRecvCommand->Length()); } else { //release the socket ret = ReleaseSocket(); } break; } case 'G': { // upload the device log file and write to STAT log file AppendCommandToSTATLog("*** DEVICE LOG ***", pRecvCommand->Command(), pRecvCommand->Length()); break; } case STAT_REFRESH: case STAT_END: { ret = END_SCRIPT; break; } case 'N': { // Retrieve the TEF shared data StoreData(pRecvCommand->Command(), pRecvCommand->Length(), iTEFSharedData); AppendCommandToSTATLog("*** RETRIEVE TEF SHARED DATA ***", pRecvCommand->Command(), pRecvCommand->Length()); } break; default: { Sleep(iDelay); break; } } } if (ret == ITS_OK) { // Data received from certain of the commands is stored // for retreival later. switch(pSendCommand->cCommandID) { case 'W': case 'V': //execute returns pid case 'J': //poll returns 0 1 case '3': receivedData += oRecvCommand.Command(); break; default: break; } } } break; } lastCommand = pSendCommand->cCommandID; if(monitor) { monitor->OnCompleteCommand( iCurrentCommand ); } } pDecoder->Release(); return ret; }
CClientConnection::~CClientConnection(void) { ReleaseSocket(); }
CSocket::~CSocket() { ReleaseSocket(); delete [] m_SendBuffer; }