2 * Copyright 2011, 2014 André Hentschel
3 * Copyright 2021 Hans Leidekker for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifdef HAVE_PCAP_PCAP_H
27 #include <pcap/pcap.h>
31 #define WIN32_NO_STATUS
36 #include "wine/debug.h"
39 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
41 static const struct pcap_callbacks
*callbacks
;
43 static int CDECL
wrap_activate( void *handle
)
45 return pcap_activate( handle
);
48 static void CDECL
wrap_breakloop( void *handle
)
50 return pcap_breakloop( handle
);
53 static int CDECL
wrap_can_set_rfmon( void *handle
)
55 return pcap_can_set_rfmon( handle
);
58 static void CDECL
wrap_close( void *handle
)
63 static int CDECL
wrap_compile( void *handle
, void *program
, const char *buf
, int optimize
, unsigned int mask
)
65 return pcap_compile( handle
, program
, buf
, optimize
, mask
);
68 static void * CDECL
wrap_create( const char *src
, char *errbuf
)
70 return pcap_create( src
, errbuf
);
73 static int CDECL
wrap_datalink( void *handle
)
75 return pcap_datalink( handle
);
78 static int CDECL
wrap_datalink_name_to_val( const char *name
)
80 return pcap_datalink_name_to_val( name
);
83 static const char * CDECL
wrap_datalink_val_to_description( int link
)
85 return pcap_datalink_val_to_description( link
);
88 static const char * CDECL
wrap_datalink_val_to_name( int link
)
90 return pcap_datalink_val_to_name( link
);
93 void wrap_pcap_handler( unsigned char *user
, const struct pcap_pkthdr
*hdr
, const unsigned char *packet
)
95 struct handler_callback
*cb
= (struct handler_callback
*)user
;
96 callbacks
->handler( cb
, hdr
, packet
);
99 static int CDECL
wrap_dispatch( void *handle
, int count
,
100 void (CALLBACK
*callback
)(unsigned char *, const void *, const unsigned char *),
101 unsigned char *user
)
105 struct handler_callback cb
;
106 cb
.callback
= callback
;
108 return pcap_dispatch( handle
, count
, wrap_pcap_handler
, (unsigned char *)&cb
);
110 return pcap_dispatch( handle
, count
, NULL
, user
);
113 static void CDECL
wrap_dump( unsigned char *user
, const void *hdr
, const unsigned char *packet
)
115 return pcap_dump( user
, hdr
, packet
);
118 static void * CDECL
wrap_dump_open( void *handle
, const char *name
)
120 return pcap_dump_open( handle
, name
);
123 static int CDECL
wrap_findalldevs( struct pcap_if_hdr
**devs
, char *errbuf
)
126 ret
= pcap_findalldevs( (pcap_if_t
**)devs
, errbuf
);
128 ERR_(winediag
)( "Failed to access raw network (pcap), this requires special permissions.\n" );
132 static void CDECL
wrap_free_datalinks( int *links
)
134 pcap_free_datalinks( links
);
137 static void CDECL
wrap_free_tstamp_types( int *types
)
139 pcap_free_tstamp_types( types
);
142 static void CDECL
wrap_freealldevs( struct pcap_if_hdr
*devs
)
144 pcap_freealldevs( (pcap_if_t
*)devs
);
147 static void CDECL
wrap_freecode( void *program
)
149 return pcap_freecode( program
);
152 static int CDECL
wrap_get_tstamp_precision( void *handle
)
154 return pcap_get_tstamp_precision( handle
);
157 static char * CDECL
wrap_geterr( void *handle
)
159 return pcap_geterr( handle
);
162 static int CDECL
wrap_getnonblock( void *handle
, char *errbuf
)
164 return pcap_getnonblock( handle
, errbuf
);
167 static const char * CDECL
wrap_lib_version( void )
169 return pcap_lib_version();
172 static int CDECL
wrap_list_datalinks( void *handle
, int **buf
)
174 return pcap_list_datalinks( handle
, buf
);
177 static int CDECL
wrap_list_tstamp_types( void *handle
, int **types
)
179 return pcap_list_tstamp_types( handle
, types
);
182 static int CDECL
wrap_lookupnet( const char *device
, unsigned int *net
, unsigned int *mask
, char *errbuf
)
184 return pcap_lookupnet( device
, net
, mask
, errbuf
);
187 static int CDECL
wrap_loop( void *handle
, int count
,
188 void (CALLBACK
*callback
)(unsigned char *, const void *, const unsigned char *),
189 unsigned char *user
)
193 struct handler_callback cb
;
194 cb
.callback
= callback
;
196 return pcap_loop( handle
, count
, wrap_pcap_handler
, (unsigned char *)&cb
);
198 return pcap_loop( handle
, count
, NULL
, user
);
201 static int CDECL
wrap_major_version( void *handle
)
203 return pcap_major_version( handle
);
206 static int CDECL
wrap_minor_version( void *handle
)
208 return pcap_minor_version( handle
);
211 static const unsigned char * CDECL
wrap_next( void *handle
, void *hdr
)
213 return pcap_next( handle
, hdr
);
216 static int CDECL
wrap_next_ex( void *handle
, void **hdr
, const unsigned char **data
)
218 return pcap_next_ex( handle
, (struct pcap_pkthdr
**)hdr
, data
);
221 static void * CDECL
wrap_open_live( const char *source
, int snaplen
, int promisc
, int to_ms
, char *errbuf
)
223 return pcap_open_live( source
, snaplen
, promisc
, to_ms
, errbuf
);
226 static int CDECL
wrap_sendpacket( void *handle
, const unsigned char *buf
, int size
)
228 return pcap_sendpacket( handle
, buf
, size
);
231 static int CDECL
wrap_set_buffer_size( void *handle
, int size
)
233 return pcap_set_buffer_size( handle
, size
);
236 static int CDECL
wrap_set_datalink( void *handle
, int link
)
238 return pcap_set_datalink( handle
, link
);
241 static int CDECL
wrap_set_promisc( void *handle
, int enable
)
243 return pcap_set_promisc( handle
, enable
);
246 static int CDECL
wrap_set_rfmon( void *handle
, int enable
)
248 return pcap_set_rfmon( handle
, enable
);
251 static int CDECL
wrap_set_snaplen( void *handle
, int len
)
253 return pcap_set_snaplen( handle
, len
);
256 static int CDECL
wrap_set_timeout( void *handle
, int timeout
)
258 return pcap_set_timeout( handle
, timeout
);
261 static int CDECL
wrap_set_tstamp_precision( void *handle
, int precision
)
263 return pcap_set_tstamp_precision( handle
, precision
);
266 static int CDECL
wrap_set_tstamp_type( void *handle
, int type
)
268 return pcap_set_tstamp_type( handle
, type
);
271 static int CDECL
wrap_setfilter( void *handle
, void *program
)
273 return pcap_setfilter( handle
, program
);
276 static int CDECL
wrap_setnonblock( void *handle
, int nonblock
, char *errbuf
)
278 return pcap_setnonblock( handle
, nonblock
, errbuf
);
281 static int CDECL
wrap_snapshot( void *handle
)
283 return pcap_snapshot( handle
);
286 static int CDECL
wrap_stats( void *handle
, void *stats
)
288 return pcap_stats( handle
, stats
);
291 static const char * CDECL
wrap_statustostr( int status
)
293 return pcap_statustostr( status
);
296 static int CDECL
wrap_tstamp_type_name_to_val( const char *name
)
298 return pcap_tstamp_type_name_to_val( name
);
301 static const char * CDECL
wrap_tstamp_type_val_to_description( int val
)
303 return pcap_tstamp_type_val_to_description( val
);
306 static const char * CDECL
wrap_tstamp_type_val_to_name( int val
)
308 return pcap_tstamp_type_val_to_name( val
);
311 static const struct pcap_funcs funcs
=
320 wrap_datalink_name_to_val
,
321 wrap_datalink_val_to_description
,
322 wrap_datalink_val_to_name
,
328 wrap_free_tstamp_types
,
331 wrap_get_tstamp_precision
,
336 wrap_list_tstamp_types
,
345 wrap_set_buffer_size
,
351 wrap_set_tstamp_precision
,
352 wrap_set_tstamp_type
,
358 wrap_tstamp_type_name_to_val
,
359 wrap_tstamp_type_val_to_description
,
360 wrap_tstamp_type_val_to_name
,
363 NTSTATUS CDECL
__wine_init_unix_lib( HMODULE module
, DWORD reason
, const void *ptr_in
, void *ptr_out
)
365 if (reason
!= DLL_PROCESS_ATTACH
) return STATUS_SUCCESS
;
367 *(const struct pcap_funcs
**)ptr_out
= &funcs
;
368 return STATUS_SUCCESS
;
370 #endif /* HAVE_PCAP_PCAP_H */