/[dynamips]/upstream/dynamips-0.2.7-RC2/gen_eth.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.7-RC2/gen_eth.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations)
Sat Oct 6 16:24:54 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 2754 byte(s)
dynamips-0.2.7-RC2

1 /*
2 * Copyright (c) 2006 Christophe Fillot.
3 * E-mail: cf@utc.fr
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 <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <pthread.h>
28
29 #ifdef CYGWIN
30 /* Needed for pcap_open() flags */
31 #define HAVE_REMOTE
32 #endif
33
34 #include "pcap.h"
35 #include "utils.h"
36 #include "gen_eth.h"
37
38 /* Initialize a generic ethernet driver */
39 pcap_t *gen_eth_init(char *device)
40 {
41 char pcap_errbuf[PCAP_ERRBUF_SIZE];
42 pcap_t *p;
43
44 #ifndef CYGWIN
45 if (!(p = pcap_open_live(device,2048,TRUE,10,pcap_errbuf)))
46 goto pcap_error;
47
48 /* Accept only incoming packets */
49 pcap_setdirection(p,PCAP_D_IN);
50 #else
51 p = pcap_open(device,2048,
52 PCAP_OPENFLAG_PROMISCUOUS |
53 PCAP_OPENFLAG_NOCAPTURE_LOCAL |
54 PCAP_OPENFLAG_MAX_RESPONSIVENESS |
55 PCAP_OPENFLAG_NOCAPTURE_RPCAP,
56 10,NULL,pcap_errbuf);
57
58 if (!p)
59 goto pcap_error;
60 #endif
61
62 return p;
63
64 pcap_error:
65 fprintf(stderr,"gen_eth_init: unable to open device '%s' "
66 "with PCAP (%s)\n",device,pcap_errbuf);
67 return NULL;
68 }
69
70 /* Free resources of a generic ethernet driver */
71 void gen_eth_close(pcap_t *p)
72 {
73 pcap_close(p);
74 }
75
76 /* Send an ethernet frame */
77 ssize_t gen_eth_send(pcap_t *p,char *buffer,size_t len)
78 {
79 return(pcap_sendpacket(p,(u_char *)buffer,len));
80 }
81
82 /* Receive an ethernet frame */
83 ssize_t gen_eth_recv(pcap_t *p,char *buffer,size_t len)
84 {
85 struct pcap_pkthdr pkt_info;
86 u_char *pkt_ptr;
87 ssize_t rlen;
88
89 if (!(pkt_ptr = (u_char *)pcap_next(p,&pkt_info)))
90 return(-1);
91
92 rlen = m_min(len,pkt_info.caplen);
93
94 memcpy(buffer,pkt_ptr,rlen);
95 return(rlen);
96 }
97
98 /* Display Ethernet interfaces of the system */
99 int gen_eth_show_dev_list(void)
100 {
101 char pcap_errbuf[PCAP_ERRBUF_SIZE];
102 pcap_if_t *dev_list,*dev;
103 int res;
104
105 printf("Network device list:\n\n");
106
107 #ifndef CYGWIN
108 res = pcap_findalldevs(&dev_list,pcap_errbuf);
109 #else
110 res = pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&dev_list,pcap_errbuf);
111 #endif
112
113 if (res < 0) {
114 fprintf(stderr,"PCAP: unable to find device list (%s)\n",pcap_errbuf);
115 return(-1);
116 }
117
118 for(dev=dev_list;dev;dev=dev->next) {
119 printf(" %s : %s\n",
120 dev->name,
121 dev->description ? dev->description : "no info provided");
122 }
123
124 printf("\n");
125
126 pcap_freealldevs(dev_list);
127 return(0);
128 }

  ViewVC Help
Powered by ViewVC 1.1.26