📄 networkaddress.cxx
字号:
int error = getaddrinfo(the_host.c_str(), NULL, &hints, &res); if (error) { cpLog(LOG_ERR, "Failed to resolve %s, reason:%s", the_host.c_str(), gai_strerror(error)); return; } char ip[256]; cpLog(LOG_DEBUG_STACK, "getnameinfo()"); error = getnameinfo(res->ai_addr, res->ai_addrlen, ip, 256, NULL, 0, NI_NUMERICHOST ); if (error) { cpLog(LOG_ERR, "Failed to getnameinfo %s, reason:%s", the_host.c_str(), gai_strerror(error)); freeAddrInfo(res); return; } #if 0 //Do not set the hostname unless someone asks for it //Host name will get set when someone asks the name using getHostName() interface if(nameIsAddress) { char hName[256]; //Get the host name of the address error = getnameinfo(res->ai_addr, res->ai_addrlen, hName, 256, NULL, 0, NI_NAMEREQD ); if (error) { cpLog(LOG_ERR, "Failed to resolve address %s to a name", ip); hostName = ip; } else hostName = hName; }#endif freeAddrInfo(res); ipAddress = ip; cpLog(LOG_DEBUG_STACK, "Set ipAddress to %s", ipAddress.c_str()); ipAddressSet = true;}NetworkAddress::NetworkAddress( const NetworkAddress& x){ aPort = x.aPort; ipAddress = x.getIpName(); ipAddressSet = x.ipAddressSet; hostName = x.hostName; x.getSockAddr(&(this->sa_cache)); sockAddrSet = x.sockAddrSet;}intNetworkAddress::getHostByName(const char* hostName, struct addrinfo* res, struct hostent* hEnt /* default arg */){#if defined(__GLIBC__) struct addrinfo hints; // Setup structures memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME; hints.ai_family = NetworkConfig::instance().getAddrFamily(); hints.ai_socktype = SOCK_DGRAM; int error = getaddrinfo(hostName, NULL, &hints, &res); if (error) { cpLog(LOG_ERR, gai_strerror(error)); return(NetworkAddress::getHostLookupFailed); } return NetworkAddress::getHostLookupOK;#else#if defined(__svr4__) || defined(__FreeBSD__)|| defined (__SUNPRO_CC) || defined(__sun)|| defined(__QNX__) || defined(__APPLE__) struct addrinfo hints; // Setup structures memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME; hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; int error = getaddrinfo(hostName, NULL, &hints, &res); if (error) { cpLog(LOG_ERR, gai_strerror(error)); return(NetworkAddress::getHostLookupFailed); } return NetworkAddress::getHostLookupOK;#else#if defined (WIN32) struct hostent* x = 0; x = gethostbyname(hostName); if (x == 0) { cpLog(LOG_DEBUG, "********* Failed to get host by name:%s", hostName); return getHostLookupFailed; } *hEnt = *x; return getHostLookupOK;#else#error no implementation for critical function#endif#endif#endif}#if defined (WIN32)struct hostent FAR *_gethostbyname( const char FAR *hostName ){ struct hostent* x;#undef gethostbyname x = gethostbyname(hostName); if (x == 0) { int address = inet_addr(hostName); if( address != 0xFFFFFFFF ) { // Tried gethostbyaddr, but it did not seem to work...// x = gethostbyaddr((char *)&address, IPV4_LENGTH, AF_INET); if (x == 0) { struct hostentex : hostent { char FAR * hex_aliases; char hex_alias; char FAR * hex_addr_list; char hex_addr[IPV4_LENGTH]; char hex_name[IPV4_LENGTH * 4]; }; static struct hostentex entex; entex.h_name = entex.hex_name; entex.h_aliases = & entex.hex_aliases; entex.h_addrtype = AF_INET; entex.h_length = IPV4_LENGTH; entex.h_addr_list = & entex.hex_addr_list; entex.hex_aliases = & entex.hex_alias; entex.hex_alias = '\0'; entex.hex_addr_list = & entex.hex_addr[0]; memcpy((void *)entex.hex_addr, (void*)& address, IPV4_LENGTH); strncpy(entex.hex_name, hostName, IPV4_LENGTH - 1); entex.hex_name[IPV4_LENGTH - 1] = '\0'; x = & entex; } } } return x;}#endif/** * Return TRUE if the address is a valid IP address (dot-decimal form) * such as 128.128.128.128. Checks for invalid or ill formed addresses * If the address is valid, copy it into ipAddress */boolNetworkAddress::is_valid_ip_addr(const Data& addr) const{ return (is_valid_ip4_addr(addr) || is_valid_ip6_addr(addr));}boolNetworkAddress::is_valid_ip6_addr(const Data& addr){ cpLog (LOG_DEBUG_STACK, "is_valid_ip6_addr(%s)", addr.c_str()); bool double_colon = false; int pos = addr.find("["); if (pos!=Data::npos) { cpLog (LOG_DEBUG_STACK, "Invalid v6 address (found [)"); return false; } pos = addr.find(":"); if (pos!=Data::npos) { //"a.b.c.d:5060" pos = addr.find(":", pos+1); if(pos != Data::npos) { pos = addr.find("::", 0); if(pos != Data::npos) { double_colon = true; //ONLY "::" if (pos == (addr.length()-2)) { cpLog (LOG_DEBUG_STACK, "Invalid v6 address (invalid ::)"); return false; } //"::abcd::" pos = addr.find("::", pos+1); if (pos!=Data::npos) { cpLog (LOG_DEBUG_STACK, "Invalid v6 address"); return false; } } } else { return false; } } int count = 0; char* szInitialString = new char[255]; strncpy(szInitialString, addr.c_str(), 254); szInitialString[254] = '\0'; char* ptr; char* szTokOut = strtok_r(szInitialString, ":", &ptr); while(szTokOut) { if (!is_v6_string(szTokOut)) { cpLog (LOG_DEBUG_STACK, "Invalid v6 address (invalid string %s)", szTokOut); // return false; } count++; if (count > 8) { break; } szTokOut=strtok_r(NULL, ":", &ptr); }; if (double_colon) { cpLog (LOG_DEBUG_STACK, "Valid v6 address (contains ::)"); delete []szInitialString; return true; } if (count == 8) { cpLog (LOG_DEBUG_STACK, "Valid v6 address"); delete []szInitialString; return true; } cpLog (LOG_DEBUG_STACK, "Invalid v6 address"); delete []szInitialString; return false;}boolNetworkAddress::is_v6_string(const Data& addr){ if (addr.length() > 4) { return false; } int next = 0; Data tmp; char buf[5]; memcpy(buf, addr.c_str(), 4); while (next < addr.length()) { if (!isdigit(buf[next]) && !isxdigit(buf[next])) { return false; } next++; } return true;}boolNetworkAddress::is_valid_ip4_addr(const Data& addr) const{ cpLog (LOG_DEBUG_STACK, "is_valid_ip4_addr(%s)", addr.c_str());#if defined(WIN32) typedef ULONG ulong;#endif unsigned long maskcheck = ~255; // mask to check if 'tween 0 and 255 const char *advancer = addr.c_str(); unsigned long octet; char *nextchar; // always check for spaces and right number // first and last fields must be 1-255, middle two 0-255 if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t')) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } octet = strtoul(advancer, &nextchar, 10); if((*nextchar != '.') || (octet & maskcheck) || (octet == 0)) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } advancer = nextchar+1; if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t')) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } octet = strtoul(advancer, &nextchar, 10); if((*nextchar != '.') || (octet & maskcheck)) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } advancer = nextchar+1; if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t')) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } octet = strtoul(advancer, &nextchar, 10); if((*nextchar != '.') || (octet & maskcheck)) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } advancer = nextchar+1; if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t')) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } octet = strtoul(advancer, &nextchar, 10); if((*nextchar) || (octet & maskcheck) || (octet == 0)) { cpLog (LOG_DEBUG_STACK, "Invalid v4 address"); return false; } cpLog (LOG_DEBUG_STACK, "Valid v4 address"); return true;}DataNetworkAddress::getHostByAddress(Data& ipAddr) const{ Data retVal; struct addrinfo hints; struct addrinfo *res = 0; // Setup structures memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; Data the_host = ipAddr; if(the_host.find("[", 0) != Data::npos) the_host = ipAddr.substr(1, the_host.length()-2); bool hostNameIsAddress = false; hints.ai_family = NetworkConfig::instance().getAddrFamily(); if (is_valid_ip4_addr(the_host)) { hints.ai_family = PF_INET; hostNameIsAddress = true; } else if (is_valid_ip6_addr(the_host)) { hints.ai_family = PF_INET6; hostNameIsAddress = true; } retVal = the_host; if(hostNameIsAddress) { cpLog(LOG_DEBUG_STACK, "getaddrinfo():%s", the_host.c_str()); int error = getaddrinfo(the_host.c_str(), NULL, &hints, &res); if (error) { cpLog(LOG_ERR, gai_strerror(error)); if(res != 0) { freeAddrInfo(res); } return(the_host); } char hName[256+1]; memset(hName, 0, 256+1); //Get the host name of the address error = getnameinfo(res->ai_addr, res->ai_addrlen, hName, 256, NULL, 0, NI_NAMEREQD ); if (error) { cpLog(LOG_NOTICE, "Failed to resolve address (%s) to a name:%s", the_host.c_str(), hName); retVal = the_host; } else { retVal = hName; } freeAddrInfo(res); } return retVal;}voidNetworkAddress::freeAddrInfo(struct addrinfo* res){ struct addrinfo* tmp = res;#if defined (__SUNPRO_CC) //On Solaris freeaddrinfo leaks, not freeing ai_addr while(res) { free(res->ai_addr); res->ai_addr=0; res = res->ai_next; }#endif if(tmp != 0) { freeaddrinfo(tmp); }}// End of File#if defined (WIN32)char* formatWindowsError(char* buffer, int size){ int err = GetLastError(); FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR)buffer, size, NULL ); return buffer;}#endif /* Local Variables: *//* c-file-style: "stroustrup" *//* indent-tabs-mode: nil *//* c-file-offsets: ((access-label . -) (inclass . ++)) *//* c-basic-offset: 4 *//* End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -