📄 connectivity_core_utils.cpp
字号:
bRet = TRUE; break; case PONG_FAILURE: goto Cleanup; case PONG_HOP: case PONG_RETRY: default: //There is no need to do anything for these cases break; } } } Cleanup: return bRet; } catch(...) { THROWIMEXCEPTION("80043212"); }}BOOL SendPingsEx(SOCKET sock, sockaddr_in& saDest, ICMP_HEADER *pIcmpHdr,vector<SOCK_TIME> &aHops, unsigned int *pTimeOut, IntelMobileString& pNetworkAdaptorKey){ try { int nTimeToLive, nError, nRetry; PONG_STATUS status; BOOL bRet = FALSE; int nTmp; //Start sending out pings // SCR#699, no need to get the route, directly send ping to the destination nTimeToLive = 255;// for ( nTimeToLive = 255; !bRet; nTimeToLive++ ) { //Set the TTL nError = setsockopt(sock, IPPROTO_IP, IP_TTL, reinterpret_cast<char *>(&nTimeToLive), sizeof(nTimeToLive)); if ( SOCKET_ERROR == nError ) goto Cleanup; //I can retry this send up to 2 times for ( status = PONG_RETRY, nRetry = 0; PONG_RETRY == status && nRetry < 1; nRetry++ ) { //Initialize the remaining header items in the packet pIcmpHdr->i_seq = ++s_nPacketSeq; pIcmpHdr->timestamp = clock(); pIcmpHdr->i_cksum = 0; pIcmpHdr->i_cksum = Checksum(reinterpret_cast<USHORT *>(pIcmpHdr), nPACKET_SIZE); //Send the packet g_StartTime = g_cQTime.GetTimeEx(); nError = sendto(sock, reinterpret_cast<char *>(pIcmpHdr), nPACKET_SIZE, 0, reinterpret_cast<sockaddr *>(&saDest), sizeof(saDest)); // nTmp = WSAGetLastError(); if ( SOCKET_ERROR == nError ) goto Cleanup; //Receive the return PONG status = RecvPongEx(sock, saDest,aHops,pTimeOut, pNetworkAdaptorKey); switch ( status ) { case PONG_COMPLETE: bRet = TRUE; break; case PONG_FAILURE: goto Cleanup; case PONG_HOP: case PONG_RETRY: default: //There is no need to do anything for these cases break; } } } Cleanup: return bRet; } catch(...) { THROWIMEXCEPTION("80043212"); }}/*///---------///---------The following code is used to test the IsReachable for the various protocols //////---------These method including the IsReachable are only internal use /// ///---------------------------------------------------------------------------------------------------------///*/bool GetBestLocalIP(PURI_INFO pUri, IntelMobileString &bestLocalIP){ bool bRet = false; ICMP_HEADER *pIcmpHdr; BYTE *pData = NULL; sockaddr_in saDest; vector<SOCK_TIME> *paHops;//// TODO: for CE? what's the use?// vector<SOCK_TIME>::iterator iterHop; SOCKET_INFO info; int nError; int nRet; char MachineName[256]; addrinfo *pAddrInfo; addrinfo AddrInfo; DWORD Size; try { gethostname(MachineName, Size); memset(&AddrInfo, 0, sizeof(addrinfo)); nRet = getaddrinfo(MachineName, NULL, &AddrInfo, &pAddrInfo); if ( !nRet ) { memcpy(&info.saAddr, pAddrInfo->ai_addr, sizeof(sockaddr_in)); info.saAddr.sin_family = AF_INET; info.saAddr.sin_addr.s_addr = INADDR_ANY; } //Create a destination sockaddr memset(&saDest, 0, sizeof(saDest)); saDest.sin_family = AF_INET; saDest.sin_addr.s_addr = pUri->IPAddr; } catch(...) { THROWIMEXCEPTION("80043213"); } try { info.sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if ( INVALID_SOCKET == info.sock ) goto Cleanup; nError = bind(info.sock, reinterpret_cast<sockaddr *>(&info.saAddr), sizeof(info.saAddr)); if ( SOCKET_ERROR == nError ) { shutdown(info.sock, SHUT_RDWR); goto Cleanup; } struct sockaddr_in s; socklen_t l; char buf[128]; getsockname(info.sock, (struct sockaddr *)&s, &l); inet_ntop(AF_INET, &s.sin_addr.s_addr, buf, l); IntelMobileChar wbuf[128];#ifdef _UNICODE mbstowcs(wbuf, buf, 128);#else strncpy(wbuf, buf, 128);#endif bestLocalIP = wbuf; } catch(...) { THROWIMEXCEPTION("80043214"); }Cleanup:GracefulExit: return bRet;}/* ________________________________________________________________ICMPIsReachable________________________________________________________________ */bool ICMPIsReachable(int *pReachable, unsigned int *pTime, PURI_INFO pUri, unsigned long ulSelectedIP, vector<unsigned long> *pRoute ){ bool bRet = false; ICMP_HEADER *pIcmpHdr; BYTE *pData = NULL; sockaddr_in saDest; vector<SOCK_TIME> *paHops;//// TODO: for CE? what's the use?// vector<SOCK_TIME>::iterator iterHop; list<SOCKET_INFO>::iterator iterSock; SOCKET_INFO info; int nError; int nRet; char MachineName[256]; addrinfo *pAddrInfo; addrinfo AddrInfo; DWORD Size; vector <int>::size_type VecSize; if ( pTime ) *pTime = 0; if ( pRoute ) pRoute->clear(); if ( !pReachable ) goto GracefulExit; *pReachable = false; /* if ( !StartNetwork() ) goto GracefulExit; */ try { Size = 256; paHops = new vector<SOCK_TIME>; if ( paHops ) { g_paHops[g_Tail] = paHops; g_Tail = (g_Tail + 1) % MAX_IP_Q; } else goto Cleanup; gethostname(MachineName, Size); memset(&AddrInfo, 0, sizeof(addrinfo)); nRet = getaddrinfo(MachineName, NULL, &AddrInfo, &pAddrInfo); if ( !nRet ) { memcpy(&info.saAddr, pAddrInfo->ai_addr, sizeof(sockaddr_in)); info.saAddr.sin_family = AF_INET; if ( !ulSelectedIP ) info.saAddr.sin_addr.s_addr = INADDR_ANY; else info.saAddr.sin_addr.s_addr = ulSelectedIP; } //Clear out the hops array before continuing paHops->clear(); //Initialize a send buffer pData = new BYTE[nPACKET_SIZE]; if ( !pData ) goto Cleanup; memset(pData, 0, nPACKET_SIZE);//sizeof(*pIcmpHdr)); //Initialize the ICMP header pIcmpHdr = reinterpret_cast<ICMP_HEADER *>(pData); pIcmpHdr->i_type = cICMP_ECHO; pIcmpHdr->i_id = s_nAppId; //Create a destination sockaddr memset(&saDest, 0, sizeof(saDest)); saDest.sin_family = AF_INET; saDest.sin_addr.s_addr = pUri->IPAddr; } catch(...) { THROWIMEXCEPTION("80043213"); } try { info.sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if ( INVALID_SOCKET == info.sock ) goto Cleanup; printf("socket id %d\n", info.sock); nError = bind(info.sock, reinterpret_cast<sockaddr *>(&info.saAddr), sizeof(info.saAddr)); if ( SOCKET_ERROR == nError ) { close(info.sock); goto Cleanup; } } catch(...) { THROWIMEXCEPTION("80043214"); } //Send the pings out to the destination machine if ( !SendPings(info.sock, saDest, pIcmpHdr, *paHops ) ) paHops->clear(); close(info.sock);Cleanup: //Cleanup allocated resources if ( pData ) delete [] pData; try { VecSize = paHops->size(); if ( VecSize>0 ) { if ( pRoute ) for ( unsigned int i=0;i<VecSize;i++) //nhu: temp comment out //pRoute->push_back( paHops->at(i).SockAddr.sin_addr.s_addr ); if ( pTime ) *pTime = (unsigned int)((*paHops)[VecSize-1].StopTime -(*paHops)[VecSize-1].StartTime); *pReachable = true; bRet = true; } } catch(...) { THROWIMEXCEPTION("80043215"); }GracefulExit:// Debug information/* printf( "Debug: ICMPIsReachable\n" ); if ( pTime ) printf( "\tTotalTime: %d\n", *pTime ); if ( pRoute ) printf( "\tRouteLength: %d\n", pRoute->size() ); if ( VecSize>0 ) for ( unsigned int i=0;i<VecSize;i++) { printf("\tDebug of ICMP\tIP: "); BYTE *pb=(BYTE*)&paHops->at(i).SockAddr.sin_addr.S_un.S_addr ; for ( int j=0;j<4;j++ ) printf("%d.",pb[j]); printf("\t\ttime: %ld\n", paHops->at(i).StopTime-paHops->at(i).StartTime ); } */ return bRet;}/* ________________________________________________________________HTTPIsReachable________________________________________________________________ */#define BUFLEN 1024int HttpRequest(int &sockfd, char *request){ char pBuf[BUFLEN], response[BUFLEN], pBase[BUFLEN]; char resCode[4],*pHCode=NULL; int bytes=0, total_bytes=0; printf("sockfd %d\n", sockfd); if(request!=NULL) int t=write( sockfd, request, strlen( request) ); fd_set set; FD_ZERO( &set ); FD_SET( sockfd, &set ); struct timeval tv; tv.tv_sec = 3; tv.tv_usec = 0; if( select( sockfd+1, &set, NULL, NULL, &tv ) == -1 ) { perror("select"); }/* while( (bytes = read( s, pBuf, BUFLEN )) != 0 ) { total_bytes += bytes; if( (data_size + bytes ) > alloc_size ) { pBase = realloc( pBase, (alloc_size + XFERLEN) ); if( pBase == NULL ) { // get outta dodge and free the // the allocated memory...there // could be a chance that we ran // out of resource, and we'll // free it. fflush( stderr ); if( pBase ) free( pBase ); if( pBuf ) free( pBuf ); return( response ); } } pData = pBase + data_size; alloc_size += XFERLEN; } memcpy( pData, pBuf, bytes ); // copy data pData += bytes; // increment pointer data_size += bytes; // increment size of data }*/ if( (bytes = read( sockfd, pBase, BUFLEN )) != 0 ) { pHCode = strchr( pBase, ' ' ); if( pHCode != NULL ) { pHCode++; strncpy(resCode , pHCode, 3 ); pHCode[4]='\0'; // now get message pHCode += 4; // increment past code // and search for new line /* pHMsgEnd = strchr( pHCode, '\n' ); if( pHMsgEnd != NULL ) // get the rest of line for the response message { strncpy( response.szHMsg, pHCode, (pHMsgEnd - pHCode) <= (HMSGSIZE - 1) ? (pHMsgEnd - pHCode ) : (HMSGSIZE - 1) ); }*/ return atoi(resCode); } }return 0;} bool HTTPIsReachable(int *pReachable, unsigned int *pTime, PURI_INFO pUri){ try { bool bRet = false; CQTime cQTime; DWORD StartTime; DWORD StopTime; DWORD dwStatusCode; CHAR szBuffer[80]; DWORD dwLen = 80; int conn; *pReachable = false; if ( pTime ) *pTime = 0; if ( pUri->Port == 0 ) pUri->Port = HTTPPORT; StartTime = cQTime.GetTime(); if (pUri->DestPath.length() ) conn = (int)socketClient( pUri->pHostIP, pUri->Port,0); if (conn < 0) { printf("conn<0\n"); goto GracefulExit; } StopTime=cQTime.GetTime(); // Debug information char req[1024]; sprintf( req, "GET /%s HTTP/1.0\r\nHost: %s\r\n", pUri->FileName.c_str(),pUri->DestPath.c_str() ); strcat( req, "User-Agent: hget/" LIBHTTP_VERSION "\r\n"); strcat( req, "Pragma: no-cache\r\n" ); strcat( req, "Accept: */*\r\n\r\n" ); printf("request:%s\n", req); dwStatusCode = HttpRequest(conn, req); close(conn); //printf("return code %d\n", dwStatusCode); if ( dwStatusCode == 200 || pUri->FileName.length()==0 ) { *pReachable = true; bRet = true; if ( pTime ) { unsigned int st; st=StopTime-StartTime; *pTime = st; } } GracefulExit: return bRet; } catch(...) { THROWIMEXCEPTION("800432146"); } return false;}/* ________________________________________________________________HTTPSIsReachable________________________________________________________________ */#define _debug#ifdef _debug#define debug printf#else#define debug#endif#define BUF_LEN 1024int HttpsRequest(int &sockfd, char *request) //{ //SSL object... printf("%d %s\n", sockfd, request); int ret, n; SSL *ssl; SSL_CTX *ctx; //SSL init............... SSL_load_error_strings(); SSL_library_init(); ctx = SSL_CTX_new(SSLv23_client_method());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -