(linenum→info "unix/slp.c:2238")

glibc/2.7/nss/test-netdb.c

    1: /* Copyright (C) 1998,99,2000,01 Free Software Foundation, Inc.
    2:    This file is part of the GNU C Library.
    3:    Contributed by Andreas Jaeger <aj@suse.de>, 1998.
    4: 
    5:    The GNU C Library is free software; you can redistribute it and/or
    6:    modify it under the terms of the GNU Lesser General Public
    7:    License as published by the Free Software Foundation; either
    8:    version 2.1 of the License, or (at your option) any later version.
    9: 
   10:    The GNU C Library is distributed in the hope that it will be useful,
   11:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13:    Lesser General Public License for more details.
   14: 
   15:    You should have received a copy of the GNU Lesser General Public
   16:    License along with the GNU C Library; if not, write to the Free
   17:    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   18:    02111-1307 USA.  */
   19: 
   20: /*
   21:   Testing of some network related lookup functions.
   22:   The system databases looked up are:
   23:   - /etc/services
   24:   - /etc/hosts
   25:   - /etc/networks
   26:   - /etc/protocols
   27:   - /etc/rpc
   28:   The tests try to be fairly generic and simple so that they work on
   29:   every possible setup (and might therefore not detect some possible
   30:   errors).
   31: */
   32: 
   33: #include <netdb.h>
   34: #include <rpc/netdb.h>
   35: #include <stdio.h>
   36: #include <stdlib.h>
   37: #include <string.h>
   38: #include <arpa/inet.h>
   39: #include <netinet/in.h>
   40: #include <sys/param.h>
   41: #include <sys/socket.h>
   42: #include <unistd.h>
   43: #include <errno.h>
   44: #include "nss.h"
   45: 
   46: /*
   47:   The following define is necessary for glibc 2.0.6
   48: */
   49: #ifndef INET6_ADDRSTRLEN
   50: # define INET6_ADDRSTRLEN 46
   51: #endif
   52: 
   53: int error_count;
   54: 
   55: static void
   56: output_servent (const char *call, struct servent *sptr)
   57: {
   58:   char **pptr;
   59: 
   60:   if (sptr == NULL)
   61:     printf ("Call: %s returned NULL\n", call);
   62:   else
   63:     {
   64:       printf ("Call: %s, returned: s_name: %s, s_port: %d, s_proto: %s\n",
   65:               call, sptr->s_name, ntohs(sptr->s_port), sptr->s_proto);
   66:       for (pptr = sptr->s_aliases; *pptr != NULL; pptr++)
   67:         printf ("  alias: %s\n", *pptr);
   68:     }
   69: }
   70: 
   71: 
   72: static void
   73: test_services (void)
   74: {
   75:   struct servent *sptr;
   76: 
   77:   sptr = getservbyname ("domain", "tcp");
   78:   output_servent ("getservbyname (\"domain\", \"tcp\")", sptr);
   79: 
   80:   sptr = getservbyname ("domain", "udp");
   81:   output_servent ("getservbyname (\"domain\", \"udp\")", sptr);
   82: 
   83:   sptr = getservbyname ("domain", NULL);
   84:   output_servent ("getservbyname (\"domain\", NULL)", sptr);
   85: 
   86:   sptr = getservbyname ("not-existant", NULL);
   87:   output_servent ("getservbyname (\"not-existant\", NULL)", sptr);
   88: 
   89:   /* This shouldn't return anything.  */
   90:   sptr = getservbyname ("", "");
   91:   output_servent ("getservbyname (\"\", \"\")", sptr);
   92: 
   93:   sptr = getservbyname ("", "tcp");
   94:   output_servent ("getservbyname (\"\", \"tcp\")", sptr);
   95: 
   96:   sptr = getservbyport (htons(53), "tcp");
   97:   output_servent ("getservbyport (htons(53), \"tcp\")", sptr);
   98: 
   99:   sptr = getservbyport (htons(53), NULL);
  100:   output_servent ("getservbyport (htons(53), NULL)", sptr);
  101: 
  102:   sptr = getservbyport (htons(1), "udp"); /* shouldn't exist */
  103:   output_servent ("getservbyport (htons(1), \"udp\")", sptr);
  104: 
  105:   setservent (0);
  106:   do
  107:     {
  108:       sptr = getservent ();
  109:       output_servent ("getservent ()", sptr);
  110:     }
  111:   while (sptr != NULL);
  112:   endservent ();
  113: }
  114: 
  115: 
  116: static void
  117: output_hostent (const char *call, struct hostent *hptr)
  118: {
  119:   char **pptr;
  120:   char buf[INET6_ADDRSTRLEN];
  121: 
  122:   if (hptr == NULL)
  123:     printf ("Call: %s returned NULL\n", call);
  124:   else
  125:     {
  126:       printf ("Call: %s returned: name: %s, addr_type: %d\n",
  127:               call, hptr->h_name, hptr->h_addrtype);
  128:       if (hptr->h_aliases)
  129:         for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  130:           printf ("  alias: %s\n", *pptr);
  131: 
  132:       for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++)
  133:         printf ("  ip: %s\n",
  134:                 inet_ntop (hptr->h_addrtype, *pptr, buf, sizeof (buf)));
  135:     }
  136: }
  137: 
  138: static void
  139: test_hosts (void)
  140: {
  141:   struct hostent *hptr1, *hptr2;
  142:   char *name = NULL;
  143:   size_t namelen = 0;
  144:   struct in_addr ip;
  145: 
  146:   hptr1 = gethostbyname ("localhost");
  147:   hptr2 = gethostbyname ("LocalHost");
  148:   if (hptr1 != NULL || hptr2 != NULL)
  149:     {
  150:       if (hptr1 == NULL)
  151:         {
  152:           printf ("localhost not found - but LocalHost found:-(\n");
  153:           ++error_count;
  154:         }
  155:       else if (hptr2 == NULL)
  156:         {
  157:           printf ("LocalHost not found - but localhost found:-(\n");
  158:           ++error_count;
  159:         }
  160:       else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
  161:         {
  162:           printf ("localhost and LocalHost have different canoncial name\n");
  163:           printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
  164:           printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
  165:           ++error_count;
  166:         }
  167:       else
  168:         output_hostent ("gethostbyname(\"localhost\")", hptr1);
  169:     }
  170: 
  171:   hptr1 = gethostbyname ("127.0.0.1");
  172:   output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);
  173: 
  174:   hptr1 = gethostbyname ("10.1234");
  175:   output_hostent ("gethostbyname (\"10.1234\")", hptr1);
  176: 
  177:   hptr1 = gethostbyname2 ("localhost", AF_INET);
  178:   output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);
  179: 
  180:   while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
  181:     {
  182:       namelen += 2;             /* tiny increments to test a lot */
  183:       name = realloc (name, namelen);
  184:     }
  185:   if (gethostname (name, namelen) == 0)
  186:     {
  187:       printf ("Hostname: %s\n", name);
  188:       if (name != NULL)
  189:         {
  190:           hptr1 = gethostbyname (name);
  191:           output_hostent ("gethostbyname (gethostname(...))", hptr1);
  192:         }
  193:     }
  194: 
  195:   ip.s_addr = htonl (INADDR_LOOPBACK);
  196:   hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
  197:   if (hptr1 != NULL)
  198:     {
  199:       printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
  200:     }
  201: 
  202:   sethostent (0);
  203:   do
  204:     {
  205:       hptr1 = gethostent ();
  206:       output_hostent ("gethostent ()", hptr1);
  207:     }
  208:   while (hptr1 != NULL);
  209:   endhostent ();
  210: 
  211: }
  212: 
  213: 
  214: static void
  215: output_netent (const char *call, struct netent *nptr)
  216: {
  217:   char **pptr;
  218: 
  219:   if (nptr == NULL)
  220:     printf ("Call: %s returned NULL\n", call);
  221:   else
  222:     {
  223:       struct in_addr ip;
  224: 
  225:       ip.s_addr = htonl(nptr->n_net);
  226:       printf ("Call: %s, returned: n_name: %s, network_number: %s\n",
  227:               call, nptr->n_name, inet_ntoa (ip));
  228: 
  229:       for (pptr = nptr->n_aliases; *pptr != NULL; pptr++)
  230:         printf ("  alias: %s\n", *pptr);
  231:     }
  232: }
  233: 
  234: static void
  235: test_network (void)
  236: {
  237:   struct netent *nptr;
  238:   u_int32_t ip;
  239: 
  240:   /*
  241:      This test needs the following line in /etc/networks:
  242:      loopback        127.0.0.0
  243:   */
  244:   nptr = getnetbyname ("loopback");
  245:   output_netent ("getnetbyname (\"loopback\")",nptr);
  246: 
  247:   nptr = getnetbyname ("LoopBACK");
  248:   output_netent ("getnetbyname (\"LoopBACK\")",nptr);
  249: 
  250:   ip = inet_network ("127.0.0.0");
  251:   nptr = getnetbyaddr (ip, AF_INET);
  252:   output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);
  253: 
  254:   setnetent (0);
  255:   do
  256:     {
  257:       nptr = getnetent ();
  258:       output_netent ("getnetent ()", nptr);
  259:     }
  260:   while (nptr != NULL);
  261:   endnetent ();
  262: }
  263: 
  264: 
  265: static void
  266: output_protoent (const char *call, struct protoent *prptr)
  267: {
  268:   char **pptr;
  269: 
  270:   if (prptr == NULL)
  271:     printf ("Call: %s returned NULL\n", call);
  272:   else
  273:     {
  274:       printf ("Call: %s, returned: p_name: %s, p_proto: %d\n",
  275:               call, prptr->p_name, prptr->p_proto);
  276:       for (pptr = prptr->p_aliases; *pptr != NULL; pptr++)
  277:         printf ("  alias: %s\n", *pptr);
  278:     }
  279: }
  280: 
  281: 
  282: static void
  283: test_protocols (void)
  284: {
  285:   struct protoent *prptr;
  286: 
  287:   prptr = getprotobyname ("IP");
  288:   output_protoent ("getprotobyname (\"IP\")", prptr);
  289: 
  290:   prptr = getprotobynumber (1);
  291:   output_protoent ("getprotobynumber (1)", prptr);
  292: 
  293:   setprotoent (0);
  294:   do
  295:     {
  296:       prptr = getprotoent ();
  297:       output_protoent ("getprotoent ()", prptr);
  298:     }
  299:   while (prptr != NULL);
  300:   endprotoent ();
  301: }
  302: 
  303: 
  304: static void
  305: output_rpcent (const char *call, struct rpcent *rptr)
  306: {
  307:   char **pptr;
  308: 
  309:   if (rptr == NULL)
  310:     printf ("Call: %s returned NULL\n", call);
  311:   else
  312:     {
  313:       printf ("Call: %s, returned: r_name: %s, r_number: %d\n",
  314:                 call, rptr->r_name, rptr->r_number);
  315:       for (pptr = rptr->r_aliases; *pptr != NULL; pptr++)
  316:         printf ("  alias: %s\n", *pptr);
  317:     }
  318: }
  319: 
  320: static void
  321: test_rpc (void)
  322: {
  323:   struct rpcent *rptr;
  324: 
  325:   rptr = getrpcbyname ("portmap");
  326:   output_rpcent ("getrpcyname (\"portmap\")", rptr);
  327: 
  328:   rptr = getrpcbynumber (100000);
  329:   output_rpcent ("getrpcbynumber (100000)", rptr);
  330: 
  331:   setrpcent (0);
  332:   do
  333:     {
  334:       rptr = getrpcent ();
  335:       output_rpcent ("getrpcent ()", rptr);
  336:     }
  337:   while (rptr != NULL);
  338:   endrpcent ();
  339: }
  340: 
  341: /* Override /etc/nsswitch.conf for this program.  This is mainly
  342:    useful for developers. */
  343: static void  __attribute__ ((unused))
  344: setdb (const char *dbname)
  345: {
  346:   if (strcmp ("db", dbname))
  347:       {
  348:         /*
  349:           db is not implemented for hosts, networks
  350:         */
  351:         __nss_configure_lookup ("hosts", dbname);
  352:         __nss_configure_lookup ("networks", dbname);
  353:       }
  354:   __nss_configure_lookup ("protocols", dbname);
  355:   __nss_configure_lookup ("rpc", dbname);
  356:   __nss_configure_lookup ("services", dbname);
  357: }
  358: 
  359: 
  360: int
  361: main (void)
  362: {
  363:   /*
  364:     setdb ("db");
  365:   */
  366: 
  367:   test_hosts ();
  368:   test_network ();
  369:   test_protocols ();
  370:   test_rpc ();
  371:   test_services ();
  372: 
  373:   if (error_count)
  374:     printf ("\n %d errors occurred!\n", error_count);
  375:   else
  376:     printf ("No visible errors occurred!\n");
  377: 
  378:   return (error_count != 0);
  379: }
Syntax (Markdown)