Back to index

lightning-sunbird  0.9+nobinonly
gethost.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * File: gethost.c
00040  *
00041  * Description: tests various functions in prnetdb.h
00042  *
00043  * Usage: gethost [-6] [hostname]
00044  */
00045 
00046 #include "prio.h"
00047 #include "prnetdb.h"
00048 #include "plgetopt.h"
00049 
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 
00053 #define DEFAULT_HOST_NAME "mcom.com"
00054 
00055 static void Help(void)
00056 {
00057     fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
00058     fprintf(stderr, "\t-h          help\n");
00059     fprintf(stderr, "\thostname    Name of host    (default: %s)\n",
00060             DEFAULT_HOST_NAME);
00061 }  /* Help */
00062 
00063 /*
00064  * Prints the contents of a PRHostEnt structure
00065  */
00066 void PrintHostent(const PRHostEnt *he)
00067 {
00068     int i;
00069     int j;
00070 
00071     printf("h_name: %s\n", he->h_name);
00072     for (i = 0; he->h_aliases[i]; i++) {
00073         printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
00074     }
00075     printf("h_addrtype: %d\n", he->h_addrtype);
00076     printf("h_length: %d\n", he->h_length);
00077     for (i = 0; he->h_addr_list[i]; i++) {
00078         printf("h_addr_list[%d]: ", i);
00079         for (j = 0; j < he->h_length; j++) {
00080             if (j != 0) printf(".");
00081             printf("%u", (unsigned char)he->h_addr_list[i][j]);
00082         }
00083         printf("\n");
00084     }
00085 }
00086 
00087 int main(int argc, char **argv)
00088 {
00089     const char *hostName = DEFAULT_HOST_NAME;
00090     PRHostEnt he, reversehe;
00091     char buf[PR_NETDB_BUF_SIZE];
00092     char reversebuf[PR_NETDB_BUF_SIZE];
00093     PRIntn idx;
00094     PRNetAddr addr;
00095     PLOptStatus os;
00096     PLOptState *opt = PL_CreateOptState(argc, argv, "h");
00097 
00098     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
00099         if (PL_OPT_BAD == os) continue;
00100         switch (opt->option) {
00101             case 0:  /* naked */
00102                 hostName = opt->value;
00103                 break;
00104             case 'h':  /* Help message */
00105             default:
00106                 Help();
00107                 return 2;
00108         }
00109     }
00110     PL_DestroyOptState(opt);
00111 
00112     if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
00113         fprintf(stderr, "PR_GetHostByName failed\n");
00114         exit(1);
00115     }
00116     PrintHostent(&he);
00117     idx = 0;
00118     while (1) {
00119         idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
00120         if (idx == -1) {
00121             fprintf(stderr, "PR_EnumerateHostEnt failed\n");
00122             exit(1);
00123         }
00124         if (idx == 0) break;  /* normal loop termination */
00125         printf("reverse lookup\n");
00126         if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
00127                 &reversehe) == PR_FAILURE) {
00128             fprintf(stderr, "PR_GetHostByAddr failed\n");
00129             exit(1);
00130         }
00131         PrintHostent(&reversehe);
00132     }
00133 
00134     printf("PR_GetIPNodeByName with PR_AF_INET\n");
00135     if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT,
00136             buf, sizeof(buf), &he) == PR_FAILURE) {
00137         fprintf(stderr, "PR_GetIPNodeByName failed\n");
00138         exit(1);
00139     }
00140     PrintHostent(&he);
00141     printf("PR_GetIPNodeByName with PR_AF_INET6\n");
00142     if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT,
00143             buf, sizeof(buf), &he) == PR_FAILURE) {
00144         fprintf(stderr, "PR_GetIPNodeByName failed\n");
00145         exit(1);
00146     }
00147     PrintHostent(&he);
00148     idx = 0;
00149     printf("PR_GetHostByAddr with PR_AF_INET6\n");
00150     while (1) {
00151         idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
00152         if (idx == -1) {
00153             fprintf(stderr, "PR_EnumerateHostEnt failed\n");
00154             exit(1);
00155         }
00156         if (idx == 0) break;  /* normal loop termination */
00157         printf("reverse lookup\n");
00158         if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
00159                 &reversehe) == PR_FAILURE) {
00160             fprintf(stderr, "PR_GetHostByAddr failed\n");
00161             exit(1);
00162         }
00163         PrintHostent(&reversehe);
00164     }
00165     printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
00166   
00167     PR_StringToNetAddr("::1", &addr);
00168     if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
00169         fprintf(stderr, "addr should not be ipv4 mapped address\n");
00170         exit(1);
00171     }
00172     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00173         fprintf(stderr, "addr should be loopback address\n");
00174         exit(1);
00175     }
00176 
00177     PR_StringToNetAddr("127.0.0.1", &addr);
00178     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00179         fprintf(stderr, "addr should be loopback address\n");
00180         exit(1);
00181     }
00182     PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
00183     if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
00184         fprintf(stderr, "addr should be ipv4 mapped address\n");
00185         exit(1);
00186     }
00187     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00188         fprintf(stderr, "addr should be loopback address\n");
00189         exit(1);
00190     }
00191 
00192     if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
00193         fprintf(stderr, "PR_InitializeNetAddr failed\n");
00194         exit(1);
00195     }
00196     if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
00197         fprintf(stderr, "addr should be unspecified address\n");
00198         exit(1);
00199     }
00200     if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
00201         fprintf(stderr, "PR_InitializeNetAddr failed\n");
00202         exit(1);
00203     }
00204     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00205         fprintf(stderr, "addr should be loopback address\n");
00206         exit(1);
00207     }
00208 
00209     if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
00210         fprintf(stderr, "PR_SetNetAddr failed\n");
00211         exit(1);
00212     }
00213     if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
00214         fprintf(stderr, "addr should be unspecified address\n");
00215         exit(1);
00216     }
00217     if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
00218         fprintf(stderr, "PR_SetNetAddr failed\n");
00219         exit(1);
00220     }
00221     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00222         fprintf(stderr, "addr should be loopback address\n");
00223         exit(1);
00224     }
00225 
00226     addr.inet.family = PR_AF_INET;
00227     addr.inet.port = 0;
00228     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00229     if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
00230         fprintf(stderr, "addr should be unspecified address\n");
00231         exit(1);
00232     }
00233        {
00234               char buf[256];
00235               PR_NetAddrToString(&addr, buf, 256);
00236               printf("IPv4 INADDRANY: %s\n", buf);
00237        }
00238     addr.inet.family = PR_AF_INET;
00239     addr.inet.port = 0;
00240     addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
00241     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00242         fprintf(stderr, "addr should be loopback address\n");
00243         exit(1);
00244     }
00245        {
00246               char buf[256];
00247               PR_NetAddrToString(&addr, buf, 256);
00248               printf("IPv4 LOOPBACK: %s\n", buf);
00249        }
00250 
00251     if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
00252         fprintf(stderr, "PR_SetNetAddr failed\n");
00253         exit(1);
00254     }
00255     if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
00256         fprintf(stderr, "addr should be unspecified address\n");
00257         exit(1);
00258     }
00259        {
00260               char buf[256];
00261               PR_NetAddrToString(&addr, buf, 256);
00262               printf("IPv6 INADDRANY: %s\n", buf);
00263        }
00264     if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
00265         fprintf(stderr, "PR_SetNetAddr failed\n");
00266         exit(1);
00267     }
00268     if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
00269         fprintf(stderr, "addr should be loopback address\n");
00270         exit(1);
00271     }
00272        {
00273               char buf[256];
00274               PR_NetAddrToString(&addr, buf, 256);
00275               printf("IPv6 LOOPBACK: %s\n", buf);
00276        }
00277        {
00278               PRIPv6Addr v6addr;
00279               char tmp_buf[256];
00280 
00281        PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
00282 
00283               PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr);
00284        PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr);
00285               addr.ipv6.ip = v6addr;
00286               PR_NetAddrToString(&addr, tmp_buf, 256);
00287               printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);
00288        }
00289     printf("PASS\n");
00290     return 0;
00291 }