int enet_address_set_host (ENetAddress * address, const char * name) { struct hostent * hostEntry = NULL; #ifdef HAS_GETHOSTBYNAME_R struct hostent hostData; char buffer [2048]; int errnum; #ifdef linux gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); #else hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); #endif #else hostEntry = gethostbyname (name); #endif if (hostEntry == NULL || hostEntry -> h_addrtype != AF_INET) return -1; address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; return 0; }
PRIVATESTUFF void invoke_gethostbyname(void *p) { struct t_data *td = (struct t_data *) p; struct hostent hp, *h; char buf[1000]; int h_errno; u_long sip; /* IP address in network byte order */ fprintf(DEBUGIO, "invoke_gethostbyname: name = %s\r\n", td->name); fprintf(DEBUGIO, "invoke_gethostbyname: 0x%lx sleeping for 2 seconds\r\n", (unsigned long) pthread_self()); sleep(2); fprintf(DEBUGIO, "invoke_gethostbyname: 0x%lx done sleeping\r\n", (unsigned long) pthread_self()); #if defined(linux) gethostbyname_r(td->name, &hp, buf, sizeof(buf), &h, &h_errno); #else /* linux */ h = gethostbyname_r(td->name, &hp, buf, sizeof(buf), &h_errno); #endif /* linux */ if (h == NULL) { td->status = h_errno; fprintf(DEBUGIO, "invoke_gethostbyname: error = %d\r\n", td->status); } else { td->status = 0; fprintf(DEBUGIO, "invoke_gethostbyname: success = %d\r\n", td->status); sip = *((u_long *) hp.h_addr_list[0]); memcpy(td->resbuf, &sip, sizeof(sip)); td->resbuflen = sizeof(sip); } fprintf(DEBUGIO, "invoke_gethostbyname: done\r\n"); }
int enet_address_set_host (ENetAddress * address, const char * name) { struct hostent * hostEntry = NULL; #ifdef HAS_GETHOSTBYNAME_R struct hostent hostData; char buffer [2048]; int errnum; #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); #else hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); #endif #else hostEntry = gethostbyname (name); #endif if (hostEntry == NULL || hostEntry -> h_addrtype != AF_INET) { #ifdef HAS_INET_PTON if (! inet_pton (AF_INET, name, & address -> host)) #else if (! inet_aton (name, (struct in_addr *) & address -> host)) #endif return -1; return 0; } address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; return 0; }
unsigned long safe_gethostbyname(const char *host) { struct hostent *hp, hp_a, *hp_b; char buf[1024]; int hp_errno; u_long ip; if (is_ipaddr(host)) return inet_addr(host); #if HAVE_GETHOSTBYNAME_R #if defined(linux) if (!gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_b, &hp_errno)) hp = &hp_a; else hp = NULL; #else hp = gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_errno); #endif #else pthread_mutex_lock(&nss_lock); hp = gethostbyname(host); #endif if (!hp) { _pam_log(LOG_ERR, "safe_gethostbyname: hostname '%s' not found", host); #ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_unlock(&nss_lock); #endif return 0; } #ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_unlock(&nss_lock); #endif return *((unsigned int *) hp->h_addr); }
struct hostent *gethostbyjosh (char *hostname) { /* A OS independant wrapper to gethostbyname */ struct hostent *host; #ifndef FREEBSD char hostbuf[HOSTBUF_LEN]; int errn; #endif host = (struct hostent *) malloc (sizeof (struct hostent)); #ifdef SOLARIS gethostbyname_r (hostname, host, hostbuf, HOSTBUF_LEN, &errn); #endif #ifdef LINUX gethostbyname_r (hostname, host, hostbuf, (size_t) HOSTBUF_LEN, &host, &errn); #endif #ifdef FREEBSD host = gethostbyname (hostname); #endif if (host == NULL) { fprintf (stderr, "adding popserver '%s':\n", hostname); perror ("gethostbyjosh"); exit (-1); } return (host); }
struct hostent *ei_gethostbyname_r(const char *name, struct hostent *hostp, char *buffer, int buflen, int *h_errnop) { #ifndef _REENTRANT /* threads disabled, no need to call reentrant function */ return gethostbyname(name); #else #ifndef HAVE_GETHOSTBYNAME_R return my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop); #else #if (defined(__GLIBC__) || (__FreeBSD_version >= 602000) || defined(__DragonFly__)) struct hostent *result; gethostbyname_r(name, hostp, buffer, buflen, &result, h_errnop); return result; #else return gethostbyname_r(name,hostp,buffer,buflen,h_errnop); #endif #endif #endif }
struct hostent *ei_gethostbyname_r(const char *name, struct hostent *hostp, char *buffer, int buflen, int *h_errnop) { #ifndef _REENTRANT /* threads disabled, no need to call reentrant function */ return gethostbyname(name); #else #ifndef HAVE_GETHOSTBYNAME_R return my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop); #else #ifdef __GLIBC__ struct hostent *result; gethostbyname_r(name, hostp, buffer, buflen, &result, h_errnop); return result; #else return gethostbyname_r(name,hostp,buffer,buflen,h_errnop); #endif #endif #endif }
void TCPSocket::connect(const char *name, unsigned short port) { if (this->_state != CREATED) { throw SocketStateException("Wrong state for operation"); } struct sockaddr_in remote_addr; const int GETHOSTBYNAME_R_BUFSIZE = 4096; char tmp_buf[GETHOSTBYNAME_R_BUFSIZE]; struct hostent *result; #if defined( linux ) || defined( __linux ) struct hostent ret; int h_errnop; if (gethostbyname_r(name, &ret, tmp_buf, GETHOSTBYNAME_R_BUFSIZE, &result, &h_errnop) == -1) { throw DNSException(h_errno); } if (result == NULL) { throw DNSException(h_errno); } #endif #if defined( sun ) || defined( __sun ) struct hostent entry; int h_errnop; if ((result = gethostbyname_r(name, &entry, tmp_buf, GETHOSTBYNAME_R_BUFSIZE, &h_errnop)) == NULL) { throw DNSException(h_errno); } #endif memset(&remote_addr, 0, sizeof (remote_addr)); remote_addr.sin_addr = *((in_addr *) result->h_addr_list[0]); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(port); if (this->_nonblocking == false) { if (::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr)) == -1) { if (errno == EAGAIN) throw EAGAINException(); else throw ConnectException(errno); } this->_state = CONNECTED; } if (this->_nonblocking == true) { ::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr)); this->_state = CONNECTING; } #ifdef DEBUG fprintf(stderr, "connected to %s port %d\n", name, port); #endif }
struct hostent *sigar_gethostbyname(const char *name, sigar_hostent_t *data) { struct hostent *hp = NULL; #if defined(__linux__) gethostbyname_r(name, &data->hs, data->buffer, sizeof(data->buffer), &hp, &data->error); #elif defined(__sun) hp = gethostbyname_r(name, &data->hs, data->buffer, sizeof(data->buffer), &data->error); #elif defined(SIGAR_HAS_HOSTENT_DATA) if (gethostbyname_r(name, &data->hs, &data->hd) == 0) { hp = &data->hs; } else { data->error = h_errno; } #else hp = gethostbyname(name); #endif return hp; }
int enet_address_set_host (ENetAddress * address, const char * name) { #ifdef HAS_GETADDRINFO struct addrinfo hints, * resultList = NULL, * result = NULL; memset (& hints, 0, sizeof (hints)); hints.ai_family = AF_INET; if (getaddrinfo (name, NULL, NULL, & resultList) != 0) return -1; for (result = resultList; result != NULL; result = result -> ai_next) { if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in)) { struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr; address -> host = sin -> sin_addr.s_addr; freeaddrinfo (resultList); return 0; } } if (resultList != NULL) freeaddrinfo (resultList); #else struct hostent * hostEntry = NULL; #ifdef HAS_GETHOSTBYNAME_R struct hostent hostData; char buffer [2048]; int errnum; #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); #else hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); #endif #else hostEntry = gethostbyname (name); #endif if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET) { address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; return 0; } #endif return enet_address_set_host_ip (address, name); }
SocketAddress SocketAddress::getAnyLocalHost(UInt16 port) { struct in_addr addr; addr.s_addr = hton32(INADDR_ANY); SocketAddress rval = getFromNativeForm(addr, port, "localhost"); char buf[256]; gethostname(buf, sizeof(buf)); String hname(buf); if (hname.indexOf('.') == String::npos) { #if defined(OW_HAVE_GETHOSTBYNAME_R) && defined(OW_GETHOSTBYNAME_R_ARGUMENTS) hostent hostbuf; hostent* hent = &hostbuf; #if (OW_GETHOSTBYNAME_R_ARGUMENTS == 6) char local_buf[2048]; int h_err = 0; if (gethostbyname_r(buf, &hostbuf, local_buf, sizeof(local_buf), &hent, &h_err) == -1) { hent = NULL; } #elif (OW_GETHOSTBYNAME_R_ARGUMENTS == 5) char local_buf[2048]; int h_err(0); // returns NULL if not successful hent = gethostbyname_r(buf, &hostbuf, local_buf, sizeof(local_buf), &h_err); #elif (OW_GETHOSTBYNAME_R_ARGUMENTS == 3) hostent_data hostdata; if (gethostbyname_r(buf, &hostbuf, &hostdata) != 0) { hent = NULL; } #else #error Not yet supported: gethostbyname_r() with other argument counts. #endif /* OW_GETHOSTBYNAME_R_ARGUMENTS */ #else MutexLock mlock(gethostbynameMutex); hostent* hent = gethostbyname(buf); #endif if (hent && hent->h_name && (strlen(hent->h_name) > 0)) { hname = String(hent->h_name); } } rval.m_name = hname; return rval; }
CAMLprim value unix_gethostbyname(value name) { struct hostent * hp; char * hostname; #if HAS_GETHOSTBYNAME_R struct hostent h; char buffer[NETDB_BUFFER_SIZE]; int err; #endif if (! caml_string_is_c_safe(name)) raise_not_found(); #if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT hostname = caml_strdup(String_val(name)); #else hostname = String_val(name); #endif #if HAS_GETHOSTBYNAME_R == 5 { enter_blocking_section(); hp = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &err); leave_blocking_section(); } #elif HAS_GETHOSTBYNAME_R == 6 { int rc; enter_blocking_section(); rc = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &hp, &err); leave_blocking_section(); if (rc != 0) hp = NULL; } #else #ifdef GETHOSTBYNAME_IS_REENTRANT enter_blocking_section(); #endif hp = gethostbyname(hostname); #ifdef GETHOSTBYNAME_IS_REENTRANT leave_blocking_section(); #endif #endif #if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT stat_free(hostname); #endif if (hp == (struct hostent *) NULL) raise_not_found(); return alloc_host_entry(hp); }
CAMLprim value unix_gethostbyname_r(CAML_R, value name) { struct hostent * hp; char * hostname; #if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT hostname = stat_alloc(string_length(name) + 1); strcpy(hostname, String_val(name)); #else hostname = String_val(name); #endif #if HAS_GETHOSTBYNAME_R == 5 { struct hostent h; char buffer[NETDB_BUFFER_SIZE]; int h_errno; caml_enter_blocking_section_r(ctx); hp = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &h_errno); caml_leave_blocking_section_r(ctx); } #elif HAS_GETHOSTBYNAME_R == 6 { struct hostent h; char buffer[NETDB_BUFFER_SIZE]; int h_errno, rc; caml_enter_blocking_section_r(ctx); rc = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &hp, &h_errno); caml_leave_blocking_section_r(ctx); if (rc != 0) hp = NULL; } #else #ifdef GETHOSTBYNAME_IS_REENTRANT caml_enter_blocking_section_r(ctx); #endif hp = gethostbyname(hostname); #ifdef GETHOSTBYNAME_IS_REENTRANT caml_leave_blocking_section_r(ctx); #endif #endif #if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT stat_free(hostname); #endif if (hp == (struct hostent *) NULL) caml_raise_not_found_r(ctx); return alloc_host_entry_r(ctx, hp); }
static void check_vulnerable(void) { struct hostent resbuf; struct hostent *result; int herrno; int retval; char name[sizeof(temp.buffer)]; size_t len; /* * <glibc>/nss/digits_dots.c: * strlen(name) = size_needed - sizeof(*host_addr) - * sizeof(*h_addr_ptrs) - 1; */ len = sizeof(temp.buffer) - 16 - 2 * sizeof(char *) - 1; memset(name, '0', len); name[len] = '\0'; retval = gethostbyname_r(name, &resbuf, temp.buffer, sizeof(temp.buffer), &result, &herrno); if (strcmp(temp.canary, CANARY) != 0) { tst_resm(TFAIL, "vulnerable"); return; } if (retval == ERANGE) { tst_resm(TPASS, "not vulnerable"); return; } tst_resm(TFAIL, "gethostbyname_r() returned %s, expected ERANGE", tst_strerrno(retval)); }
/** * initialize primarily functions to initialize static global variables * that will not be changed frequently. These variables are currently * _hostName. * */ void ComputerSystem::initialize() { char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; struct hostent *hostEntry; if (gethostname(hostName, sizeof(hostName)) != 0) { _hostName.assign("Not initialized"); } hostName[sizeof(hostName)-1] = 0; // Now get the official hostname. If this call fails then return // the value from gethostname(). char hostEntryBuffer[8192]; struct hostent hostEntryStruct; int hostEntryErrno; gethostbyname_r( hostName, &hostEntryStruct, hostEntryBuffer, sizeof(hostEntryBuffer), &hostEntry, &hostEntryErrno); if (hostEntry) { _hostName.assign(hostEntry->h_name); } else { _hostName.assign(hostName); } }
int pqGethostbyname(const char *name, struct hostent * resultbuf, char *buffer, size_t buflen, struct hostent ** result, int *herrno) { #if defined(FRONTEND) && defined(ENABLE_THREAD_SAFETY) && defined(HAVE_GETHOSTBYNAME_R) /* * broken (well early POSIX draft) gethostbyname_r() which returns 'struct * hostent *' */ *result = gethostbyname_r(name, resultbuf, buffer, buflen, herrno); return (*result == NULL) ? -1 : 0; #else /* no gethostbyname_r(), just use gethostbyname() */ *result = gethostbyname(name); if (*result != NULL) *herrno = h_errno; if (*result != NULL) return 0; else return -1; #endif }
/** * 解析host */ int easy_inet_parse_host(easy_addr_t *address, const char *host, int port) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); if (host && host[0]) { // 如果是ip地址, 用inet_addr转一下, 否则用gethostbyname if (easy_inet_is_ipaddr(host)) { if ((addr.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) return EASY_ERROR; } else { // FIXME: gethostbyname会阻塞 char buffer[1024]; struct hostent h, *hp; int rc; if (gethostbyname_r(host, &h, buffer, 1024, &hp, &rc) || hp == NULL) return EASY_ERROR; addr.sin_addr.s_addr = *((in_addr_t *) (hp->h_addr)); } } else { addr.sin_addr.s_addr = htonl(INADDR_ANY); } memcpy(address, &addr, sizeof(easy_addr_t)); return EASY_OK; }
int open_tcp(network_t *network, char *hostname, int port) { int clientfd, ret, res; char buffer[1024]; struct hostent *hp, hostbuf; struct sockaddr_in serveraddr; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return SOCKET_FAILED; } /* setting recv timeout 10s*/ struct timeval timeout = {10, 0}; setsockopt(clientfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval)); /* check errno for cause of error */ /* Fill in the server's IP address and port */ res = gethostbyname_r(hostname, &hostbuf, buffer, sizeof(buffer), &hp, &ret); if(res || hp == NULL) { return HOST_FAILED; /* check h_errno for cause of error */ } bzero((char *)&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr_list[0], (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* Establish a connection with the server */ if (connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) { return CONNECT_FAILED; } network->socketfd = clientfd; network->len = 0; strcpy(network->host, hostname); network->port = port; bzero(network->buffer, BUFFER_SIZE); return CONNECT_OK; }
void boss::Socket::get_in_addr(const char * address, struct in_addr& sinaddr, const char * func_name) { int ret = inet_pton(AF_INET, address, &sinaddr); if (ret < 0){ snprintf(_error_text,sizeof(_error_text)-1, "%s: inet_pton(%s): %s", func_name, address, strerror(errno)); throw SocketException(_error_text); } else if (ret == 0) { struct hostent hostent; struct hostent *hostent_ptr; char buf[2048]; int herror; gethostbyname_r(address, &hostent, buf, sizeof(buf), &hostent_ptr, &herror); if (hostent_ptr == NULL) { snprintf(_error_text,sizeof(_error_text)-1, "%s: gethostbyname_r(%s): %s", func_name, address, hstrerror(herror)); throw SocketException(_error_text); } else { sinaddr = *(struct in_addr *) hostent_ptr->h_addr; } } return; }
Sock::status_t Sock::connectTo(int sock, const char* hostname, unsigned int port) { struct sockaddr_in s_addr; struct hostent sent_struct, *sent = NULL; int rc, err; char buff[HOST_BUF_SIZE]; // using reentrant version of gethostbyname() rc = gethostbyname_r(hostname, &sent_struct, buff, HOST_BUF_SIZE, &sent, &err); if((rc) || (sent == NULL)) { return ERR_SOCK_RESOLVE; } memset((char *)&s_addr, 0, sizeof(s_addr)); s_addr.sin_family = AF_INET; memcpy(&s_addr.sin_addr.s_addr, sent->h_addr, sent->h_length); s_addr.sin_port = htons(port); // Enable keepalive so we will be informed if the peer // node goes away before it can inform us. setKeepalive(sock); rc = connect(sock, (struct sockaddr *)&s_addr, sizeof(s_addr)); if(rc < 0) { if (errno != EINPROGRESS) return ERR_SOCK_CONNECT; return SOCK_CONNECTING; } return SUCCESS; }
/** host_resolve : string -> 'int32 <doc>Resolve the given host string into an IP address.</doc> **/ static value host_resolve( value host ) { unsigned int ip; val_check(host,string); const char *hostName = val_string(host); gc_enter_blocking(); ip = inet_addr(hostName); if( ip == INADDR_NONE ) { struct hostent *h; # if defined(NEKO_WINDOWS) || defined(NEKO_MAC) || defined(BLACKBERRY) h = gethostbyname(hostName); # else struct hostent hbase; char buf[1024]; int errcode; gethostbyname_r(hostName,&hbase,buf,1024,&h,&errcode); # endif if( h == NULL ) { gc_exit_blocking(); return alloc_null(); } ip = *((unsigned int*)h->h_addr); } gc_exit_blocking(); return alloc_int32(ip); }
int open_tcp(network_t *network, const char * hostname, const int port)/*建立一个到远程服务器的socket连接*/ { struct sockaddr_in serveraddr; struct hostent *hp, hostbuf; struct timeval nTimeout = {10,0}; int res,ret,re; char buffer[1024]; if((network->clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr,"Error in open_tcp(). create socket failed.\n"); return -1; } if((re = setsockopt(network->clientfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(struct timeval)))!=0){ fprintf(stderr,"Error in open_tcp(). Timeout set error!,error : %d\n",re); return -1; } res = gethostbyname_r(hostname, &hostbuf, buffer, sizeof(buffer), &hp, &ret); if(res || hp == NULL){ fprintf(stderr,"Error in open_tcp(). res or hp get failed.\n"); return -1; } bzero((char*) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char*)hp->h_addr_list[0],(char *)&serveraddr.sin_addr.s_addr,hp->h_length); serveraddr.sin_port = htons(port); if(connect(network->clientfd, (struct sockaddr*) &serveraddr,sizeof(serveraddr)) < 0) { fprintf(stderr, "Error in open_tcp(). connect failed.\n"); return -1; } bzero(network->nowbuf,sizeof(network->nowbuf)); network->nowbuflen=0; return 0; }
int getrpcport (const char *host, u_long prognum, u_long versnum, u_int proto) { struct sockaddr_in addr; struct hostent hostbuf, *hp; size_t buflen; char *buffer; int herr; buflen = 1024; buffer = alloca (buflen); while (gethostbyname_r (host, &hostbuf, buffer, buflen, &hp, &herr) != 0 || hp == NULL) if (herr != NETDB_INTERNAL || errno != ERANGE) return 0; else { /* Enlarge the buffer. */ buflen *= 2; buffer = alloca (buflen); } memcpy ((char *) &addr.sin_addr, hp->h_addr, hp->h_length); addr.sin_family = AF_INET; addr.sin_port = 0; return pmap_getport (&addr, prognum, versnum, proto); }
void runFailure1() { struct hostent result_buf; char buf[] = "s"; struct hostent * result; int h_errnop; gethostbyname_r(anystring(), &result_buf, &buf, 50, &result, &h_errnop); }
/* $begin open_clientfd */ int open_clientfd(char *hostname, int port) { int clientfd; int ret; struct hostent host; struct hostent *hp; struct sockaddr_in serveraddr; char buf[8096]; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */ // printf("hostname is %s\n", hostname); /* Fill in the server's IP address and port */ if (gethostbyname_r(hostname, &host, buf, 8096, &hp, &ret)) return -2; /* check h_errno for cause of error */ // printf("All right\n"); bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr_list[0], (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* Establish a connection with the server */ if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd; }
Sock::status_t Sock::hostToIdDigits(const char *hostname, unsigned int digits, std::string& idStr) { struct hostent hostent_struct, *record = NULL; char buff[HOST_BUF_SIZE]; int rc = 0, err = 0; in_addr * address; // using reentrant version of gethostbyname() rc = gethostbyname_r(hostname, &hostent_struct, buff, HOST_BUF_SIZE, &record, &err); if ((rc) || (record == NULL)) { return ERR_SOCK_RESOLVE; } address = (in_addr *)record->h_addr; int ip = ntohl(address->s_addr); std::ostringstream oss; oss << std::hex << std::setw(digits) << std::setfill('0') << ip; idStr.assign(oss.str()); return SUCCESS; }
/* linux version */ int gw_gethostbyname(struct hostent *ent, const char *name, char **buff) { struct hostent *tmphp, hp; int herr, res; size_t bufflen; tmphp = NULL; /* for compiler please */ bufflen = 1024; *buff = (char*) gw_malloc(bufflen); while ((res=gethostbyname_r(name, &hp,*buff, bufflen, &tmphp, &herr)) == ERANGE) { /* enlarge the buffer */ bufflen *= 2; *buff = (char*) gw_realloc(*buff, bufflen); } if (res != 0 || tmphp == NULL) { error(herr, "Error while gw_gethostbyname occurs."); gw_free(*buff); *buff = NULL; res = -1; } else { *ent = hp; } return res; }
void runSuccess() { struct hostent result_buf; char buf[] = "s"; struct hostent * result; int h_errnop; gethostbyname_r(anystring(), &result_buf, &buf, 1, &result, &h_errnop); }
bool Util::safe_gethostbyname(const char *address, HostEnt &result) { #if defined(__APPLE__) struct hostent *hp = gethostbyname(address); if (!hp) { return false; } result.hostbuf = *hp; freehostent(hp); return true; #else struct hostent *hp; int res; size_t hstbuflen = 1024; result.tmphstbuf = (char*)malloc(hstbuflen); while ((res = gethostbyname_r(address, &result.hostbuf, result.tmphstbuf, hstbuflen, &hp, &result.herr)) == ERANGE) { hstbuflen *= 2; result.tmphstbuf = (char*)realloc(result.tmphstbuf, hstbuflen); } return !res && hp; #endif }
int main(void) { struct hostent resbuf; struct hostent *result; int herrno; int retval; /*** strlen (name) = size_needed - sizeof (*host_addr) - sizeof (*h_addr_ptrs) - 1; ***/ size_t len = sizeof(temp.buffer) - 16*sizeof(unsigned char) - 2*sizeof(char *) - 1; char name[sizeof(temp.buffer)]; memset(name, '0', len); name[len] = '\0'; retval = gethostbyname_r(name, &resbuf, temp.buffer, sizeof(temp.buffer), &result, &herrno); if (strcmp(temp.canary, CANARY) != 0) { puts("vulnerable"); exit(EXIT_SUCCESS); } if (retval == ERANGE) { puts("not vulnerable"); exit(EXIT_SUCCESS); } puts("should not happen"); exit(EXIT_FAILURE); }