tLinux.c - plan9port - [fork] Plan 9 from user space | |
git clone git://src.adamsgaard.dk/plan9port | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
tLinux.c (1157B) | |
--- | |
1 #include <u.h> | |
2 #include <sys/socket.h> | |
3 #include <net/if.h> | |
4 #include <netpacket/packet.h> | |
5 #include <net/ethernet.h> | |
6 #include <netinet/in.h> | |
7 #include <sys/ioctl.h> | |
8 #include <libc.h> | |
9 #include <ip.h> | |
10 #include <bio.h> | |
11 #include <fcall.h> | |
12 #include <libsec.h> | |
13 #include "dat.h" | |
14 #include "protos.h" | |
15 #include "y.tab.h" | |
16 | |
17 int | |
18 opendevice(char *dev, int promisc) | |
19 { | |
20 int fd; | |
21 struct ifreq ifr; | |
22 struct sockaddr_ll sa; | |
23 | |
24 if((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) | |
25 return -1; | |
26 | |
27 if(dev){ | |
28 memset(&ifr, 0, sizeof ifr); | |
29 strncpy(ifr.ifr_name, dev, sizeof ifr.ifr_name); | |
30 if(ioctl(fd, SIOCGIFINDEX, &ifr) < 0){ | |
31 close(fd); | |
32 return -1; | |
33 } | |
34 memset(&sa, 0, sizeof sa); | |
35 sa.sll_family = AF_PACKET; | |
36 sa.sll_protocol = htons(ETH_P_ALL); | |
37 sa.sll_ifindex = ifr.ifr_ifindex; | |
38 if(bind(fd, (struct sockaddr*)&sa, sizeof sa) < 0){ | |
39 close(fd); | |
40 return -1; | |
41 } | |
42 } | |
43 | |
44 if(promisc){ | |
45 memset(&ifr, 0, sizeof ifr); | |
46 strncpy(ifr.ifr_name, dev, sizeof ifr.ifr_name); | |
47 if(ioctl(fd, SIOCGIFFLAGS, &ifr) < 0){ | |
48 close(fd); | |
49 return -1; | |
50 } | |
51 ifr.ifr_flags |= IFF_PROMISC; | |
52 if(ioctl(fd, SIOCSIFFLAGS, &ifr) < 0){ | |
53 close(fd); | |
54 return -1; | |
55 } | |
56 } | |
57 return fd; | |
58 } |