home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / libpcap-0.3 / nametoaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  7.4 KB  |  372 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  3.  *    The Regents of the University of California.  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 const char rcsid[] =
  27.     "@(#) $Header: nametoaddr.c,v 1.45 96/10/17 23:26:53 leres Exp $ (LBL)";
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/types.h>                /* concession to AIX */
  32. #include <sys/socket.h>
  33.  
  34. #if __STDC__
  35. struct mbuf;
  36. struct rtentry;
  37. #endif
  38.  
  39. #include <net/if.h>
  40. #include <netinet/in.h>
  41. #include <netinet/if_ether.h>
  42. #include <arpa/inet.h>
  43.  
  44. #include <ctype.h>
  45. #include <errno.h>
  46. #include <stdlib.h>
  47. #include <memory.h>
  48. #include <netdb.h>
  49. #include <stdio.h>
  50.  
  51. #include "pcap-int.h"
  52.  
  53. #include "gencode.h"
  54. #include <pcap-namedb.h>
  55.  
  56. #include "gnuc.h"
  57. #ifdef HAVE_OS_PROTO_H
  58. #include "os-proto.h"
  59. #endif
  60.  
  61. #ifndef NTOHL
  62. #define NTOHL(x) (x) = ntohl(x)
  63. #define NTOHS(x) (x) = ntohs(x)
  64. #endif
  65.  
  66. static inline int xdtoi(int);
  67.  
  68. /*
  69.  *  Convert host name to internet address.
  70.  *  Return 0 upon failure.
  71.  */
  72. bpf_u_int32 **
  73. pcap_nametoaddr(const char *name)
  74. {
  75. #ifndef h_addr
  76.     static bpf_u_int32 *hlist[2];
  77. #endif
  78.     bpf_u_int32 **p;
  79.     struct hostent *hp;
  80.  
  81.     if ((hp = gethostbyname(name)) != NULL) {
  82. #ifndef h_addr
  83.         hlist[0] = (bpf_u_int32 *)hp->h_addr;
  84.         NTOHL(hp->h_addr);
  85.         return hlist;
  86. #else
  87.         for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
  88.             NTOHL(**p);
  89.         return (bpf_u_int32 **)hp->h_addr_list;
  90. #endif
  91.     }
  92.     else
  93.         return 0;
  94. }
  95.  
  96. /*
  97.  *  Convert net name to internet address.
  98.  *  Return 0 upon failure.
  99.  */
  100. bpf_u_int32
  101. pcap_nametonetaddr(const char *name)
  102. {
  103.     struct netent *np;
  104.  
  105.     if ((np = getnetbyname(name)) != NULL)
  106.         return np->n_net;
  107.     else
  108.         return 0;
  109. }
  110.  
  111. /*
  112.  * Convert a port name to its port and protocol numbers.
  113.  * We assume only TCP or UDP.
  114.  * Return 0 upon failure.
  115.  */
  116. int
  117. pcap_nametoport(const char *name, int *port, int *proto)
  118. {
  119.     struct servent *sp;
  120.     char *other;
  121.  
  122.     sp = getservbyname(name, (char *)0);
  123.     if (sp != NULL) {
  124.         NTOHS(sp->s_port);
  125.         *port = sp->s_port;
  126.         *proto = pcap_nametoproto(sp->s_proto);
  127.         /*
  128.          * We need to check /etc/services for ambiguous entries.
  129.          * If we find the ambiguous entry, and it has the
  130.          * same port number, change the proto to PROTO_UNDEF
  131.          * so both TCP and UDP will be checked.
  132.          */
  133.         if (*proto == IPPROTO_TCP)
  134.             other = "udp";
  135.         else
  136.             other = "tcp";
  137.  
  138.         sp = getservbyname(name, other);
  139.         if (sp != 0) {
  140.             NTOHS(sp->s_port);
  141. #ifdef notdef
  142.             if (*port != sp->s_port)
  143.                 /* Can't handle ambiguous names that refer
  144.                    to different port numbers. */
  145.                 warning("ambiguous port %s in /etc/services",
  146.                     name);
  147. #endif
  148.             *proto = PROTO_UNDEF;
  149.         }
  150.         return 1;
  151.     }
  152. #if defined(ultrix) || defined(__osf__)
  153.     /* Special hack in case NFS isn't in /etc/services */
  154.     if (strcmp(name, "nfs") == 0) {
  155.         *port = 2049;
  156.         *proto = PROTO_UNDEF;
  157.         return 1;
  158.     }
  159. #endif
  160.     return 0;
  161. }
  162.  
  163. int
  164. pcap_nametoproto(const char *str)
  165. {
  166.     struct protoent *p;
  167.  
  168.     p = getprotobyname(str);
  169.     if (p != 0)
  170.         return p->p_proto;
  171.     else
  172.         return PROTO_UNDEF;
  173. }
  174.  
  175. #include "ethertype.h"
  176.  
  177. struct eproto {
  178.     char *s;
  179.     u_short p;
  180. };
  181.  
  182. /* Static data base of ether protocol types. */
  183. struct eproto eproto_db[] = {
  184.     { "pup", ETHERTYPE_PUP },
  185.     { "xns", ETHERTYPE_NS },
  186.     { "ip", ETHERTYPE_IP },
  187.     { "arp", ETHERTYPE_ARP },
  188.     { "rarp", ETHERTYPE_REVARP },
  189.     { "sprite", ETHERTYPE_SPRITE },
  190.     { "mopdl", ETHERTYPE_MOPDL },
  191.     { "moprc", ETHERTYPE_MOPRC },
  192.     { "decnet", ETHERTYPE_DN },
  193.     { "lat", ETHERTYPE_LAT },
  194.     { "sca", ETHERTYPE_SCA },
  195.     { "lanbridge", ETHERTYPE_LANBRIDGE },
  196.     { "vexp", ETHERTYPE_VEXP },
  197.     { "vprod", ETHERTYPE_VPROD },
  198.     { "atalk", ETHERTYPE_ATALK },
  199.     { "atalkarp", ETHERTYPE_AARP },
  200.     { "loopback", ETHERTYPE_LOOPBACK },
  201.     { "decdts", ETHERTYPE_DECDTS },
  202.     { "decdns", ETHERTYPE_DECDNS },
  203.     { (char *)0, 0 }
  204. };
  205.  
  206. int
  207. pcap_nametoeproto(const char *s)
  208. {
  209.     struct eproto *p = eproto_db;
  210.  
  211.     while (p->s != 0) {
  212.         if (strcmp(p->s, s) == 0)
  213.             return p->p;
  214.         p += 1;
  215.     }
  216.     return PROTO_UNDEF;
  217. }
  218.  
  219. /* Hex digit to integer. */
  220. static inline int
  221. xdtoi(c)
  222.     register int c;
  223. {
  224.     if (isdigit(c))
  225.         return c - '0';
  226.     else if (islower(c))
  227.         return c - 'a' + 10;
  228.     else
  229.         return c - 'A' + 10;
  230. }
  231.  
  232. int
  233. __pcap_atoin(const char *s, bpf_u_int32 *addr)
  234. {
  235.     u_int n;
  236.     int len;
  237.  
  238.     *addr = 0;
  239.     len = 0;
  240.     while (1) {
  241.         n = 0;
  242.         while (*s && *s != '.')
  243.             n = n * 10 + *s++ - '0';
  244.         *addr <<= 8;
  245.         *addr |= n & 0xff;
  246.         len += 8;
  247.         if (*s == '\0')
  248.             return len;
  249.         ++s;
  250.     }
  251.     /* NOTREACHED */
  252. }
  253.  
  254. int
  255. __pcap_atodn(const char *s, bpf_u_int32 *addr)
  256. {
  257. #define AREASHIFT 10
  258. #define AREAMASK 0176000
  259. #define NODEMASK 01777
  260.  
  261.     u_int node, area;
  262.  
  263.     if (sscanf((char *)s, "%d.%d", &area, &node) != 2)
  264.         bpf_error("malformed decnet address '%s'", s);
  265.  
  266.     *addr = (area << AREASHIFT) & AREAMASK;
  267.     *addr |= (node & NODEMASK);
  268.  
  269.     return(32);
  270. }
  271.  
  272. /*
  273.  * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
  274.  * ethernet address.  Assumes 's' is well formed.
  275.  */
  276. u_char *
  277. pcap_ether_aton(const char *s)
  278. {
  279.     register u_char *ep, *e;
  280.     register u_int d;
  281.  
  282.     e = ep = (u_char *)malloc(6);
  283.  
  284.     while (*s) {
  285.         if (*s == ':')
  286.             s += 1;
  287.         d = xdtoi(*s++);
  288.         if (isxdigit(*s)) {
  289.             d <<= 4;
  290.             d |= xdtoi(*s++);
  291.         }
  292.         *ep++ = d;
  293.     }
  294.  
  295.     return (e);
  296. }
  297.  
  298. #ifndef HAVE_ETHER_HOSTTON
  299. /* Roll our own */
  300. u_char *
  301. pcap_ether_hostton(const char *name)
  302. {
  303.     register struct pcap_etherent *ep;
  304.     register u_char *ap;
  305.     static FILE *fp = NULL;
  306.     static init = 0;
  307.  
  308.     if (!init) {
  309.         fp = fopen(PCAP_ETHERS_FILE, "r");
  310.         ++init;
  311.         if (fp == NULL)
  312.             return (NULL);
  313.     } else if (fp == NULL)
  314.         return (NULL);
  315.     else
  316.         rewind(fp);
  317.     
  318.     while ((ep = pcap_next_etherent(fp)) != NULL) {
  319.         if (strcmp(ep->name, name) == 0) {
  320.             ap = (u_char *)malloc(6);
  321.             if (ap != NULL) {
  322.                 memcpy(ap, ep->addr, 6);
  323.                 return (ap);
  324.             }
  325.             break;
  326.         }
  327.     }
  328.     return (NULL);
  329. }
  330. #else
  331.  
  332. #ifndef sgi
  333. extern int ether_hostton(char *, struct ether_addr *);
  334. #endif
  335.  
  336. /* Use the os supplied routines */
  337. u_char *
  338. pcap_ether_hostton(const char *name)
  339. {
  340.     register u_char *ap;
  341.     u_char a[6];
  342.  
  343.     ap = NULL;
  344.     if (ether_hostton((char *)name, (struct ether_addr *)a) == 0) {
  345.         ap = (u_char *)malloc(6);
  346.         if (ap != NULL)
  347.             memcpy(ap, a, 6);
  348.     }
  349.     return (ap);
  350. }
  351. #endif
  352.  
  353. u_short
  354. __pcap_nametodnaddr(const char *name)
  355. {
  356. #ifdef    DECNETLIB
  357.     struct nodeent *getnodebyname();
  358.     struct nodeent *nep;
  359.     unsigned short res;
  360.  
  361.     nep = getnodebyname(name);
  362.     if (nep == ((struct nodeent *)0))
  363.         bpf_error("unknown decnet host name '%s'\n", name);
  364.  
  365.     memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
  366.     return(res);
  367. #else
  368.     bpf_error("decnet name support not included, '%s' cannot be translated\n",
  369.         name);
  370. #endif
  371. }
  372.