📄 prnetdb.c
字号:
if (conversion != _PRIPAddrNoConversion && from->h_addrtype == AF_INET) { PR_ASSERT(from->h_length == 4); to->h_addrtype = PR_AF_INET6; to->h_length = 16; } else {#if defined(_PR_INET6) || defined(_PR_INET6_PROBE) if (AF_INET6 == from->h_addrtype) to->h_addrtype = PR_AF_INET6; else#endif to->h_addrtype = from->h_addrtype; to->h_length = from->h_length; } /* Copy the official name */ if (!from->h_name) return PR_FAILURE; len = strlen(from->h_name) + 1; to->h_name = Alloc(len, buf, bufsize, 0); if (!to->h_name) return PR_FAILURE; memcpy(to->h_name, from->h_name, len); /* Count the aliases, then allocate storage for the pointers */ if (!from->h_aliases) { na = 1; } else { for (na = 1, ap = from->h_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ } to->h_aliases = (char**)Alloc( na * sizeof(char*), buf, bufsize, sizeof(char**)); if (!to->h_aliases) return PR_FAILURE; /* Copy the aliases, one at a time */ if (!from->h_aliases) { to->h_aliases[0] = 0; } else { for (na = 0, ap = from->h_aliases; *ap != 0; na++, ap++) { len = strlen(*ap) + 1; to->h_aliases[na] = Alloc(len, buf, bufsize, 0); if (!to->h_aliases[na]) return PR_FAILURE; memcpy(to->h_aliases[na], *ap, len); } to->h_aliases[na] = 0; } /* Count the addresses, then allocate storage for the pointers */ for (na = 1, ap = from->h_addr_list; *ap != 0; na++, ap++){;} /* nothing to execute */ to->h_addr_list = (char**)Alloc( na * sizeof(char*), buf, bufsize, sizeof(char**)); if (!to->h_addr_list) return PR_FAILURE; /* Copy the addresses, one at a time */ for (na = 0, ap = from->h_addr_list; *ap != 0; na++, ap++) { to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); if (!to->h_addr_list[na]) return PR_FAILURE; if (conversion != _PRIPAddrNoConversion && from->h_addrtype == AF_INET) { if (conversion == _PRIPAddrIPv4Mapped) { MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); } else { PR_ASSERT(conversion == _PRIPAddrIPv4Compat); MakeIPv4CompatAddr(*ap, to->h_addr_list[na]); } } else { memcpy(to->h_addr_list[na], *ap, to->h_length); } } to->h_addr_list[na] = 0; return PR_SUCCESS;}#if !defined(_PR_HAVE_GETPROTO_R)/*** Copy a protoent, and all of the memory that it refers to into** (hopefully) stacked buffers.*/static PRStatus CopyProtoent( struct protoent *from, char *buf, PRIntn bufsize, PRProtoEnt *to){ PRIntn len, na; char **ap; /* Do the easy stuff */ to->p_num = from->p_proto; /* Copy the official name */ if (!from->p_name) return PR_FAILURE; len = strlen(from->p_name) + 1; to->p_name = Alloc(len, &buf, &bufsize, 0); if (!to->p_name) return PR_FAILURE; memcpy(to->p_name, from->p_name, len); /* Count the aliases, then allocate storage for the pointers */ for (na = 1, ap = from->p_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ to->p_aliases = (char**)Alloc( na * sizeof(char*), &buf, &bufsize, sizeof(char**)); if (!to->p_aliases) return PR_FAILURE; /* Copy the aliases, one at a time */ for (na = 0, ap = from->p_aliases; *ap != 0; na++, ap++) { len = strlen(*ap) + 1; to->p_aliases[na] = Alloc(len, &buf, &bufsize, 0); if (!to->p_aliases[na]) return PR_FAILURE; memcpy(to->p_aliases[na], *ap, len); } to->p_aliases[na] = 0; return PR_SUCCESS;}#endif /* !defined(_PR_HAVE_GETPROTO_R) *//* * ################################################################# * NOTE: tmphe, tmpbuf, bufsize, h, and h_err are local variables * or arguments of PR_GetHostByName, PR_GetIPNodeByName, and * PR_GetHostByAddr. DO NOT CHANGE THE NAMES OF THESE LOCAL * VARIABLES OR ARGUMENTS. * ################################################################# */#if defined(_PR_HAVE_GETHOST_R_INT)#define GETHOSTBYNAME(name) \ (gethostbyname_r(name, &tmphe, tmpbuf, bufsize, &h, &h_err), h)#define GETHOSTBYNAME2(name, af) \ (gethostbyname2_r(name, af, &tmphe, tmpbuf, bufsize, &h, &h_err), h)#define GETHOSTBYADDR(addr, addrlen, af) \ (gethostbyaddr_r(addr, addrlen, af, \ &tmphe, tmpbuf, bufsize, &h, &h_err), h)#elif defined(_PR_HAVE_GETHOST_R_POINTER)#define GETHOSTBYNAME(name) \ gethostbyname_r(name, &tmphe, tmpbuf, bufsize, &h_err)#define GETHOSTBYNAME2(name, af) \ gethostbyname2_r(name, af, &tmphe, tmpbuf, bufsize, &h_err)#define GETHOSTBYADDR(addr, addrlen, af) \ gethostbyaddr_r(addr, addrlen, af, &tmphe, tmpbuf, bufsize, &h_err)#else#define GETHOSTBYNAME(name) gethostbyname(name)#define GETHOSTBYNAME2(name, af) gethostbyname2(name, af)#define GETHOSTBYADDR(addr, addrlen, af) gethostbyaddr(addr, addrlen, af)#endif /* definition of GETHOSTBYXXX */PR_IMPLEMENT(PRStatus) PR_GetHostByName( const char *name, char *buf, PRIntn bufsize, PRHostEnt *hp){ struct hostent *h; PRStatus rv = PR_FAILURE;#if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; struct hostent tmphe; int h_err;#endif if (!_pr_initialized) _PR_ImplicitInitialization();#if defined(_PR_HAVE_GETHOST_R) tmpbuf = localbuf; if (bufsize > sizeof(localbuf)) { tmpbuf = (char *)PR_Malloc(bufsize); if (NULL == tmpbuf) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return rv; } }#endif LOCK_DNS();#ifdef XP_OS2_VACPP h = GETHOSTBYNAME((char *)name);#else h = GETHOSTBYNAME(name);#endif if (NULL == h) { PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); } else { _PRIPAddrConversion conversion = _PRIPAddrNoConversion; rv = CopyHostent(h, &buf, &bufsize, conversion, hp); if (PR_SUCCESS != rv) PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); } UNLOCK_DNS();#if defined(_PR_HAVE_GETHOST_R) if (tmpbuf != localbuf) PR_Free(tmpbuf);#endif return rv;}#if defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME)typedef struct hostent * (*_pr_getipnodebyname_t)(const char *, int, int, int *);typedef struct hostent * (*_pr_getipnodebyaddr_t)(const void *, size_t, int, int *);typedef void (*_pr_freehostent_t)(struct hostent *);extern void * _pr_getipnodebyname_fp;extern void * _pr_getipnodebyaddr_fp;extern void * _pr_freehostent_fp;#endif#if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2)/*** Append the V4 addresses to the end of the list*/static PRStatus AppendV4AddrsToHostent( struct hostent *from, char **buf, PRIntn *bufsize, PRHostEnt *to){ PRIntn na, na_old; char **ap; char **new_addr_list; /* Count the addresses, then grow storage for the pointers */ for (na_old = 0, ap = to->h_addr_list; *ap != 0; na_old++, ap++) {;} /* nothing to execute */ for (na = na_old + 1, ap = from->h_addr_list; *ap != 0; na++, ap++) {;} /* nothing to execute */ new_addr_list = (char**)Alloc( na * sizeof(char*), buf, bufsize, sizeof(char**)); if (!new_addr_list) return PR_FAILURE; /* Copy the V6 addresses, one at a time */ for (na = 0, ap = to->h_addr_list; *ap != 0; na++, ap++) { new_addr_list[na] = to->h_addr_list[na]; } to->h_addr_list = new_addr_list; /* Copy the V4 addresses, one at a time */ for (ap = from->h_addr_list; *ap != 0; na++, ap++) { to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); if (!to->h_addr_list[na]) return PR_FAILURE; MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); } to->h_addr_list[na] = 0; return PR_SUCCESS;}#endifPR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( const char *name, PRUint16 af, PRIntn flags, char *buf, PRIntn bufsize, PRHostEnt *hp){ struct hostent *h = 0; PRStatus rv = PR_FAILURE;#if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; struct hostent tmphe; int h_err;#endif#if defined(_PR_HAVE_GETIPNODEBYNAME) PRUint16 md_af = af; int error_num; int tmp_flags = 0;#endif#if defined(_PR_HAVE_GETHOSTBYNAME2) PRBool did_af_inet = PR_FALSE;#endif if (!_pr_initialized) _PR_ImplicitInitialization(); if (af != PR_AF_INET && af != PR_AF_INET6) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); return PR_FAILURE; }#if defined(_PR_HAVE_GETIPNODEBYNAME) if (flags & PR_AI_V4MAPPED) tmp_flags |= AI_V4MAPPED; if (flags & PR_AI_ADDRCONFIG) tmp_flags |= AI_ADDRCONFIG; if (flags & PR_AI_ALL) tmp_flags |= AI_ALL; if (af == PR_AF_INET6) md_af = AF_INET6; else md_af = af;#endif#if defined(_PR_HAVE_GETHOST_R) tmpbuf = localbuf; if (bufsize > sizeof(localbuf)) { tmpbuf = (char *)PR_Malloc(bufsize); if (NULL == tmpbuf) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return rv; } }#endif /* Do not need to lock the DNS lock if getipnodebyname() is called */#ifdef _PR_INET6#ifdef _PR_HAVE_GETHOSTBYNAME2 LOCK_DNS(); if (af == PR_AF_INET6) { if ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet6_if) {#ifdef _PR_INET6_PROBE if (_pr_ipv6_is_present == PR_TRUE)#endif h = GETHOSTBYNAME2(name, AF_INET6); } if ((NULL == h) && (flags & PR_AI_V4MAPPED) && ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if)) { did_af_inet = PR_TRUE; h = GETHOSTBYNAME2(name, AF_INET); } } else { if ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if) { did_af_inet = PR_TRUE; h = GETHOSTBYNAME2(name, af); } }#elif defined(_PR_HAVE_GETIPNODEBYNAME) h = getipnodebyname(name, md_af, tmp_flags, &error_num);#else#error "Unknown name-to-address translation function"#endif /* _PR_HAVE_GETHOSTBYNAME2 */#elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) if (_pr_ipv6_is_present == PR_TRUE) h = (*((_pr_getipnodebyname_t)_pr_getipnodebyname_fp))(name, md_af, tmp_flags, &error_num); else { LOCK_DNS(); h = GETHOSTBYNAME(name); }#else /* _PR_INET6 */ LOCK_DNS();#ifdef XP_OS2_VACPP h = GETHOSTBYNAME((char *)name);#else h = GETHOSTBYNAME(name);#endif#endif /* _PR_INET6 */ if (NULL == h) {#if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num);#elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) if (_pr_ipv6_is_present == PR_TRUE) PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); else PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO());#else PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO());#endif } else { _PRIPAddrConversion conversion = _PRIPAddrNoConversion; if (af == PR_AF_INET6) conversion = _PRIPAddrIPv4Mapped; rv = CopyHostent(h, &buf, &bufsize, conversion, hp); if (PR_SUCCESS != rv) PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0);#if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) freehostent(h);#elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) if (_pr_ipv6_is_present == PR_TRUE) (*((_pr_freehostent_t)_pr_freehostent_fp))(h);#endif#if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) if ((PR_SUCCESS == rv) && (flags & PR_AI_V4MAPPED) && ((flags & PR_AI_ALL) || ((flags & PR_AI_ADDRCONFIG) && _pr_have_inet_if)) && !did_af_inet && (h = GETHOSTBYNAME2(name, AF_INET)) != 0) { rv = AppendV4AddrsToHostent(h, &buf, &bufsize, hp); if (PR_SUCCESS != rv) PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); }#endif } /* Must match the convoluted logic above for LOCK_DNS() */#ifdef _PR_INET6#ifdef _PR_HAVE_GETHOSTBYNAME2 UNLOCK_DNS();#endif /* _PR_HAVE_GETHOSTBYNAME2 */#elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) if (_pr_ipv6_is_present == PR_FALSE) UNLOCK_DNS();#else /* _PR_INET6 */ UNLOCK_DNS();#endif /* _PR_INET6 */#if defined(_PR_HAVE_GETHOST_R) if (tmpbuf != localbuf) PR_Free(tmpbuf);#endif return rv;}PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry){ struct hostent *h; PRStatus rv = PR_FAILURE; const void *addr; PRUint32 tmp_ip; int addrlen; PRInt32 af;#if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; struct hostent tmphe; int h_err;#endif#if defined(_PR_HAVE_GETIPNODEBYADDR) int error_num;#endif if (!_pr_initialized) _PR_ImplicitInitialization(); if (hostaddr->raw.family == PR_AF_INET6) {#if defined(_PR_INET6_PROBE) if (_pr_ipv6_is_present == PR_TRUE) af = AF_INET6;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -