libnet  latest
A Portable Framework for Low-Level Network Packet Construction
Packet32.h
1 /*
2  * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3  * Copyright (c) 2005 - 2007 CACE Technologies, Davis (California)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16  * nor the names of its contributors may be used to endorse or promote
17  * products derived from this software without specific prior written
18  * permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
45 #ifndef __PACKET32
46 #define __PACKET32
47 
48 #include <winsock2.h>
49 
50 #ifdef HAVE_AIRPCAP_API
51 #include <airpcap.h>
52 #else
53 #if !defined(AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_)
54 #define AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_
55 typedef struct _AirpcapHandle *PAirpcapHandle;
56 #endif /* AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_ */
57 #endif /* HAVE_AIRPCAP_API */
58 
59 #ifdef HAVE_DAG_API
60 #include <dagc.h>
61 #endif /* HAVE_DAG_API */
62 
63 // Working modes
64 #define PACKET_MODE_CAPT 0x0
65 #define PACKET_MODE_STAT 0x1
66 #define PACKET_MODE_MON 0x2
67 #define PACKET_MODE_DUMP 0x10
68 #define PACKET_MODE_STAT_DUMP MODE_DUMP | MODE_STAT
69 
70 
72 #define Packet_ALIGNMENT sizeof(int)
73 #define Packet_WORDALIGN(x) (((x)+(Packet_ALIGNMENT-1))&~(Packet_ALIGNMENT-1))
75 
76 #define NdisMediumNull -1
77 #define NdisMediumCHDLC -2
78 #define NdisMediumPPPSerial -3
79 #define NdisMediumBare80211 -4
80 #define NdisMediumRadio80211 -5
81 #define NdisMediumPpi -6
82 
83 // Loopback behaviour definitions
84 #define NPF_DISABLE_LOOPBACK 1
85 #define NPF_ENABLE_LOOPBACK 2
86 
87 
92 typedef struct NetType
93 {
94  UINT LinkType;
95  ULONGLONG LinkSpeed;
96 }NetType;
97 
98 
99 //some definitions stolen from libpcap
100 
101 #ifndef BPF_MAJOR_VERSION
102 
108 struct bpf_program
109 {
110  UINT bf_len;
111  struct bpf_insn *bf_insns;
112 };
113 
119 struct bpf_insn
120 {
121  USHORT code;
122  UCHAR jt;
123  UCHAR jf;
124  int k;
125 };
126 
132 struct bpf_stat
133 {
134  UINT bs_recv;
135  UINT bs_drop;
138  UINT ps_ifdrop;
141  UINT bs_capt;
142 };
144 
150 struct bpf_hdr
151 {
152  struct timeval bh_tstamp;
153  UINT bh_caplen;
155  UINT bh_datalen;
158  USHORT bh_hdrlen;
159 };
163 
172  struct timeval ts;
173  UINT caplen;
174  UINT len;
177 };
178 
179 
180 #endif
181 
182 struct bpf_stat;
183 
184 #define DOSNAMEPREFIX TEXT("Packet_")
185 #define MAX_LINK_NAME_LENGTH 64 //< Maximum length of the devices symbolic links
186 #define NMAX_PACKET 65535
187 
194 typedef struct npf_if_addr {
195  struct sockaddr_storage IPAddress;
196  struct sockaddr_storage SubnetMask;
197  struct sockaddr_storage Broadcast;
198 }npf_if_addr;
199 
200 
201 #define ADAPTER_NAME_LENGTH 256 + 12
202 #define ADAPTER_DESC_LENGTH 128
203 #define MAX_MAC_ADDR_LENGTH 8
204 #define MAX_NETWORK_ADDRESSES 16
205 
206 
207 typedef struct WAN_ADAPTER_INT WAN_ADAPTER;
209 
210 #define INFO_FLAG_NDIS_ADAPTER 0
211 #define INFO_FLAG_NDISWAN_ADAPTER 1
212 #define INFO_FLAG_DAG_CARD 2
213 #define INFO_FLAG_DAG_FILE 6
214 #define INFO_FLAG_DONT_EXPORT 8
215 #define INFO_FLAG_AIRPCAP_CARD 16
216 #define INFO_FLAG_NPFIM_DEVICE 32
217 
224 typedef struct _ADAPTER {
225  HANDLE hFile;
226  CHAR SymbolicLink[MAX_LINK_NAME_LENGTH];
227  int NumWrites;
228  HANDLE ReadEvent;
230 
237  UINT ReadTimeOut;
238  CHAR Name[ADAPTER_NAME_LENGTH];
240  PWAN_ADAPTER pWanAdapter;
241  UINT Flags;
242 
243 #ifdef HAVE_AIRPCAP_API
244  PAirpcapHandle AirpcapAd;
245 #endif // HAVE_AIRPCAP_API
246 
247 #ifdef HAVE_NPFIM_API
248  void* NpfImHandle;
249 #endif // HAVE_NPFIM_API
250 
251 #ifdef HAVE_DAG_API
252  dagc_t *pDagCard;
253  PCHAR DagBuffer;
254  struct timeval DagReadTimeout;
255  unsigned DagFcsLen;
256  DWORD DagFastProcess;
257 #endif // HAVE_DAG_API
258 } ADAPTER, *LPADAPTER;
259 
265 typedef struct _PACKET {
266  HANDLE hEvent;
267  OVERLAPPED OverLapped;
268  PVOID Buffer;
269  UINT Length;
272  BOOLEAN bIoComplete;
274 } PACKET, *LPPACKET;
275 
284  ULONG Oid;
285  ULONG Length;
287  UCHAR Data[1];
288 };
291 
292 #ifdef __cplusplus
293 extern "C" {
294 #endif
295 
300 /*
301 BOOLEAN QueryWinPcapRegistryStringA(CHAR *SubKeyName,
302  CHAR *Value,
303  UINT *pValueLen,
304  CHAR *DefaultVal);
305 
306 BOOLEAN QueryWinPcapRegistryStringW(WCHAR *SubKeyName,
307  WCHAR *Value,
308  UINT *pValueLen,
309  WCHAR *DefaultVal);
310 */
311 
312 //---------------------------------------------------------------------------
313 // EXPORTED FUNCTIONS
314 //---------------------------------------------------------------------------
315 
316 PCHAR PacketGetVersion();
317 PCHAR PacketGetDriverVersion();
318 BOOLEAN PacketSetMinToCopy(LPADAPTER AdapterObject,int nbytes);
319 BOOLEAN PacketSetNumWrites(LPADAPTER AdapterObject,int nwrites);
320 BOOLEAN PacketSetMode(LPADAPTER AdapterObject,int mode);
321 BOOLEAN PacketSetReadTimeout(LPADAPTER AdapterObject,int timeout);
322 BOOLEAN PacketSetBpf(LPADAPTER AdapterObject,struct bpf_program *fp);
323 BOOLEAN PacketSetLoopbackBehavior(LPADAPTER AdapterObject, UINT LoopbackBehavior);
324 INT PacketSetSnapLen(LPADAPTER AdapterObject,int snaplen);
325 BOOLEAN PacketGetStats(LPADAPTER AdapterObject,struct bpf_stat *s);
326 BOOLEAN PacketGetStatsEx(LPADAPTER AdapterObject,struct bpf_stat *s);
327 BOOLEAN PacketSetBuff(LPADAPTER AdapterObject,int dim);
328 BOOLEAN PacketGetNetType (LPADAPTER AdapterObject,NetType *type);
329 LPADAPTER PacketOpenAdapter(PCHAR AdapterName);
330 BOOLEAN PacketSendPacket(LPADAPTER AdapterObject,LPPACKET pPacket,BOOLEAN Sync);
331 INT PacketSendPackets(LPADAPTER AdapterObject,PVOID PacketBuff,ULONG Size, BOOLEAN Sync);
332 LPPACKET PacketAllocatePacket(void);
333 VOID PacketInitPacket(LPPACKET lpPacket,PVOID Buffer,UINT Length);
334 VOID PacketFreePacket(LPPACKET lpPacket);
335 BOOLEAN PacketReceivePacket(LPADAPTER AdapterObject,LPPACKET lpPacket,BOOLEAN Sync);
336 BOOLEAN PacketSetHwFilter(LPADAPTER AdapterObject,ULONG Filter);
337 BOOLEAN PacketGetAdapterNames(PTSTR pStr,PULONG BufferSize);
338 BOOLEAN PacketGetNetInfoEx(PCHAR AdapterName, npf_if_addr* buffer, PLONG NEntries);
339 BOOLEAN PacketRequest(LPADAPTER AdapterObject,BOOLEAN Set,PPACKET_OID_DATA OidData);
340 HANDLE PacketGetReadEvent(LPADAPTER AdapterObject);
341 BOOLEAN PacketSetDumpName(LPADAPTER AdapterObject, void *name, int len);
342 BOOLEAN PacketSetDumpLimits(LPADAPTER AdapterObject, UINT maxfilesize, UINT maxnpacks);
343 BOOLEAN PacketIsDumpEnded(LPADAPTER AdapterObject, BOOLEAN sync);
344 BOOL PacketStopDriver();
345 VOID PacketCloseAdapter(LPADAPTER lpAdapter);
346 BOOLEAN PacketStartOem(PCHAR errorString, UINT errorStringLength);
347 BOOLEAN PacketStartOemEx(PCHAR errorString, UINT errorStringLength, ULONG flags);
348 PAirpcapHandle PacketGetAirPcapHandle(LPADAPTER AdapterObject);
349 
350 //
351 // Used by PacketStartOemEx
352 //
353 #define PACKET_START_OEM_NO_NETMON 0x00000001
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 
359 #endif //__PACKET32
struct _ADAPTER ADAPTER
Describes an opened network adapter.
A single BPF pseudo-instruction.
Definition: bpf.h:240
Describes an opened network adapter.
Definition: Packet32.h:224
Structure that contains a couple of statistics values on the current capture.
Definition: bpf.h:78
USHORT code
Instruction type and addressing mode.
Definition: Packet32.h:121
ULONG Oid
OID code.
Definition: Packet32.h:284
Addresses of a network adapter.
Definition: Packet32.h:194
Structure that contains a group of packets coming from the driver.
Definition: Packet32.h:265
UCHAR jf
Jump if false.
Definition: Packet32.h:123
WAN_ADAPTER * PWAN_ADAPTER
Describes an opened wan (dialup, VPN...) network adapter using the NetMon API.
Definition: Packet32.h:208
OVERLAPPED OverLapped
Definition: Packet32.h:267
UINT Flags
Adapter&#39;s flags. Tell if this adapter must be treated in a different way, using the Netmon API or the...
Definition: Packet32.h:241
DWORD ulBytesReceived
Number of valid bytes present in the buffer, i.e.
Definition: Packet32.h:271
UINT caplen
Length of captured portion.
Definition: Packet32.h:173
A BPF pseudo-assembly program.
Definition: bpf.h:70
Structure containing an OID request.
Definition: Packet32.h:283
UINT bf_len
Indicates the number of instructions of the program, i.e. the number of struct bpf_insn that will fol...
Definition: Packet32.h:110
#define ADAPTER_NAME_LENGTH
Maximum length for the name of an adapter. The value is the same used by the IP Helper API...
Definition: Packet32.h:201
struct npf_if_addr npf_if_addr
Addresses of a network adapter.
struct NetType NetType
Network type structure.
Network type structure.
Definition: Packet32.h:92
UCHAR jt
Jump if true.
Definition: Packet32.h:122
Dump packet header.
Definition: Packet32.h:171
struct WAN_ADAPTER_INT WAN_ADAPTER
Describes an opened wan (dialup, VPN...) network adapter using the NetMon API.
Definition: Packet32.h:207
UINT bs_capt
number of packets that pass the filter, find place in the kernel buffer and thus reach the applicatio...
Definition: Packet32.h:141
PVOID Buffer
Buffer with containing the packets.
Definition: Packet32.h:268
UINT bs_recv
Number of packets that the driver received from the network adapter from the beginning of the current...
Definition: Packet32.h:134
Packet header.
Definition: bpf.h:146
USHORT bh_hdrlen
Length of bpf header (this struct plus alignment padding).
Definition: Packet32.h:158
HANDLE hEvent
Definition: Packet32.h:266
struct _PACKET PACKET
Structure that contains a group of packets coming from the driver.
UINT LinkType
The MAC of the current network adapter (see function PacketGetNetType() for more information) ...
Definition: Packet32.h:94
int k
Generic field used for various purposes.
Definition: Packet32.h:124
ULONG Length
Length of the data field.
Definition: Packet32.h:286
ULONGLONG LinkSpeed
The speed of the network in bits per second.
Definition: Packet32.h:95