home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / snoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  4.5 KB  |  159 lines

  1. /*
  2.  *  $Id: snoop.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  snoop.c
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Copyright (c) 1993, 1994, 1995, 1996, 1997
  12.  *    The Regents of the University of California.  All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that: (1) source code distributions
  16.  * retain the above copyright notice and this paragraph in its entirety, (2)
  17.  * distributions including binary code include the above copyright notice and
  18.  * this paragraph in its entirety in the documentation or other materials
  19.  * provided with the distribution, and (3) all advertising materials mentioning
  20.  * features or use of this software display the following acknowledgement:
  21.  * ``This product includes software developed by the University of California,
  22.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  23.  * the University nor the names of its contributors may be used to endorse
  24.  * or promote products derived from this software without specific prior
  25.  * written permission.
  26.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  27.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  29.  */
  30.  
  31. #include <sys/param.h>
  32. #include <sys/file.h>
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../include/config.h"
  36. #endif
  37. #include "../include/libnet.h"
  38.  
  39. #include <net/raw.h>
  40. #include <net/if.h>
  41.  
  42. #include <netinet/ip_var.h>
  43. #include <netinet/if_ether.h>
  44. #include <netinet/udp_var.h>
  45. #include <netinet/tcpip.h>
  46.  
  47. #include "../include/gnuc.h"
  48. #include "../include/bpf.h"
  49. #ifdef HAVE_OS_PROTO_H
  50. #include "../include/os-proto.h"
  51. #endif
  52.  
  53.  
  54. struct link_int *
  55. open_link_interface(char *device, char *ebuf)
  56. {
  57.     int fd;
  58.     struct sockaddr_raw sr;
  59.     u_int v;
  60.     struct link_int *l;
  61.  
  62.     l = (struct link_int *)malloc(sizeof(*l));
  63.     if (l == NULL)
  64.     {
  65.         sprintf(ebuf, "malloc: %s", ll_strerror(errno));
  66.         return (NULL);
  67.     }
  68.     memset(l, 0, sizeof(*l));
  69.     l->fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_DRAIN);
  70.     if (l->fd < 0)
  71.     {
  72.         sprintf(ebuf, "drain socket: %s", ll_strerror(errno));
  73.         goto bad;
  74.     }
  75.  
  76.     memset(&sr, 0, sizeof(sr));
  77.     sr.sr_family = AF_RAW;
  78.     strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
  79.  
  80.     if (bind(l->fd, (struct sockaddr *)&sr, sizeof(sr)))
  81.     {
  82.         sprintf(ebuf, "drain bind: %s", ll_strerror(errno));
  83.         goto bad;
  84.     }
  85.  
  86.     /*
  87.      * XXX hack - map device name to link layer type
  88.      */
  89.     if (strncmp("et", device, 2) == 0      ||    /* Challenge 10 Mbit */
  90.         strncmp("ec", device, 2) == 0  ||    /* Indigo/Indy 10 Mbit, O2 10/100 */
  91.             strncmp("ef", device, 2) == 0 ||    /* O200/2000 10/100 Mbit */
  92.             strncmp("gfe", device, 3) == 0 ||   /* GIO 100 Mbit */
  93.             strncmp("fxp", device, 3) == 0 ||   /* Challenge VME Enet */
  94.             strncmp("ep", device, 2) == 0 ||    /* Challenge 8x10 Mbit EPLEX */
  95.             strncmp("vfe", device, 3) == 0 ||   /* Challenge VME 100Mbit */
  96.             strncmp("fa", device, 2) == 0 ||
  97.             strncmp("qaa", device, 3) == 0)
  98.     {
  99.         l->linktype = DLT_EN10MB;
  100.     }
  101.     else if (strncmp("ipg", device, 3) == 0 ||
  102.             strncmp("rns", device, 3) == 0 ||    /* O2/200/2000 FDDI */
  103.             strncmp("xpi", device, 3) == 0)
  104.         {
  105.             l->linktype = DLT_FDDI;
  106.     }
  107.     else if (strncmp("ppp", device, 3) == 0) {
  108.         l->linktype = DLT_RAW;
  109.     } else if (strncmp("lo", device, 2) == 0) {
  110.         l->linktype = DLT_NULL;
  111.     } else {
  112.         sprintf(ebuf, "drain: unknown physical layer type");
  113.         goto bad;
  114.     }
  115.  
  116.     return (l);
  117.  bad:
  118.     close(fd);
  119.     free(l);
  120.     return (NULL);
  121. }
  122.  
  123.  
  124. int
  125. close_link_interface(struct link_int *l)
  126. {
  127.     return (close(l->fd));
  128. }
  129.  
  130.  
  131. int
  132. write_link_layer(struct link_int *l, const u_char *device, u_char *buf, int len)
  133. {
  134.     int c;
  135.     struct ifreq ifr;
  136.     struct ether_header *eh = (struct ether_header *)buf;
  137.   
  138.     memset(&ifr, 0, sizeof(ifr));
  139.     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  140.   
  141.     if (ioctl(l->fd, SIOCGIFADDR, &ifr) == -1)
  142.     {
  143.         perror("ioctl SIOCGIFADDR");
  144.         return (-1);
  145.     }
  146.   
  147.     memcpy(eh->ether_shost, ifr.ifr_addr.sa_data, sizeof(eh->ether_shost));
  148.   
  149.     if (write(l->fd, buf, len) == -1)
  150.     {
  151. #if (__DEBUG)
  152.         fprintf(stderr, "write_link_layer: (%s)\n", strerror(errno));
  153. #endif
  154.         return (-1);
  155.     }
  156.  
  157.     return (len);
  158. }
  159.