static LDAPHostEnt * prldap_gethostbyaddr( const char *addr, int length, int type, LDAPHostEnt *result, char *buffer, int buflen, int *statusp, void *extradata ) { PRHostEnt prhent; PRNetAddr iaddr; if ( PR_SetNetAddr(PR_IpAddrNull, PRLDAP_DEFAULT_ADDRESS_FAMILY, 0, &iaddr) == PR_FAILURE || PR_StringToNetAddr( addr, &iaddr ) == PR_FAILURE ) { return( NULL ); } if( !statusp || (*statusp = PR_GetHostByAddr(&iaddr, buffer, buflen, &prhent )) == PR_FAILURE ) { return( NULL ); } return( prldap_convert_hostent( result, &prhent )); }
PRStatus RCHostLookup::ByAddress(const RCNetAddr& host_addr) { PRStatus rv; PRNetAddr addr; PRHostEnt hostentry; PRIntn index = 0, max; RCNetAddr* vector = NULL; RCNetAddr* old_vector = NULL; char *buffer = (char*)PR_Malloc(PR_NETDB_BUF_SIZE); if (NULL == buffer) return PR_FAILURE; rv = PR_GetHostByAddr(host_addr, buffer, PR_NETDB_BUF_SIZE, &hostentry); if (PR_SUCCESS == rv) { for (max = 0, index = 0;; ++max) { index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); if (0 == index) break; } if (max > 0) { vector = new RCNetAddr[max]; while (--max > 0) { index = PR_EnumerateHostEnt(index, &hostentry, 0, &addr); if (0 == index) break; vector[index] = &addr; } { RCEnter entry(&ml); old_vector = address; address = vector; max_index = max; } if (NULL != old_vector) delete [] old_vector; } } if (NULL != buffer) PR_DELETE(buffer); return PR_SUCCESS; } /* RCHostLookup::ByAddress */
/* LASDNSBuild * Builds a hash table of all the hostnames provided (plus their aliases * if aliasflg is true). Wildcards are only permitted in the leftmost * field. They're represented in the hash table by a leading period. * E.g. ".mcom.com". * * RETURNS Zero on success, else LAS_EVAL_INVALID */ int LASDnsBuild(NSErr_t *errp, char *attr_pattern, LASDnsContext_t *context, int aliasflg) { size_t delimiter; /* length of valid tokeni */ char token[256]; /* max length dns name */ int i; char **p; pool_handle_t *pool; PRStatus error=PR_SUCCESS; char buffer[PR_NETDB_BUF_SIZE]; #ifdef UTEST struct hostent *he, host; #else PRHostEnt *he, host; #endif char *end_attr_pattern; if (attr_pattern == NULL) { nserrGenerate(errp, ACLERRINVAL, ACLERR4770, ACL_Program, 1, XP_GetAdminStr(DBT_lasdnsbuildInvalidAttributePattern_)); return LAS_EVAL_INVALID; } context->Table = PR_NewHashTable(0, PR_HashCaseString, PR_CompareCaseStrings, PR_CompareValues, &ACLPermAllocOps, NULL); pool = pool_create(); context->pool = pool; if ((!context->Table) || (!context->pool)) { nserrGenerate(errp, ACLERRNOMEM, ACLERR4700, ACL_Program, 1, XP_GetAdminStr(DBT_lasdnsbuildUnableToAllocateHashT_)); return LAS_EVAL_INVALID; } end_attr_pattern = attr_pattern + strlen(attr_pattern); do { size_t maxsize = sizeof(token); /* Get a single hostname from the pattern string */ delimiter = strcspn(attr_pattern, ", \t"); if (delimiter >= maxsize) { delimiter = maxsize-1; } PL_strncpyz(token, attr_pattern, delimiter + 1); token[delimiter] = '\0'; /* Skip any white space after the token */ attr_pattern += delimiter; if (attr_pattern < end_attr_pattern) { attr_pattern += strspn(attr_pattern, ", \t"); } /* If there's a wildcard, strip it off but leave the "." * Can't have aliases for a wildcard pattern. * Treat "*" as a special case. If so, go ahead and hash it. */ if (token[0] == '*') { if (token[1] != '\0') { if (!PR_HashTableAdd(context->Table, pool_strdup(pool, &token[1]), (void *)-1)) { nserrGenerate(errp, ACLERRFAIL, ACLERR4710, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token); return LAS_EVAL_INVALID; } } else { if (!PR_HashTableAdd(context->Table, pool_strdup(pool, token), (void *)-1)) { nserrGenerate(errp, ACLERRFAIL, ACLERR4720, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token); return LAS_EVAL_INVALID; } } } else { /* This is a single hostname add it to the hash table */ if (!PR_HashTableAdd(context->Table, pool_strdup(pool, &token[0]), (void *)-1)) { nserrGenerate(errp, ACLERRFAIL, ACLERR4730, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token); return LAS_EVAL_INVALID; } if (aliasflg) { void *iter = NULL; int addrcnt = 0; PRNetAddr *netaddr = (PRNetAddr *)PERM_CALLOC(sizeof(PRNetAddr)); PRAddrInfo *infop = PR_GetAddrInfoByName(token, PR_AF_UNSPEC, (PR_AI_ADDRCONFIG|PR_AI_NOCANONNAME)); if (!netaddr) { if (infop) { PR_FreeAddrInfo(infop); } return LAS_EVAL_NEED_MORE_INFO; /* hostname not known to dns? */ } if (!infop) { if (netaddr) { PERM_FREE(netaddr); } return LAS_EVAL_NEED_MORE_INFO; /* hostname not known to dns? */ } /* need to count the address, first */ while ((iter = PR_EnumerateAddrInfo(iter, infop, 0, netaddr))) { addrcnt++; } if (0 == addrcnt) { PERM_FREE(netaddr); PR_FreeAddrInfo(infop); return LAS_EVAL_NEED_MORE_INFO; /* hostname not known to dns? */ } iter = NULL; /* from the beginning */ memset(netaddr, 0, sizeof(PRNetAddr)); for (i = 0; i < addrcnt; i++) { iter = PR_EnumerateAddrInfo( iter, infop, 0, netaddr ); if (NULL == iter) { break; } error = PR_GetHostByAddr(netaddr, buffer, PR_NETDB_BUF_SIZE, &host); if (error == PR_SUCCESS) { he = &host; } else { continue; } if (he->h_name) { /* Add it to the hash table */ if (!PR_HashTableAdd(context->Table, pool_strdup(pool, he->h_name), (void *)-1)) { nserrGenerate(errp, ACLERRFAIL, ACLERR4750, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), he->h_name); PERM_FREE(netaddr); PR_FreeAddrInfo(infop); return LAS_EVAL_INVALID; } } if (he->h_aliases && he->h_aliases[0]) { for (p = he->h_aliases; *p; ++p) { /* Add it to the hash table */ if (!PR_HashTableAdd(context->Table, pool_strdup(pool, *p), (void *)-1)) { nserrGenerate(errp, ACLERRFAIL, ACLERR4760, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), *p); PERM_FREE(netaddr); PR_FreeAddrInfo(infop); return LAS_EVAL_INVALID; } } } } /* for (i = 0; i < addrcnt; i++) */ PERM_FREE(netaddr); PR_FreeAddrInfo(infop); } /* if aliasflg */ } /* else - single hostname */ } while ((attr_pattern != NULL) && (attr_pattern[0] != '\0') && (delimiter != 0)); return 0; }
char *dns_ip2host(char *ip, int verify) { /* struct in_addr iaddr; */ PRNetAddr iaddr; char *hn; static unsigned long laddr = 0; static char myhostname[256]; PRHostEnt hent; char buf[PR_NETDB_BUF_SIZE]; PRStatus err; err = PR_InitializeNetAddr(PR_IpAddrNull, 0, &iaddr); /* richm: ipv6 cleanup - use inet_aton or other more appropriate function instead of inet_addr */ if((iaddr.inet.ip = inet_addr(ip)) == (in_addr_t)-1) goto bong; /* * See if it happens to be the localhost IP address, and try * the local host name if so. */ if (laddr == 0) { laddr = inet_addr("127.0.0.1"); myhostname[0] = 0; PR_GetSystemInfo(PR_SI_HOSTNAME, myhostname, sizeof(myhostname)); } /* Have to match the localhost IP address and have a hostname */ if ((iaddr.inet.ip == laddr) && (myhostname[0] != 0)) { /* * Now try for a fully-qualified domain name, starting with * the local hostname. */ err = PR_GetHostByName(myhostname, buf, PR_NETDB_BUF_SIZE, &hent); /* Don't verify if we get a fully-qualified name this way */ verify = 0; } else { err = PR_GetHostByAddr(&iaddr, buf, PR_NETDB_BUF_SIZE, &hent); } if ((err == PR_FAILURE) || !(hn = net_find_fqdn(&hent))) goto bong; if(verify) { char **haddr = 0; err = PR_GetHostByName(hn, buf, PR_NETDB_BUF_SIZE, &hent); if(err == PR_SUCCESS) { for(haddr = hent.h_addr_list; *haddr; haddr++) { if(((struct in_addr *)(*haddr))->s_addr == iaddr.inet.ip) break; } } if((err == PR_FAILURE) || (!(*haddr))) goto bong; } return hn; bong: return NULL; }
int main(int argc, char **argv) #endif { const char *hostName = DEFAULT_HOST_NAME; PRHostEnt he, reversehe; char buf[PR_NETDB_BUF_SIZE]; char reversebuf[PR_NETDB_BUF_SIZE]; PRIntn idx; PRNetAddr addr; PLOptStatus os; PLOptState *opt = PL_CreateOptState(argc, argv, "h"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 0: /* naked */ hostName = opt->value; break; case 'h': /* Help message */ default: Help(); return 2; } } PL_DestroyOptState(opt); PR_STDIO_INIT(); outFile = PR_GetSpecialFD(PR_StandardError); if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) { PR_fprintf(outFile, "PR_GetHostByName failed\n"); exit(1); } PrintHostent(&he); idx = 0; while (1) { idx = PR_EnumerateHostEnt(idx, &he, 0, &addr); if (idx == -1) { PR_fprintf(outFile, "PR_EnumerateHostEnt failed\n"); exit(1); } if (idx == 0) break; /* normal loop termination */ PR_fprintf(outFile, "reverse lookup\n"); if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), &reversehe) == PR_FAILURE) { PR_fprintf(outFile, "PR_GetHostByAddr failed\n"); exit(1); } PrintHostent(&reversehe); } PR_fprintf(outFile, "PR_GetIPNodeByName with PR_AF_INET\n"); if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT, buf, sizeof(buf), &he) == PR_FAILURE) { PR_fprintf(outFile, "PR_GetIPNodeByName failed\n"); exit(1); } PrintHostent(&he); PR_fprintf(outFile, "PR_GetIPNodeByName with PR_AF_INET6\n"); if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT, buf, sizeof(buf), &he) == PR_FAILURE) { PR_fprintf(outFile, "PR_GetIPNodeByName failed\n"); exit(1); } PrintHostent(&he); idx = 0; PR_fprintf(outFile, "PR_GetHostByAddr with PR_AF_INET6\n"); while (1) { idx = PR_EnumerateHostEnt(idx, &he, 0, &addr); if (idx == -1) { PR_fprintf(outFile, "PR_EnumerateHostEnt failed\n"); exit(1); } if (idx == 0) break; /* normal loop termination */ PR_fprintf(outFile, "reverse lookup\n"); if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), &reversehe) == PR_FAILURE) { PR_fprintf(outFile, "PR_GetHostByAddr failed\n"); exit(1); } PrintHostent(&reversehe); } PR_fprintf(outFile, "PR_GetHostByAddr with PR_AF_INET6 done\n"); PR_StringToNetAddr("::1", &addr); if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) { PR_fprintf(outFile, "addr should not be ipv4 mapped address\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } PR_StringToNetAddr("127.0.0.1", &addr); if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } PR_StringToNetAddr("::FFFF:127.0.0.1", &addr); if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) { PR_fprintf(outFile, "addr should be ipv4 mapped address\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_InitializeNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { PR_fprintf(outFile, "addr should be unspecified address\n"); exit(1); } if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_InitializeNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_SetNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { PR_fprintf(outFile, "addr should be unspecified address\n"); exit(1); } if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_SetNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } addr.inet.family = PR_AF_INET; addr.inet.port = 0; addr.inet.ip = PR_htonl(PR_INADDR_ANY); if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { PR_fprintf(outFile, "addr should be unspecified address\n"); exit(1); } { char buf[256]; PR_NetAddrToString(&addr, buf, 256); PR_fprintf(outFile, "IPv4 INADDRANY: %s\n", buf); } addr.inet.family = PR_AF_INET; addr.inet.port = 0; addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } { char buf[256]; PR_NetAddrToString(&addr, buf, 256); PR_fprintf(outFile, "IPv4 LOOPBACK: %s\n", buf); } if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_SetNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { PR_fprintf(outFile, "addr should be unspecified address\n"); exit(1); } { char buf[256]; PR_NetAddrToString(&addr, buf, 256); PR_fprintf(outFile, "IPv6 INADDRANY: %s\n", buf); } if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) { PR_fprintf(outFile, "PR_SetNetAddr failed\n"); exit(1); } if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { PR_fprintf(outFile, "addr should be loopback address\n"); exit(1); } { char buf[256]; PR_NetAddrToString(&addr, buf, 256); PR_fprintf(outFile, "IPv6 LOOPBACK: %s\n", buf); } { PRIPv6Addr v6addr; char tmp_buf[256]; PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr); PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr); PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr); addr.ipv6.ip = v6addr; PR_NetAddrToString(&addr, tmp_buf, 256); PR_fprintf(outFile, "IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf); } PR_fprintf(outFile, "PASS\n"); return 0; }