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>
33 #define WIN32_NO_STATUS
38 #include "wine/unixlib.h"
39 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wpcap
);
43 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
45 static NTSTATUS
wrap_activate( void *args
)
47 struct pcap
*pcap
= args
;
48 return pcap_activate( pcap
->handle
);
51 static NTSTATUS
wrap_breakloop( void *args
)
53 struct pcap
*pcap
= args
;
54 pcap_breakloop( pcap
->handle
);
55 return STATUS_SUCCESS
;
58 static NTSTATUS
wrap_can_set_rfmon( void *args
)
60 struct pcap
*pcap
= args
;
61 return pcap_can_set_rfmon( pcap
->handle
);
64 static NTSTATUS
wrap_close( void *args
)
66 struct pcap
*pcap
= args
;
67 pcap_close( pcap
->handle
);
69 return STATUS_SUCCESS
;
72 static NTSTATUS
wrap_compile( void *args
)
74 const struct compile_params
*params
= args
;
75 return pcap_compile( params
->pcap
->handle
, params
->program
, params
->buf
, params
->optimize
, params
->mask
);
78 static NTSTATUS
wrap_create( void *args
)
80 const struct create_params
*params
= args
;
81 struct pcap
*ret
= malloc( sizeof(*ret
) );
83 if (ret
&& !(ret
->handle
= pcap_create( params
->src
, params
->errbuf
)))
89 return STATUS_SUCCESS
;
92 static NTSTATUS
wrap_datalink( void *args
)
94 struct pcap
*pcap
= args
;
95 return pcap_datalink( pcap
->handle
);
98 static NTSTATUS
wrap_datalink_name_to_val( void *args
)
100 const struct datalink_name_to_val_params
*params
= args
;
101 return pcap_datalink_name_to_val( params
->name
);
104 static NTSTATUS
wrap_datalink_val_to_description( void *args
)
106 const struct datalink_val_to_description_params
*params
= args
;
107 *params
->ret
= pcap_datalink_val_to_description( params
->link
);
108 return STATUS_SUCCESS
;
111 static NTSTATUS
wrap_datalink_val_to_name( void *args
)
113 const struct datalink_val_to_name_params
*params
= args
;
114 *params
->ret
= pcap_datalink_val_to_name( params
->link
);
115 return STATUS_SUCCESS
;
118 static NTSTATUS
wrap_dump( void *args
)
120 const struct dump_params
*params
= args
;
121 struct pcap_pkthdr hdr_unix
;
123 hdr_unix
.ts
.tv_sec
= params
->hdr
->ts
.tv_sec
;
124 hdr_unix
.ts
.tv_usec
= params
->hdr
->ts
.tv_usec
;
125 hdr_unix
.caplen
= params
->hdr
->caplen
;
126 hdr_unix
.len
= params
->hdr
->len
;
127 pcap_dump( params
->user
, &hdr_unix
, params
->packet
);
128 return STATUS_SUCCESS
;
131 static NTSTATUS
wrap_dump_open( void *args
)
133 const struct dump_open_params
*params
= args
;
134 *params
->ret
= pcap_dump_open( params
->pcap
->handle
, params
->name
);
135 return STATUS_SUCCESS
;
138 static NTSTATUS
wrap_findalldevs( void *args
)
140 const struct findalldevs_params
*params
= args
;
142 ret
= pcap_findalldevs( (pcap_if_t
**)params
->devs
, params
->errbuf
);
143 if (params
->devs
&& !*params
->devs
)
144 ERR_(winediag
)( "Failed to access raw network (pcap), this requires special permissions.\n" );
148 static NTSTATUS
wrap_free_datalinks( void *args
)
151 pcap_free_datalinks( links
);
152 return STATUS_SUCCESS
;
155 static NTSTATUS
wrap_free_tstamp_types( void *args
)
158 pcap_free_tstamp_types( types
);
159 return STATUS_SUCCESS
;
162 static NTSTATUS
wrap_freealldevs( void *args
)
164 struct pcap_interface
*devs
= args
;
165 pcap_freealldevs( (pcap_if_t
*)devs
);
166 return STATUS_SUCCESS
;
169 static NTSTATUS
wrap_freecode( void *args
)
171 void *program
= args
;
172 pcap_freecode( program
);
173 return STATUS_SUCCESS
;
176 static NTSTATUS
wrap_get_tstamp_precision( void *args
)
178 struct pcap
*pcap
= args
;
179 return pcap_get_tstamp_precision( pcap
->handle
);
182 static NTSTATUS
wrap_geterr( void *args
)
184 const struct geterr_params
*params
= args
;
185 *params
->ret
= pcap_geterr( params
->pcap
->handle
);
186 return STATUS_SUCCESS
;
189 static NTSTATUS
wrap_getnonblock( void *args
)
191 const struct getnonblock_params
*params
= args
;
192 return pcap_getnonblock( params
->pcap
->handle
, params
->errbuf
);
195 static NTSTATUS
wrap_lib_version( void *args
)
197 const struct lib_version_params
*params
= args
;
198 const char *str
= pcap_lib_version();
199 unsigned int len
= min( strlen(str
) + 1, params
->size
);
200 memcpy( params
->version
, str
, len
);
201 params
->version
[len
- 1] = 0;
202 return STATUS_SUCCESS
;
205 static NTSTATUS
wrap_list_datalinks( void *args
)
207 const struct list_datalinks_params
*params
= args
;
208 return pcap_list_datalinks( params
->pcap
->handle
, params
->buf
);
211 static NTSTATUS
wrap_list_tstamp_types( void *args
)
213 const struct list_tstamp_types_params
*params
= args
;
214 return pcap_list_tstamp_types( params
->pcap
->handle
, params
->types
);
217 static NTSTATUS
wrap_lookupnet( void *args
)
219 const struct lookupnet_params
*params
= args
;
220 return pcap_lookupnet( params
->device
, params
->net
, params
->mask
, params
->errbuf
);
223 static NTSTATUS
wrap_major_version( void *args
)
225 struct pcap
*pcap
= args
;
226 return pcap_major_version( pcap
->handle
);
229 static NTSTATUS
wrap_minor_version( void *args
)
231 struct pcap
*pcap
= args
;
232 return pcap_minor_version( pcap
->handle
);
235 static NTSTATUS
wrap_next_ex( void *args
)
237 const struct next_ex_params
*params
= args
;
238 struct pcap
*pcap
= params
->pcap
;
239 struct pcap_pkthdr
*hdr_unix
;
242 if ((ret
= pcap_next_ex( pcap
->handle
, &hdr_unix
, params
->data
)) == 1)
244 if (hdr_unix
->ts
.tv_sec
> INT_MAX
|| hdr_unix
->ts
.tv_usec
> INT_MAX
) WARN( "truncating timeval values(s)\n" );
245 pcap
->hdr
.ts
.tv_sec
= hdr_unix
->ts
.tv_sec
;
246 pcap
->hdr
.ts
.tv_usec
= hdr_unix
->ts
.tv_usec
;
247 pcap
->hdr
.caplen
= hdr_unix
->caplen
;
248 pcap
->hdr
.len
= hdr_unix
->len
;
249 *params
->hdr
= &pcap
->hdr
;
254 static NTSTATUS
wrap_open_live( void *args
)
256 const struct open_live_params
*params
= args
;
257 struct pcap
*ret
= malloc( sizeof(*ret
) );
258 if (ret
&& !(ret
->handle
= pcap_open_live( params
->source
, params
->snaplen
, params
->promisc
,
259 params
->to_ms
, params
->errbuf
)))
265 return STATUS_SUCCESS
;
268 static NTSTATUS
wrap_sendpacket( void *args
)
270 const struct sendpacket_params
*params
= args
;
271 return pcap_sendpacket( params
->pcap
->handle
, params
->buf
, params
->size
);
274 static NTSTATUS
wrap_set_buffer_size( void *args
)
276 const struct set_buffer_size_params
*params
= args
;
277 return pcap_set_buffer_size( params
->pcap
->handle
, params
->size
);
280 static NTSTATUS
wrap_set_datalink( void *args
)
282 const struct set_datalink_params
*params
= args
;
283 return pcap_set_datalink( params
->pcap
->handle
, params
->link
);
286 static NTSTATUS
wrap_set_promisc( void *args
)
288 const struct set_promisc_params
*params
= args
;
289 return pcap_set_promisc( params
->pcap
->handle
, params
->enable
);
292 static NTSTATUS
wrap_set_rfmon( void *args
)
294 const struct set_rfmon_params
*params
= args
;
295 return pcap_set_rfmon( params
->pcap
->handle
, params
->enable
);
298 static NTSTATUS
wrap_set_snaplen( void *args
)
300 const struct set_snaplen_params
*params
= args
;
301 return pcap_set_snaplen( params
->pcap
->handle
, params
->len
);
304 static NTSTATUS
wrap_set_timeout( void *args
)
306 const struct set_timeout_params
*params
= args
;
307 return pcap_set_timeout( params
->pcap
->handle
, params
->timeout
);
310 static NTSTATUS
wrap_set_tstamp_precision( void *args
)
312 const struct set_tstamp_precision_params
*params
= args
;
313 return pcap_set_tstamp_precision( params
->pcap
->handle
, params
->precision
);
316 static NTSTATUS
wrap_set_tstamp_type( void *args
)
318 const struct set_tstamp_type_params
*params
= args
;
319 return pcap_set_tstamp_type( params
->pcap
->handle
, params
->type
);
322 static NTSTATUS
wrap_setfilter( void *args
)
324 const struct setfilter_params
*params
= args
;
325 return pcap_setfilter( params
->pcap
->handle
, params
->program
);
328 static NTSTATUS
wrap_setnonblock( void *args
)
330 const struct setnonblock_params
*params
= args
;
331 return pcap_setnonblock( params
->pcap
->handle
, params
->nonblock
, params
->errbuf
);
334 static NTSTATUS
wrap_snapshot( void *args
)
336 struct pcap
*pcap
= args
;
337 return pcap_snapshot( pcap
->handle
);
340 static NTSTATUS
wrap_stats( void *args
)
342 const struct stats_params
*params
= args
;
343 return pcap_stats( params
->pcap
->handle
, params
->stats
);
346 static NTSTATUS
wrap_statustostr( void *args
)
348 const struct statustostr_params
*params
= args
;
349 *params
->ret
= pcap_statustostr( params
->status
);
350 return STATUS_SUCCESS
;
353 static NTSTATUS
wrap_tstamp_type_name_to_val( void *args
)
355 const struct tstamp_type_name_to_val_params
*params
= args
;
356 return pcap_tstamp_type_name_to_val( params
->name
);
359 static NTSTATUS
wrap_tstamp_type_val_to_description( void *args
)
361 const struct tstamp_type_val_to_description_params
*params
= args
;
362 *params
->ret
= pcap_tstamp_type_val_to_description( params
->val
);
363 return STATUS_SUCCESS
;
366 static NTSTATUS
wrap_tstamp_type_val_to_name( void *args
)
368 const struct tstamp_type_val_to_name_params
*params
= args
;
369 *params
->ret
= pcap_tstamp_type_val_to_name( params
->val
);
370 return STATUS_SUCCESS
;
373 const unixlib_entry_t __wine_unix_call_funcs
[] =
382 wrap_datalink_name_to_val
,
383 wrap_datalink_val_to_description
,
384 wrap_datalink_val_to_name
,
390 wrap_free_tstamp_types
,
393 wrap_get_tstamp_precision
,
398 wrap_list_tstamp_types
,
406 wrap_set_buffer_size
,
412 wrap_set_tstamp_precision
,
413 wrap_set_tstamp_type
,
419 wrap_tstamp_type_name_to_val
,
420 wrap_tstamp_type_val_to_description
,
421 wrap_tstamp_type_val_to_name
,
424 #endif /* HAVE_PCAP_PCAP_H */