michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: gethost.c michael@0: * michael@0: * Description: tests various functions in prnetdb.h michael@0: * michael@0: * Usage: gethost [-6] [hostname] michael@0: */ michael@0: michael@0: #include "prio.h" michael@0: #include "prnetdb.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #define DEFAULT_HOST_NAME "mcom.com" michael@0: michael@0: static void Help(void) michael@0: { michael@0: fprintf(stderr, "Usage: gethost [-h] [hostname]\n"); michael@0: fprintf(stderr, "\t-h help\n"); michael@0: fprintf(stderr, "\thostname Name of host (default: %s)\n", michael@0: DEFAULT_HOST_NAME); michael@0: } /* Help */ michael@0: michael@0: /* michael@0: * Prints the contents of a PRHostEnt structure michael@0: */ michael@0: void PrintHostent(const PRHostEnt *he) michael@0: { michael@0: int i; michael@0: int j; michael@0: michael@0: printf("h_name: %s\n", he->h_name); michael@0: for (i = 0; he->h_aliases[i]; i++) { michael@0: printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]); michael@0: } michael@0: printf("h_addrtype: %d\n", he->h_addrtype); michael@0: printf("h_length: %d\n", he->h_length); michael@0: for (i = 0; he->h_addr_list[i]; i++) { michael@0: printf("h_addr_list[%d]: ", i); michael@0: for (j = 0; j < he->h_length; j++) { michael@0: if (j != 0) printf("."); michael@0: printf("%u", (unsigned char)he->h_addr_list[i][j]); michael@0: } michael@0: printf("\n"); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: const char *hostName = DEFAULT_HOST_NAME; michael@0: PRHostEnt he, reversehe; michael@0: char buf[PR_NETDB_BUF_SIZE]; michael@0: char reversebuf[PR_NETDB_BUF_SIZE]; michael@0: PRIntn idx; michael@0: PRNetAddr addr; michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "h"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) { michael@0: case 0: /* naked */ michael@0: hostName = opt->value; michael@0: break; michael@0: case 'h': /* Help message */ michael@0: default: michael@0: Help(); michael@0: return 2; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetHostByName failed\n"); michael@0: exit(1); michael@0: } michael@0: PrintHostent(&he); michael@0: idx = 0; michael@0: while (1) { michael@0: idx = PR_EnumerateHostEnt(idx, &he, 0, &addr); michael@0: if (idx == -1) { michael@0: fprintf(stderr, "PR_EnumerateHostEnt failed\n"); michael@0: exit(1); michael@0: } michael@0: if (idx == 0) break; /* normal loop termination */ michael@0: printf("reverse lookup\n"); michael@0: if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), michael@0: &reversehe) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetHostByAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: PrintHostent(&reversehe); michael@0: } michael@0: michael@0: printf("PR_GetIPNodeByName with PR_AF_INET\n"); michael@0: if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT, michael@0: buf, sizeof(buf), &he) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetIPNodeByName failed\n"); michael@0: exit(1); michael@0: } michael@0: PrintHostent(&he); michael@0: printf("PR_GetIPNodeByName with PR_AF_INET6\n"); michael@0: if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT, michael@0: buf, sizeof(buf), &he) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetIPNodeByName failed\n"); michael@0: exit(1); michael@0: } michael@0: PrintHostent(&he); michael@0: idx = 0; michael@0: printf("PR_GetHostByAddr with PR_AF_INET6\n"); michael@0: while (1) { michael@0: idx = PR_EnumerateHostEnt(idx, &he, 0, &addr); michael@0: if (idx == -1) { michael@0: fprintf(stderr, "PR_EnumerateHostEnt failed\n"); michael@0: exit(1); michael@0: } michael@0: if (idx == 0) break; /* normal loop termination */ michael@0: printf("reverse lookup\n"); michael@0: if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), michael@0: &reversehe) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetHostByAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: PrintHostent(&reversehe); michael@0: } michael@0: printf("PR_GetHostByAddr with PR_AF_INET6 done\n"); michael@0: michael@0: PR_StringToNetAddr("::1", &addr); michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) { michael@0: fprintf(stderr, "addr should not be ipv4 mapped address\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: PR_StringToNetAddr("127.0.0.1", &addr); michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: PR_StringToNetAddr("::FFFF:127.0.0.1", &addr); michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be ipv4 mapped address\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be unspecified address\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be unspecified address\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.port = 0; michael@0: addr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be unspecified address\n"); michael@0: exit(1); michael@0: } michael@0: { michael@0: char buf[256]; michael@0: PR_NetAddrToString(&addr, buf, 256); michael@0: printf("IPv4 INADDRANY: %s\n", buf); michael@0: } michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.port = 0; michael@0: addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: { michael@0: char buf[256]; michael@0: PR_NetAddrToString(&addr, buf, 256); michael@0: printf("IPv4 LOOPBACK: %s\n", buf); michael@0: } michael@0: michael@0: if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be unspecified address\n"); michael@0: exit(1); michael@0: } michael@0: { michael@0: char buf[256]; michael@0: PR_NetAddrToString(&addr, buf, 256); michael@0: printf("IPv6 INADDRANY: %s\n", buf); michael@0: } michael@0: if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) { michael@0: fprintf(stderr, "addr should be loopback address\n"); michael@0: exit(1); michael@0: } michael@0: { michael@0: char buf[256]; michael@0: PR_NetAddrToString(&addr, buf, 256); michael@0: printf("IPv6 LOOPBACK: %s\n", buf); michael@0: } michael@0: { michael@0: PRIPv6Addr v6addr; michael@0: char tmp_buf[256]; michael@0: michael@0: PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr); michael@0: michael@0: PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr); michael@0: PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr); michael@0: addr.ipv6.ip = v6addr; michael@0: PR_NetAddrToString(&addr, tmp_buf, 256); michael@0: printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf); michael@0: } michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }