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
21 #include <pcap/pcap.h>
23 /* pcap.h might define those: */
31 #include "wine/heap.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wpcap
);
35 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
37 #ifndef PCAP_SRC_FILE_STRING
38 #define PCAP_SRC_FILE_STRING "file://"
41 #define PCAP_SRC_FILE 2
43 #ifndef PCAP_SRC_IF_STRING
44 #define PCAP_SRC_IF_STRING "rpcap://"
46 #ifndef PCAP_SRC_IFLOCAL
47 #define PCAP_SRC_IFLOCAL 3
50 static inline WCHAR
*heap_strdupAtoW(const char *str
)
57 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
58 ret
= heap_alloc(len
*sizeof(WCHAR
));
60 MultiByteToWideChar(CP_ACP
, 0, str
, -1, ret
, len
);
66 void CDECL
wine_pcap_breakloop(pcap_t
*p
)
69 return pcap_breakloop(p
);
72 void CDECL
wine_pcap_close(pcap_t
*p
)
78 int CDECL
wine_pcap_compile(pcap_t
*p
, struct bpf_program
*program
, const char *buf
, int optimize
,
81 TRACE("(%p %p %s %i %u)\n", p
, program
, debugstr_a(buf
), optimize
, mask
);
82 return pcap_compile(p
, program
, buf
, optimize
, mask
);
85 int CDECL
wine_pcap_datalink(pcap_t
*p
)
88 return pcap_datalink(p
);
91 int CDECL
wine_pcap_datalink_name_to_val(const char *name
)
93 TRACE("(%s)\n", debugstr_a(name
));
94 return pcap_datalink_name_to_val(name
);
97 const char* CDECL
wine_pcap_datalink_val_to_description(int dlt
)
100 return pcap_datalink_val_to_description(dlt
);
103 const char* CDECL
wine_pcap_datalink_val_to_name(int dlt
)
105 TRACE("(%i)\n", dlt
);
106 return pcap_datalink_val_to_name(dlt
);
111 void (CALLBACK
*pfn_cb
)(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
113 } PCAP_HANDLER_CALLBACK
;
115 static void pcap_handler_callback(u_char
*user_data
, const struct pcap_pkthdr
*h
, const u_char
*p
)
117 PCAP_HANDLER_CALLBACK
*pcb
;
118 TRACE("(%p %p %p)\n", user_data
, h
, p
);
119 pcb
= (PCAP_HANDLER_CALLBACK
*)user_data
;
120 pcb
->pfn_cb(pcb
->user_data
, h
, p
);
121 TRACE("Callback COMPLETED\n");
124 int CDECL
wine_pcap_dispatch(pcap_t
*p
, int cnt
,
125 void (CALLBACK
*callback
)(u_char
*, const struct pcap_pkthdr
*, const u_char
*),
128 TRACE("(%p %i %p %p)\n", p
, cnt
, callback
, user
);
132 PCAP_HANDLER_CALLBACK pcb
;
133 pcb
.pfn_cb
= callback
;
134 pcb
.user_data
= user
;
135 return pcap_dispatch(p
, cnt
, pcap_handler_callback
, (unsigned char *)&pcb
);
138 return pcap_dispatch(p
, cnt
, NULL
, user
);
141 int CDECL
wine_pcap_findalldevs(pcap_if_t
**alldevsp
, char *errbuf
)
145 TRACE("(%p %p)\n", alldevsp
, errbuf
);
146 ret
= pcap_findalldevs(alldevsp
, errbuf
);
147 if(alldevsp
&& !*alldevsp
)
148 ERR_(winediag
)("Failed to access raw network (pcap), this requires special permissions.\n");
153 int CDECL
wine_pcap_findalldevs_ex(char *source
, void *auth
, pcap_if_t
**alldevs
, char *errbuf
)
155 FIXME("(%s %p %p %p): partial stub\n", debugstr_a(source
), auth
, alldevs
, errbuf
);
156 return wine_pcap_findalldevs(alldevs
, errbuf
);
159 void CDECL
wine_pcap_freealldevs(pcap_if_t
*alldevs
)
161 TRACE("(%p)\n", alldevs
);
162 pcap_freealldevs(alldevs
);
165 void CDECL
wine_pcap_freecode(struct bpf_program
*fp
)
168 return pcap_freecode(fp
);
171 typedef struct _AirpcapHandle
*PAirpcapHandle
;
172 PAirpcapHandle CDECL
wine_pcap_get_airpcap_handle(pcap_t
*p
)
178 char* CDECL
wine_pcap_geterr(pcap_t
*p
)
181 return pcap_geterr(p
);
184 int CDECL
wine_pcap_getnonblock(pcap_t
*p
, char *errbuf
)
186 TRACE("(%p %p)\n", p
, errbuf
);
187 return pcap_getnonblock(p
, errbuf
);
190 const char* CDECL
wine_pcap_lib_version(void)
192 const char* ret
= pcap_lib_version();
193 TRACE("%s\n", debugstr_a(ret
));
197 int CDECL
wine_pcap_list_datalinks(pcap_t
*p
, int **dlt_buffer
)
199 TRACE("(%p %p)\n", p
, dlt_buffer
);
200 return pcap_list_datalinks(p
, dlt_buffer
);
203 char* CDECL
wine_pcap_lookupdev(char *errbuf
)
208 TRACE("(%p)\n", errbuf
);
211 if (pcap_findalldevs( &devs
, errbuf
) == -1) return NULL
;
212 if (!devs
) return NULL
;
213 if ((ret
= heap_alloc( strlen(devs
->name
) + 1 ))) strcpy( ret
, devs
->name
);
214 pcap_freealldevs( devs
);
219 int CDECL
wine_pcap_lookupnet(const char *device
, unsigned int *netp
, unsigned int *maskp
,
222 TRACE("(%s %p %p %p)\n", debugstr_a(device
), netp
, maskp
, errbuf
);
223 return pcap_lookupnet(device
, netp
, maskp
, errbuf
);
226 int CDECL
wine_pcap_loop(pcap_t
*p
, int cnt
,
227 void (CALLBACK
*callback
)(u_char
*, const struct pcap_pkthdr
*, const u_char
*),
230 TRACE("(%p %i %p %p)\n", p
, cnt
, callback
, user
);
234 PCAP_HANDLER_CALLBACK pcb
;
235 pcb
.pfn_cb
= callback
;
236 pcb
.user_data
= user
;
237 return pcap_loop(p
, cnt
, pcap_handler_callback
, (unsigned char *)&pcb
);
240 return pcap_loop(p
, cnt
, NULL
, user
);
243 int CDECL
wine_pcap_major_version(pcap_t
*p
)
246 return pcap_major_version(p
);
249 int CDECL
wine_pcap_minor_version(pcap_t
*p
)
252 return pcap_minor_version(p
);
255 const unsigned char* CDECL
wine_pcap_next(pcap_t
*p
, struct pcap_pkthdr
*h
)
257 TRACE("(%p %p)\n", p
, h
);
258 return pcap_next(p
, h
);
261 int CDECL
wine_pcap_next_ex(pcap_t
*p
, struct pcap_pkthdr
**pkt_header
, const unsigned char **pkt_data
)
263 TRACE("(%p %p %p)\n", p
, pkt_header
, pkt_data
);
264 return pcap_next_ex(p
, pkt_header
, pkt_data
);
267 #ifndef PCAP_OPENFLAG_PROMISCUOUS
268 #define PCAP_OPENFLAG_PROMISCUOUS 1
271 pcap_t
* CDECL
wine_pcap_open(const char *source
, int snaplen
, int flags
, int read_timeout
,
272 void *auth
, char *errbuf
)
274 int promisc
= flags
& PCAP_OPENFLAG_PROMISCUOUS
;
275 FIXME("(%s %i %i %i %p %p): partial stub\n", debugstr_a(source
), snaplen
, flags
, read_timeout
,
277 return pcap_open_live(source
, snaplen
, promisc
, read_timeout
, errbuf
);
280 pcap_t
* CDECL
wine_pcap_open_live(const char *source
, int snaplen
, int promisc
, int to_ms
,
283 TRACE("(%s %i %i %i %p)\n", debugstr_a(source
), snaplen
, promisc
, to_ms
, errbuf
);
284 return pcap_open_live(source
, snaplen
, promisc
, to_ms
, errbuf
);
287 int CDECL
wine_pcap_parsesrcstr(const char *source
, int *type
, char *host
, char *port
, char *name
, char *errbuf
)
289 int t
= PCAP_SRC_IFLOCAL
;
290 const char *p
= source
;
292 FIXME("(%s %p %p %p %p %p): partial stub\n", debugstr_a(source
), type
, host
, port
, name
, errbuf
);
301 if (!strncmp(p
, PCAP_SRC_IF_STRING
, strlen(PCAP_SRC_IF_STRING
)))
302 p
+= strlen(PCAP_SRC_IF_STRING
);
303 else if (!strncmp(p
, PCAP_SRC_FILE_STRING
, strlen(PCAP_SRC_FILE_STRING
)))
305 p
+= strlen(PCAP_SRC_FILE_STRING
);
315 sprintf(errbuf
, "The name has not been specified in the source string.");
325 int CDECL
wine_pcap_sendpacket(pcap_t
*p
, const unsigned char *buf
, int size
)
327 TRACE("(%p %p %i)\n", p
, buf
, size
);
328 return pcap_sendpacket(p
, buf
, size
);
331 int CDECL
wine_pcap_set_datalink(pcap_t
*p
, int dlt
)
333 TRACE("(%p %i)\n", p
, dlt
);
334 return pcap_set_datalink(p
, dlt
);
337 int CDECL
wine_pcap_setbuff(pcap_t
* p
, int dim
)
339 FIXME("(%p %i) stub\n", p
, dim
);
343 int CDECL
wine_pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
345 TRACE("(%p %p)\n", p
, fp
);
346 return pcap_setfilter(p
, fp
);
349 int CDECL
wine_pcap_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
351 TRACE("(%p %i %p)\n", p
, nonblock
, errbuf
);
352 return pcap_setnonblock(p
, nonblock
, errbuf
);
355 int CDECL
wine_pcap_snapshot(pcap_t
*p
)
358 return pcap_snapshot(p
);
361 int CDECL
wine_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
363 TRACE("(%p %p)\n", p
, ps
);
364 return pcap_stats(p
, ps
);
367 int CDECL
wine_wsockinit(void)
371 if (WSAStartup(MAKEWORD(1,1), &wsadata
)) return -1;
375 pcap_dumper_t
* CDECL
wine_pcap_dump_open(pcap_t
*p
, const char *fname
)
377 pcap_dumper_t
*dumper
;
378 WCHAR
*fnameW
= heap_strdupAtoW(fname
);
381 TRACE("(%p %s)\n", p
, debugstr_a(fname
));
383 unix_path
= wine_get_unix_file_name(fnameW
);
388 TRACE("unix_path %s\n", debugstr_a(unix_path
));
390 dumper
= pcap_dump_open(p
, unix_path
);
391 heap_free(unix_path
);
396 void CDECL
wine_pcap_dump(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
398 TRACE("(%p %p %p)\n", user
, h
, sp
);
399 return pcap_dump(user
, h
, sp
);