BOOL EM_UserInfo::GetLocalHostIP(char *szBuf) { ASSERT(NULL != szBuf); char buf[256]={NULL}; GetLocalHostName(buf); char ip[128] = {NULL}; hostent *host = gethostbyname(buf); if (host != NULL) { sockaddr_in sin; memcpy(& sin.sin_addr.s_addr, host->h_addr_list[0], host->h_length); strcpy(ip, inet_ntoa(sin.sin_addr)); if (szBuf != NULL) strcpy(szBuf, ip); else { ErrorOccured(); return FALSE; } strcpy(m_szIP, ip); } else { ErrorOccured(); return FALSE; } return TRUE; }
EM_UserInfo::EM_UserInfo(char *szDisplayName, char *szGender, char *szGroupName) { char buf[1024]; // 把用户信息COPY过来 strcpy(m_szDisplayName, szDisplayName); strcpy(m_szGender, szGender); strcpy(m_szGroupName, szGroupName); GetLocalHostName(buf); strcpy(m_szHostName, buf); GetLocalHostIP(buf); strcpy(m_szIP, buf); // 写进文件 UpdateFile(); }
Server::Server(const size_t& maxConnections) { static int serverThreadID = 0; std::string zergHost = GetLocalHostName(); std::string service = "1234"; //spawn host thread ServerThread* serverThread; serverThread = new ServerThread(zergHost, service, maxConnections); serverThread->manager = theJobManager; m_serverHandles.push_back(serverThread); theJobManager->m_threads.push_back(serverThread); theJobManager->m_threads.back()->Start("Server Thread " + IntToString(serverThreadID)); serverThreadID++; zergHost = "localhost"; serverThread = new ServerThread(zergHost, service, maxConnections); serverThread->manager = theJobManager; m_serverHandles.push_back(serverThread); theJobManager->m_threads.push_back(serverThread); theJobManager->m_threads.back()->Start("Server Thread " + IntToString(serverThreadID)); serverThreadID++; }
CString CaNode::GetIPAddress() { if (IsLocalNode()) { /* class CaSockInit { public: CaSockInit() { WSADATA winsock_data; m_bOk = WSAStartup(MAKEWORD(2,0), & winsock_data) == 0; } ~CaSockInit() {WSACleanup();} BOOL m_bOk; }; CString strLocalHostName = _T(""); CaSockInit wsainit; if (!wsainit.m_bOk) return _T(""); // // Get Local Host Name: char hostname[256]; int nLen = 255; if (gethostname(hostname, nLen) == 0) { strLocalHostName = hostname; return strLocalHostName; } */ return GetLocalHostName(); } return m_strIPAddress; }
//=============================================================================* void BAR_CSCOnlineProxy::LoginForCommissioning(const string& sPassword) //=============================================================================* { //=== open a commissioning session //=== and retrieve unknown interface IUnknown* pUnk = NULL; const string& sUser = GetCurrentUserName(); const string& sLocalHost = GetLocalHostName(); HRESULT hr = m_pICommissioningMgt->Login( CComBSTR(sUser.c_str()), CComBSTR(sPassword.c_str()), CComBSTR(sLocalHost.c_str()), &m_lSessionKey, &pUnk); if ( FAILED(hr) ) { Throw_LoginForCommissioningFailed(hr); } assert( pUnk != NULL ); if ( pUnk == NULL ) Throw_FatalError(L"null interface from loginforcommissioning()"); BAR_TRACE3(2, "logged in for commissioning" ", user=%s, localhost=%s, key=(%i)", sUser.c_str(), sLocalHost.c_str(), m_lSessionKey); //=== query transfer interface via unknown interface BAR_TRACE(2, "querying transfer interface"); hr = pUnk->QueryInterface(IID_ITransfer, (void**)&m_pITransfer); pUnk->Release(); if ( FAILED(hr) ) Throw_ConnectionToControlFailed(hr, L"querying transfer interface failed"); assert( m_pITransfer != NULL ); if ( m_pITransfer == NULL ) Throw_FatalError(L"transfer interface is null after successful query"); }
//////////////////////////////////////////////////////////////////////////////// // NAME: Send // DESCRIPTION: Sending the mail. . // ARGUMENTS: none // USES GLOBAL: m_sSMTPSrvName, m_iSMTPSrvPort, SendBuf, RecvBuf, m_sLogin, // m_sPassword, m_sMailFrom, Recipients, CCRecipients, // BCCRecipients, m_sMsgBody, Attachments, // MODIFIES GL: SendBuf // RETURNS: void // AUTHOR: Jakub Piwowarczyk // AUTHOR/DATE: JP 2010-01-28 // JP 2010-07-08 //////////////////////////////////////////////////////////////////////////////// void CSmtp::Send() { unsigned int i,rcpt_count,res,FileId; char *FileBuf = NULL, *FileName = NULL; FILE* hFile = NULL; unsigned long int FileSize,TotalSize,MsgPart; bool bAccepted; // ***** CONNECTING TO SMTP SERVER ***** // connecting to remote host: if( (hSocket = ConnectRemoteServer(m_sSMTPSrvName.c_str(), m_iSMTPSrvPort)) == INVALID_SOCKET ) throw ECSmtp(ECSmtp::WSA_INVALID_SOCKET, NULL); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 220: bAccepted = true; break; default: throw ECSmtp(ECSmtp::SERVER_NOT_READY, RecvBuf); } }while(!bAccepted); // EHLO <SP> <domain> <CRLF> sprintf(SendBuf,"EHLO %s\r\n",GetLocalHostName()!=NULL ? m_sLocalHostName.c_str() : "domain"); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: throw ECSmtp(ECSmtp::COMMAND_EHLO, RecvBuf); } }while(!bAccepted); if(!m_sLogin.empty()) { // AUTH <SP> LOGIN <CRLF> strcpy(SendBuf,"AUTH LOGIN\r\n"); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: break; case 334: bAccepted = true; break; default: throw ECSmtp(ECSmtp::COMMAND_AUTH_LOGIN, RecvBuf); } }while(!bAccepted); // send login: if(!m_sLogin.size()) throw ECSmtp(ECSmtp::UNDEF_LOGIN, RecvBuf); std::string encoded_login = base64_encode(reinterpret_cast<const unsigned char*>(m_sLogin.c_str()),m_sLogin.size()); sprintf(SendBuf,"%s\r\n",encoded_login.c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 334: bAccepted = true; break; default: throw ECSmtp(ECSmtp::UNDEF_XYZ_RESPONSE, RecvBuf); } }while(!bAccepted); // send password: if(!m_sPassword.size()) throw ECSmtp(ECSmtp::UNDEF_PASSWORD, RecvBuf); std::string encoded_password = base64_encode(reinterpret_cast<const unsigned char*>(m_sPassword.c_str()),m_sPassword.size()); sprintf(SendBuf,"%s\r\n",encoded_password.c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 235: bAccepted = true; break; case 334: break; case 535: throw ECSmtp(ECSmtp::BAD_LOGIN_PASS, RecvBuf); default: throw ECSmtp(ECSmtp::UNDEF_XYZ_RESPONSE, RecvBuf); } }while(!bAccepted); } // ***** SENDING E-MAIL ***** // MAIL <SP> FROM:<reverse-path> <CRLF> if(!m_sMailFrom.size()) throw ECSmtp(ECSmtp::UNDEF_MAIL_FROM, RecvBuf); sprintf(SendBuf,"MAIL FROM:<%s>\r\n",m_sMailFrom.c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: throw ECSmtp(ECSmtp::COMMAND_MAIL_FROM, RecvBuf); } }while(!bAccepted); // RCPT <SP> TO:<forward-path> <CRLF> if(!(rcpt_count = Recipients.size())) throw ECSmtp(ECSmtp::UNDEF_RECIPIENTS, NULL); for(i=0;i<Recipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(Recipients.at(i).Mail).c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: rcpt_count--; } }while(!bAccepted); } if(rcpt_count <= 0) throw ECSmtp(ECSmtp::COMMAND_RCPT_TO, RecvBuf); for(i=0;i<CCRecipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(CCRecipients.at(i).Mail).c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: ; // not necessary to throw } }while(!bAccepted); } for(i=0;i<BCCRecipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(BCCRecipients.at(i).Mail).c_str()); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: ; // not necessary to throw } }while(!bAccepted); } // DATA <CRLF> strcpy(SendBuf,"DATA\r\n"); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 354: bAccepted = true; break; case 250: break; default: throw ECSmtp(ECSmtp::COMMAND_DATA, RecvBuf); } }while(!bAccepted); // send header(s) FormatHeader(SendBuf); SendData(); // send text message if(GetMsgLines()) { for(i=0;i<GetMsgLines();i++) { sprintf(SendBuf,"%s\r\n",GetMsgLineText(i)); SendData(); } } else { sprintf(SendBuf,"%s\r\n"," "); SendData(); } // next goes attachments (if they are) if((FileBuf = new char[55]) == NULL) throw ECSmtp(ECSmtp::LACK_OF_MEMORY, RecvBuf); if((FileName = new char[255]) == NULL) throw ECSmtp(ECSmtp::LACK_OF_MEMORY, RecvBuf); TotalSize = 0; for(FileId=0;FileId<Attachments.size();FileId++) { strcpy(FileName,Attachments[FileId].c_str()); sprintf(SendBuf,"--%s\r\n",BOUNDARY_TEXT); strcat(SendBuf,"Content-Type: application/x-msdownload; name=\""); strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]); strcat(SendBuf,"\"\r\n"); strcat(SendBuf,"Content-Transfer-Encoding: base64\r\n"); strcat(SendBuf,"Content-Disposition: attachment; filename=\""); strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]); strcat(SendBuf,"\"\r\n"); strcat(SendBuf,"\r\n"); SendData(); // opening the file: hFile = fopen(FileName,"rb"); if(hFile == NULL) throw ECSmtp(ECSmtp::FILE_NOT_EXIST, RecvBuf); // checking file size: FileSize = 0; while(!feof(hFile)) FileSize += fread(FileBuf,sizeof(char),54,hFile); TotalSize += FileSize; // sending the file: if(TotalSize/1024 > MSG_SIZE_IN_MB*1024) throw ECSmtp(ECSmtp::MSG_TOO_BIG, RecvBuf); else { fseek (hFile,0,SEEK_SET); MsgPart = 0; for(i=0;i<FileSize/54+1;i++) { res = fread(FileBuf,sizeof(char),54,hFile); MsgPart ? strcat(SendBuf,base64_encode(reinterpret_cast<const unsigned char*>(FileBuf),res).c_str()) : strcpy(SendBuf,base64_encode(reinterpret_cast<const unsigned char*>(FileBuf),res).c_str()); strcat(SendBuf,"\r\n"); MsgPart += res + 2; if(MsgPart >= BUFFER_SIZE/2) { // sending part of the message MsgPart = 0; SendData(); // FileBuf, FileName, fclose(hFile); } } if(MsgPart) { SendData(); // FileBuf, FileName, fclose(hFile); } } fclose(hFile); } delete[] FileBuf; delete[] FileName; // sending last message block (if there is one or more attachments) if(Attachments.size()) { sprintf(SendBuf,"\r\n--%s--\r\n",BOUNDARY_TEXT); SendData(); } // <CRLF> . <CRLF> strcpy(SendBuf,"\r\n.\r\n"); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 250: bAccepted = true; break; default: throw ECSmtp(ECSmtp::MSG_BODY_ERROR, RecvBuf); } }while(!bAccepted); // ***** CLOSING CONNECTION ***** // QUIT <CRLF> strcpy(SendBuf,"QUIT\r\n"); SendData(); bAccepted = false; do { ReceiveData(); switch(SmtpXYZdigits()) { case 221: bAccepted = true; break; default: throw ECSmtp(ECSmtp::COMMAND_QUIT, RecvBuf); } }while(!bAccepted); #ifdef LINUX close(hSocket); #else closesocket(hSocket); #endif hSocket = 0; }
/* ** This function verify that the last SQL error number was -39100 and that the ** error text starts with the E_GC0132 text, to return a additional ** warning to explain why the "register as link" command failed. ** ** Input parameters ** csDestNodeName : ** csCurrentNodeName : ** ** return value ** The additional warning or Empty string. */ CString VerifyStarSqlError(LPCTSTR csDestNodeName, LPCTSTR csCurrentNodeName) { TCHAR *szErrNo132 = _T("E_GC0132"); LPSQLERRORINFO lpsqlErrInfo; CString csTemp; csTemp.Empty(); lpsqlErrInfo = GetNewestSqlError(); if (lpsqlErrInfo && lpsqlErrInfo->sqlcode == -39100L && x_strncmp((LPTSTR)lpsqlErrInfo->lpSqlErrTxt,szErrNo132,lstrlen(szErrNo132)) == 0) { csTemp.Format(IDS_E_VNODE_NOT_DEFINE,(LPCTSTR)csDestNodeName, (IsLocalNodeName((const char*)csCurrentNodeName, FALSE) ? (LPCTSTR)GetLocalHostName():(LPCTSTR)csCurrentNodeName)); } return csTemp; }
bool CSmtp::Send() { unsigned int i,rcpt_count,res,FileId; char *FileBuf = NULL, *FileName = NULL; FILE* hFile = NULL; unsigned long int FileSize,TotalSize,MsgPart; // ***** CONNECTING TO SMTP SERVER ***** assert(m_pcSMTPSrvName); // connecting to remote host: if( (hSocket = ConnectRemoteServer(m_pcSMTPSrvName, m_iSMTPSrvPort)) == INVALID_SOCKET ) { m_oError = CSMTP_WSA_INVALID_SOCKET; return false; } Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 220: break; default: m_oError = CSMTP_SERVER_NOT_READY; return false; } // EHLO <SP> <domain> <CRLF> sprintf(SendBuf,"EHLO %s\r\n",GetLocalHostName()!=NULL ? m_pcLocalHostName : "domain"); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 250: break; default: m_oError = CSMTP_COMMAND_EHLO; return false; } // AUTH <SP> LOGIN <CRLF> strcpy(SendBuf,"AUTH LOGIN\r\n"); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 334: break; default: m_oError = CSMTP_COMMAND_AUTH_LOGIN; return false; } // send login: if(!m_pcLogin) { m_oError = CSMTP_UNDEF_LOGIN; return false; } std::string encoded_login = base64_encode(reinterpret_cast<const unsigned char*>(m_pcLogin),strlen(m_pcLogin)); sprintf(SendBuf,"%s\r\n",encoded_login.c_str()); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 334: break; default: m_oError = CSMTP_UNDEF_XYZ_RESPOMSE; return false; } // send password: if(!m_pcPassword) { m_oError = CSMTP_UNDEF_PASSWORD; return false; } std::string encoded_password = base64_encode(reinterpret_cast<const unsigned char*>(m_pcPassword),strlen(m_pcPassword)); sprintf(SendBuf,"%s\r\n",encoded_password.c_str()); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 235: break; case 535: m_oError = CSMTP_BAD_LOGIN_PASS; return false; default: m_oError = CSMTP_UNDEF_XYZ_RESPOMSE; return false; } // ***** SENDING E-MAIL ***** // MAIL <SP> FROM:<reverse-path> <CRLF> if(m_pcMailFrom == NULL) { m_oError = CSMTP_UNDEF_MAILFROM; return false; } sprintf(SendBuf,"MAIL FROM:<%s>\r\n",m_pcMailFrom); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 250: break; default: m_oError = CSMTP_COMMAND_MAIL_FROM; return false; } // RCPT <SP> TO:<forward-path> <CRLF> rcpt_count = Recipients.size(); for(i=0;i<Recipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(Recipients.at(i).Mail).c_str()); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 250: break; default: m_oError = CSMTP_COMMAND_RCPT_TO; rcpt_count--; } } if(!rcpt_count) return false; for(i=0;i<CCRecipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(CCRecipients.at(i).Mail).c_str()); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; } for(i=0;i<BCCRecipients.size();i++) { sprintf(SendBuf,"RCPT TO:<%s>\r\n",(BCCRecipients.at(i).Mail).c_str()); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; } // DATA <CRLF> strcpy(SendBuf,"DATA\r\n"); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 354: break; default: m_oError = CSMTP_COMMAND_DATA; return false; } // send header(s) if(!FormatHeader(SendBuf)) { m_oError = CSMTP_UNDEF_MSG_HEADER; return false; } if(!SendData()) return false; // send text message sprintf(SendBuf,"%s\r\n",m_pcMsgBody); // NOTICE: each line ends with <CRLF> if(!SendData()) return false; // next goes attachments (if they are) if((FileBuf = new char[55]) == NULL) { m_oError = CSMTP_LACK_OF_MEMORY; return false; } if((FileName = new char[255]) == NULL) { m_oError = CSMTP_LACK_OF_MEMORY; return false; } TotalSize = 0; for(FileId=0;FileId<Attachments.size();FileId++) { strcpy(FileName,Attachments[FileId].c_str()); sprintf(SendBuf,"--%s\r\n",BOUNDARY_TEXT); strcat(SendBuf,"Content-Type: application/x-msdownload; name=\""); strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]); strcat(SendBuf,"\"\r\n"); strcat(SendBuf,"Content-Transfer-Encoding: base64\r\n"); strcat(SendBuf,"Content-Disposition: attachment; filename=\""); strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]); strcat(SendBuf,"\"\r\n"); strcat(SendBuf,"\r\n"); if(!SendData()) return false; // opening the file: hFile = fopen(FileName,"rb"); if(hFile == NULL) { m_oError = CSMTP_FILE_NOT_EXIST; break; } // checking file size: FileSize = 0; while(!feof(hFile)) FileSize += fread(FileBuf,sizeof(char),54,hFile); TotalSize += FileSize; // sending the file: if(TotalSize/1024 > MSG_SIZE_IN_MB*1024) m_oError = CSMTP_MSG_TOO_BIG; else { fseek (hFile,0,SEEK_SET); MsgPart = 0; for(i=0;i<FileSize/54+1;i++) { res = fread(FileBuf,sizeof(char),54,hFile); MsgPart ? strcat(SendBuf,base64_encode(reinterpret_cast<const unsigned char*>(FileBuf),res).c_str()) : strcpy(SendBuf,base64_encode(reinterpret_cast<const unsigned char*>(FileBuf),res).c_str()); strcat(SendBuf,"\r\n"); MsgPart += res + 2; if(MsgPart >= BUFFER_SIZE/2) { // sending part of the message MsgPart = 0; if(!SendData()) { delete[] FileBuf; delete[] FileName; fclose(hFile); return false; } } } if(MsgPart) { if(!SendData()) { delete[] FileBuf; delete[] FileName; fclose(hFile); return false; } } } fclose(hFile); } delete[] FileBuf; delete[] FileName; // sending last message block (if there is one or more attachments) if(Attachments.size()) { sprintf(SendBuf,"\r\n--%s--\r\n",BOUNDARY_TEXT); if(!SendData()) return false; } // <CRLF> . <CRLF> strcpy(SendBuf,"\r\n.\r\n"); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 250: break; default: m_oError = CSMTP_MSG_BODY_ERROR; return false; } // ***** CLOSING CONNECTION ***** // QUIT <CRLF> strcpy(SendBuf,"QUIT\r\n"); if(!SendData()) return false; Sleep(DELAY_IN_MS); if(!ReceiveData()) return false; switch(SmtpXYZdigits()) { case 221: break; default: m_oError = CSMTP_COMMAND_QUIT; hSocket = NULL; return false; } closesocket(hSocket); hSocket = NULL; return true; }