nsprpub/pr/tests/gethost.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/gethost.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,259 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * File: gethost.c
    1.11 + *
    1.12 + * Description: tests various functions in prnetdb.h
    1.13 + *
    1.14 + * Usage: gethost [-6] [hostname]
    1.15 + */
    1.16 +
    1.17 +#include "prio.h"
    1.18 +#include "prnetdb.h"
    1.19 +#include "plgetopt.h"
    1.20 +
    1.21 +#include <stdio.h>
    1.22 +#include <stdlib.h>
    1.23 +
    1.24 +#define DEFAULT_HOST_NAME "mcom.com"
    1.25 +
    1.26 +static void Help(void)
    1.27 +{
    1.28 +    fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
    1.29 +    fprintf(stderr, "\t-h          help\n");
    1.30 +    fprintf(stderr, "\thostname    Name of host    (default: %s)\n",
    1.31 +            DEFAULT_HOST_NAME);
    1.32 +}  /* Help */
    1.33 +
    1.34 +/*
    1.35 + * Prints the contents of a PRHostEnt structure
    1.36 + */
    1.37 +void PrintHostent(const PRHostEnt *he)
    1.38 +{
    1.39 +    int i;
    1.40 +    int j;
    1.41 +
    1.42 +    printf("h_name: %s\n", he->h_name);
    1.43 +    for (i = 0; he->h_aliases[i]; i++) {
    1.44 +        printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
    1.45 +    }
    1.46 +    printf("h_addrtype: %d\n", he->h_addrtype);
    1.47 +    printf("h_length: %d\n", he->h_length);
    1.48 +    for (i = 0; he->h_addr_list[i]; i++) {
    1.49 +        printf("h_addr_list[%d]: ", i);
    1.50 +        for (j = 0; j < he->h_length; j++) {
    1.51 +            if (j != 0) printf(".");
    1.52 +            printf("%u", (unsigned char)he->h_addr_list[i][j]);
    1.53 +        }
    1.54 +        printf("\n");
    1.55 +    }
    1.56 +}
    1.57 +
    1.58 +int main(int argc, char **argv)
    1.59 +{
    1.60 +    const char *hostName = DEFAULT_HOST_NAME;
    1.61 +    PRHostEnt he, reversehe;
    1.62 +    char buf[PR_NETDB_BUF_SIZE];
    1.63 +    char reversebuf[PR_NETDB_BUF_SIZE];
    1.64 +    PRIntn idx;
    1.65 +    PRNetAddr addr;
    1.66 +    PLOptStatus os;
    1.67 +    PLOptState *opt = PL_CreateOptState(argc, argv, "h");
    1.68 +
    1.69 +    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    1.70 +        if (PL_OPT_BAD == os) continue;
    1.71 +        switch (opt->option) {
    1.72 +            case 0:  /* naked */
    1.73 +                hostName = opt->value;
    1.74 +                break;
    1.75 +            case 'h':  /* Help message */
    1.76 +            default:
    1.77 +                Help();
    1.78 +                return 2;
    1.79 +        }
    1.80 +    }
    1.81 +    PL_DestroyOptState(opt);
    1.82 +
    1.83 +    if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
    1.84 +        fprintf(stderr, "PR_GetHostByName failed\n");
    1.85 +        exit(1);
    1.86 +    }
    1.87 +    PrintHostent(&he);
    1.88 +    idx = 0;
    1.89 +    while (1) {
    1.90 +        idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
    1.91 +        if (idx == -1) {
    1.92 +            fprintf(stderr, "PR_EnumerateHostEnt failed\n");
    1.93 +            exit(1);
    1.94 +        }
    1.95 +        if (idx == 0) break;  /* normal loop termination */
    1.96 +        printf("reverse lookup\n");
    1.97 +        if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
    1.98 +                &reversehe) == PR_FAILURE) {
    1.99 +            fprintf(stderr, "PR_GetHostByAddr failed\n");
   1.100 +            exit(1);
   1.101 +        }
   1.102 +        PrintHostent(&reversehe);
   1.103 +    }
   1.104 +
   1.105 +    printf("PR_GetIPNodeByName with PR_AF_INET\n");
   1.106 +    if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT,
   1.107 +            buf, sizeof(buf), &he) == PR_FAILURE) {
   1.108 +        fprintf(stderr, "PR_GetIPNodeByName failed\n");
   1.109 +        exit(1);
   1.110 +    }
   1.111 +    PrintHostent(&he);
   1.112 +    printf("PR_GetIPNodeByName with PR_AF_INET6\n");
   1.113 +    if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT,
   1.114 +            buf, sizeof(buf), &he) == PR_FAILURE) {
   1.115 +        fprintf(stderr, "PR_GetIPNodeByName failed\n");
   1.116 +        exit(1);
   1.117 +    }
   1.118 +    PrintHostent(&he);
   1.119 +    idx = 0;
   1.120 +    printf("PR_GetHostByAddr with PR_AF_INET6\n");
   1.121 +    while (1) {
   1.122 +        idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
   1.123 +        if (idx == -1) {
   1.124 +            fprintf(stderr, "PR_EnumerateHostEnt failed\n");
   1.125 +            exit(1);
   1.126 +        }
   1.127 +        if (idx == 0) break;  /* normal loop termination */
   1.128 +        printf("reverse lookup\n");
   1.129 +        if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
   1.130 +                &reversehe) == PR_FAILURE) {
   1.131 +            fprintf(stderr, "PR_GetHostByAddr failed\n");
   1.132 +            exit(1);
   1.133 +        }
   1.134 +        PrintHostent(&reversehe);
   1.135 +    }
   1.136 +    printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
   1.137 +  
   1.138 +    PR_StringToNetAddr("::1", &addr);
   1.139 +    if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
   1.140 +        fprintf(stderr, "addr should not be ipv4 mapped address\n");
   1.141 +        exit(1);
   1.142 +    }
   1.143 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.144 +        fprintf(stderr, "addr should be loopback address\n");
   1.145 +        exit(1);
   1.146 +    }
   1.147 +
   1.148 +    PR_StringToNetAddr("127.0.0.1", &addr);
   1.149 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.150 +        fprintf(stderr, "addr should be loopback address\n");
   1.151 +        exit(1);
   1.152 +    }
   1.153 +    PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
   1.154 +    if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
   1.155 +        fprintf(stderr, "addr should be ipv4 mapped address\n");
   1.156 +        exit(1);
   1.157 +    }
   1.158 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.159 +        fprintf(stderr, "addr should be loopback address\n");
   1.160 +        exit(1);
   1.161 +    }
   1.162 +
   1.163 +    if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
   1.164 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
   1.165 +        exit(1);
   1.166 +    }
   1.167 +    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
   1.168 +        fprintf(stderr, "addr should be unspecified address\n");
   1.169 +        exit(1);
   1.170 +    }
   1.171 +    if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
   1.172 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
   1.173 +        exit(1);
   1.174 +    }
   1.175 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.176 +        fprintf(stderr, "addr should be loopback address\n");
   1.177 +        exit(1);
   1.178 +    }
   1.179 +
   1.180 +    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
   1.181 +        fprintf(stderr, "PR_SetNetAddr failed\n");
   1.182 +        exit(1);
   1.183 +    }
   1.184 +    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
   1.185 +        fprintf(stderr, "addr should be unspecified address\n");
   1.186 +        exit(1);
   1.187 +    }
   1.188 +    if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
   1.189 +        fprintf(stderr, "PR_SetNetAddr failed\n");
   1.190 +        exit(1);
   1.191 +    }
   1.192 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.193 +        fprintf(stderr, "addr should be loopback address\n");
   1.194 +        exit(1);
   1.195 +    }
   1.196 +
   1.197 +    addr.inet.family = PR_AF_INET;
   1.198 +    addr.inet.port = 0;
   1.199 +    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   1.200 +    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
   1.201 +        fprintf(stderr, "addr should be unspecified address\n");
   1.202 +        exit(1);
   1.203 +    }
   1.204 +	{
   1.205 +		char buf[256];
   1.206 +		PR_NetAddrToString(&addr, buf, 256);
   1.207 +		printf("IPv4 INADDRANY: %s\n", buf);
   1.208 +	}
   1.209 +    addr.inet.family = PR_AF_INET;
   1.210 +    addr.inet.port = 0;
   1.211 +    addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
   1.212 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.213 +        fprintf(stderr, "addr should be loopback address\n");
   1.214 +        exit(1);
   1.215 +    }
   1.216 +	{
   1.217 +		char buf[256];
   1.218 +		PR_NetAddrToString(&addr, buf, 256);
   1.219 +		printf("IPv4 LOOPBACK: %s\n", buf);
   1.220 +	}
   1.221 +
   1.222 +    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
   1.223 +        fprintf(stderr, "PR_SetNetAddr failed\n");
   1.224 +        exit(1);
   1.225 +    }
   1.226 +    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
   1.227 +        fprintf(stderr, "addr should be unspecified address\n");
   1.228 +        exit(1);
   1.229 +    }
   1.230 +	{
   1.231 +		char buf[256];
   1.232 +		PR_NetAddrToString(&addr, buf, 256);
   1.233 +		printf("IPv6 INADDRANY: %s\n", buf);
   1.234 +	}
   1.235 +    if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
   1.236 +        fprintf(stderr, "PR_SetNetAddr failed\n");
   1.237 +        exit(1);
   1.238 +    }
   1.239 +    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
   1.240 +        fprintf(stderr, "addr should be loopback address\n");
   1.241 +        exit(1);
   1.242 +    }
   1.243 +	{
   1.244 +		char buf[256];
   1.245 +		PR_NetAddrToString(&addr, buf, 256);
   1.246 +		printf("IPv6 LOOPBACK: %s\n", buf);
   1.247 +	}
   1.248 +	{
   1.249 +		PRIPv6Addr v6addr;
   1.250 +		char tmp_buf[256];
   1.251 +
   1.252 +    	PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
   1.253 +
   1.254 +		PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr);
   1.255 +    	PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr);
   1.256 +		addr.ipv6.ip = v6addr;
   1.257 +		PR_NetAddrToString(&addr, tmp_buf, 256);
   1.258 +		printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);
   1.259 +	}
   1.260 +    printf("PASS\n");
   1.261 +    return 0;
   1.262 +}

mercurial