home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / nametoaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-04  |  4.9 KB  |  259 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Name to id translation routines used by the scanner.
  22.  * These functions are not time critical.
  23.  */
  24.  
  25. #ifndef lint
  26. static char rcsid[] =
  27.     "@(#) $Header: nametoaddr.c,v 1.9 91/02/04 16:56:46 mccanne Exp $ (LBL)";
  28. #endif
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <net/if.h>
  35. #include <netdb.h>
  36. #include <netinet/in.h>
  37. #include <netinet/if_ether.h>
  38. #include <errno.h>
  39. #include <arpa/inet.h>
  40.  
  41. #ifdef ultrix
  42. #include <sys/time.h>
  43. #include <rpc/types.h>
  44. #include <nfs/nfs.h>
  45. #endif
  46.  
  47. #include "interface.h"
  48. #include "etherent.h"
  49. #include "nametoaddr.h"
  50.  
  51. /*
  52.  *  Convert host name to internet address.
  53.  *  Return 0 upon failure.
  54.  */
  55. u_long **
  56. s_nametoaddr(name)
  57.     char *name;
  58. {
  59. #ifndef h_addr
  60.     static u_long *hlist[2];
  61. #endif
  62.     u_long **p;
  63.     struct hostent *hp;
  64.  
  65.     if (hp = gethostbyname(name)) {
  66. #ifndef h_addr
  67.         hlist[0] = (u_long *)hp->h_addr;
  68.         NTOHL(hp->h_addr);
  69.         return hlist;
  70. #else
  71.         for (p = (u_long **)hp->h_addr_list; *p; ++p)
  72.             NTOHL(**p);
  73.         return (u_long **)hp->h_addr_list;
  74. #endif
  75.     }
  76.     else
  77.         return 0;
  78. }
  79.  
  80. /*
  81.  *  Convert net name to internet address.
  82.  *  Return 0 upon failure.
  83.  */
  84. u_long
  85. s_nametonetaddr(name)
  86.     char *name;
  87. {
  88.     struct netent *np;
  89.  
  90.     if (np = getnetbyname(name))
  91.         return np->n_net;
  92.     else
  93.         return 0;
  94. }
  95.  
  96. /*
  97.  * Convert a port name to its port and protocol numbers.
  98.  * We assume only TCP or UDP.
  99.  * Return 0 upon failure.
  100.  */
  101. s_nametoport(name, port, proto)
  102.     char *name;
  103.     int *port;
  104.     int *proto;
  105. {
  106.     struct servent *sp;
  107.     char *other;
  108.  
  109.     sp = getservbyname(name, (char *)0);
  110.     if (sp != 0) {
  111.         NTOHS(sp->s_port);
  112.         *port = sp->s_port;
  113.         *proto = s_nametoproto(sp->s_proto);
  114.         /*
  115.          * We need to check /etc/services for ambiguous entries. 
  116.          * If we find the ambiguous entry, and it has the
  117.          * same port number, change the proto to PROTO_UNDEF
  118.          * so both TCP and UDP will be checked.
  119.          */
  120.         if (*proto == IPPROTO_TCP)
  121.             other = "udp";
  122.         else
  123.             other = "tcp";
  124.  
  125.         sp = getservbyname(name, other);
  126.         if (sp != 0) {
  127.             NTOHS(sp->s_port);
  128.             if (*port != sp->s_port)
  129.                 /* Can't handle ambigous names that refer
  130.                    to different port numbers. */
  131.                 warning("ambiguous port %s in /etc/services",
  132.                     name);
  133.             *proto = PROTO_UNDEF;
  134.         }
  135.         return 1;
  136.     }
  137. #ifdef ultrix
  138.     /* Special hack in case NFS isn't in /etc/services */
  139.     if (strcmp(name, "nfs") == 0) {
  140.         *port = NFS_PORT;
  141.         *proto = PROTO_UNDEF;
  142.         return 1;
  143.     }
  144. #endif
  145.     return 0;
  146. }
  147.  
  148. int
  149. s_nametoproto(str)
  150.     char *str;
  151. {
  152.     struct protoent *p;
  153.  
  154.     p = getprotobyname(str);
  155.     if (p != 0)
  156.         return p->p_proto;
  157.     else
  158.         return PROTO_UNDEF;
  159. }
  160.  
  161. #include "etherproto.h"
  162.  
  163. int
  164. s_nametoeproto(s)
  165.     char *s;
  166. {
  167.     struct eproto *p = eproto_db;
  168.  
  169.     while (p->s != 0) {
  170.         if (strcmp(p->s, s) == 0)
  171.             return p->p;
  172.         p += 1;
  173.     }
  174.     return PROTO_UNDEF;
  175. }
  176.  
  177. /* Hex digit to integer. */
  178. static inline int
  179. xdtoi(c)
  180. {
  181.     if (isdigit(c))
  182.         return c - '0';
  183.     else if (islower(c))
  184.         return c - 'a' + 10;
  185.     else
  186.         return c - 'A' + 10;
  187. }
  188.  
  189. u_long
  190. atoin(s)
  191.     char *s;
  192. {
  193.     u_long addr = 0;
  194.     u_int n;
  195.  
  196.     while (1) {
  197.         n = 0;
  198.         while (*s && *s != '.') 
  199.             n = n * 10 + *s++ - '0';
  200.         addr <<= 8;
  201.         addr |= n & 0xff;
  202.         if (*s == '\0')
  203.             return addr;
  204.         ++s;
  205.     }
  206.     /* NOTREACHED */
  207. }
  208.     
  209.  
  210. /*
  211.  * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new 
  212.  * ethernet address.  Assumes 's' is well formed.
  213.  */
  214. u_char *
  215. ETHER_aton(s)
  216.     char *s;
  217. {
  218.     register u_char *ep, *e;
  219.     register u_int d;
  220.  
  221.     e = ep = (u_char *)malloc(6);
  222.     
  223.     while (*s) {
  224.         if (*s == ':')
  225.             s += 1;
  226.         d = xdtoi(*s++);
  227.         if (isxdigit(*s)) {
  228.             d <<= 4;
  229.             d |= xdtoi(*s++);
  230.         }
  231.         *ep++ = d;
  232.     }
  233.  
  234.     return e;
  235. }
  236.  
  237. #ifndef ETHER_SERVICE
  238. u_char *
  239. ETHER_hostton(name)
  240.     char *name;
  241. {
  242.     struct etherent *ep;
  243.     FILE *fp;
  244.     u_char *ap;
  245.  
  246.     fp = fopen(ETHERS_FILE, "r");
  247.     if (fp != 0) {
  248.         while (ep = next_etherent(fp)) {
  249.             if (strcmp(ep->name, name) == 0) {
  250.                 ap = (u_char *)malloc(6);
  251.                 bcopy(ep->addr, ap, 6);
  252.                 return ap;
  253.             }
  254.         }
  255.     }
  256.     return (u_char *)0;
  257. }
  258. #endif
  259.