Debian Codegraph @

common/gen_eth.c

raw
1 /*
2 * Copyright (c) 2006 Christophe Fillot.
3 * E-mail: [email protected]
4 *
5 * gen_eth.c: module used to send/receive Ethernet packets.
6 *
7 * Use libpcap (0.9+) or WinPcap (0.4alpha1+) to receive and send packets.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <netdb.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <pthread.h>
29
30 #ifdef CYGWIN
31 /* Needed for pcap_open() flags */
32 #define HAVE_REMOTE
33 #endif
34
35 #include "pcap.h"
36 #include "utils.h"
37 #include "gen_eth.h"
38
39 /* Initialize a generic ethernet driver */
40 pcap_t *gen_eth_init(char *device)
41 {
42 char pcap_errbuf[PCAP_ERRBUF_SIZE];
43 pcap_t *p;
44
45 #ifndef CYGWIN
46 if (!(p = pcap_open_live(device,65535,TRUE,10,pcap_errbuf)))
47 goto pcap_error;
48
49
50 #ifdef __APPLE__
51 pcap_setdirection(p,PCAP_D_IN);
52 #else
53 pcap_setdirection(p,PCAP_D_INOUT);
54 #endif /* __APPLE__ */
55 #ifdef BIOCFEEDBACK
56 {
57 int on = 1;
58 ioctl(pcap_fileno(p), BIOCFEEDBACK, &on);
59 }
60 #endif
61 #else
62 p = pcap_open(device,65535,
63 PCAP_OPENFLAG_PROMISCUOUS |
64 PCAP_OPENFLAG_NOCAPTURE_LOCAL |
65 PCAP_OPENFLAG_MAX_RESPONSIVENESS |
66 PCAP_OPENFLAG_NOCAPTURE_RPCAP,
67 10,NULL,pcap_errbuf);
68
69 if (!p)
70 goto pcap_error;
71 #endif
72
73 return p;
74
75 pcap_error:
76 fprintf(stderr,"gen_eth_init: unable to open device '%s' "
77 "with PCAP (%s)\n",device,pcap_errbuf);
78 return NULL;
79 }
80
81 /* Free resources of a generic ethernet driver */
82 void gen_eth_close(pcap_t *p)
83 {
84 pcap_close(p);
85 }
86
87 /* Send an ethernet frame */
88 ssize_t gen_eth_send(pcap_t *p,char *buffer,size_t len)
89 {
90 return(pcap_sendpacket(p,(u_char *)buffer,len));
91 }
92
93 /* Receive an ethernet frame */
94 ssize_t gen_eth_recv(pcap_t *p,char *buffer,size_t len)
95 {
96 struct pcap_pkthdr pkt_info;
97 u_char *pkt_ptr;
98 ssize_t rlen;
99
100 if (!(pkt_ptr = (u_char *)pcap_next(p,&pkt_info)))
101 return(-1);
102
103 rlen = m_min(len,pkt_info.caplen);
104
105 memcpy(buffer,pkt_ptr,rlen);
106 return(rlen);
107 }
108
109 /* Display Ethernet interfaces of the system */
110 int gen_eth_show_dev_list(void)
111 {
112 char pcap_errbuf[PCAP_ERRBUF_SIZE];
113 pcap_if_t *dev_list,*dev;
114 int res;
115
116 printf("Network device list:\n\n");
117
118 #ifndef CYGWIN
119 res = pcap_findalldevs(&dev_list,pcap_errbuf);
120 #else
121 res = pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&dev_list,pcap_errbuf);
122 #endif
123
124 if (res < 0) {
125 fprintf(stderr,"PCAP: unable to find device list (%s)\n",pcap_errbuf);
126 return(-1);
127 }
128
129 for(dev=dev_list;dev;dev=dev->next) {
130 printf(" %s : %s\n",
131 dev->name,
132 dev->description ? dev->description : "no info provided");
133 }
134
135 printf("\n");
136
137 pcap_freealldevs(dev_list);
138 return(0);
139 }