gethost.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
0N/A/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
1879N/A/* ***** BEGIN LICENSE BLOCK *****
0N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
0N/A *
0N/A * The contents of this file are subject to the Mozilla Public License Version
0N/A * 1.1 (the "License"); you may not use this file except in compliance with
0N/A * the License. You may obtain a copy of the License at
0N/A * http://www.mozilla.org/MPL/
0N/A *
0N/A * Software distributed under the License is distributed on an "AS IS" basis,
0N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
0N/A * for the specific language governing rights and limitations under the
0N/A * License.
0N/A *
0N/A * The Original Code is the Netscape Portable Runtime (NSPR).
0N/A *
0N/A * The Initial Developer of the Original Code is
0N/A * Netscape Communications Corporation.
1472N/A * Portions created by the Initial Developer are Copyright (C) 1999-2000
1472N/A * the Initial Developer. All Rights Reserved.
1472N/A *
0N/A * Contributor(s):
0N/A *
0N/A * Alternatively, the contents of this file may be used under the terms of
1879N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1879N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
0N/A * in which case the provisions of the GPL or the LGPL are applicable instead
0N/A * of those above. If you wish to allow use of your version of this file only
0N/A * under the terms of either the GPL or the LGPL, and not to allow others to
0N/A * use your version of this file under the terms of the MPL, indicate your
0N/A * decision by deleting the provisions above and replace them with the notice
0N/A * and other provisions required by the GPL or the LGPL. If you do not delete
0N/A * the provisions above, a recipient may use your version of this file under
0N/A * the terms of any one of the MPL, the GPL or the LGPL.
0N/A *
0N/A * ***** END LICENSE BLOCK ***** */
0N/A
0N/A/*
0N/A * File: gethost.c
0N/A *
0N/A * Description: tests various functions in prnetdb.h
0N/A *
4312N/A * Usage: gethost [-6] [hostname]
0N/A */
0N/A
0N/A#include "prio.h"
0N/A#include "prnetdb.h"
0N/A#include "plgetopt.h"
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A
0N/A#define DEFAULT_HOST_NAME "mcom.com"
0N/A
0N/Astatic void Help(void)
0N/A{
0N/A fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
0N/A fprintf(stderr, "\t-h help\n");
0N/A fprintf(stderr, "\thostname Name of host (default: %s)\n",
0N/A DEFAULT_HOST_NAME);
0N/A} /* Help */
0N/A
0N/A/*
0N/A * Prints the contents of a PRHostEnt structure
0N/A */
0N/Avoid PrintHostent(const PRHostEnt *he)
0N/A{
0N/A int i;
0N/A int j;
0N/A
0N/A printf("h_name: %s\n", he->h_name);
0N/A for (i = 0; he->h_aliases[i]; i++) {
0N/A printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
0N/A }
0N/A printf("h_addrtype: %d\n", he->h_addrtype);
0N/A printf("h_length: %d\n", he->h_length);
0N/A for (i = 0; he->h_addr_list[i]; i++) {
0N/A printf("h_addr_list[%d]: ", i);
0N/A for (j = 0; j < he->h_length; j++) {
0N/A if (j != 0) printf(".");
0N/A printf("%u", (unsigned char)he->h_addr_list[i][j]);
0N/A }
0N/A printf("\n");
0N/A }
0N/A}
0N/A
0N/Aint main(int argc, char **argv)
0N/A{
0N/A const char *hostName = DEFAULT_HOST_NAME;
0N/A PRHostEnt he, reversehe;
0N/A char buf[PR_NETDB_BUF_SIZE];
0N/A char reversebuf[PR_NETDB_BUF_SIZE];
0N/A PRIntn idx;
0N/A PRNetAddr addr;
0N/A PLOptStatus os;
0N/A PLOptState *opt = PL_CreateOptState(argc, argv, "h");
0N/A
0N/A while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
0N/A if (PL_OPT_BAD == os) continue;
0N/A switch (opt->option) {
0N/A case 0: /* naked */
0N/A hostName = opt->value;
0N/A break;
0N/A case 'h': /* Help message */
0N/A default:
0N/A Help();
0N/A return 2;
0N/A }
0N/A }
0N/A PL_DestroyOptState(opt);
0N/A
0N/A if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
0N/A fprintf(stderr, "PR_GetHostByName failed\n");
0N/A exit(1);
0N/A }
0N/A PrintHostent(&he);
0N/A idx = 0;
0N/A while (1) {
0N/A idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
0N/A if (idx == -1) {
0N/A fprintf(stderr, "PR_EnumerateHostEnt failed\n");
0N/A exit(1);
0N/A }
0N/A if (idx == 0) break; /* normal loop termination */
0N/A printf("reverse lookup\n");
0N/A if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
0N/A &reversehe) == PR_FAILURE) {
0N/A fprintf(stderr, "PR_GetHostByAddr failed\n");
exit(1);
}
PrintHostent(&reversehe);
}
printf("PR_GetIPNodeByName with PR_AF_INET\n");
if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT,
buf, sizeof(buf), &he) == PR_FAILURE) {
fprintf(stderr, "PR_GetIPNodeByName failed\n");
exit(1);
}
PrintHostent(&he);
printf("PR_GetIPNodeByName with PR_AF_INET6\n");
if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT,
buf, sizeof(buf), &he) == PR_FAILURE) {
fprintf(stderr, "PR_GetIPNodeByName failed\n");
exit(1);
}
PrintHostent(&he);
idx = 0;
printf("PR_GetHostByAddr with PR_AF_INET6\n");
while (1) {
idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
if (idx == -1) {
fprintf(stderr, "PR_EnumerateHostEnt failed\n");
exit(1);
}
if (idx == 0) break; /* normal loop termination */
printf("reverse lookup\n");
if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
&reversehe) == PR_FAILURE) {
fprintf(stderr, "PR_GetHostByAddr failed\n");
exit(1);
}
PrintHostent(&reversehe);
}
printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
PR_StringToNetAddr("::1", &addr);
if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
fprintf(stderr, "addr should not be ipv4 mapped address\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
PR_StringToNetAddr("127.0.0.1", &addr);
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
fprintf(stderr, "addr should be ipv4 mapped address\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_InitializeNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
fprintf(stderr, "addr should be unspecified address\n");
exit(1);
}
if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_InitializeNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_SetNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
fprintf(stderr, "addr should be unspecified address\n");
exit(1);
}
if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_SetNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "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) {
fprintf(stderr, "addr should be unspecified address\n");
exit(1);
}
{
char buf[256];
PR_NetAddrToString(&addr, buf, 256);
printf("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) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
{
char buf[256];
PR_NetAddrToString(&addr, buf, 256);
printf("IPv4 LOOPBACK: %s\n", buf);
}
if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_SetNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
fprintf(stderr, "addr should be unspecified address\n");
exit(1);
}
{
char buf[256];
PR_NetAddrToString(&addr, buf, 256);
printf("IPv6 INADDRANY: %s\n", buf);
}
if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_SetNetAddr failed\n");
exit(1);
}
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
fprintf(stderr, "addr should be loopback address\n");
exit(1);
}
{
char buf[256];
PR_NetAddrToString(&addr, buf, 256);
printf("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);
printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);
}
printf("PASS\n");
return 0;
}