📄 address.cpp
字号:
#define ATOI(x) if ((x >= 48) && (x <= 57)) x = x-48; /* 0-9 */ \ else if ((x >= 97) && (x <=102)) x = x-87; /* a-f */ \ else if ((x >= 65) && (x <= 70)) x = x-55; /* A-F */ \ else x=0// parse a coloned stringint IpAddress::parse_coloned_ipstring(const char *inaddr){ ADDRESS_TRACE; unsigned char tmp_address_buffer[ADDRBUF]; char temp[60]; // temp buffer for destruction // check len, an ipv6 can never be bigger than 39 // 123456789012345678901234567890123456789 // 1BCD:2BCD:3BCD:4BCD:5BCD:6BCD:7BCD:8BCD if (!inaddr || (strlen(inaddr) > 60)) return FALSE; strcpy(temp, inaddr); trim_white_space(temp); if (strlen(temp) > 39) return FALSE; char *in_ptr = temp; char *out_ptr = (char*)tmp_address_buffer; char *end_first_part = NULL; char second[39]; int second_used = FALSE; int colon_count = 0; int had_double_colon = FALSE; int last_was_colon = FALSE; int had_dot = FALSE; int dot_count = 0; int digit_count = 0; char digits[4]; char last_deliminiter = 0; while (*in_ptr != 0) { if (*in_ptr == '.') { last_deliminiter = *in_ptr; had_dot = TRUE; dot_count++; if (dot_count > 3) return FALSE; if ((digit_count > 3) || (digit_count < 1)) return FALSE; for (int i=0; i<digit_count; i++) if (!my_isdigit(digits[i])) return FALSE; digits[digit_count] = 0; int value = atoi(digits); if ((value > 0) && (value <= 255)) *out_ptr++ = (unsigned char) value; else { if (strcmp(digits, "0") == 0) *out_ptr++ = (unsigned char) 0; else return FALSE; } digit_count = 0; } else if (*in_ptr == ':') { last_deliminiter = *in_ptr; if (had_dot) return FALSE; // don't allow : after a dot if (digit_count) { // move digits to right { for (int i=0; i<digit_count; i++) { ATOI(digits[digit_count - 1 - i]); digits[3-i] = digits[digit_count - 1 - i]; } } { for (int i=0; i<4-digit_count; i++) digits[i] = 0; } { // pack two digits into one byte for (int i=0; i < 4; i += 2) { unsigned char c = digits[i]; unsigned char d = digits[i+1]; *out_ptr++ = (c*16 + d); } } digit_count = 0; } colon_count++; if (last_was_colon) { if (had_double_colon) return FALSE; end_first_part = out_ptr; out_ptr = second; second_used = TRUE; had_double_colon = TRUE; } else { last_was_colon = TRUE; } } else { if (digit_count >= 4) return FALSE; if (!isxdigit(*in_ptr)) return FALSE; digits[digit_count] = tolower(*in_ptr); digit_count++; if (digit_count > 4) return FALSE; last_was_colon = 0; } in_ptr++; } // put last bytes from digits into buffer if (digit_count) { if (last_deliminiter == ':') { { // move digits to right for (int i=0; i<digit_count; i++) { ATOI(digits[digit_count - 1 - i]); digits[3-i] = digits[digit_count - 1 - i]; } } { for (int i=0; i<4-digit_count; i++) digits[i] = 0; } { // pack two digits into one byte for (int i=0; i < 4; i += 2) { unsigned char c = digits[i]; unsigned char d = digits[i+1]; *out_ptr++ = (c*16 + d); } } digit_count = 0; } else if (last_deliminiter == '.') { if ((digit_count > 3) || (digit_count < 1)) return FALSE; for (int i=0; i<digit_count; i++) if (!my_isdigit(digits[i])) return FALSE; digits[digit_count] = 0; int value = atoi(digits); if ((value > 0) && (value <= 255)) *out_ptr++ = (unsigned char) value; else { if (strcmp(digits, "0") == 0) *out_ptr++ = (unsigned char) 0; else return FALSE; } digit_count = 0; } else return FALSE; } // must have between two and seven colons if ((colon_count > 7) || (colon_count < 2)) return FALSE; // if there was a dot there must be three of them if ((dot_count > 0) && (dot_count != 3)) return FALSE; if (second_used) { int len_first = SAFE_INT_CAST(end_first_part - (char*)tmp_address_buffer); int len_second = SAFE_INT_CAST(out_ptr - second); int i=0; for (i=0; i<IP6LEN-(len_first + len_second); i++) *end_first_part++ = 0; for (i=0; i<len_second; i++) *end_first_part++ = second[i]; } if (!end_first_part) end_first_part = out_ptr; // check for short address if (end_first_part - (char*)tmp_address_buffer != IP6LEN) return FALSE; ip_version = version_ipv6; smival.value.string.len = IP6LEN; memcpy(address_buffer, tmp_address_buffer, ADDRBUF); return TRUE;}#undef ATOI//-----[ IP Address parse Address ]---------------------------------bool IpAddress::parse_address(const char *inaddr){ ADDRESS_TRACE;#if !defined HAVE_GETHOSTBYNAME_R && !defined HAVE_REENTRANT_GETHOSTBYNAME#ifdef _THREADS SnmpSynchronize s(syscall_mutex);#endif#endif addr_changed = true; // parse the input char array fill up internal buffer with four ip // bytes set and return validity flag char ds[48]; // intialize the friendly_name member variable memset(iv_friendly_name, 0, sizeof(char) * MAX_FRIENDLY_NAME); iv_friendly_name_status = 0; // is this a dotted IP notation string or a friendly name if (parse_dotted_ipstring(inaddr)) { // since this is a valid dotted string don't do any DNS return TRUE; } else if (parse_coloned_ipstring(inaddr)) { // since this is a valid ipv6 string don't do any DNS return TRUE; } else // not a dotted string, try to resolve it via DNS {#if defined (CPU) && CPU == PPC603 int lookupResult = hostGetByName(inaddr); if (lookupResult == ERROR) { iv_friendly_name_status = lookupResult; return FALSE; } // now lets check out the dotted string strcpy(ds,inet_ntoa(lookupResult)); if (!parse_dotted_ipstring(ds)) return FALSE; // save the friendly name strcpy(iv_friendly_name, inaddr); return TRUE;#else hostent *lookupResult = 0;#ifdef HAVE_GETHOSTBYNAME_R char buf[2048]; // TODO: Too big buffer? int herrno = 0; hostent lookup_buf;#if defined(__sun) || defined (__QNX_NEUTRINO) lookupResult = gethostbyname_r(inaddr, &lookup_buf, buf, 2048, &herrno);#else gethostbyname_r(inaddr, &lookup_buf, buf, 2048, &lookupResult, &herrno);#endif#ifdef SNMP_PP_IPv6 if (!lookupResult) {#ifdef __sun lookupResult = gethostbyname_r(inaddr, AF_INET6, &lookup_buf, buf, 2048, &lookupResult, &herrno);#else gethostbyname2_r(inaddr, AF_INET6, &lookup_buf, buf, 2048, &lookupResult, &herrno);#endif }#endif // SNMP_PP_IPv6#else // not HAVE_GETHOSTBYNAME_R lookupResult = gethostbyname(inaddr);#ifdef SNMP_PP_IPv6 if (!lookupResult) {#ifdef HAVE_GETHOSTBYNAME2 lookupResult = gethostbyname2(inaddr, AF_INET6);#else lookupResult = gethostbyname(inaddr);#endif // HAVE_GETHOSTBYNAME2 }#endif // SNMP_PP_IPv6#endif // HAVE_GETHOSTBYNAME_R if (lookupResult) {#ifdef SNMP_PP_IPv6 if (lookupResult->h_length == sizeof(in6_addr)) { in6_addr ipAddr; memcpy((void *) &ipAddr, (void *) lookupResult->h_addr, sizeof(in6_addr)); // now lets check out the coloned string if (!inet_ntop(AF_INET6, &ipAddr, ds, 48)) return FALSE; debugprintf(4, "from inet_ntop: %s", ds); if (!parse_coloned_ipstring(ds)) return FALSE; // save the friendly name strcpy(iv_friendly_name, inaddr); return TRUE; }#endif // SNMP_PP_IPv6 if (lookupResult->h_length == sizeof(in_addr)) { in_addr ipAddr; memcpy((void *) &ipAddr, (void *) lookupResult->h_addr, sizeof(in_addr)); // now lets check out the dotted string strcpy(ds,inet_ntoa(ipAddr)); if (!parse_dotted_ipstring(ds)) return FALSE; // save the friendly name strcpy(iv_friendly_name, inaddr); return TRUE; } } // end if lookup result else {#ifdef HAVE_GETHOSTBYNAME_R iv_friendly_name_status = herrno;#else iv_friendly_name_status = h_errno;#endif return FALSE; }#endif //PPC603 } // end else not a dotted string return TRUE;}// using the currently defined address, do a DNS// and try to fill up the nameint IpAddress::addr_to_friendly(){ ADDRESS_TRACE;#if !defined HAVE_GETHOSTBYADDR_R && !defined HAVE_REENTRANT_GETHOSTBYADDR#ifdef _THREADS SnmpSynchronize s(syscall_mutex);#endif#endif#if defined (CPU) && CPU == PPC603 int lookupResult; char hName[MAXHOSTNAMELEN+1];#else hostent *lookupResult;#endif char ds[48]; // can't look up an invalid address if (!valid_flag) return -1; // lets try and get the friendly name from the DNS strcpy(ds, this->IpAddress::get_printable());#if !(defined (CPU) && CPU == PPC603) && defined HAVE_GETHOSTBYADDR_R int herrno = 0; hostent lookup; char buf[2048]; // TODO: Buf size too big?#endif if (ip_version == version_ipv4) { in_addr ipAddr;#if defined HAVE_INET_ATON if (inet_aton((char*)ds, &ipAddr) == 0) return -1; // bad address#elif defined HAVE_INET_PTON if (inet_pton(AF_INET, (char*)ds, &ipAddr) <= 0) return -1; // bad address#else ipAddr.s_addr = inet_addr((char*)ds); if (ipAddr.s_addr == INADDR_NONE) return -1; // bad address#endif#if defined (CPU) && CPU == PPC603 lookupResult = hostGetByAddr(ipAddr.s_addr, hName);#elif defined HAVE_GETHOSTBYADDR_R#if defined(__sun) || defined(__QNX_NEUTRINO) lookupResult = gethostbyaddr_r((char *) &ipAddr, sizeof(in_addr), AF_INET, &lookup, buf, 2048, &herrno);#else gethostbyaddr_r((char *) &ipAddr, sizeof(in_addr), AF_INET, &lookup, buf, 2048, &lookupResult, &herrno);#endif#else lookupResult = gethostbyaddr((char *) &ipAddr, sizeof(in_addr), AF_INET);#endif } else {#ifdef SNMP_PP_IPv6 in6_addr ipAddr; if (inet_pton(AF_INET6, (char*)ds, &ipAddr) <= 0) return -1; // bad address#if defined (CPU) && CPU == PPC603 lookupResult = hostGetByAddr(ipAddr.s_addr, hName);#elif defined HAVE_GETHOSTBYADDR_R#if defined(__sun) || defined(__QNX_NEUTRINO) lookupResult = gethostbyaddr_r((char *) &ipAddr, sizeof(in_addr), AF_INET6, &lookup, buf, 2048, &herrno);#else gethostbyaddr_r((char *) &ipAddr, sizeof(in_addr), AF_INET6, &lookup, buf, 2048, &lookupResult, &herrno);#endif#else lookupResult = gethostbyaddr((char *) &ipAddr, sizeof(in6_addr), AF_INET6);#endif // HAVE_GETHOSTBYADDR_R#else return -1;#endif // SNMP_PP_IPv6 } // if we found the name, then update the iv friendly name#if defined (CPU) && CPU == PPC603 if (lookupResult != ERROR) { strncpy(iv_friendly_name, hName, MAX_FRIENDLY_NAME); return 0; } else { iv_friendly_name_status = lookupResult; return lookupResult; } return -1; //should not get here#else if (lookupResult) { strcpy(iv_friendly_name, lookupResult->h_name); return 0; } else {#ifdef HAVE_GETHOSTBYADDR_R iv_friendly_name_status = herrno;#else iv_friendly_name_status = h_errno;#endif return iv_friendly_name_status; }#endif //PPC603}//----[ IP address format output ]------------------------------------void IpAddress::format_output() const{ ADDRESS_TRACE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -