2 * $Id: libnet_link_snoop.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_snoop.c - snoop routines
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Copyright (c) 1993, 1994, 1995, 1996, 1997
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that: (1) source code distributions
15 * retain the above copyright notice and this paragraph in its entirety, (2)
16 * distributions including binary code include the above copyright notice and
17 * this paragraph in its entirety in the documentation or other materials
18 * provided with the distribution, and (3) all advertising materials mentioning
19 * features or use of this software display the following acknowledgement:
20 * ``This product includes software developed by the University of California,
21 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
22 * the University nor the names of its contributors may be used to endorse
23 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 #include <sys/param.h>
34 #include "../include/config.h"
36 #include "../include/libnet.h"
41 #include <netinet/ip_var.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/udp_var.h>
44 #include <netinet/tcpip.h>
46 #include "../include/gnuc.h"
47 #include "../include/bpf.h"
48 #ifdef HAVE_OS_PROTO_H
49 #include "../include/os-proto.h"
53 struct libnet_link_int *
54 libnet_open_link_interface(char *device, char *ebuf)
57 struct sockaddr_raw sr;
59 struct libnet_link_int *l;
61 l = (struct libnet_link_int *)malloc(sizeof(*l));
64 sprintf(ebuf, "malloc: %s", ll_strerror(errno));
67 memset(l, 0, sizeof(*l));
68 l->fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_DRAIN);
71 sprintf(ebuf, "drain socket: %s", ll_strerror(errno));
75 memset(&sr, 0, sizeof(sr));
76 sr.sr_family = AF_RAW;
77 strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
79 if (bind(l->fd, (struct sockaddr *)&sr, sizeof(sr)))
81 sprintf(ebuf, "drain bind: %s", ll_strerror(errno));
86 * XXX hack - map device name to link layer type
88 if (strncmp("et", device, 2) == 0 || /* Challenge 10 Mbit */
89 strncmp("ec", device, 2) == 0 || /* Indigo/Indy 10 Mbit, O2 10/100 */
90 strncmp("ef", device, 2) == 0 || /* O200/2000 10/100 Mbit */
91 strncmp("gfe", device, 3) == 0 || /* GIO 100 Mbit */
92 strncmp("fxp", device, 3) == 0 || /* Challenge VME Enet */
93 strncmp("ep", device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */
94 strncmp("vfe", device, 3) == 0 || /* Challenge VME 100Mbit */
95 strncmp("fa", device, 2) == 0 ||
96 strncmp("qaa", device, 3) == 0)
98 l->linktype = DLT_EN10MB;
100 else if (strncmp("ipg", device, 3) == 0 ||
101 strncmp("rns", device, 3) == 0 || /* O2/200/2000 FDDI */
102 strncmp("xpi", device, 3) == 0)
104 l->linktype = DLT_FDDI;
106 else if (strncmp("ppp", device, 3) == 0) {
107 l->linktype = DLT_RAW;
108 } else if (strncmp("lo", device, 2) == 0) {
109 l->linktype = DLT_NULL;
111 sprintf(ebuf, "drain: unknown physical layer type");
124 libnet_close_link_interface(struct libnet_link_int *l)
126 if (close(l->fd) == 0)
138 libnet_write_link_layer(struct libnet_link_int *l, const char *device,
139 u_char *buf, int len)
143 struct ether_header *eh = (struct ether_header *)buf;
145 memset(&ifr, 0, sizeof(ifr));
146 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
148 if (ioctl(l->fd, SIOCGIFADDR, &ifr) == -1)
150 perror("ioctl SIOCGIFADDR");
154 memcpy(eh->ether_shost, ifr.ifr_addr.sa_data, sizeof(eh->ether_shost));
156 if (write(l->fd, buf, len) == -1)
159 libnet_error(LIBNET_ERR_WARNING,
160 "libnet_write_link_layer: (%s)\n", strerror(errno));