Main Page Modules Data Structures File List Data Fields Globals Related Pages

# Packet32.h

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 1999 - 2003
    00003  * NetGroup, Politecnico di Torino (Italy)
    00004  * All rights reserved.
    00005  *
    00006  * Redistribution and use in source and binary forms, with or without
    00007  * modification, are permitted provided that the following conditions
    00008  * are met:
    00009  *
    00010  * 1. Redistributions of source code must retain the above copyright
    00011  * notice, this list of conditions and the following disclaimer.
    00012  * 2. Redistributions in binary form must reproduce the above copyright
    00013  * notice, this list of conditions and the following disclaimer in the
    00014  * documentation and/or other materials provided with the distribution.
    00015  * 3. Neither the name of the Politecnico di Torino nor the names of its
    00016  * contributors may be used to endorse or promote products derived from
    00017  * this software without specific prior written permission.
    00018  *
    00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    00023  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00030  *
    00031  */
    00032
    00044 #ifndef __PACKET32
    00045 #define __PACKET32
    00046
    00047 #include <winsock2.h>
    00048 #include "devioctl.h"
    00049 #ifdef HAVE_DAG_API
    00050 #include <dagc.h>
    00051 #endif /* HAVE_DAG_API */
    00052
    00053 // Working modes
    00054 #define PACKET_MODE_CAPT 0x0
    00055 #define PACKET_MODE_STAT 0x1
    00056 #define PACKET_MODE_MON 0x2
    00057 #define PACKET_MODE_DUMP 0x10
    00058 #define PACKET_MODE_STAT_DUMP MODE_DUMP | MODE_STAT
    00059
    00060 // ioctls
    00061 #define FILE_DEVICE_PROTOCOL        0x8000
    00062
    00063 #define IOCTL_PROTOCOL_STATISTICS   CTL_CODE(FILE_DEVICE_PROTOCOL, 2 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00064 #define IOCTL_PROTOCOL_RESET        CTL_CODE(FILE_DEVICE_PROTOCOL, 3 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00065 #define IOCTL_PROTOCOL_READ         CTL_CODE(FILE_DEVICE_PROTOCOL, 4 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00066 #define IOCTL_PROTOCOL_WRITE        CTL_CODE(FILE_DEVICE_PROTOCOL, 5 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00067 #define IOCTL_PROTOCOL_MACNAME      CTL_CODE(FILE_DEVICE_PROTOCOL, 6 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00068 #define IOCTL_OPEN                  CTL_CODE(FILE_DEVICE_PROTOCOL, 7 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00069 #define IOCTL_CLOSE                 CTL_CODE(FILE_DEVICE_PROTOCOL, 8 , METHOD_BUFFERED, FILE_ANY_ACCESS)
    00070
    00071 #define  pBIOCSETBUFFERSIZE 9592
    00072 #define  pBIOCSETF 9030
    00073 #define  pBIOCGSTATS 9031
    00074 #define  pBIOCSRTIMEOUT 7416
    00075 #define  pBIOCSMODE 7412
    00076 #define  pBIOCSWRITEREP 7413
    00077 #define  pBIOCSMINTOCOPY 7414
    00078 #define  pBIOCSETOID 2147483648
    00079 #define  pBIOCQUERYOID 2147483652
    00080 #define  pATTACHPROCESS 7117
    00081 #define  pDETACHPROCESS 7118
    00082 #define  pBIOCSETDUMPFILENAME 9029
    00083 #define  pBIOCEVNAME 7415
    00084 #define  pBIOCSENDPACKETSNOSYNC 9032
    00085 #define  pBIOCSENDPACKETSSYNC 9033
    00086 #define  pBIOCSETDUMPLIMITS 9034
    00087 #define  pBIOCISDUMPENDED 7411
    00088
    00089 #define  pBIOCSTIMEZONE 7471
    00090
    00091
    00093 #define Packet_ALIGNMENT sizeof(int)
    00094
    00095 #define Packet_WORDALIGN(x) (((x)+(Packet_ALIGNMENT-1))&~(Packet_ALIGNMENT-1))
    00096
    00097
    00098 #define NdisMediumNull  -1      // Custom linktype: NDIS doesn't provide an equivalent
    00099 #define NdisMediumCHDLC -2      // Custom linktype: NDIS doesn't provide an equivalent
    00100 #define NdisMediumPPPSerial -3  // Custom linktype: NDIS doesn't provide an equivalent
    00101
    00107 typedef struct NetType
    00108 {
    00109     UINT LinkType;
    00110     ULONGLONG LinkSpeed;
    00111 }NetType;
    00112
    00113
    00114 //some definitions stolen from libpcap
    00115
    00116 #ifndef BPF_MAJOR_VERSION
    00117
    00123 struct bpf_program {
    00124     UINT bf_len;
    00125     struct bpf_insn *bf_insns;
    00126 };
    00127
    00133 struct bpf_insn {
    00134     USHORT  code;
    00135     UCHAR   jt;
    00136     UCHAR   jf;
    00137     int k;
    00138 };
    00139
    00145 struct bpf_stat {
    00146     UINT bs_recv;
    00147
    00148
    00149     UINT bs_drop;
    00150
    00151
    00152     UINT ps_ifdrop;
    00153     UINT bs_capt;
    00154
    00155 };
    00156
    00162 struct bpf_hdr {
    00163     struct timeval  bh_tstamp;
    00164
    00165     UINT    bh_caplen;
    00166
    00167
    00168     UINT    bh_datalen;
    00169     USHORT      bh_hdrlen;
    00170
    00171
    00172
    00173 };
    00174
    00182 struct dump_bpf_hdr{
    00183     struct timeval  ts;
    00184     UINT            caplen;
    00185
    00186
    00187     UINT            len;
    00188 };
    00189
    00190
    00191 #endif
    00192
    00193 #define        DOSNAMEPREFIX   TEXT("Packet_")
    00194 #define        MAX_LINK_NAME_LENGTH 64          //< Maximum length of the devices symbolic links
    00195 #define        NMAX_PACKET 65535
    00196
    00203 typedef struct npf_if_addr {
    00204     struct sockaddr_storage IPAddress;
    00205     struct sockaddr_storage SubnetMask;
    00206     struct sockaddr_storage Broadcast;
    00207 }npf_if_addr;
    00208
    00209
    00210 #define ADAPTER_NAME_LENGTH 256 + 12
    00211 #define ADAPTER_DESC_LENGTH 128
    00212 #define MAX_MAC_ADDR_LENGTH 8
    00213 #define MAX_NETWORK_ADDRESSES 16
    00214
    00215
    00216 typedef struct WAN_ADAPTER_INT WAN_ADAPTER;
    00217 typedef WAN_ADAPTER *PWAN_ADAPTER;
    00218
    00219 #define INFO_FLAG_NDIS_ADAPTER      0
    00220 #define INFO_FLAG_NDISWAN_ADAPTER   1
    00221 #define INFO_FLAG_DAG_CARD          2
    00222 #define INFO_FLAG_DAG_FILE          6
    00223
    00229 typedef struct _ADAPTER_INFO
    00230 {
    00231     struct _ADAPTER_INFO *Next;
    00232     CHAR Name[ADAPTER_NAME_LENGTH + 1];
    00233     CHAR Description[ADAPTER_DESC_LENGTH + 1];
    00234     UINT MacAddressLen;
    00235     UCHAR MacAddress[MAX_MAC_ADDR_LENGTH];
    00236     NetType LinkLayer;
    00237     INT NNetworkAddresses;
    00238     npf_if_addr *NetworkAddresses;
    00239     UINT Flags;
    00240 }
    00241 ADAPTER_INFO, *PADAPTER_INFO;
    00242
    00249 typedef struct _ADAPTER  {
    00250     HANDLE hFile;
    00251     CHAR  SymbolicLink[MAX_LINK_NAME_LENGTH];
    00252     int NumWrites;
    00253
    00254     HANDLE ReadEvent;
    00255
    00256
    00257
    00258
    00259
    00260
    00261
    00262     UINT ReadTimeOut;
    00263
    00264     CHAR Name[ADAPTER_NAME_LENGTH];
    00265     PWAN_ADAPTER pWanAdapter;
    00266     UINT Flags;
    00267 #ifdef HAVE_DAG_API
    00268     dagc_t *pDagCard;
    00269     PCHAR DagBuffer;
    00270     struct timeval DagReadTimeout;
    00271     unsigned DagFcsLen;
    00272     DWORD DagFastProcess;
    00273 #endif // HAVE_DAG_API
    00274 }  ADAPTER, *LPADAPTER;
    00275
    00281 typedef struct _PACKET {
    00282     HANDLE       hEvent;
    00283     OVERLAPPED   OverLapped;
    00284     PVOID        Buffer;
    00285
    00286     UINT         Length;
    00287     DWORD        ulBytesReceived;
    00288
    00289     BOOLEAN      bIoComplete;
    00290 }  PACKET, *LPPACKET;
    00291
    00299 struct _PACKET_OID_DATA {
    00300     ULONG Oid;
    00301
    00302     ULONG Length;
    00303     UCHAR Data[1];
    00304
    00305 };
    00306 typedef struct _PACKET_OID_DATA PACKET_OID_DATA, *PPACKET_OID_DATA;
    00307
    00308
    00309 #if _DBG
    00310 #define ODS(_x) OutputDebugString(TEXT(_x))
    00311 #define ODSEx(_x, _y)
    00312 #else
    00313 #ifdef _DEBUG_TO_FILE
    00314
    00317 #define ODS(_x) { \
    00318     FILE *f; \
    00319     f = fopen("winpcap_debug.txt", "a"); \
    00320     fprintf(f, "%s", _x); \
    00321     fclose(f); \
    00322 }
    00323
    00327 #define ODSEx(_x, _y) { \
    00328     FILE *f; \
    00329     f = fopen("winpcap_debug.txt", "a"); \
    00330     fprintf(f, _x, _y); \
    00331     fclose(f); \
    00332 }
    00333
    00334
    00335
    00336 LONG PacketDumpRegistryKey(PCHAR KeyName, PCHAR FileName);
    00337 #else
    00338 #define ODS(_x)
    00339 #define ODSEx(_x, _y)
    00340 #endif
    00341 #endif
    00342
    00343 /* We load dinamically the dag library in order link it only when it's present on the system */
    00344 #ifdef HAVE_DAG_API
    00345 typedef dagc_t* (*dagc_open_handler)(const char *source, unsigned flags, char *ebuf);
    00346 typedef void (*dagc_close_handler)(dagc_t *dagcfd);
    00347 typedef int (*dagc_getlinktype_handler)(dagc_t *dagcfd);
    00348 typedef int (*dagc_getlinkspeed_handler)(dagc_t *dagcfd);
    00349 typedef int (*dagc_setsnaplen_handler)(dagc_t *dagcfd, unsigned snaplen);
    00350 typedef unsigned (*dagc_getfcslen_handler)(dagc_t *dagcfd);
    00351 typedef int (*dagc_receive_handler)(dagc_t *dagcfd, u_char **buffer, u_int *bufsize);
    00352 typedef int (*dagc_stats_handler)(dagc_t *dagcfd, dagc_stats_t *ps);
    00353 typedef int (*dagc_wait_handler)(dagc_t *dagcfd, struct timeval *timeout);
    00354 typedef int (*dagc_finddevs_handler)(dagc_if_t **alldevsp, char *ebuf);
    00355 typedef int (*dagc_freedevs_handler)(dagc_if_t *alldevsp);
    00356 #endif // HAVE_DAG_API
    00357
    00358 #ifdef __cplusplus
    00359 extern "C" {
    00360 #endif
    00361
    00366 void PacketPopulateAdaptersInfoList();
    00367 PWCHAR SChar2WChar(PCHAR string);
    00368 PCHAR WChar2SChar(PWCHAR string);
    00369 BOOL PacketGetFileVersion(LPTSTR FileName, PCHAR VersionBuff, UINT VersionBuffLen);
    00370 PADAPTER_INFO PacketFindAdInfo(PCHAR AdapterName);
    00371 BOOLEAN PacketUpdateAdInfo(PCHAR AdapterName);
    00372
    00373
    00374 //---------------------------------------------------------------------------
    00375 // EXPORTED FUNCTIONS
    00376 //---------------------------------------------------------------------------
    00377
    00378 PCHAR PacketGetVersion();
    00379 PCHAR PacketGetDriverVersion();
    00380 BOOLEAN PacketSetMinToCopy(LPADAPTER AdapterObject,int nbytes);
    00381 BOOLEAN PacketSetNumWrites(LPADAPTER AdapterObject,int nwrites);
    00382 BOOLEAN PacketSetMode(LPADAPTER AdapterObject,int mode);
    00383 BOOLEAN PacketSetReadTimeout(LPADAPTER AdapterObject,int timeout);
    00384 BOOLEAN PacketSetBpf(LPADAPTER AdapterObject,struct bpf_program *fp);
    00385 INT PacketSetSnapLen(LPADAPTER AdapterObject,int snaplen);
    00386 BOOLEAN PacketGetStats(LPADAPTER AdapterObject,struct bpf_stat *s);
    00387 BOOLEAN PacketGetStatsEx(LPADAPTER AdapterObject,struct bpf_stat *s);
    00388 BOOLEAN PacketSetBuff(LPADAPTER AdapterObject,int dim);
    00389 BOOLEAN PacketGetNetType (LPADAPTER AdapterObject,NetType *type);
    00390 LPADAPTER PacketOpenAdapter(PCHAR AdapterName);
    00391 BOOLEAN PacketSendPacket(LPADAPTER AdapterObject,LPPACKET pPacket,BOOLEAN Sync);
    00392 INT PacketSendPackets(LPADAPTER AdapterObject,PVOID PacketBuff,ULONG Size, BOOLEAN Sync);
    00393 LPPACKET PacketAllocatePacket(void);
    00394 VOID PacketInitPacket(LPPACKET lpPacket,PVOID  Buffer,UINT  Length);
    00395 VOID PacketFreePacket(LPPACKET lpPacket);
    00396 BOOLEAN PacketReceivePacket(LPADAPTER AdapterObject,LPPACKET lpPacket,BOOLEAN Sync);
    00397 BOOLEAN PacketSetHwFilter(LPADAPTER AdapterObject,ULONG Filter);
    00398 BOOLEAN PacketGetAdapterNames(PTSTR pStr,PULONG  BufferSize);
    00399 BOOLEAN PacketGetNetInfoEx(PCHAR AdapterName, npf_if_addr* buffer, PLONG NEntries);
    00400 BOOLEAN PacketRequest(LPADAPTER  AdapterObject,BOOLEAN Set,PPACKET_OID_DATA  OidData);
    00401 HANDLE PacketGetReadEvent(LPADAPTER AdapterObject);
    00402 BOOLEAN PacketSetDumpName(LPADAPTER AdapterObject, void *name, int len);
    00403 BOOLEAN PacketSetDumpLimits(LPADAPTER AdapterObject, UINT maxfilesize, UINT maxnpacks);
    00404 BOOLEAN PacketIsDumpEnded(LPADAPTER AdapterObject, BOOLEAN sync);
    00405 BOOL PacketStopDriver();
    00406 VOID PacketCloseAdapter(LPADAPTER lpAdapter);
    00407
    00408 #ifdef __cplusplus
    00409 }
    00410 #endif
    00411
    00412 #endif //__PACKET32

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.