📄 connectivity_core_utils.cpp
字号:
{ memcpy(&sa1, pAddrInfo->ai_addr, sizeof(sockaddr_in)); sa1.sin_family = AF_INET; sa1.sin_addr.s_addr = ulSelectedIP; } else perror("getaddrinfo"); // struct sockaddr_in sa1;/* bzero((char*)&sa1, sizeof(sa1)); sa1.sin_family=AF_INET; sa1.sin_addr.s_addr=htonl(INADDR_ANY); sa1.sin_port=htons(0);*/ } else { bool mFlag = getIpofNetworkAdptor(&sa1, pNetworkAdaptorKey); if(!mFlag) return -1; }; if ( bind(s, (SOCKADDR*)&sa1, sizeof(sa1)) == SOCKET_ERROR ) return -1; unsigned long mUnblocking = 1;/* //if (connect_nonb(s,(struct sockaddr *)(&sa),sizeof(struct sockaddr),*pTimeOut) != 0) if (connect_nonb(s,(struct sockaddr *)(&sa),sizeof(struct sockaddr),2000) != 0) { perror("connect_nonb"); }*/ //if (ioctlsocket(s, FIONBIO, &mUnblocking) == SOCKET_ERROR)/* if (ioctl(s, FIONBIO, &mUnblocking) == SOCKET_ERROR) //if set, https will wrong { perror("ioctl"); close(s); return -1; };*/ const int ival = 1; int len = sizeof(ival); int ret = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const void*) &ival, len); if(ret < 0) printf("setsocketopt error\n"); if (connect(s,(struct sockaddr *)(&sa),sizeof(struct sockaddr)) != 0) { perror("connect"); close(s); return -1; }; fd_set r; FD_ZERO(&r); FD_SET(s, &r); // printf("timeout\n"); if(select(s+1,0,&r,0,&tv) <= 0) // ftp bug { perror(", select"); //why ftp select will wrong? close(s); return -1; //return s; } else return s; } catch(...) { THROWIMEXCEPTION("8004320F"); }}/*///---------///---------The following methods: ParseURI is used to analyze a string as: //////---------http://www.*****.***:80 ,or FTP://www.******.*** and so on. The result /// ///---------include the remote host name, IP Address and so on //////---------------------------------------------------------------------------------------------------------///*//* ______________________________________________________________ ParseURI______________________________________________________________ */int ParseURI(byte *pURI, PURI_INFO pUriInfo){ try { string sParsed; string sTmp; string sUri; size_t StartPos; size_t EndPos; bool bPort; int iRet; char buf[1000]; iRet = -1; bPort = false; if (!pURI || !pUriInfo) goto GracefulExit; sUri = (char*)pURI; // transform(sUri.begin(), sUri.end(), sUri.begin(), tolower); // default values pUriInfo->Protocol = UNKNOWN_PROTOCOL; pUriInfo->Port = 0; pUriInfo->IPAddr = 0; // loopback rrw pUriInfo->pHostIP = NULL; // get protocol StartPos = sUri.find("://"); if (StartPos == -1) { pUriInfo->Protocol = HTTP_PROTOCOL; StartPos = 0; } else { // find protocol sTmp = sUri.substr( 0, StartPos ); if (!sTmp.compare("http")) pUriInfo->Protocol = HTTP_PROTOCOL; if (!sTmp.compare("tcp")) pUriInfo->Protocol = TCP_PROTOCOL; if (!sTmp.compare("udp")) pUriInfo->Protocol = UDP_PROTOCOL; if (!sTmp.compare("ftp")) pUriInfo->Protocol = FTP_PROTOCOL; if (!sTmp.compare("icmp")) pUriInfo->Protocol = ICMP_PROTOCOL; if (!sTmp.compare("https")) pUriInfo->Protocol = HTTPS_PROTOCOL; StartPos += 3; } // find destination, file, port sTmp = sUri.substr(StartPos, sUri.length()); EndPos = sTmp.find("/"); if ( EndPos == -1 ) { EndPos = sTmp.length(); //pUriInfo->FileName="/"; } else pUriInfo->FileName = sTmp.substr(EndPos+1, sTmp.length()); sTmp = sTmp.substr(0, EndPos); EndPos = sTmp.find(":"); if ( EndPos == -1 ) EndPos = sTmp.length(); else { bPort = true; pUriInfo->Port = atoi( sTmp.substr(EndPos+1, sTmp.length()).c_str() ); } pUriInfo->DestPath = sTmp.substr(0, EndPos); // check if uri doesn't have a port when using TCP or UDP if (((pUriInfo->Protocol == TCP_PROTOCOL) || (pUriInfo->Protocol == UDP_PROTOCOL)) && !bPort) { iRet = -2; // rrw invalid uri goto GracefulExit; } // fill the pHostIP & IPAddr //StartNetwork(); pUriInfo->IPAddr = inet_addr(pUriInfo->DestPath.c_str() ); if ( pUriInfo->IPAddr == INADDR_NONE ) { pUriInfo->pHostIP = gethostbyname( pUriInfo->DestPath.c_str() ); if ( pUriInfo->pHostIP ) memcpy(&pUriInfo->IPAddr, pUriInfo->pHostIP->h_addr, pUriInfo->pHostIP->h_length); else printf("Don't get the host by name %s\n", pUriInfo->DestPath.c_str()); } else { pUriInfo->pHostIP = gethostbyaddr(reinterpret_cast<char *>(&pUriInfo->IPAddr), sizeof(pUriInfo->IPAddr), AF_INET); if(pUriInfo->pHostIP==NULL) { perror("gethostbyaddr, make a temp takeplace"); return 0; } } if ( !pUriInfo->pHostIP ) iRet = -1; else iRet = 0; GracefulExit: return iRet; } catch(...) { THROWIMEXCEPTION("80043210"); }}/*///---------///---------The following methods: Checksum,RecvPong and SendPings are used to //////---------generate/ send /receive/ check (the response) package for the ICMP /// ///---------------------------------------------------------------------------------------------------------///*//* ________________________________________________________________Checksum________________________________________________________________ */USHORT Checksum(USHORT *pBuffer, int nSize) { unsigned long ulSum = 0; while ( nSize > 1 ) { ulSum += *pBuffer++; nSize -= sizeof(USHORT); } if ( nSize ) ulSum += *reinterpret_cast<BYTE *>(pBuffer); ulSum = (ulSum >> 16) + (ulSum & 0xffff); ulSum += (ulSum >> 16); return static_cast<USHORT>(~ulSum); //This should be a valid checksum}/* ________________________________________________________________RecvPong________________________________________________________________ */PONG_STATUS RecvPong(SOCKET sock, sockaddr_in& saDest, vector<SOCK_TIME>& aHops){ try { int nError, nSize; PONG_STATUS statusRet = PONG_FAILURE; BYTE *pDataIn = NULL; SOCK_TIME saSrc; IP_HEADER *pIpHdrIn; ICMP_HEADER *pIcmpHdrIn, *pIcmpHdrOrig; fd_set fdRead; // SCR#699, use timeout of 3000ms timeval tv = { 3, 0}; //Use a 3000ms timeout //Initialize a receive buffer pDataIn = new BYTE[nPACKET_SIZE + sizeof(IP_HEADER)]; if ( !pDataIn ) goto Cleanup; //Wait for a response while ( PONG_FAILURE == statusRet || PONG_RETRY == statusRet ) { //Use a select here to make sure we have data FD_ZERO(&fdRead); FD_SET(sock, &fdRead); nError = select((int ) sock + 1, &fdRead, NULL, NULL, &tv); if ( !nError || SOCKET_ERROR == nError ) goto Cleanup; //Receive the next packet nError = recvfrom(sock, reinterpret_cast<char *>(pDataIn), nPACKET_SIZE + sizeof(IP_HEADER), 0, reinterpret_cast<sockaddr *>(&saSrc), reinterpret_cast<socklen_t *>(&(nSize = sizeof(saSrc)))); if ( SOCKET_ERROR == nError ) //ICMP_UNREACH_PORT {// nError = WSAGetLastError(); goto Cleanup; } pIpHdrIn = reinterpret_cast<IP_HEADER *>(pDataIn); pIcmpHdrIn = reinterpret_cast<ICMP_HEADER *>(pIpHdrIn + 1); //Check to see if this is the reply to our ping if ( IPPROTO_ICMP == pIpHdrIn->proto ) { //Verify the checksum if ( !Checksum(reinterpret_cast<USHORT *>(pIcmpHdrIn), ntohs(pIpHdrIn->total_len) - sizeof(*pIpHdrIn)) ) { // Found pong stop time g_StopTime = g_cQTime.GetTime(); //Check to see the status if ( cICMP_TIME_EXCEEDED == pIcmpHdrIn->i_type && cTTL_EXCEEDED == pIcmpHdrIn->i_code ) { //Make sure this is really the packet I am looking for pIcmpHdrOrig = reinterpret_cast<ICMP_HEADER *>((reinterpret_cast<BYTE *>(pIcmpHdrIn) + 8 + sizeof(IP_HEADER))); if ( s_nAppId == pIcmpHdrOrig->i_id && s_nPacketSeq == pIcmpHdrOrig->i_seq ) { //Save this sockaddr struct in my list of hops saSrc.StartTime = g_StartTime; saSrc.StopTime = g_StopTime; aHops.push_back(saSrc); statusRet = PONG_HOP; } } else if ( saSrc.SockAddr.sin_addr.s_addr == saDest.sin_addr.s_addr && s_nAppId == pIcmpHdrIn->i_id && s_nPacketSeq == pIcmpHdrIn->i_seq ) { //Make sure this is really the packet I am looking for //Save this sockaddr struct in my list of hops saSrc.StartTime = g_StartTime; saSrc.StopTime = g_StopTime; aHops.push_back(saSrc); statusRet = PONG_COMPLETE; } } else { //If the only packet I get has a messed up checksum then I need to retry the send //but I will keep going in this case just in case this isn't the packet I was looking //for statusRet = PONG_RETRY; } } } Cleanup: //Cleanup allocated resources if ( pDataIn ) delete [] pDataIn; return statusRet; } catch(...) { THROWIMEXCEPTION("80043211"); }}PONG_STATUS RecvPongEx(SOCKET sock, sockaddr_in& saDest, vector<SOCK_TIME>& aHops,unsigned int *pTimeOut, IntelMobileString& pNetworkAdaptorKey){ try { int nError, nSize; PONG_STATUS statusRet = PONG_FAILURE; BYTE *pDataIn = NULL; SOCK_TIME saSrc; IP_HEADER *pIpHdrIn; ICMP_HEADER *pIcmpHdrIn, *pIcmpHdrOrig; fd_set fdRead; // SCR#699, use timeout of 3000ms if(*pTimeOut == 0) *pTimeOut = MAXTIMEOUT; timeval tv = { (long)(*pTimeOut)/1000,(long)(*pTimeOut)%1000 }; //Use a 3000ms timeout //Initialize a receive buffer pDataIn = new BYTE[nPACKET_SIZE + sizeof(IP_HEADER)]; if ( !pDataIn ) goto Cleanup; //Wait for a response while ( PONG_FAILURE == statusRet || PONG_RETRY == statusRet ) { //Use a select here to make sure we have data FD_ZERO(&fdRead); FD_SET(sock, &fdRead); nError = select((int ) sock + 1, &fdRead, NULL, NULL, &tv); if ( !nError || SOCKET_ERROR == nError ) goto Cleanup; //Receive the next packet nError = recvfrom(sock, reinterpret_cast<char *>(pDataIn), nPACKET_SIZE + sizeof(IP_HEADER), 0, reinterpret_cast<sockaddr *>(&saSrc), reinterpret_cast<socklen_t *>(&(nSize = sizeof(saSrc)))); if ( SOCKET_ERROR == nError ) //ICMP_UNREACH_PORT { // nError = WSAGetLastError(); goto Cleanup; } pIpHdrIn = reinterpret_cast<IP_HEADER *>(pDataIn); pIcmpHdrIn = reinterpret_cast<ICMP_HEADER *>(pIpHdrIn + 1); //Check to see if this is the reply to our ping if ( IPPROTO_ICMP == pIpHdrIn->proto ) { //Verify the checksum if ( !Checksum(reinterpret_cast<USHORT *>(pIcmpHdrIn), ntohs(pIpHdrIn->total_len) - sizeof(*pIpHdrIn)) ) { // Found pong stop time g_StopTime = g_cQTime.GetTimeEx(); //Check to see the status if ( cICMP_TIME_EXCEEDED == pIcmpHdrIn->i_type && cTTL_EXCEEDED == pIcmpHdrIn->i_code ) { //Make sure this is really the packet I am looking for pIcmpHdrOrig = reinterpret_cast<ICMP_HEADER *>((reinterpret_cast<BYTE *>(pIcmpHdrIn) + 8 + sizeof(IP_HEADER))); if ( s_nAppId == pIcmpHdrOrig->i_id && s_nPacketSeq == pIcmpHdrOrig->i_seq ) { //Save this sockaddr struct in my list of hops saSrc.StartTime = g_StartTime; saSrc.StopTime = g_StopTime; aHops.push_back(saSrc); statusRet = PONG_HOP; } } else if ( saSrc.SockAddr.sin_addr.s_addr == saDest.sin_addr.s_addr && s_nAppId == pIcmpHdrIn->i_id && s_nPacketSeq == pIcmpHdrIn->i_seq ) { //Make sure this is really the packet I am looking for //Save this sockaddr struct in my list of hops saSrc.StartTime = g_StartTime; saSrc.StopTime = g_StopTime; aHops.push_back(saSrc); statusRet = PONG_COMPLETE; } } else { //If the only packet I get has a messed up checksum then I need to retry the send //but I will keep going in this case just in case this isn't the packet I was looking //for statusRet = PONG_RETRY; } } } Cleanup: //Cleanup allocated resources if ( pDataIn ) delete [] pDataIn; return statusRet; } catch(...) { THROWIMEXCEPTION("80043211"); }}/*______________________________________________________________SendPings________________________________________________________________ */BOOL SendPings(SOCKET sock, sockaddr_in& saDest, ICMP_HEADER *pIcmpHdr, vector<SOCK_TIME> &aHops){ 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 < 2; 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.GetTime(); 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 = RecvPong(sock, saDest, aHops); switch ( status ) { case PONG_COMPLETE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -