OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / src / libnet_link_snoop.c
1 /*
2  *  $Id: libnet_link_snoop.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  libnet_snoop.c - snoop routines
6  *
7  *  Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
8  *  All rights reserved.
9  *
10  * Copyright (c) 1993, 1994, 1995, 1996, 1997
11  *      The Regents of the University of California.  All rights reserved.
12  *
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
24  * written permission.
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.
28  */
29
30 #include <sys/param.h>
31 #include <sys/file.h>
32
33 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #include "../include/libnet.h"
37
38 #include <net/raw.h>
39 #include <net/if.h>
40
41 #include <netinet/ip_var.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/udp_var.h>
44 #include <netinet/tcpip.h>
45
46 #include "../include/gnuc.h"
47 #include "../include/bpf.h"
48 #ifdef HAVE_OS_PROTO_H
49 #include "../include/os-proto.h"
50 #endif
51
52
53 struct libnet_link_int *
54 libnet_open_link_interface(char *device, char *ebuf)
55 {
56     int fd;
57     struct sockaddr_raw sr;
58     u_int v;
59     struct libnet_link_int *l;
60
61     l = (struct libnet_link_int *)malloc(sizeof(*l));
62     if (l == NULL)
63     {
64         sprintf(ebuf, "malloc: %s", ll_strerror(errno));
65         return (NULL);
66     }
67     memset(l, 0, sizeof(*l));
68     l->fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_DRAIN);
69     if (l->fd < 0)
70     {
71         sprintf(ebuf, "drain socket: %s", ll_strerror(errno));
72         goto bad;
73     }
74
75     memset(&sr, 0, sizeof(sr));
76     sr.sr_family = AF_RAW;
77     strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
78
79     if (bind(l->fd, (struct sockaddr *)&sr, sizeof(sr)))
80     {
81         sprintf(ebuf, "drain bind: %s", ll_strerror(errno));
82         goto bad;
83     }
84
85     /*
86      * XXX hack - map device name to link layer type
87      */
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)
97     {
98         l->linktype = DLT_EN10MB;
99     }
100     else if (strncmp("ipg", device, 3) == 0 ||
101             strncmp("rns", device, 3) == 0 ||   /* O2/200/2000 FDDI */
102             strncmp("xpi", device, 3) == 0)
103         {
104             l->linktype = DLT_FDDI;
105         }
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;
110         } else {
111                 sprintf(ebuf, "drain: unknown physical layer type");
112                 goto bad;
113         }
114
115         return (l);
116  bad:
117         close(fd);
118         free(l);
119         return (NULL);
120 }
121
122
123 int
124 libnet_close_link_interface(struct libnet_link_int *l)
125 {
126     if (close(l->fd) == 0)
127     {
128         return (1);
129     }
130     else
131     {
132         return (-1);
133     }
134 }
135
136
137 int
138 libnet_write_link_layer(struct libnet_link_int *l, const char *device,
139             u_char *buf, int len)
140 {
141     int c;
142     struct ifreq ifr;
143     struct ether_header *eh = (struct ether_header *)buf;
144   
145     memset(&ifr, 0, sizeof(ifr));
146     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
147   
148     if (ioctl(l->fd, SIOCGIFADDR, &ifr) == -1)
149     {
150         perror("ioctl SIOCGIFADDR");
151         return (-1);
152     }
153   
154     memcpy(eh->ether_shost, ifr.ifr_addr.sa_data, sizeof(eh->ether_shost));
155   
156     if (write(l->fd, buf, len) == -1)
157     {
158 #if (__DEBUG)
159         libnet_error(LIBNET_ERR_WARNING,
160                 "libnet_write_link_layer: (%s)\n", strerror(errno));
161 #endif
162         return (-1);
163     }
164
165     return (len);
166 }