4 * Copyright 2011, 2014 André Hentschel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wpcap
);
34 const struct pcap_funcs
*pcap_funcs
= NULL
;
36 int CDECL
pcap_activate( void *handle
)
38 TRACE( "%p\n", handle
);
39 return pcap_funcs
->activate( handle
);
42 void CDECL
pcap_breakloop( void *handle
)
44 TRACE( "%p\n", handle
);
45 pcap_funcs
->breakloop( handle
);
48 int CDECL
pcap_can_set_rfmon( void *handle
)
50 TRACE( "%p\n", handle
);
51 return pcap_funcs
->can_set_rfmon( handle
);
54 void CDECL
pcap_close( void *handle
)
56 TRACE( "%p\n", handle
);
57 pcap_funcs
->close( handle
);
60 int CDECL
pcap_compile( void *handle
, void *program
, const char *buf
, int optimize
, unsigned int mask
)
62 TRACE( "%p, %p, %s, %d, %u\n", handle
, program
, debugstr_a(buf
), optimize
, mask
);
63 return pcap_funcs
->compile( handle
, program
, buf
, optimize
, mask
);
66 void * CDECL
pcap_create( const char *src
, char *errbuf
)
68 TRACE( "%s, %p\n", src
, errbuf
);
69 return pcap_funcs
->create( src
, errbuf
);
72 int CDECL
pcap_datalink( void *handle
)
74 TRACE( "%p\n", handle
);
75 return pcap_funcs
->datalink( handle
);
78 int CDECL
pcap_datalink_name_to_val( const char *name
)
80 TRACE( "%s\n", debugstr_a(name
) );
81 return pcap_funcs
->datalink_name_to_val( name
);
84 const char * CDECL
pcap_datalink_val_to_description( int link
)
86 TRACE( "%d\n", link
);
87 return pcap_funcs
->datalink_val_to_description( link
);
90 const char * CDECL
pcap_datalink_val_to_name( int link
)
92 TRACE( "%d\n", link
);
93 return pcap_funcs
->datalink_val_to_name( link
);
96 int CDECL
pcap_dispatch( void *handle
, int count
,
97 void (CALLBACK
*callback
)(unsigned char *, const void *, const unsigned char *),
100 TRACE( "%p, %d, %p, %p\n", handle
, count
, callback
, user
);
101 return pcap_funcs
->dispatch( handle
, count
, callback
, user
);
104 void CDECL
pcap_dump( unsigned char *user
, const void *hdr
, const unsigned char *packet
)
106 TRACE( "%p, %p, %p\n", user
, hdr
, packet
);
107 pcap_funcs
->dump( user
, hdr
, packet
);
110 static inline WCHAR
*strdupAW( const char *str
)
115 int len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
116 if ((ret
= malloc( len
* sizeof(WCHAR
) ))) MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
121 void * CDECL
pcap_dump_open( void *handle
, const char *filename
)
127 TRACE( "%p, %s\n", handle
, debugstr_a(filename
) );
129 if (!(filenameW
= strdupAW( filename
))) return NULL
;
130 unix_path
= wine_get_unix_file_name( filenameW
);
132 if (!unix_path
) return NULL
;
134 TRACE( "unix_path %s\n", debugstr_a(unix_path
) );
136 dumper
= pcap_funcs
->dump_open( handle
, unix_path
);
137 RtlFreeHeap( GetProcessHeap(), 0, unix_path
);
141 int CDECL
pcap_findalldevs( struct pcap_if_hdr
**devs
, char *errbuf
)
143 TRACE( "%p, %p\n", devs
, errbuf
);
144 return pcap_funcs
->findalldevs( devs
, errbuf
);
147 int CDECL
pcap_findalldevs_ex( char *source
, void *auth
, struct pcap_if_hdr
**devs
, char *errbuf
)
149 FIXME( "%s, %p, %p, %p: partial stub\n", debugstr_a(source
), auth
, devs
, errbuf
);
150 return pcap_funcs
->findalldevs( devs
, errbuf
);
153 void CDECL
pcap_free_datalinks( int *links
)
155 TRACE( "%p\n", links
);
156 pcap_funcs
->free_datalinks( links
);
159 void CDECL
pcap_free_tstamp_types( int *types
)
161 TRACE( "%p\n", types
);
162 pcap_funcs
->free_tstamp_types( types
);
165 void CDECL
pcap_freealldevs( struct pcap_if_hdr
*devs
)
167 TRACE( "%p\n", devs
);
168 pcap_funcs
->freealldevs( devs
);
171 void CDECL
pcap_freecode( void *program
)
173 TRACE( "%p\n", program
);
174 pcap_funcs
->freecode( program
);
177 void * CDECL
pcap_get_airpcap_handle( void *handle
)
179 TRACE( "%p\n", handle
);
183 int CDECL
pcap_get_tstamp_precision( void *handle
)
185 TRACE( "%p\n", handle
);
186 return pcap_funcs
->get_tstamp_precision( handle
);
189 char * CDECL
pcap_geterr( void *handle
)
191 TRACE( "%p\n", handle
);
192 return pcap_funcs
->geterr( handle
);
195 int CDECL
pcap_getnonblock( void *handle
, char *errbuf
)
197 TRACE( "%p, %p\n", handle
, errbuf
);
198 return pcap_funcs
->getnonblock( handle
, errbuf
);
201 static char lib_version
[256];
202 static BOOL WINAPI
init_lib_version( INIT_ONCE
*once
, void *param
, void **ctx
)
204 const char *str
= pcap_funcs
->lib_version();
205 if (strlen( str
) < sizeof(lib_version
)) strcpy( lib_version
, str
);
209 const char * CDECL
pcap_lib_version( void )
211 static INIT_ONCE once
= INIT_ONCE_STATIC_INIT
;
212 if (!lib_version
[0]) InitOnceExecuteOnce( &once
, init_lib_version
, NULL
, NULL
);
213 TRACE( "%s\n", debugstr_a(lib_version
) );
217 int CDECL
pcap_list_datalinks( void *handle
, int **buf
)
219 TRACE( "%p, %p\n", handle
, buf
);
220 return pcap_funcs
->list_datalinks( handle
, buf
);
223 int CDECL
pcap_list_tstamp_types( void *handle
, int **types
)
225 TRACE( "%p, %p\n", handle
, types
);
226 return pcap_funcs
->list_tstamp_types( handle
, types
);
229 char * CDECL
pcap_lookupdev( char *errbuf
)
232 struct pcap_if_hdr
*devs
;
234 TRACE( "%p\n", errbuf
);
237 if (pcap_funcs
->findalldevs( &devs
, errbuf
) == -1) return NULL
;
238 if (!devs
) return NULL
;
239 if ((ret
= malloc( strlen(devs
->name
) + 1 ))) strcpy( ret
, devs
->name
);
240 pcap_funcs
->freealldevs( devs
);
245 int CDECL
pcap_lookupnet( const char *device
, unsigned int *net
, unsigned int *mask
, char *errbuf
)
247 TRACE( "%s, %p, %p, %p\n", debugstr_a(device
), net
, mask
, errbuf
);
248 return pcap_funcs
->lookupnet( device
, net
, mask
, errbuf
);
251 int CDECL
pcap_loop( void *handle
, int count
,
252 void (CALLBACK
*callback
)(unsigned char *, const void *, const unsigned char *),
255 TRACE( "%p, %d, %p, %p\n", handle
, count
, callback
, user
);
256 return pcap_funcs
->loop( handle
, count
, callback
, user
);
259 int CDECL
pcap_major_version( void *handle
)
261 TRACE( "%p\n", handle
);
262 return pcap_funcs
->major_version( handle
);
265 int CDECL
pcap_minor_version( void *handle
)
267 TRACE( "%p\n", handle
);
268 return pcap_funcs
->minor_version( handle
);
271 const unsigned char * CDECL
pcap_next( void *handle
, void *hdr
)
273 TRACE( "%p, %p\n", handle
, hdr
);
274 return pcap_funcs
->next( handle
, hdr
);
277 int CDECL
pcap_next_ex( void *handle
, void **hdr
, const unsigned char **data
)
279 TRACE( "%p, %p, %p\n", handle
, hdr
, data
);
280 return pcap_funcs
->next_ex( handle
, hdr
, data
);
283 #define PCAP_OPENFLAG_PROMISCUOUS 1
284 void * CDECL
pcap_open( const char *source
, int snaplen
, int flags
, int timeout
, void *auth
, char *errbuf
)
286 FIXME( "%s, %d, %d, %d, %p, %p: partial stub\n", debugstr_a(source
), snaplen
, flags
, timeout
, auth
, errbuf
);
287 return pcap_funcs
->open_live( source
, snaplen
, flags
& PCAP_OPENFLAG_PROMISCUOUS
, timeout
, errbuf
);
290 void * CDECL
pcap_open_live( const char *source
, int snaplen
, int promisc
, int to_ms
, char *errbuf
)
292 TRACE( "%s, %d, %d, %d, %p\n", debugstr_a(source
), snaplen
, promisc
, to_ms
, errbuf
);
293 return pcap_funcs
->open_live( source
, snaplen
, promisc
, to_ms
, errbuf
);
296 #define PCAP_SRC_FILE 2
297 #define PCAP_SRC_IFLOCAL 3
299 int CDECL
pcap_parsesrcstr( const char *source
, int *type
, char *host
, char *port
, char *name
, char *errbuf
)
301 int t
= PCAP_SRC_IFLOCAL
;
302 const char *p
= source
;
304 FIXME( "%s, %p, %p, %p, %p, %p: partial stub\n", debugstr_a(source
), type
, host
, port
, name
, errbuf
);
313 if (!strncmp(p
, "rpcap://", strlen("rpcap://")))
314 p
+= strlen("rpcap://");
315 else if (!strncmp(p
, "file://", strlen("file://")))
317 p
+= strlen("file://");
327 sprintf(errbuf
, "The name has not been specified in the source string.");
337 int CDECL
pcap_sendpacket( void *handle
, const unsigned char *buf
, int size
)
339 TRACE( "%p, %p, %d\n", handle
, buf
, size
);
340 return pcap_funcs
->sendpacket( handle
, buf
, size
);
343 int CDECL
pcap_set_buffer_size( void *handle
, int size
)
345 TRACE( "%p, %d\n", handle
, size
);
346 return pcap_funcs
->set_buffer_size( handle
, size
);
349 int CDECL
pcap_set_datalink( void *handle
, int link
)
351 TRACE( "%p, %d\n", handle
, link
);
352 return pcap_funcs
->set_datalink( handle
, link
);
355 int CDECL
pcap_set_promisc( void *handle
, int enable
)
357 TRACE( "%p, %d\n", handle
, enable
);
358 return pcap_funcs
->set_promisc( handle
, enable
);
361 int CDECL
pcap_set_rfmon( void *handle
, int enable
)
363 TRACE( "%p, %d\n", handle
, enable
);
364 return pcap_funcs
->set_rfmon( handle
, enable
);
367 int CDECL
pcap_set_snaplen( void *handle
, int len
)
369 TRACE( "%p, %d\n", handle
, len
);
370 return pcap_funcs
->set_snaplen( handle
, len
);
373 int CDECL
pcap_set_timeout( void *handle
, int timeout
)
375 TRACE( "%p, %d\n", handle
, timeout
);
376 return pcap_funcs
->set_timeout( handle
, timeout
);
379 int CDECL
pcap_set_tstamp_precision( void *handle
, int precision
)
381 TRACE( "%p, %d\n", handle
, precision
);
382 return pcap_funcs
->set_tstamp_precision( handle
, precision
);
385 int CDECL
pcap_set_tstamp_type( void *handle
, int type
)
387 TRACE( "%p, %d\n", handle
, type
);
388 return pcap_funcs
->set_tstamp_type( handle
, type
);
391 int CDECL
pcap_setbuff( void *handle
, int size
)
393 FIXME( "%p, %d\n", handle
, size
);
397 int CDECL
pcap_setfilter( void *handle
, void *program
)
399 TRACE( "%p, %p\n", handle
, program
);
400 return pcap_funcs
->setfilter( handle
, program
);
403 int CDECL
pcap_setnonblock( void *handle
, int nonblock
, char *errbuf
)
405 TRACE( "%p, %d, %p\n", handle
, nonblock
, errbuf
);
406 return pcap_funcs
->setnonblock( handle
, nonblock
, errbuf
);
409 int CDECL
pcap_snapshot( void *handle
)
411 TRACE( "%p\n", handle
);
412 return pcap_funcs
->snapshot( handle
);
415 int CDECL
pcap_stats( void *handle
, void *stats
)
417 TRACE( "%p, %p\n", handle
, stats
);
418 return pcap_funcs
->stats( handle
, stats
);
421 const char * CDECL
pcap_statustostr( int status
)
423 TRACE( "%d\n", status
);
424 return pcap_funcs
->statustostr( status
);
427 int CDECL
pcap_tstamp_type_name_to_val( const char *name
)
429 TRACE( "%s\n", debugstr_a(name
) );
430 return pcap_funcs
->tstamp_type_name_to_val( name
);
433 const char * CDECL
pcap_tstamp_type_val_to_description( int val
)
435 TRACE( "%d\n", val
);
436 return pcap_funcs
->tstamp_type_val_to_description( val
);
439 const char * CDECL
pcap_tstamp_type_val_to_name( int val
)
441 TRACE( "%d\n", val
);
442 return pcap_funcs
->tstamp_type_val_to_name( val
);
445 int CDECL
wsockinit( void )
449 if (WSAStartup( MAKEWORD(1, 1), &wsadata
)) return -1;
453 static void CDECL
pcap_handler_cb( struct handler_callback
*cb
, const void *hdr
, const unsigned char *packet
)
455 TRACE( "%p, %p, %p\n", cb
, hdr
, packet
);
456 cb
->callback( cb
->user
, hdr
, packet
);
457 TRACE( "callback completed\n" );
460 const struct pcap_callbacks pcap_callbacks
=
465 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, void *reserved
)
469 case DLL_PROCESS_ATTACH
:
470 DisableThreadLibraryCalls( hinst
);
471 if (__wine_init_unix_lib( hinst
, reason
, &pcap_callbacks
, &pcap_funcs
))
472 ERR( "No pcap support, expect problems\n" );
474 case DLL_PROCESS_DETACH
: