/* ** Searches the DNS mapping cache for #name#, adding a new entry if needed. ** Returns a pointer to the mapping entry, or NULL on error. */ static const struct hostent* LookupByName(const char *name) { char **cachedName; char **extendedAliases; struct hostent *nameEntry; int i; int listLen; for(i = 0; i < cacheCount; i++) { if(strcasecmp(name, cache[i].h_name) == 0) { return &cache[i]; } for(cachedName = cache[i].h_aliases; *cachedName != NULL; cachedName++) { if(strcasecmp(*cachedName, name) == 0) { return &cache[i]; } } } sethostent(0); nameEntry = gethostbyname(name); if(nameEntry == NULL) { endhostent(); return NULL; } else if(nameEntry->h_length != sizeof(struct in_addr)) { endhostent(); return NULL; /* We don't (yet) handle non-in_addr addresses. */ } /* We extend cached entries' h_aliases lists to include nicknames. */ for(i = 0; i < cacheCount; i++) { if(strcmp(nameEntry->h_name, cache[i].h_name) == 0) { for(listLen = 0; cache[i].h_aliases[listLen] != NULL; listLen++) { ; /* Nothing more to do. */ } extendedAliases = (char **)REALLOC(cache[i].h_aliases, sizeof(char **) * (listLen + 2)); if(extendedAliases != NULL) { extendedAliases[listLen] = strdup(name); extendedAliases[listLen + 1] = NULL; cache[i].h_aliases = extendedAliases; } endhostent(); return &cache[i]; } } nameEntry = CacheHostent(nameEntry); endhostent(); return nameEntry; }
void endnetent(void) { endhostent(); __endnetent(); }
/* * Record login in wtmp file. */ void dologin(struct passwd *pw, struct sockaddr_in *sin) { char line[32]; char remotehost[32]; int wtmp, f; struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, sizeof (struct in_addr), AF_INET); if (hp) { strncpy(remotehost, hp->h_name, sizeof (remotehost)); endhostent(); } else strncpy(remotehost, inet_ntoa(sin->sin_addr), sizeof (remotehost)); sprintf(line, "uucp%.4d", getpid()); logwtmp (line, pw->pw_name, remotehost); #if defined (PATH_LASTLOG) && defined (HAVE_STRUCT_LASTLOG) #define SCPYN(a, b) strncpy(a, b, sizeof (a)) if ((f = open(PATH_LASTLOG, O_RDWR)) >= 0) { struct lastlog ll; time(&ll.ll_time); lseek(f, (long)pw->pw_uid * sizeof(struct lastlog), 0); strcpy(line, remotehost); SCPYN(ll.ll_line, line); SCPYN(ll.ll_host, remotehost); (void) write(f, (char *) &ll, sizeof ll); (void) close(f); } #endif }
int ExecveX(PCStr(where),PCStr(path),ConstV av,ConstV ev,int flag) { const char *nav[2]; /**/ char *nev[1024]; char **oev; int rcode; BeforeExec(); endhostent(); if( av[0] == NULL ) { nav[0] = nav[1] = NULL; av = nav; } oev = (char**)ev; if( flag & EXP_NOENV ) { filterDGENV(oev,nev,elnumof(nev)); ev = nev; } if( flag & EXP_NOFDS ) { closeFds(0); } if( flag & EXP_PATH ) { environ = (char**)ev; rcode = execvp(path,(char**)av); } else { rcode = execve(path,(char**)av,(char**)ev); } execerror(where,path,rcode); return -1; }
static int findhost(struct sockaddr_in *addr, char *hostname) { struct hostent *hostent; int i; static int last_len; static char last_addr[40]; static char *last_host=NULL; if ( last_host!=NULL && strcmp(last_host,hostname)==0 ) { memcpy(&addr->sin_addr,last_addr,last_len); return( 1 ); } else { hostent = gethostbyname(hostname); if ( hostent==NULL ) return( 0 ); } for ( i=0; hostent->h_addr_list[i]!=NULL; ++i ); memcpy(&addr->sin_addr,hostent->h_addr_list[rand()%i],hostent->h_length); if ( hostent->h_length < sizeof(last_addr)) { /* Cache the last hostname, in case they ask for it again */ free(last_host); last_host = copy(hostname); last_len = hostent->h_length; memcpy(last_addr,hostent->h_addr_list[rand()%i],hostent->h_length); } endhostent(); return( 1 ); }
struct hostent * gethostbyname2_p(const char *name, int af, struct net_data *net_data) { struct irs_ho *ho; char tmp[NS_MAXDNAME]; struct hostent *hp; const char *cp; char **hap; if (!net_data || !(ho = net_data->ho)) return (NULL); if (net_data->ho_stayopen && net_data->ho_last && net_data->ho_last->h_addrtype == af) { if (ns_samename(name, net_data->ho_last->h_name) == 1) return (net_data->ho_last); for (hap = net_data->ho_last->h_aliases; hap && *hap; hap++) if (ns_samename(name, *hap) == 1) return (net_data->ho_last); } if (!strchr(name, '.') && (cp = res_hostalias(net_data->res, name, tmp, sizeof tmp))) name = cp; if ((hp = fakeaddr(name, af, net_data)) != NULL) return (hp); net_data->ho_last = (*ho->byname2)(ho, name, af); if (!net_data->ho_stayopen) endhostent(); return (net_data->ho_last); }
/* * Lookup the local host table (usually /etc/hosts) for a given hostname. * * If found, ip_addr is populated and 0 is returned. * If NOT found, -1 is return and ip_addr is untouched. */ static int hosts_file_resolve(const char *hostname, uint32_t *ip_addr) { int ret; struct hostent *host; assert(hostname); assert(ip_addr); DBG("Looking in local host table for %s", hostname); /* Query the local host table if the hostname is present. */ while ((host = gethostent()) != NULL) { if (strncasecmp(hostname, host->h_name, strlen(hostname)) == 0) { /* IP is found, copying and returning success. */ memcpy(ip_addr, host->h_addr_list[0], sizeof(uint32_t)); ret = 0; goto end; } } /* Not found. */ ret = -1; end: endhostent(); return ret; }
cmi_status_t cmj_resolve_nod_tnd(cmi_descriptor *nod, cmi_descriptor *tnd, struct sockaddr_in *inp) { cmi_status_t status = SS_NORMAL; char hn[MAX_HOST_NAME_LEN]; struct hostent *hp; int loop_limit = MAX_GETHOST_TRIES; /* tnd may contain host:port */ status = cmj_getsockaddr(tnd, inp); if (CMI_ERROR(status)) return status; if (inp->sin_addr.s_addr == INADDR_ANY) { /* use nod as a host name if tnd was just a port */ assert(CMI_DESC_LENGTH(nod) < (SIZEOF(hn)-1)); memcpy(hn, CMI_DESC_POINTER(nod), CMI_DESC_LENGTH(nod)); hn[CMI_DESC_LENGTH(nod)] = '\0'; /* test to see if nod is a dotted quad text string */ inp->sin_addr.s_addr = INET_ADDR(hn); if (inp->sin_addr.s_addr == (in_addr_t)-1) { /* assume hn is a host and query netdb */ for (; 0 < loop_limit && (NULL == (hp = GETHOSTBYNAME(hn))) && TRY_AGAIN == h_errno; loop_limit--) ; endhostent(); if (!hp) return CMI_NETFAIL; inp->sin_addr = *(struct in_addr *)hp->h_addr; } } return status; }
static int hosts(int argc, char *argv[]) { char addr[IN6ADDRSZ]; int i, rv = RV_OK; struct hostent *he; sethostent(1); if (argc == 2) { while ((he = gethostent()) != NULL) hostsprint(he); } else { for (i = 2; i < argc; i++) { he = NULL; if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0) he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6); else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0) he = gethostbyaddr(addr, INADDRSZ, AF_INET); if (he != NULL) hostsprint(he); else if ((rv = hostsaddrinfo(argv[i])) == RV_NOTFOUND) break; } } endhostent(); return rv; }
static int hosts(int argc, char *argv[]) { struct hostent *he; char addr[IN6ADDRSZ]; int i, rv; assert(argc > 1); assert(argv != NULL); sethostent(1); rv = RV_OK; if (argc == 2) { while ((he = gethostent()) != NULL) hostsprint(he); } else { for (i = 2; i < argc; i++) { if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0) he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6); else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0) he = gethostbyaddr(addr, INADDRSZ, AF_INET); else he = gethostbyname(argv[i]); if (he != NULL) hostsprint(he); else { rv = RV_NOTFOUND; break; } } } endhostent(); return rv; }
int main(void) { struct hostent *ptr; char **p; sethostent(0); while((ptr = gethostent()) != NULL) { printf("name:%s , addrtype:%d , length:%d ,addr_list: ", ptr->h_name,ptr->h_addrtype,ptr->h_length); p = ptr->h_addr_list; while(*p) { printf("[%s] ",*p); p++; } printf("\n"); } endhostent(); return 0; }
void parse_end(void) { endprotoent() ; endpwent() ; endgrent() ; endnetent() ; endhostent() ; }
endhostent_r(void) #endif { #ifdef HOST_R_ENT_ARGS UNUSED(hdptr); #endif endhostent(); HOST_R_END_RESULT(HOST_R_OK); }
main() { //打开DNS服务器或者其他host文件 sethostent(1); struct hostent*hptr; while((hptr=gethostent())) { printf("IP地址:%hhu.%hhu.%hhu.%hhu\n",hptr->h_addr[0],hptr->h_addr[1],hptr->h_addr[2],hptr->h_addr[3]); } endhostent(); }
void resolver_shutdown(void) { if (_initialized) { thread_mutex_destroy(&_resolver_mutex); _initialized = 0; #ifdef HAVE_ENDHOSTENT endhostent(); #endif } }
int resolve(uint32_t *addr, const char *name) { struct hostent *h; if((h = gethostbyname(name)) == NULL) return -1; memcpy(addr, h->h_addr, h->h_length); endhostent(); return 0; }
int ivona_init_sock(char *host, int port) { if (!inet_aton(host,&sinadr.sin_addr)) { struct hostent *h = gethostbyname(host); if (!h) return -1; memcpy(&sinadr.sin_addr, h->h_addr, sizeof(struct in_addr)); endhostent(); } sinadr.sin_family = AF_INET; sinadr.sin_port = htons(port); return 0; }
/** * Return the next (non-commented) line from the host-file. * Format is: * ip-address host-name [alias..] {\n | # ..} */ struct hostent * W32_CALL gethostent (void) { struct _hostent h; char *tok, *ip, *name, *alias; char buf [2*MAX_HOSTLEN]; int i; if (!netdb_init() || !hostFile) { h_errno = NO_RECOVERY; return (NULL); } while (1) { if (!fgets(buf,sizeof(buf),hostFile)) return (NULL); tok = strltrim (buf); if (*tok == '#' || *tok == ';' || *tok == '\n') continue; ip = strtok (tok, " \t"); name = strtok (NULL, " \t\n"); if (ip && name && isaddr(ip)) break; } if (hostClose) endhostent(); memset (&h, 0, sizeof(h)); if (!strcmp(ip,"0.0.0.0")) /* inet_addr() maps 0 -> INADDR_NONE */ h.h_address[0] = INADDR_ANY; else h.h_address[0] = inet_addr (ip); h.h_num_addr = 1; h.h_name = name; alias = strtok (NULL, " \t\n"); for (i = 0; alias && i < MAX_HOST_ALIASES; i++) { static char aliases [MAX_NETENT_ALIASES][MAX_HOSTLEN]; if (*alias == '#' || *alias == ';') break; h.h_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i])); alias = strtok (NULL, " \t\n"); } return fill_hostent (&h); }
/** Get full qualified domain name. * Retreive the full qualified domain name for a given hostname * @param name the hostname * @return the fqdn on success, NULL otherwise */ char * get_fqdn (const char *name) /*{{{*/ { char *fqdn; struct hostent *hent; fqdn = NULL; sethostent (0); if ((hent = gethostbyname (name)) && hent -> h_name) fqdn = strdup (hent -> h_name); endhostent (); return fqdn; }/*}}}*/
int noit_check_etc_hosts_cache_refresh(eventer_t e, int mask, void *closure, struct timeval *now) { static struct stat last_stat; struct stat sb; struct hostent *ent; int reload = 0; memset(&sb, 0, sizeof(sb)); stat("/etc/hosts", &sb); #define CSTAT(f) (sb.f == last_stat.f) reload = ! (CSTAT(st_dev) && CSTAT(st_ino) && CSTAT(st_mode) && CSTAT(st_uid) && CSTAT(st_gid) && CSTAT(st_size) && CSTAT(st_mtime)); memcpy(&last_stat, &sb, sizeof(sb)); if(reload) { mtev_hash_delete_all(&etc_hosts_cache, free, free); while(NULL != (ent = gethostent())) { int i = 0; char *name = ent->h_name; while(name) { void *vnode; static_host_node *node; if(!mtev_hash_retrieve(&etc_hosts_cache, name, strlen(name), &vnode)) { vnode = node = calloc(1, sizeof(*node)); node->target = strdup(name); mtev_hash_store(&etc_hosts_cache, node->target, strlen(node->target), node); } node = vnode; if(ent->h_addrtype == AF_INET) { node->has_ip4 = 1; memcpy(&node->ip4, ent->h_addr_list[0], ent->h_length); } if(ent->h_addrtype == AF_INET6) { node->has_ip6 = 1; memcpy(&node->ip6, ent->h_addr_list[0], ent->h_length); } name = ent->h_aliases[i++]; } } endhostent(); mtevL(noit_debug, "reloaded %d /etc/hosts targets\n", mtev_hash_size(&etc_hosts_cache)); } eventer_add_in_s_us(noit_check_etc_hosts_cache_refresh, NULL, 1, 0); return 0; }
int tconnect(int argc, descriptor *argv) /*: connect to TCP socket */ { char *hostname, filename[1000]; unsigned char *p; int port, fd, i, d[4]; FILE *fp; struct hostent *h; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); /* check arguments */ ArgString(1); hostname = StringVal(argv[1]); ArgInteger(2); port = IntegerVal(argv[2]); /* get host address */ if (sscanf(hostname, "%d.%d.%d.%d", &d[0], &d[1], &d[2], &d[3]) == 4) { p = (unsigned char *) &sin.sin_addr; for (i = 0; i < 4; i++) p[i] = d[i]; } else { h = gethostbyname(hostname); if (!h) Fail; memcpy(&sin.sin_addr, h->h_addr, sizeof(struct in_addr)); endhostent(); } /* create socket and connect */ sin.sin_family = AF_INET; sin.sin_port = htons(port); if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) Fail; if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) Fail; /* create stdio file pointer */ fp = fdopen(fd, "r+"); if (!fp) Fail; /* return Icon file */ sprintf(filename, "%s:%d", hostname, port); RetFile(fp, Fs_Read | Fs_Write, filename); }
int tx_open(char *address) { close(_tx_server); address = strdup(address); /* I want to be able to modify it */ char *p = strrchr(address, ':'); if (!p) { /* AF_UNIX address */ struct sockaddr_un sun; sun.sun_family = AF_UNIX; strncpy(sun.sun_path, address, sizeof sun.sun_path); _tx_server = socket(AF_UNIX, SOCK_STREAM, 0); if (_tx_server != -1 && connect(_tx_server, (struct sockaddr *)&sun, sizeof sun) == -1) { close(_tx_server); _tx_server = -1; } } else { /* AF_INET address */ struct sockaddr_in sin; _tx_server = -1; // cheap error handling bzero(&sin, sizeof(sin)); *p++ = 0; struct hostent *host = gethostbyname(address); if (!host) goto fail; bcopy(host->h_addr, &sin.sin_addr, host->h_length); sin.sin_family = host->h_addrtype; struct servent *service = getservbyname(p, "tcp"); sin.sin_port = service? service->s_port: htons(atoi(p)); if (!ntohs(sin.sin_port)) goto fail; _tx_server = socket(AF_INET, SOCK_STREAM, 0); if (_tx_server != -1 && connect(_tx_server, (struct sockaddr *)&sin, sizeof sin) == -1) { close(_tx_server); _tx_server = -1; } fail: endhostent(); endservent(); } free(address); return (_tx_server == -1) ? TX_FAIL : TX_OK; }
struct hostent * _gethostbyhtaddr( const char *addr, int len, int af) { register struct hostent *p; sethostent(0); while ((p = gethostent()) != NULL) if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) break; endhostent(); return (p); }
/* ** Searches the DNS mapping cache for #address#, adding a new entry if needed. ** Returns a pointer to the mapping entry, or NULL on error. */ static const struct hostent* LookupByAddress(IPAddress address) { struct in_addr addrAsInAddr; struct hostent *addrEntry; struct in_addr **cachedAddr; int i; for(i = 0; i < cacheCount; i++) { for(cachedAddr = (struct in_addr**)cache[i].h_addr_list; *cachedAddr != NULL; cachedAddr++) { if((**cachedAddr).s_addr == address) { return &cache[i]; } } } addrAsInAddr.s_addr = address; sethostent(0); addrEntry = gethostbyaddr((char *)&addrAsInAddr, sizeof(addrAsInAddr), AF_INET); if(addrEntry == NULL) { endhostent(); return NULL; } else if(addrEntry->h_length != sizeof(struct in_addr)) { endhostent(); return NULL; /* We don't (yet) handle non-in_addr addresses. */ } addrEntry = CacheHostent(addrEntry); endhostent(); return addrEntry; }
/* * return the next (non-commented) line from the host-file * Format is: * ip-address [=] host-name [alias..] {\n | # ..} */ static int _gethostent (struct _hostent *h) { char *ip, *name, *alias; char buf[100]; if (!hostFile) return (0); do { if (!fgets(buf,sizeof(buf)-1,hostFile)) return (0); } while (buf[0] == '#' || buf[0] == ';' || buf[0] == '\n'); if (hostClose) endhostent(); ip = strtok (buf," \t"); name = strtok (NULL,"= \t\n"); h->h_address = inet_addr (ip); h->h_name = strdup (name); if (!h->h_name) return (NULL); h->h_aliases = NULL; alias = strtok (NULL," \t\n"); if (alias && *alias != '#' && *alias != ';') { char **alist = calloc ((1+MAX_HOST_ALIASES) * sizeof(char*), 1); int i = 0; do { if (*alias == '#' || *alias == ';') break; if (!alist || (i == MAX_HOST_ALIASES) || (alist[i++] = strdup(alias)) == NULL) break; alias = strtok (NULL," \t\n"); } while (alias); h->h_aliases = alist; } return (1); }
int Execvp(PCStr(where),PCStr(path),const char *const av[]) { int rcode; CStr(pwd,1024); CStr(names,1024); const char *nav[2]; /**/ const char *env; BeforeExec(); endhostent(); if( av[0] == NULL ) { nav[0] = nav[1] = NULL; av = nav; } rcode = execvp(path,(char**)av); execerror(where,path,rcode); return -1; }
int ttd_bind(char *hostname, int port, ttd_server server_type, ttd_conn *conn) { ttd_conn ttd; struct hostent *hp; struct servent *sp; ttd = (ttd_conn) malloc( sizeof( *ttd ) ); if (hostname == NULL) hostname = "localhost"; hp = gethostbyname( hostname ); if (hp == 0) return TTD_UnknownHostnameRC; ttd->addr.sin_family = hp->h_addrtype; bcopy( (char *) hp->h_addr, (char *) &ttd->addr.sin_addr, hp->h_length ); endhostent(); if (port != 0) ttd->addr.sin_port = htons( port ); else { sp = getservbyname( "ttd", "udp" ); if (sp != 0) ttd->addr.sin_port = sp->s_port; else ttd->addr.sin_port = htons( DEFAULTPORT ); } endservent(); ttd->socket = socket( AF_INET, SOCK_DGRAM, 0 ); if (ttd->socket < 0) return TTD_UnixErrorRC; if (connect( ttd->socket, (struct sockaddr *) &(ttd->addr), sizeof( struct sockaddr_in ) ) < 0) return TTD_UnixErrorRC; ttd->server = server_type; ttd->seq = time( (long *) 0 ) * 2; *conn = ttd; return TTD_SuccessRC; }
PAM_EXTERN int pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc, const char **argv) { int retval; if (sizeof(U32) != 4) { _pam_log (LOG_ALERT, "pam_rhosts module can\'t work on this hardware " "(yet)"); return PAM_AUTH_ERR; } sethostent(1); retval = _pam_auth_rhosts (pamh, flags, argc, argv); endhostent(); return retval; }
main() { //参数主要对域名查找设置有效,对本地/etc/hosts没有意义 sethostent(1); struct hostent *ent; while(1) { ent = gethostent(); if(!ent) break; printf(":%s:%hhu.%hhu.%hhu.%hhu\n", //printf(":%s:%u.%u.%u.%u\n", ent->h_name, ent->h_addr[0], ent->h_addr[1], ent->h_addr[2], ent->h_addr[3]); } endhostent(); ///////////////////////////////// struct utsname name; uname(&name); printf("%s\n",name.sysname); printf("%s\n",name.nodename); printf("%s\n",name.release); printf("%s\n",name.version); printf("%s\n",name.machine); //printf("%s\n",name.domainname); ////////////////////////////// struct hostent *ipent; ipent=gethostbyname("www.baidu.com"); printf("%s:%hhu.%hhu.%hhu.%hhu\n", ipent->h_name, ipent->h_addr[0], ipent->h_addr[1], ipent->h_addr[2], ipent->h_addr[3]); }
void test2() { // 打开主机配置数据库文件 sethostent(1); hostent* ent; while (1) { ent = gethostent(); if (ent) { printf("主机名:%s\n", ent->h_name); printf("ip地址:%hhu.%hhu.%hhu.%hhu\n", ent->h_addr[0], ent->h_addr[1], ent->h_addr[2], ent->h_addr[3]); } else { break; } } // 关闭 endhostent(); }