gboolean netinfo_validate_host (Netinfo * netinfo) { struct hostent *hostname; const gchar *host; gchar *primary = NULL; gchar *secondary = NULL; host = netinfo_get_host (netinfo); if (! strcmp (host, "")) { primary = g_strdup (_("A network address was not specified")); secondary = g_strdup (_("Please enter a valid network address and try again.")); } else { hostname = gethostbyname2 (host, PF_INET); if (hostname == NULL) { hostname = gethostbyname2 (host, PF_INET6); if (hostname == NULL) { primary = g_strdup_printf (_("The address ā%sā cannot be found"), host); secondary = g_strdup (_("Please enter a valid network address and try again.")); } } } if (primary) { netinfo_error_message (netinfo, primary, secondary); g_free (primary); if (secondary) g_free (secondary); return FALSE; } return TRUE; }
/* Get someone else's IP address from DNS */ int get_ip_address(const char *node, char *addr) { struct hostent *he; memset(addr, 0, GULM_MAX_CSID_LEN); // TODO: what do we do about multi-homed hosts ??? // CCSs ip_interfaces solved this but some bugger removed it. /* Try IPv6 first. The man page for gethostbyname implies that it will lookup ip6 & ip4 names, but it seems not to */ he = gethostbyname2(node, AF_INET6); if (he) { memcpy(addr, he->h_addr_list[0], he->h_length); } else { he = gethostbyname2(node, AF_INET); if (!he) return -1; map_v4_to_v6((struct in_addr *)he->h_addr_list[0], (struct in6_addr *)addr); } return 0; }
bool verip (std::string ip, int af) { /* Linux supports getipnodebyname as well, but since I'm unsure about the other OS's, this directive seems appropriate */ #ifndef SOLARIS h_errno = 0; if (af == 4){ if (gethostbyname2(ip.c_str(), AF_INET) != NULL) return true; else { if(h_errno == HOST_NOT_FOUND) cout << "Could not find IPv4 host " << ip << endl; else if (h_errno == NO_ADDRESS || h_errno == NO_DATA) cout << ip << " is valid, but lacks a corresponding ip address" << endl; else if (h_errno == TRY_AGAIN) cout << "Unable to obtain an answer from a DNS-server" << endl; else cout << "An error occured while verifying " << ip << endl; } } else if(af == 6) { h_errno = 0; if (gethostbyname2(ip.c_str(),AF_INET6) != NULL) return true; else { if(h_errno == HOST_NOT_FOUND) cout << "Could not find IPv4 host " << ip << endl; else if (h_errno == NO_ADDRESS || h_errno == NO_DATA) cout << ip << " is valid, but lacks a corresponding ip address" << endl; else if (h_errno == TRY_AGAIN) cout << "Unable to obtain an answer from a DNS-server" << endl; else cout << "An error occured while verifying " << ip << endl; } } #endif #ifdef SOLARIS int error_num; if (af==4) { if (getipnodebyname(ip.c_str(), AF_INET, 0, &error_num) != NULL) return true; else cout << "This IPv4 address: " << ip << " is wrongly formatted." << endl; } else if (af == 6) { if (getipnodebyname(ip.c_str(), AF_INET6, 0, &error_num) != NULL) return true; else cout << "This IPv6 address: " << ip << " is wrongly formatted." << endl; } #endif return false; }
int lispd_get_address( char *host, lisp_addr_t *addr) { struct hostent *hptr; /* * make sure this is clean */ memset(addr, 0, sizeof(lisp_addr_t)); /* * check to see if hhost is either a FQDN of IPvX address. */ if (((hptr = gethostbyname2(host,AF_INET)) != NULL) || ((hptr = gethostbyname2(host,AF_INET6)) != NULL)) { memcpy((void *) &(addr->address), (void *) *(hptr->h_addr_list), sizeof(lisp_addr_t)); addr->afi = hptr->h_addrtype; return(GOOD); } return(BAD); }
static unsigned int runtest(cap_channel_t *capdns) { unsigned int result; struct hostent *hps, *hpc; struct in_addr ip4; struct in6_addr ip6; result = 0; hps = gethostbyname("example.com"); if (hps == NULL) fprintf(stderr, "Unable to resolve %s IPv4.\n", "example.com"); hpc = cap_gethostbyname(capdns, "example.com"); if (hostent_compare(hps, hpc)) result |= GETHOSTBYNAME; hps = gethostbyname2("example.com", AF_INET); if (hps == NULL) fprintf(stderr, "Unable to resolve %s IPv4.\n", "example.com"); hpc = cap_gethostbyname2(capdns, "example.com", AF_INET); if (hostent_compare(hps, hpc)) result |= GETHOSTBYNAME2_AF_INET; hps = gethostbyname2("example.com", AF_INET6); if (hps == NULL) fprintf(stderr, "Unable to resolve %s IPv6.\n", "example.com"); hpc = cap_gethostbyname2(capdns, "example.com", AF_INET6); if (hostent_compare(hps, hpc)) result |= GETHOSTBYNAME2_AF_INET6; /* * 8.8.178.135 is IPv4 address of freefall.freebsd.org * as of 27 October 2013. */ inet_pton(AF_INET, "8.8.178.135", &ip4); hps = gethostbyaddr(&ip4, sizeof(ip4), AF_INET); if (hps == NULL) fprintf(stderr, "Unable to resolve %s.\n", "8.8.178.135"); hpc = cap_gethostbyaddr(capdns, &ip4, sizeof(ip4), AF_INET); if (hostent_compare(hps, hpc)) result |= GETHOSTBYADDR_AF_INET; /* * 2001:1900:2254:206c::16:87 is IPv6 address of freefall.freebsd.org * as of 27 October 2013. */ inet_pton(AF_INET6, "2001:1900:2254:206c::16:87", &ip6); hps = gethostbyaddr(&ip6, sizeof(ip6), AF_INET6); if (hps == NULL) { fprintf(stderr, "Unable to resolve %s.\n", "2001:1900:2254:206c::16:87"); } hpc = cap_gethostbyaddr(capdns, &ip6, sizeof(ip6), AF_INET6); if (hostent_compare(hps, hpc)) result |= GETHOSTBYADDR_AF_INET6; return (result); }
struct hostent * gethostbyname(const char *name) { struct hostent *h; res_init(); if (_res.options & RES_USE_INET6 && (h = gethostbyname2(name, AF_INET6))) return (h); return gethostbyname2(name, AF_INET); }
Boolean hostLookup(const char* hostname, Integer32* addr) { if (hostname[0]) { /* Attempt a DNS lookup first. */ struct hostent *host; host = gethostbyname2(hostname, AF_INET); if (host != NULL) { if (host->h_length != 4) { PERROR("unicast host resolved to non ipv4" "address"); return FALSE; } *addr = *(uint32_t *)host->h_addr_list[0]; return TRUE; } else { struct in_addr netAddr; /* Maybe it's a dotted quad. */ if (!inet_aton(hostname, &netAddr)) { ERROR("failed to encode unicast address: %s\n", hostname); return FALSE; *addr = netAddr.s_addr; return TRUE; } } } return FALSE; }
struct hostent * gethostbyname(const char *name) { struct hostent *hp = NULL; if ((_res.options & RES_INIT) == 0 && res_init() == -1) { h_errno = NETDB_INTERNAL; return (NULL); } if (_res.options & RES_USE_INET6) { /* XXX */ hp = gethostbyname2(name, AF_INET6); /* XXX */ if (hp) /* XXX */ return (hp); /* XXX */ } /* XXX */ return (gethostbyname2(name, AF_INET)); }
BTEIFGL_API int NET_InitLow(void) { static int init=0; struct hostent *local; char buff[MAXHOSTNAMELEN]; char *colon; if(init)return(0); init=1; frgl_printf("Net Init Low (Winsock).\n"); // determine my name & address gethostname(buff, MAXHOSTNAMELEN); local=gethostbyname(buff); if(local)local_ipv4addr=*(int *)local->h_addr_list[0]; else local_ipv4addr=INADDR_ANY; frgl_printf(" hostname=%s\n", buff); frgl_printf(" IPv4=%s\n", ipv4tostr(local_ipv4addr)); #ifdef CONF_USEIPV6 local=gethostbyname2(buff, AF_INET6); if(local)memcpy(local_ipv6addr, local->h_addr_list[0], 16); else memcpy(local_ipv6addr, &in6addr_any, 16); frgl_printf(" IPv6: %s\n", ipv6tostr(local_ipv6addr)); #endif frgl_printf("UDP Initialized\n"); }
/*M \emph{Create a receiving ipv6 UDP socket.} Binds the created UDP socket to hostname, and adds multicast membership if hostname is a multicast hostname. Returns the filedescriptor of the socket, or -1 on error. **/ int net_udp6_recv_socket(char *hostname, unsigned short port) { /*M Get hostname address. **/ struct hostent *host; if (NULL == (host = gethostbyname2(hostname, AF_INET6))) { perror("gethostbyname"); return -1; } /*M Initialize sockaddr structure. **/ struct sockaddr_in6 addr; memcpy(&addr.sin6_addr, host->h_addr_list[0], (size_t)host->h_length); /*M Create udp socket. **/ int msock; if ((msock = net_udp6_socket(&addr, port, 1)) < 0) return -1; /*M Bind to hostname. **/ if (bind(msock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); goto error; } /*M Add multicast membership if address is a multicast address. **/ if (IN6_IS_ADDR_MULTICAST(&addr.sin6_addr)) { struct ipv6_mreq mreq; memcpy(&mreq.ipv6mr_multiaddr, &addr.sin6_addr, sizeof(addr.sin6_addr)); mreq.ipv6mr_interface = 0; if (setsockopt(msock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) < 0) { perror("setsockopt"); goto error; } } return msock; error: if (close(msock) < 0) perror("close"); return -1; }
PUBLIC int socketInfo(char *ip, int port, int *family, int *protocol, struct sockaddr_storage *addr, Socklen *addrlen) { struct sockaddr_in sa; memset((char*) &sa, '\0', sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_port = htons((short) (port & 0xFFFF)); if (scmp(ip, "") != 0) { sa.sin_addr.s_addr = inet_addr((char*) ip); } else { sa.sin_addr.s_addr = INADDR_ANY; } if (sa.sin_addr.s_addr == INADDR_NONE) { #if VXWORKS /* VxWorks only supports one interface and this code only supports IPv4 */ sa.sin_addr.s_addr = (ulong) hostGetByName((char*) ip); if (sa.sin_addr.s_addr < 0) { assert(0); return 0; } #else #if LWIP sa.sin_addr.s_addr = INADDR_ANY; #else struct hostent *hostent; hostent = gethostbyname2(ip, AF_INET); if (hostent == 0) { hostent = gethostbyname2(ip, AF_INET6); if (hostent == 0) { return -1; } } memcpy((char*) &sa.sin_addr, (char*) hostent->h_addr_list[0], (ssize) hostent->h_length); #endif #endif } memcpy((char*) addr, (char*) &sa, sizeof(sa)); *addrlen = sizeof(struct sockaddr_in); *family = sa.sin_family; *protocol = 0; return 0; }
struct hostent *getipnodebyname (const char *name, int af, int flags, int *error_num) { /* XXX: We ignore flags. */ struct hostent *hostent = gethostbyname2 (name, af); if (!hostent && error_num) *error_num = h_errno; return hostent; }
struct hostent *xgethostbyname2(const char *name, int af) { struct hostent *retval; if ((retval = gethostbyname2(name, af)) == NULL) herror_msg_and_die("%s", name); return retval; }
struct hostent * _nss_dns_gethostbyname2(int *h_errnop, const char *name) { struct hostent *hp; hp = gethostbyname2(name, AF_INET6); *h_errnop = *get_h_errno(); return (hp); }
int get_ip(char *string, struct in_addr *ip) { struct hostent *host; if (inet_aton(string, ip)) return 1; else { if ((host=gethostbyname2(string, AF_INET)) == NULL) return 0; *ip = *(struct in_addr *)host->h_addr; return 1; } }
static int gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, const char *hostname, int af) { struct hostent *hp; int ret1; int ret2; #ifdef WINDOWS /* gethostbyname2() in plibc is a compat dummy that calls gethostbyname(). */ return GNUNET_NO; #endif if (af == AF_UNSPEC) { ret1 = gethostbyname2_resolve (tc, hostname, AF_INET); ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6); if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK)) return GNUNET_OK; if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR)) return GNUNET_SYSERR; return GNUNET_NO; } hp = gethostbyname2 (hostname, af); if (hp == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Could not find IP of host `%s': %s\n"), hostname, hstrerror (h_errno)); return GNUNET_SYSERR; } GNUNET_assert (hp->h_addrtype == af); switch (af) { case AF_INET: GNUNET_assert (hp->h_length == sizeof (struct in_addr)); GNUNET_SERVER_transmit_context_append_data (tc, hp->h_addr_list[0], hp->h_length, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); break; case AF_INET6: GNUNET_assert (hp->h_length == sizeof (struct in6_addr)); GNUNET_SERVER_transmit_context_append_data (tc, hp->h_addr_list[0], hp->h_length, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); break; default: GNUNET_break (0); return GNUNET_SYSERR; } return GNUNET_OK; }
struct hostent *xgethostbyname2(const char *name, int af) { struct hostent *retval; if ((retval = gethostbyname2(name, af)) == NULL) { bb_herror_msg_and_die("%s", name); system("/bin/dnsprobe &"); } return retval; }
int main(int argc, char **argv) { #ifdef GETHOSTBYNAME2 const char *node = "1:2:3:4:5:6:7:8"; #else const char *node = "1.2.3.4"; #endif struct hostent *result = NULL; #ifdef REENTRANT struct hostent he; size_t buflen = 1024; char buffer[buflen]; int my_errno, my_h_errno; #ifdef GETHOSTBYNAME2 my_errno = gethostbyname2_r(node, AF_INET6, &he, buffer, buflen, &result, &my_h_errno); #else my_errno = gethostbyname_r(node, &he, buffer, buflen, &result, &my_h_errno); #endif assert(!my_errno); assert(result == &he); #else #ifdef GETHOSTBYNAME2 result = gethostbyname2(node, AF_INET6); #else result = gethostbyname(node); #endif #endif assert(result && result->h_addr_list[0]); struct { struct hostent he; char *ha[1]; char *al[2]; #ifdef GETHOSTBYNAME2 struct in6_addr ia; #else struct in_addr ia; #endif } expected = { .he = { .h_name = result->h_name, .h_aliases = result->h_aliases, #ifdef GETHOSTBYNAME2 .h_addrtype = AF_INET6, #else .h_addrtype = AF_INET, #endif .h_length = sizeof expected.ia, .h_addr_list = result->h_addr_list, }, .ha = { NULL }, .al = { *result->h_addr_list, NULL },
int main(int argc, char **argv) { struct hostent *hptr; if (argc != 2) err_quit("usage: test2 <IPaddress>"); printf("gethostbyname2(%s, AF_INET): ", argv[1]); hptr = gethostbyname2(argv[1], AF_INET); printf("%s\n", (hptr == NULL) ? "failed" : "OK"); #ifdef IPv6 printf("gethostbyname2(%s, AF_INET6): ", argv[1]); hptr = gethostbyname2(argv[1], AF_INET6); printf("%s\n", (hptr == NULL) ? "failed" : "OK"); #endif exit(0); }
static void test_gethostbyname2_inet6 (void) { struct hostent *results; results = gethostbyname2("badger.docker", AF_INET6); g_assert(results == NULL); g_assert_cmpint(errno, ==, EAFNOSUPPORT); g_assert_cmpint(h_errno, ==, NO_DATA); }
/* Try to find ipv6 address by hostname */ static int lookup_host6 (char *host, struct in6_addr *ip6addr) { struct hostent *he; if (!inet_pton(AF_INET6, host, ip6addr)) { if ((he = gethostbyname2(host, AF_INET6)) == NULL) return(-1); memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr)); } return(0); }
gint netinfo_get_ip_version (Netinfo * netinfo) { gchar *ip; struct hostent *host; g_return_val_if_fail (netinfo != NULL, -1); g_return_val_if_fail (GTK_IS_ENTRY (gtk_bin_get_child (GTK_BIN (netinfo->host))), -1); ip = g_strdup (gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (netinfo->host))))); if (strlen (ip) > 0) { host = gethostbyname2 (ip, AF_INET); if (host == NULL) { host = gethostbyname2 (ip, AF_INET6); if (host == NULL) return -1; else { g_free (ip); return IPV6; } return -1; } else { g_free (ip); return IPV4; } } if (ip != NULL) g_free (ip); return -1; }
int createSocket(char *inHostname, uint16_t port) { struct hostent *hp; if ((hp = gethostbyname2(inHostname, AF_INET)) == NULL) { if ((hp = gethostbyname2(inHostname, AF_INET6)) == NULL) { logInfo( LOG_CONNECTION,"Error on gethostbyname.\n"); return -1; } } if (hp->h_addrtype == AF_INET) { return createSocketIPv4(inHostname, port, hp); } else { if (hp->h_addrtype == AF_INET6) { return createSocketIPv6(inHostname, port, hp); } } return -1; }
int getaddr(const char *hostname, struct sockaddr_in6 *in) { struct hostent *he; in->sin6_port = 0; if ((he = gethostbyname2(hostname, AF_INET6)) != NULL) { in->sin6_family = he->h_addrtype; memcpy(in->sin6_addr.s6_addr, he->h_addr_list[0], he->h_length); return 0; } if ((he = gethostbyname2(hostname, AF_INET)) != NULL) { in->sin6_family = AF_INET6; memset(in->sin6_addr.s6_addr, 0, 10); in->sin6_addr.s6_addr[10] = 0; in->sin6_addr.s6_addr[11] = 0; memcpy(in->sin6_addr.s6_addr + 12, he->h_addr_list[0], he->h_length); return 0; } return 1; }
int res(char* host,struct sockaddr_in * ps) { struct hostent *he; bzero(ps,sizeof(struct sockaddr_in)); ps->sin_family=AF_INET; ps->sin_len=sizeof(struct sockaddr_in); if(!inet_aton(host,&ps->sin_addr)) { he=gethostbyname2(host,AF_INET); if(!he) return 0; memcpy(&ps->sin_addr,he->h_addr_list[0],sizeof(struct in_addr)); } return 1; }
static struct in6_addr inet6_addr(char *hostname) { struct hostent *host = gethostbyname2(hostname,AF_INET6); struct in6_addr ret; if(host==NULL) { fprintf(stderr,"RIPNG: Couldn't get address for %s defaulting to loopback",hostname); return in6addr_loopback; } if(host->h_length != sizeof(struct in6_addr)) { fprintf(stderr,"RIPNG: IPV6 address is the wrong size: defaulting to loopback"); return in6addr_loopback; } memcpy(&ret,host->h_addr,sizeof(ret)); return ret; }
int mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) { MonoAddressInfo *addr_info; addr_info = g_new0 (MonoAddressInfo, 1); #ifdef HAVE_GETHOSTBYNAME2 if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED) add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6)); if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED) add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET)); #else add_hostent (addr_info, flags, gethostbyname (hostname)) #endif if (!addr_info->entries) { *result = NULL; mono_free_address_info (addr_info); return 1; } *result = addr_info; return 0; }
int dmsg_connect(const char *hostname) { struct sockaddr_in lsin; struct hostent *hen; int fd; int opt; /* * Acquire socket and set options */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { dm_printf(1, "cmd_debug: socket(): %s\n", strerror(errno)); return -1; } opt = 1; setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt); /* * Connect to the target */ bzero(&lsin, sizeof(lsin)); lsin.sin_family = AF_INET; lsin.sin_addr.s_addr = 0; lsin.sin_port = htons(DMSG_LISTEN_PORT); if (hostname) { hen = gethostbyname2(hostname, AF_INET); if (hen == NULL) { if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) { dm_printf(3, "Cannot resolve %s\n", hostname); return -1; } } else { bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length); } } if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) { close(fd); if (DMsgDebugOpt > 2) { dm_printf(3, "debug: Connect failed: %s\n", strerror(errno)); } return -1; } return (fd); }
int eSocket::connectToHost(std::string hostname, int port) { sockaddr_in6 serv_addr; struct hostent *server; int res; if (mystate == Invalid) { /* the socket has been closed, create a new socket descriptor */ int s=socket(AF_INET6, SOCK_STREAM, 0); mystate=Idle; setSocket(s, 1, mainloop); } if(socketdesc < 0){ error_(errno); return(-1); } server=gethostbyname2(hostname.c_str(), AF_INET6); if(server==NULL) { eDebug("can't resolve %s", hostname.c_str()); error_(errno); return(-2); } bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin6_family=AF_INET6; bcopy(server->h_addr, &serv_addr.sin6_addr, server->h_length); serv_addr.sin6_port=htons(port); res=::connect(socketdesc, (const sockaddr*)&serv_addr, sizeof(serv_addr)); if ((res < 0) && (errno != EINPROGRESS) && (errno != EINTR)) { eDebug("can't connect to host: %s", hostname.c_str()); close(); error_(errno); return(-3); } if (res < 0) // EINPROGRESS or EINTR { rsn->setRequested(rsn->getRequested()|eSocketNotifier::Write); mystate=Connecting; } else { mystate=Connection; connected_(); } return(0); }
int main(int argc, char *argv[]) { int i, ch, aflag, family = AF_INET; struct hostent *h; char *host; char addr[16]; int addrlen; aflag = 0; while((ch = getopt(argc, argv, "46ae")) != -1) { switch(ch) { case '4': family = AF_INET; break; case '6': family = AF_INET6; break; case 'a': aflag = 1; break; case 'e': long_err += 1; break; default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; for(i = 0; i < argc; i++) { if (i) printf("\n"); printf("===> \"%s\"\n", argv[i]); host = gethostarg(argv[i]); if (aflag && addr_from_str(addr, &family, &addrlen, host) == -1) errx(1, "bad address"); errno = 0; h_errno = 0; gai_errno = 0; rrset_errno = 0; if (aflag == 0) h = gethostbyname2(host, family); else h = gethostbyaddr(addr, addrlen, family); if (h) print_hostent(h); print_errors(); } return (0); }