/* * for inet addresses only */ struct hostent* gethostbyname(char *name) { int i, t, fd, m; char *p, *k, *bp; int nn, na; struct in_addr in; static struct hostent h; static char buf[1024]; static char *nptr[Nname+1]; static char *aptr[Nname+1]; static char addr[Nname][4]; h.h_name = 0; t = _sock_ipattr(name); /* connect to server */ fd = open("/net/cs", O_RDWR); if(fd < 0){ h_errno = NO_RECOVERY; return 0; } /* construct the query, always expect an ip# back */ switch(t){ case Tsys: snprintf(buf, sizeof buf, "!sys=%s ip=*", name); break; case Tdom: snprintf(buf, sizeof buf, "!dom=%s ip=*", name); break; case Tip: snprintf(buf, sizeof buf, "!ip=%s", name); break; } /* query the server */ if(write(fd, buf, strlen(buf)) < 0){ h_errno = TRY_AGAIN; close(fd); return 0; } lseek(fd, 0, 0); for(i = 0; i < sizeof(buf)-1; i += m){ m = read(fd, buf+i, sizeof(buf) - 1 - i); if(m <= 0) break; buf[i+m++] = ' '; } close(fd); buf[i] = 0; /* parse the reply */ nn = na = 0; for(bp = buf;;){ k = bp; p = strchr(k, '='); if(p == 0) break; *p++ = 0; for(bp = p; *bp && *bp != ' '; bp++) ; if(*bp) *bp++ = 0; if(strcmp(k, "dom") == 0){ if(h.h_name == 0) h.h_name = p; if(nn < Nname) nptr[nn++] = p; } else if(strcmp(k, "sys") == 0){ if(nn < Nname) nptr[nn++] = p; } else if(strcmp(k, "ip") == 0){ if(inet_aton(p, &in) == 0) continue; if(na < Nname){ memmove(addr[na], (unsigned char*)&in.s_addr, 4); aptr[na] = addr[na]; na++; } } } if(nn+na == 0){ h_errno = HOST_NOT_FOUND; return 0; } nptr[nn] = 0; aptr[na] = 0; h.h_aliases = nptr; h.h_addr_list = aptr; h.h_length = 4; h.h_addrtype = AF_INET; if(h.h_name == 0) h.h_name = nptr[0]; if(h.h_name == 0) h.h_name = aptr[0]; return &h; }
/* * for inet addresses only */ struct hostent* gethostbyname(const char *name) { int i, t, fd, m; char *p, *bp; int nn, na; unsigned long x; static struct hostent h; static char buf[1024]; static char *nptr[Nname+1]; static char *aptr[Nname+1]; static char addr[Nname][4]; h.h_name = 0; t = _sock_ipattr(name); /* connect to server */ fd = open("/net/cs", O_RDWR); if(fd < 0){ _syserrno(); h_errno = NO_RECOVERY; return 0; } /* construct the query, always expect an ip# back */ switch(t){ case Tsys: snprintf(buf, sizeof buf, "!sys=%s ip=*", name); break; case Tdom: snprintf(buf, sizeof buf, "!dom=%s ip=*", name); break; case Tip: snprintf(buf, sizeof buf, "!ip=%s", name); break; } /* query the server */ if(write(fd, buf, strlen(buf)) < 0){ _syserrno(); h_errno = TRY_AGAIN; return 0; } lseek(fd, 0, 0); for(i = 0; i < sizeof(buf)-1; i += m){ m = read(fd, buf+i, sizeof(buf) - 1 - i); if(m <= 0) break; buf[i+m++] = ' '; } close(fd); buf[i] = 0; /* parse the reply */ nn = na = 0; for(bp = buf;;){ p = strchr(bp, '='); if(p == 0) break; *p++ = 0; if(strcmp(bp, "dom") == 0){ if(h.h_name == 0) h.h_name = p; if(nn < Nname) nptr[nn++] = p; } else if(strcmp(bp, "sys") == 0){ if(nn < Nname) nptr[nn++] = p; } else if(strcmp(bp, "ip") == 0){ x = inet_addr(p); x = ntohl(x); if(na < Nname){ addr[na][0] = x>>24; addr[na][1] = x>>16; addr[na][2] = x>>8; addr[na][3] = x; aptr[na] = addr[na]; na++; } }
int __gethostbyname2_r(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { int i, t, fd, m; char *p, *bp; int nn, na; unsigned long x; size_t csmsg_len = 0; /* These three used to be: static char *nptr[Nname + 1]; static char *aptr[Nname + 1]; static char addr[Nname][4]; * we need to use space in buf for them */ char **nptr, **aptr; char (*addr)[4]; size_t nptr_sz, aptr_sz, addr_sz; nptr_sz = sizeof(char *) * (Nname + 1); aptr_sz = sizeof(char *) * (Nname + 1); addr_sz = sizeof(char[Nname][4]); if (nptr_sz + aptr_sz + addr_sz >= buflen) { *result = 0; return -ERANGE; } nptr = buf; buf += nptr_sz; buflen -= nptr_sz; aptr = buf; buf += aptr_sz; buflen -= aptr_sz; addr = buf; buf += addr_sz; buflen -= addr_sz; /* for inet addresses only */ if (af != AF_INET) { *result = 0; return -EAFNOSUPPORT; } ret->h_name = 0; t = _sock_ipattr(name); /* connect to server */ fd = open("/net/cs", O_RDWR); if (fd < 0) { *h_errnop = NO_RECOVERY; *result = 0; return -errno; } /* construct the query, always expect an ip# back */ switch (t) { case Tsys: csmsg_len = snprintf(buf, buflen, "!sys=%s ip=*", name); break; case Tdom: csmsg_len = snprintf(buf, buflen, "!dom=%s ip=*", name); break; case Tip: csmsg_len = snprintf(buf, buflen, "!ip=%s", name); break; default: /* we can't get here, but want to be safe for changes to * _sock_ipattr() */ close(fd); *result = 0; return -EINVAL; } /* we don't update buflen, since we're just going to reuse the space * after our nptr/aptr/addr/etc. */ if (csmsg_len >= buflen) { close(fd); *result = 0; return -ERANGE; } /* query the server */ if (write(fd, buf, csmsg_len) < 0) { *h_errnop = TRY_AGAIN; close(fd); *result = 0; return -1; } lseek(fd, 0, 0); for (i = 0; i < buflen - 1; i += m) { m = read(fd, buf + i, buflen - 1 - i); if (m <= 0) break; buf[i + m++] = ' '; } close(fd); buf[i] = 0; /* parse the reply */ nn = na = 0; for (bp = buf;;) { p = strchr(bp, '='); if (p == 0) break; *p++ = 0; if (strcmp(bp, "dom") == 0) { if (ret->h_name == 0) ret->h_name = p; if (nn < Nname) nptr[nn++] = p; } else if (strcmp(bp, "sys") == 0) { if (nn < Nname) nptr[nn++] = p; } else if (strcmp(bp, "ip") == 0) { x = inet_addr(p); x = ntohl(x); if (na < Nname) { addr[na][0] = x >> 24; addr[na][1] = x >> 16; addr[na][2] = x >> 8; addr[na][3] = x; aptr[na] = addr[na]; na++; } }