char * netsnmp_ipv4_fmtaddr(const char *prefix, netsnmp_transport *t, void *data, int len) { netsnmp_indexed_addr_pair *addr_pair = NULL; struct hostent *host; char tmp[64]; if (data != NULL && len == sizeof(netsnmp_indexed_addr_pair)) { addr_pair = (netsnmp_indexed_addr_pair *) data; } else if (t != NULL && t->data != NULL) { addr_pair = (netsnmp_indexed_addr_pair *) t->data; } if (addr_pair == NULL) { snprintf(tmp, sizeof(tmp), "%s: unknown", prefix); } else { struct sockaddr_in *to = NULL; to = (struct sockaddr_in *) &(addr_pair->remote_addr); if (to == NULL) { snprintf(tmp, sizeof(tmp), "%s: unknown->[%s]:%hu", prefix, inet_ntoa(addr_pair->local_addr.sin.sin_addr), ntohs(addr_pair->local_addr.sin.sin_port)); } else if (t && t->flags & NETSNMP_TRANSPORT_FLAG_C) { int flags = 0; if (!(t->flags & NETSNMP_TRANSPORT_FLAG_HOSTNAME)) flags |= NI_NUMERICHOST; int res = getnameinfo((struct sockaddr *)to, sizeof(struct sockaddr_in), tmp, sizeof(tmp), NULL, 0, flags); DEBUGMSGTL(("netsnmp_ipv4_fmtaddr", "flags=%d, res=%d, tmp=%s\n", flags, res, tmp)); if (res == EAI_AGAIN) { flags |= NI_NUMERICHOST; res = getnameinfo((struct sockaddr *)to, sizeof(struct sockaddr_in6), tmp, sizeof(tmp), NULL, 0, flags); } if (res != 0) return NULL; } else if ( t && t->flags & NETSNMP_TRANSPORT_FLAG_HOSTNAME ) { /* XXX: hmm... why isn't this prefixed */ /* assuming intentional */ host = netsnmp_gethostbyaddr((char *)&to->sin_addr, 4, AF_INET); return (host ? strdup(host->h_name) : NULL); } else { snprintf(tmp, sizeof(tmp), "%s: [%s]:%hu->", prefix, inet_ntoa(to->sin_addr), ntohs(to->sin_port)); snprintf(tmp + strlen(tmp), sizeof(tmp)-strlen(tmp), "[%s]:%hu", inet_ntoa(addr_pair->local_addr.sin.sin_addr), ntohs(addr_pair->local_addr.sin.sin_port)); } } tmp[sizeof(tmp)-1] = '\0'; return strdup(tmp); }
char * netsnmp_ipv4_fmtaddr(const char *prefix, netsnmp_transport *t, void *data, int len) { netsnmp_indexed_addr_pair *addr_pair = NULL; struct hostent *host; char tmp[64]; if (data != NULL && len == sizeof(netsnmp_indexed_addr_pair)) { addr_pair = (netsnmp_indexed_addr_pair *) data; } else if (t != NULL && t->data != NULL) { addr_pair = (netsnmp_indexed_addr_pair *) t->data; } if (addr_pair == NULL) { snprintf(tmp, sizeof(tmp), "%s: unknown", prefix); } else { struct sockaddr_in *to = NULL; to = (struct sockaddr_in *) &(addr_pair->remote_addr); if (to == NULL) { snprintf(tmp, sizeof(tmp), "%s: unknown->[%s]:%hu", prefix, inet_ntoa(addr_pair->local_addr.sin.sin_addr), ntohs(addr_pair->local_addr.sin.sin_port)); } else if ( t && t->flags & NETSNMP_TRANSPORT_FLAG_HOSTNAME ) { /* XXX: hmm... why isn't this prefixed */ /* assuming intentional */ host = netsnmp_gethostbyaddr((char *)&to->sin_addr, sizeof(struct in_addr), AF_INET); return (host ? strdup(host->h_name) : NULL); } else { snprintf(tmp, sizeof(tmp), "%s: [%s]:%hu->", prefix, inet_ntoa(to->sin_addr), ntohs(to->sin_port)); snprintf(tmp + strlen(tmp), sizeof(tmp)-strlen(tmp), "[%s]:%hu", inet_ntoa(addr_pair->local_addr.sin.sin_addr), ntohs(addr_pair->local_addr.sin.sin_port)); } } tmp[sizeof(tmp)-1] = '\0'; return strdup(tmp); }
char * inet6name(const unsigned char *in6) { char *cp; static char line[NI_MAXHOST]; static char domain[MAXHOSTNAMELEN]; static int first = 1; #ifdef NETSNMP_ENABLE_IPV6 struct hostent *hp; char hbuf[NI_MAXHOST]; const int niflag = NI_NUMERICHOST; struct sockaddr_in6 sin6; const struct in6_addr *in6p = (const struct in6_addr *)in6; #endif if (first && !nflag) { first = 0; if (gethostname(domain, sizeof(domain)) == 0 && (cp = strchr(domain, '.'))) (void) strlcpy(domain, cp + 1, sizeof domain); else domain[0] = '\0'; } #ifdef NETSNMP_ENABLE_IPV6 cp = NULL; if (!nflag && !IN6_IS_ADDR_UNSPECIFIED(in6p)) { hp = netsnmp_gethostbyaddr((const char *)in6p, sizeof(*in6p), AF_INET6); if (hp) { if ((cp = strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = 0; cp = hp->h_name; } } if (IN6_IS_ADDR_UNSPECIFIED(in6p)) strlcpy(line, "*", sizeof(line)); else if (cp) strlcpy(line, cp, sizeof(line)); else { memset(&sin6, 0, sizeof(sin6)); /* sin6.sin6_len = sizeof(sin6); */ sin6.sin6_family = AF_INET6; sin6.sin6_addr = *in6p; #ifdef __KAME__ if (IN6_IS_ADDR_LINKLOCAL(in6p) || IN6_IS_ADDR_MC_LINKLOCAL(in6p)) { sin6.sin6_scope_id = ntohs(*(const uint16_t *)&in6p->s6_addr[2]); sin6.sin6_addr.s6_addr[2] = 0; sin6.sin6_addr.s6_addr[3] = 0; } #endif if (getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), hbuf, sizeof(hbuf), NULL, 0, niflag) != 0) strlcpy(hbuf, "?", sizeof hbuf); strlcpy(line, hbuf, sizeof(line)); } #else strlcpy(line, "[[XXX - inet6 address]]", sizeof(line)); #endif return (line); }
/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. */ char * inetname(struct in_addr *inp) { char *cp; static char line[50]; struct hostent *hp; struct netent *np; static char domain[MAXHOSTNAMELEN]; static int first = 1; #if defined (WIN32) || defined (cygwin) char host_temp[] = "localhost"; #endif if (first && !nflag) { first = 0; if (gethostname(domain, sizeof(domain)) == 0 && (cp = strchr(domain, '.'))) (void) strlcpy(domain, cp + 1, sizeof domain); else domain[0] = '\0'; } cp = NULL; if (!nflag && inp->s_addr != INADDR_ANY) { int net = inet_netof(*inp); int lna = inet_lnaof(*inp); if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp == NULL) { hp = netsnmp_gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); if (hp) { if ((cp = strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = '\0'; #if defined (WIN32) || defined (cygwin) /* Windows insists on returning the computer name for 127.0.0.1 * even if the hosts file lists something else such as 'localhost'. * If we are trying to look up 127.0.0.1, just return 'localhost' */ if (!strcmp(inet_ntoa(*inp),"127.0.0.1")) cp = host_temp; else #endif cp = hp->h_name; } } } if (inp->s_addr == INADDR_ANY) snprintf(line, sizeof line, "*"); else if (cp) snprintf(line, sizeof line, "%s", cp); else { inp->s_addr = ntohl(inp->s_addr); #define C(x) (unsigned)((x) & 0xff) snprintf(line, sizeof line, "%u.%u.%u.%u", C(inp->s_addr >> 24), C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); } return (line); }