Watt-32 tcp/ip  2.2 dev-rel.10
packet32.h
1 /*
2  * Copyright (c) 1999 - 2003
3  * NetGroup, Politecnico di Torino (Italy)
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 nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _w32_PACKET32_H
33 #define _w32_PACKET32_H
34 
35 /* Never include old-style BPF stuff here; use the WinPcap
36  * definition copied in below
37  */
38 #define __NET_BPF_H
39 
40 #ifdef __DMC__
41 #include <win32/wtypes.h> /* DMC has <wtypes.h> in include path */
42 #endif
43 
44 #include <winioctl.h>
45 #include <netinet/in.h> /* struct sockaddr_storage */
46 
47 /* Working modes
48  */
49 #define PACKET_MODE_CAPT 0x0 /* Capture mode */
50 #define PACKET_MODE_STAT 0x1 /* Statistical mode */
51 #define PACKET_MODE_MON 0x2 /* Monitoring mode */
52 #define PACKET_MODE_DUMP 0x10 /* Dump mode */
53 #define PACKET_MODE_STAT_DUMP (MODE_STAT|MODE_DUMP) /* Statistical dump Mode */
54 
55 /* Loopback behaviour definitions
56  */
57 #define NPF_DISABLE_LOOPBACK 1 /* Drop the packets sent by the NPF driver */
58 #define NPF_ENABLE_LOOPBACK 2 /* Capture the packets sent by the NPF driver (default) */
59 
60 /*
61  * Macro definition for defining IOCTL and FSCTL function control codes. Note
62  * that function codes 0-2047 are reserved for Microsoft Corporation, and
63  * 2048-4095 are reserved for customers.
64  */
65 #ifndef CTL_CODE
66 #define CTL_CODE(DeviceType, Function, Method, Access) ( \
67  ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) )
68 #endif
69 
70 /* Define the method codes for how buffers are passed for I/O and FS controls.
71  * These constants are normally defined in <winioctl.h> and/or <winddk.h>.
72  */
73 #ifndef METHOD_BUFFERED
74 #define METHOD_BUFFERED 0
75 #endif
76 
77 #ifndef METHOD_IN_DIRECT
78 #define METHOD_IN_DIRECT 1
79 #endif
80 
81 #ifndef METHOD_OUT_DIRECT
82 #define METHOD_OUT_DIRECT 2
83 #endif
84 
85 #ifndef METHOD_NEITHER
86 #define METHOD_NEITHER 3
87 #endif
88 
89 #if 0
90  /*
91  * Duplicates from <WinIoCtl.h>:
92  * The FILE_READ_ACCESS and FILE_WRITE_ACCESS constants are also defined in
93  * ntioapi.h as FILE_READ_DATA and FILE_WRITE_DATA. The values for these
94  * constants *MUST* always be in sync.
95  */
96  #ifndef FILE_ANY_ACCESS
97  #define FILE_ANY_ACCESS 0
98  #endif
99 
100  #ifndef FILE_READ_ACCESS
101  #define FILE_READ_ACCESS 0x0001 /* file & pipe */
102  #endif
103 
104  #ifndef FILE_READ_ACCESS
105  #define FILE_WRITE_ACCESS 0x0002 /* file & pipe */
106  #endif
107 
108  /* ioctls
109  */
110  #ifndef FILE_DEVICE_PROTOCOL
111  #define FILE_DEVICE_PROTOCOL 0x8000
112  #endif
113 
114  #ifndef FILE_DEVICE_PHYSICAL_NETCARD
115  #define FILE_DEVICE_PHYSICAL_NETCARD 0x0017
116  #endif
117 
118  #define IOCTL_PROTOCOL_STATISTICS CTL_CODE (FILE_DEVICE_PROTOCOL, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
119  #define IOCTL_PROTOCOL_RESET CTL_CODE (FILE_DEVICE_PROTOCOL, 3, METHOD_BUFFERED, FILE_ANY_ACCESS)
120  #define IOCTL_PROTOCOL_READ CTL_CODE (FILE_DEVICE_PROTOCOL, 4, METHOD_BUFFERED, FILE_ANY_ACCESS)
121  #define IOCTL_PROTOCOL_WRITE CTL_CODE (FILE_DEVICE_PROTOCOL, 5, METHOD_BUFFERED, FILE_ANY_ACCESS)
122  #define IOCTL_PROTOCOL_MACNAME CTL_CODE (FILE_DEVICE_PROTOCOL, 6, METHOD_BUFFERED, FILE_ANY_ACCESS)
123  #define IOCTL_OPEN CTL_CODE (FILE_DEVICE_PROTOCOL, 7, METHOD_BUFFERED, FILE_ANY_ACCESS)
124  #define IOCTL_CLOSE CTL_CODE (FILE_DEVICE_PROTOCOL, 8, METHOD_BUFFERED, FILE_ANY_ACCESS)
125 #endif /* 0 */
126 
127 #define pBIOCSETBUFFERSIZE 9592 /* set kernel buffer size */
128 #define pBIOCSETF 9030 /* set packet filtering program */
129 #define pBIOCGSTATS 9031 /* get the capture stats */
130 #define pATTACHPROCESS 7117 /* attach a process to the driver. Used in Win9x only */
131 #define pDETACHPROCESS 7118 /* detach a process from the driver. Used in Win9x only */
132 #define pBIOCISETLOBBEH 7410 /* set the loopback behavior of the driver with packets sent by itself: capture or drop */
133 #define pBIOCSMODE 7412 /* set working mode */
134 #define pBIOCSWRITEREP 7413 /* set number of physical repetions of every packet written by the app. */
135 #define pBIOCSMINTOCOPY 7414 /* set minimum amount of data in the kernel buffer that unlocks a read call */
136 #define pBIOCEVNAME 7415 /* get the name of the event that the driver signals when some data is present in the buffer */
137 #define pBIOCSRTIMEOUT 7416 /* set the read timeout */
138 #define pBIOCSETEVENTHANDLE 7920 /* Passes the read event HANDLE allocated by user to the kernel level driver. */
139 #define pBIOCSETDUMPFILENAME 9029 /* set the name of a the file used by kernel dump mode */
140 #define pBIOCSENDPACKETSNOSYNC 9032 /* send a buffer containing multiple packets to the network, ignoring the timestamps associated with the packets */
141 #define pBIOCSENDPACKETSSYNC 9033 /* send a buffer containing multiple packets to the network, respecting the timestamps associated with the packets */
142 #define pBIOCSETDUMPLIMITS 9034 /* set the dump file limits. See the PacketSetDumpLimits() function */
143 #define pBIOCSETOID 2147483648U /* set an OID value */
144 #define pBIOCQUERYOID 2147483652U /* get an OID value */
145 
146 /* Alignment macro. Defines the alignment size.
147  */
148 #define Packet_ALIGNMENT sizeof(int)
149 
150 /* Alignment macro. Rounds up to the next even multiple of Packet_ALIGNMENT.
151  */
152 #define Packet_WORDALIGN(x) (((x) + (Packet_ALIGNMENT-1)) & ~(Packet_ALIGNMENT-1))
153 
154 #if 0
155  #define NdisMediumNull -1 /* Custom linktype: NDIS doesn't provide an equivalent */
156  #define NdisMediumCHDLC -2 /* Custom linktype: NDIS doesn't provide an equivalent */
157  #define NdisMediumPPPSerial -3 /* Custom linktype: NDIS doesn't provide an equivalent */
158  #define NdisMediumBare80211 -4 /* Custom linktype: NDIS doesn't provide an equivalent */
159  #define NdisMediumRadio80211 -5 /* Custom linktype: NDIS doesn't provide an equivalent */
160 #endif
161 
168 typedef struct npf_if_addr {
169  struct sockaddr_storage IPAddress;
170  struct sockaddr_storage SubnetMask;
171  struct sockaddr_storage Broadcast;
172  } npf_if_addr;
173 
174 
175 #define MAX_LINK_NAME_LENGTH 64 /* Maximum length of the devices symbolic links */
176 #define ADAPTER_NAME_LENGTH (256+12) /* Max length for the name of an adapter */
177 #define ADAPTER_DESC_LENGTH 128 /* Max length for the description of an adapter */
178 #define MAX_MAC_ADDR_LENGTH 8 /* Max length for the link layer address */
179 #define MAX_NETWORK_ADDRESSES 16 /* Max # of network layer addresses */
180 
184 typedef struct ADAPTER_INFO {
185  struct ADAPTER_INFO *Next;
186  char Name [ADAPTER_NAME_LENGTH+1];
187  int NNetworkAddresses;
188  struct npf_if_addr NetworkAddresses [MAX_NETWORK_ADDRESSES];
189  } ADAPTER_INFO;
190 
194 typedef struct ADAPTER {
195  HANDLE hFile;
196  HANDLE ReadEvent;
197  UINT ReadTimeOut;
198  DWORD flags;
199  struct ADAPTER_INFO *info;
200  } ADAPTER;
201 
205 #define INFO_FLAG_NDIS_ADAPTER 0
206 #define INFO_FLAG_NDISWAN_ADAPTER 1
207 #define INFO_FLAG_AIRPCAP_CARD 16
208 #define INFO_FLAG_NPFIM_DEVICE 32
209 
218 typedef struct PACKET_OID_DATA {
219  DWORD Oid;
220  DWORD Length;
221  BYTE Data[1];
222  } PACKET_OID_DATA;
223 
224 
225 #include <sys/pack_on.h>
226 
232 struct bpf_hdr {
233  struct {
234  long tv_sec;
235  long tv_usec;
236  } bh_tstamp; /* The timestamp associated with the captured packet */
237  DWORD bh_caplen; /* Length of captured portion */
238  DWORD bh_datalen; /* Original length of packet */
239  WORD bh_hdrlen; /* Length of bpf header (this struct plus alignment padding) */
240  };
241 
242 
243 /* The receive statistics structure from NPF.
244  */
245 struct bpf_stat {
246  DWORD bs_recv; /* # of packets that the driver received from the network adapter */
247  DWORD bs_drop; /* # of packets that the driver lost from the beginning of a capture */
248  DWORD ps_ifdrop; /* drops by interface. XXX not yet supported */
249  DWORD bs_capt; /* # of packets that pass the filter */
250  };
251 
252 #include <sys/pack_off.h>
253 
254 /*
255  * Protect these symbols with a namespace. Thus allowing Watt-32 (static) to
256  * be used in libpcap/WinPcap programs.
257  */
258 
259 #define PacketInitModule W32_NAMESPACE (PacketInitModule)
260 #define PacketExitModule W32_NAMESPACE (PacketExitModule)
261 #define PacketOpenAdapter W32_NAMESPACE (PacketOpenAdapter)
262 #define PacketFindAdInfo W32_NAMESPACE (PacketFindAdInfo)
263 #define PacketGetAdInfo W32_NAMESPACE (PacketGetAdInfo)
264 #define PacketCloseAdapter W32_NAMESPACE (PacketCloseAdapter)
265 #define PacketRequest2 W32_NAMESPACE (PacketRequest2)
266 #define PacketInstallDriver W32_NAMESPACE (PacketInstallDriver)
267 #define PacketGetDriverVersion W32_NAMESPACE (PacketGetDriverVersion)
268 #define PacketGetMacAddress W32_NAMESPACE (PacketGetMacAddress)
269 #define PacketSetMode W32_NAMESPACE (PacketSetMode)
270 #define PacketSetBuff W32_NAMESPACE (PacketSetBuff)
271 #define PacketSetMinToCopy W32_NAMESPACE (PacketSetMinToCopy)
272 #define PacketSetReadTimeout W32_NAMESPACE (PacketSetReadTimeout)
273 #define PacketGetReadEvent W32_NAMESPACE (PacketGetReadEvent)
274 #define PacketGetStatsEx W32_NAMESPACE (PacketGetStatsEx)
275 #define PacketReceivePacket W32_NAMESPACE (PacketReceivePacket)
276 #define PacketSendPacket W32_NAMESPACE (PacketSendPacket)
277 #define PacketSetLoopbackBehavior W32_NAMESPACE (PacketSetLoopbackBehavior)
278 
279 BOOL PacketInitModule (void);
280 BOOL PacketExitModule (void);
281 
282 const ADAPTER *PacketOpenAdapter (const char *adapter_name);
283 const ADAPTER_INFO *PacketFindAdInfo (const char *adapter_name);
284 const ADAPTER_INFO *PacketGetAdInfo (void);
285 BOOL PacketCloseAdapter (ADAPTER *adapter);
286 const char *PacketGetDriverVersion (void);
287 
288 BOOL PacketRequest2 (const ADAPTER *adapter, BOOL Set, PACKET_OID_DATA *OidData,
289  const char *file, unsigned line);
290 
291 #define PacketRequest(a,set,oid) PacketRequest2 (a, set, oid, __FILE__, __LINE__)
292 
293 BOOL PacketGetMacAddress (const ADAPTER *adapter, void *mac);
294 BOOL PacketSetMode (const ADAPTER *adapter, DWORD mode);
295 BOOL PacketSetBuff (const ADAPTER *adapter, DWORD dim);
296 BOOL PacketSetMinToCopy (const ADAPTER *adapter, int nbytes);
297 BOOL PacketSetReadTimeout (ADAPTER *adapter, int timeout);
298 HANDLE PacketGetReadEvent (const ADAPTER *adapter);
299 BOOL PacketGetStatsEx (const ADAPTER *adapter, struct bpf_stat *st);
300 UINT PacketSendPacket (const ADAPTER *adapter, const void *buf, UINT len);
301 BOOL PacketSetLoopbackBehavior (const ADAPTER *adapter, UINT LoopbackBehavior);
302 
303 UINT MS_CDECL PacketReceivePacket (const ADAPTER *adapter, void *buf, UINT buf_len);
304 
305 /* WanPacket stuff (move to a new WanPacket.h?)
306  */
307 
308 typedef void WAN_ADAPTER; /* The details of this struct is not important to us.
309  * Ref. WAN_ADAPTER_INT in WanPacket.cpp (from CACE Technologies)
310  */
311 
312 BOOL WanPacketSetBpfFilter (WAN_ADAPTER *wan_adapter, PUCHAR FilterCode, DWORD Length);
313 WAN_ADAPTER *WanPacketOpenAdapter (void);
314 BOOL WanPacketCloseAdapter (WAN_ADAPTER *wan_adapter);
315 BOOL WanPacketSetBufferSize (WAN_ADAPTER *wan_adapter, DWORD BufferSize);
316 DWORD WanPacketReceivePacket (WAN_ADAPTER *wan_adapter, PUCHAR Buffer, DWORD BufferSize);
317 BOOL WanPacketSetMinToCopy (WAN_ADAPTER *wan_adapter, DWORD MinToCopy);
318 BOOL WanPacketGetStats (WAN_ADAPTER *wan_adapter, struct bpf_stat *s);
319 BOOL WanPacketSetReadTimeout (WAN_ADAPTER *wan_adapter, DWORD ReadTimeout);
320 BOOL WanPacketSetMode (WAN_ADAPTER *wan_adapter, DWORD Mode);
321 HANDLE WanPacketGetReadEvent (WAN_ADAPTER *wan_adapter);
322 BOOL WanPacketTestAdapter (void);
323 
324 #endif /* _w32_PACKET32_H */
Addresses of a network adapter.
Definition: packet32.h:168
Contains some information about a network adapter.
Definition: packet32.h:184
Structure containing an OID request.
Definition: packet32.h:218
Describes an opened network adapter.
Definition: packet32.h:194
Berkeley Packet Filter header.
Definition: packet32.h:232