2
* Copyright (C) 1998-99 Luca Deri <deri@ntop.org>
3
* Portions by Stefano Suin <stefano@ntop.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include <winsock2.h> /* winsock.h is included automatically */
25
#if defined(WIN32) && defined(__GNUC__)
26
/* on mingw, the definitions we need are in pcap.h - Scott Renfro <scott@renfro.org> */
33
#define ASYNC_ADDRESS_RESOLUTION
37
/* Courtesy of Wies-Software <wies@wiessoft.de> */
38
extern unsigned long waitForNextEvent(unsigned long ulDelay /* ms */);
39
#define sleep(a /* sec */) waitForNextEvent(1000*a /* ms */)
41
#define DATAFILE_DIR "."
42
#define PLUGIN_DIR "."
43
#define CONFIGFILE_DIR "."
44
#define DBFILE_DIR "."
47
typedef int NDIS_STATUS, *PNDIS_STATUS;
49
/* ******************* */
51
/* typedef ULONGLONG TrafficCounter; */
52
/* typedef unsigned long TrafficCounter; */
53
#if defined (WIN32) && !defined (__GNUC__)
54
typedef unsigned char u_char;
55
typedef unsigned short u_short;
56
typedef unsigned int u_int;
57
typedef unsigned long u_long;
59
typedef unsigned int tcp_seq;
61
typedef u_char uint8_t;
62
typedef u_short uint16_t;
63
typedef u_int uint32_t;
66
typedef unsigned long u_int32, u_int32_t;
70
#define strcasecmp _stricmp
72
extern int getopt(int num, char *const *argv, const char *opts);
73
#if defined(WIN32) && defined(__GNUC__)
74
/* on mingw, struct timezone isn't defined so s/struct timezone/void/ - Scott Renfro <scott@renfro.org> */
75
extern int gettimeofday(struct timeval*, void*);
77
extern int gettimeofday(struct timeval*, struct timezone*);
79
extern unsigned long waitForNextEvent(unsigned long ulDelay /* ms */);
81
extern ULONG GetHostIPAddr();
83
#define RETSIGTYPE void
88
#define DLT_NULL 0 /* no link-layer encapsulation */
89
#define DLT_EN10MB 1 /* Ethernet (10Mb) */
90
#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
91
#define DLT_AX25 3 /* Amateur Radio AX.25 */
92
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */
93
#define DLT_CHAOS 5 /* Chaos */
94
#define DLT_IEEE802 6 /* IEEE 802 Networks */
95
#define DLT_ARCNET 7 /* ARCNET */
96
#define DLT_SLIP 8 /* Serial Line IP */
97
#define DLT_PPP 9 /* Point-to-point Protocol */
98
#define DLT_FDDI 10 /* FDDI */
101
* Ethernet address - 6 octets
104
u_char ether_addr_octet[6];
108
* Structure of a 10Mb/s Ethernet header.
110
struct ether_header {
111
u_char ether_dhost[6];
112
u_char ether_shost[6];
116
#define ETHERTYPE_PUP 0x0200 /* PUP protocol */
117
#define ETHERTYPE_IP 0x0800 /* IP protocol */
118
#define ETHERTYPE_ARP 0x0806 /* Addr. resolution protocol */
119
#define ETHERTYPE_REVARP 0x8035 /* reverse Addr. resolution protocol */
122
/************************************************************************/
124
/* on mingw, tcp_seq is defined - Scott Renfro <scott@renfro.org> */
125
#if defined (WIN32) && !defined (__GNUC__)
126
typedef u_int tcp_seq;
131
* Per RFC 793, September, 1981.
134
u_short th_sport; /* source port */
135
u_short th_dport; /* destination port */
136
tcp_seq th_seq; /* sequence number */
137
tcp_seq th_ack; /* acknowledgement number */
138
#if BYTE_ORDER == LITTLE_ENDIAN
139
u_char th_x2:4, /* (unused) */
140
th_off:4; /* data offset */
142
u_char th_off:4, /* data offset */
143
th_x2:4; /* (unused) */
152
u_short th_win; /* window */
153
u_short th_sum; /* checksum */
154
u_short th_urp; /* urgent pointer */
157
/* ********************************************* */
160
#if BYTE_ORDER == LITTLE_ENDIAN
161
u_char ip_hl:4, /* header length */
162
ip_v:4; /* version */
164
u_char ip_v:4, /* version */
165
ip_hl:4; /* header length */
167
u_char ip_tos; /* type of service */
168
short ip_len; /* total length */
169
u_short ip_id; /* identification */
170
short ip_off; /* fragment offset field */
171
#define IP_DF 0x4000 /* dont fragment flag */
172
#define IP_MF 0x2000 /* more fragments flag */
173
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
174
u_char ip_ttl; /* time to live */
175
u_char ip_p; /* protocol */
176
u_short ip_sum; /* checksum */
177
struct in_addr ip_src,ip_dst; /* source and dest address */
180
/* ********************************************* */
183
* Udp protocol header.
184
* Per RFC 768, September, 1981.
187
u_short uh_sport; /* source port */
188
u_short uh_dport; /* destination port */
189
short uh_ulen; /* udp length */
190
u_short uh_sum; /* udp checksum */
193
/* ********************************************* */
195
typedef struct _FRAMEETH
197
BYTE DestAddr[6]; // Indirizzo ethernet destinazione
198
BYTE SrcAddr[6]; // Indirizzo ethernet sorgente
200
BYTE Type[2]; // Tipo di pacchetto (o lunghezza per IEEE 802.3)
201
// il valore deve essere letto con la relazione:
202
// 256 * Type[0] + Type[1]
203
// e non semplicemente con un cast esplicito ad
204
// uno short, altrimenti (su una macchina INTEL)
205
// i byte pi� e meno significativi risultano
208
BYTE Dati[1500]; // Dati contenuti nel pacchetto
209
} FRAMEETH, *PFRAMEETH;
211
/* ********************************************* */
216
u_int32_t ira_preference;
221
u_int8_t icmp_type; /* type of message, see below */
222
u_int8_t icmp_code; /* type sub code */
223
u_int16_t icmp_cksum; /* ones complement checksum of struct */
226
u_char ih_pptr; /* ICMP_PARAMPROB */
227
struct in_addr ih_gwaddr; /* gateway address */
228
struct ih_idseq /* echo datagram */
235
/* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
239
u_int16_t ipm_nextmtu;
244
u_int8_t irt_num_addrs;
246
u_int16_t irt_lifetime;
249
#define icmp_pptr icmp_hun.ih_pptr
250
#define icmp_gwaddr icmp_hun.ih_gwaddr
251
#define icmp_id icmp_hun.ih_idseq.icd_id
252
#define icmp_seq icmp_hun.ih_idseq.icd_seq
253
#define icmp_void icmp_hun.ih_void
254
#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
255
#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
256
#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
257
#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
258
#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
270
/* options and then 64 bits of data */
272
struct icmp_ra_addr id_radv;
276
#define icmp_otime icmp_dun.id_ts.its_otime
277
#define icmp_rtime icmp_dun.id_ts.its_rtime
278
#define icmp_ttime icmp_dun.id_ts.its_ttime
279
#define icmp_ip icmp_dun.id_ip.idi_ip
280
#define icmp_radv icmp_dun.id_radv
281
#define icmp_mask icmp_dun.id_mask
282
#define icmp_data icmp_dun.id_data
286
* Definition of type and code field values.
288
#define ICMP_ECHOREPLY 0 /* echo reply */
289
#define ICMP_UNREACH 3 /* dest unreachable, codes: */
290
#define ICMP_UNREACH_NET 0 /* bad net */
291
#define ICMP_UNREACH_HOST 1 /* bad host */
292
#define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
293
#define ICMP_UNREACH_PORT 3 /* bad port */
294
#define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
295
#define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
296
#define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
297
#define ICMP_REDIRECT 5 /* shorter route, codes: */
298
#define ICMP_REDIRECT_NET 0 /* for network */
299
#define ICMP_REDIRECT_HOST 1 /* for host */
300
#define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
301
#define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
302
#define ICMP_ECHO 8 /* echo service */
303
#define ICMP_TIMXCEED 11 /* time exceeded, code: */
304
#define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
305
#define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
306
#define ICMP_PARAMPROB 12 /* ip header bad */
307
#define ICMP_TSTAMP 13 /* timestamp request */
308
#define ICMP_TSTAMPREPLY 14 /* timestamp reply */
309
#define ICMP_IREQ 15 /* information request */
310
#define ICMP_IREQREPLY 16 /* information reply */
311
#define ICMP_MASKREQ 17 /* address mask request */
312
#define ICMP_MASKREPLY 18 /* address mask reply */
314
#define ICMP_MAXTYPE 18
316
/* ********************************************* */
319
* Address Resolution Protocol.
321
* See RFC 826 for protocol description. ARP packets are variable
322
* in size; the arphdr structure defines the fixed-length portion.
323
* Protocol type values are the same as those for 10 Mb/s Ethernet.
324
* It is followed by the variable-sized fields ar_sha, arp_spa,
325
* arp_tha and arp_tpa in that order, according to the lengths
326
* specified. Field names used correspond to RFC 826.
329
u_short ar_hrd; /* format of hardware address */
330
#define ARPHRD_ETHER 1 /* ethernet hardware address */
331
u_short ar_pro; /* format of protocol address */
332
u_char ar_hln; /* length of hardware address */
333
u_char ar_pln; /* length of protocol address */
334
u_short ar_op; /* one of: */
335
#define ARPOP_REQUEST 1 /* request to resolve address */
336
#define ARPOP_REPLY 2 /* response to previous request */
337
#define REVARP_REQUEST 3 /* Reverse ARP request */
338
#define REVARP_REPLY 4 /* Reverse ARP reply */
340
* The remaining fields are variable in size,
341
* according to the sizes above, and are defined
342
* as appropriate for specific hardware/protocol
343
* combinations. (E.g., see <netinet/if_ether.h>.)
346
uchar_t ar_sha[]; /* sender hardware address */
347
uchar_t ar_spa[]; /* sender protocol address */
348
uchar_t ar_tha[]; /* target hardware address */
349
uchar_t ar_tpa[]; /* target protocol address */
356
struct arphdr ea_hdr; /* fixed-size header */
357
u_int8_t arp_sha[ETH_ALEN]; /* sender hardware address */
358
u_int8_t arp_spa[4]; /* sender protocol address */
359
u_int8_t arp_tha[ETH_ALEN]; /* target hardware address */
360
u_int8_t arp_tpa[4]; /* target protocol address */
362
#define arp_hrd ea_hdr.ar_hrd
363
#define arp_pro ea_hdr.ar_pro
364
#define arp_hln ea_hdr.ar_hln
365
#define arp_pln ea_hdr.ar_pln
366
#define arp_op ea_hdr.ar_op
368
/* ********************************************* */
370
extern void initWinsock32();
371
extern void termWinsock32();
372
extern void sniffSinglePacket(void(*pbuf_process)(u_char *unused,
373
const struct pcap_pkthdr *h,
377
* on mingw, call closesocket, not close, at least on win2k (the args
378
* have different types: int vs. SOCKET
381
#if defined (WIN32) && !defined (__GNUC__)
382
#define close(fd) closesocket(fd)
385
/* ********* MULTITHREAD STUFF ********* */
387
#define pthread_t HANDLE
388
#define pthread_mutex_t HANDLE
389
#define pthread_cond_t HANDLE
392
typedef struct conditionalVariable {
394
CRITICAL_SECTION criticalSection;
395
} ConditionalVariable;
397
/* ********************************************* */
399
// Max number of packets handled by the public version
400
#define MAX_NUM_PACKETS 1000
402
extern short isWinNT();
404
/* #define WIN32_DEMO */