mpr: Implement WNetClearConnections().
[wine.git] / dlls / wpcap / wpcap.c
blobe7908cd038a36a6e4f68999dbb82593212cf6182
1 /*
2 * WPcap.dll Proxy.
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: */
24 #undef SOCKET
25 #undef INVALID_SOCKET
27 #include "winsock2.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/heap.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wpcap);
34 WINE_DECLARE_DEBUG_CHANNEL(winediag);
36 #ifndef PCAP_SRC_FILE_STRING
37 #define PCAP_SRC_FILE_STRING "file://"
38 #endif
39 #ifndef PCAP_SRC_FILE
40 #define PCAP_SRC_FILE 2
41 #endif
42 #ifndef PCAP_SRC_IF_STRING
43 #define PCAP_SRC_IF_STRING "rpcap://"
44 #endif
45 #ifndef PCAP_SRC_IFLOCAL
46 #define PCAP_SRC_IFLOCAL 3
47 #endif
49 static inline WCHAR *heap_strdupAtoW(const char *str)
51 LPWSTR ret = NULL;
53 if(str) {
54 DWORD len;
56 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
57 ret = heap_alloc(len*sizeof(WCHAR));
58 if(ret)
59 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
62 return ret;
65 void CDECL wine_pcap_breakloop(pcap_t *p)
67 TRACE("(%p)\n", p);
68 return pcap_breakloop(p);
71 void CDECL wine_pcap_close(pcap_t *p)
73 TRACE("(%p)\n", p);
74 pcap_close(p);
77 int CDECL wine_pcap_compile(pcap_t *p, struct bpf_program *program, const char *buf, int optimize,
78 unsigned int mask)
80 TRACE("(%p %p %s %i %u)\n", p, program, debugstr_a(buf), optimize, mask);
81 return pcap_compile(p, program, buf, optimize, mask);
84 int CDECL wine_pcap_datalink(pcap_t *p)
86 TRACE("(%p)\n", p);
87 return pcap_datalink(p);
90 int CDECL wine_pcap_datalink_name_to_val(const char *name)
92 TRACE("(%s)\n", debugstr_a(name));
93 return pcap_datalink_name_to_val(name);
96 const char* CDECL wine_pcap_datalink_val_to_description(int dlt)
98 TRACE("(%i)\n", dlt);
99 return pcap_datalink_val_to_description(dlt);
102 const char* CDECL wine_pcap_datalink_val_to_name(int dlt)
104 TRACE("(%i)\n", dlt);
105 return pcap_datalink_val_to_name(dlt);
108 typedef struct
110 void (CALLBACK *pfn_cb)(u_char *, const struct pcap_pkthdr *, const u_char *);
111 void *user_data;
112 } PCAP_HANDLER_CALLBACK;
114 static void pcap_handler_callback(u_char *user_data, const struct pcap_pkthdr *h, const u_char *p)
116 PCAP_HANDLER_CALLBACK *pcb;
117 TRACE("(%p %p %p)\n", user_data, h, p);
118 pcb = (PCAP_HANDLER_CALLBACK *)user_data;
119 pcb->pfn_cb(pcb->user_data, h, p);
120 TRACE("Callback COMPLETED\n");
123 int CDECL wine_pcap_dispatch(pcap_t *p, int cnt,
124 void (CALLBACK *callback)(u_char *, const struct pcap_pkthdr *, const u_char *),
125 unsigned char *user)
127 TRACE("(%p %i %p %p)\n", p, cnt, callback, user);
129 if (callback)
131 PCAP_HANDLER_CALLBACK pcb;
132 pcb.pfn_cb = callback;
133 pcb.user_data = user;
134 return pcap_dispatch(p, cnt, pcap_handler_callback, (unsigned char *)&pcb);
137 return pcap_dispatch(p, cnt, NULL, user);
140 int CDECL wine_pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
142 int ret;
144 TRACE("(%p %p)\n", alldevsp, errbuf);
145 ret = pcap_findalldevs(alldevsp, errbuf);
146 if(alldevsp && !*alldevsp)
147 ERR_(winediag)("Failed to access raw network (pcap), this requires special permissions.\n");
149 return ret;
152 int CDECL wine_pcap_findalldevs_ex(char *source, void *auth, pcap_if_t **alldevs, char *errbuf)
154 FIXME("(%s %p %p %p): partial stub\n", debugstr_a(source), auth, alldevs, errbuf);
155 return wine_pcap_findalldevs(alldevs, errbuf);
158 void CDECL wine_pcap_freealldevs(pcap_if_t *alldevs)
160 TRACE("(%p)\n", alldevs);
161 pcap_freealldevs(alldevs);
164 void CDECL wine_pcap_freecode(struct bpf_program *fp)
166 TRACE("(%p)\n", fp);
167 return pcap_freecode(fp);
170 typedef struct _AirpcapHandle *PAirpcapHandle;
171 PAirpcapHandle CDECL wine_pcap_get_airpcap_handle(pcap_t *p)
173 TRACE("(%p)\n", p);
174 return NULL;
177 char* CDECL wine_pcap_geterr(pcap_t *p)
179 TRACE("(%p)\n", p);
180 return pcap_geterr(p);
183 int CDECL wine_pcap_getnonblock(pcap_t *p, char *errbuf)
185 TRACE("(%p %p)\n", p, errbuf);
186 return pcap_getnonblock(p, errbuf);
189 const char* CDECL wine_pcap_lib_version(void)
191 const char* ret = pcap_lib_version();
192 TRACE("%s\n", debugstr_a(ret));
193 return ret;
196 int CDECL wine_pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
198 TRACE("(%p %p)\n", p, dlt_buffer);
199 return pcap_list_datalinks(p, dlt_buffer);
202 char* CDECL wine_pcap_lookupdev(char *errbuf)
204 TRACE("(%p)\n", errbuf);
205 return pcap_lookupdev(errbuf);
208 int CDECL wine_pcap_lookupnet(const char *device, unsigned int *netp, unsigned int *maskp,
209 char *errbuf)
211 TRACE("(%s %p %p %p)\n", debugstr_a(device), netp, maskp, errbuf);
212 return pcap_lookupnet(device, netp, maskp, errbuf);
215 int CDECL wine_pcap_loop(pcap_t *p, int cnt,
216 void (CALLBACK *callback)(u_char *, const struct pcap_pkthdr *, const u_char *),
217 unsigned char *user)
219 TRACE("(%p %i %p %p)\n", p, cnt, callback, user);
221 if (callback)
223 PCAP_HANDLER_CALLBACK pcb;
224 pcb.pfn_cb = callback;
225 pcb.user_data = user;
226 return pcap_loop(p, cnt, pcap_handler_callback, (unsigned char *)&pcb);
229 return pcap_loop(p, cnt, NULL, user);
232 int CDECL wine_pcap_major_version(pcap_t *p)
234 TRACE("(%p)\n", p);
235 return pcap_major_version(p);
238 int CDECL wine_pcap_minor_version(pcap_t *p)
240 TRACE("(%p)\n", p);
241 return pcap_minor_version(p);
244 const unsigned char* CDECL wine_pcap_next(pcap_t *p, struct pcap_pkthdr *h)
246 TRACE("(%p %p)\n", p, h);
247 return pcap_next(p, h);
250 int CDECL wine_pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, const unsigned char **pkt_data)
252 TRACE("(%p %p %p)\n", p, pkt_header, pkt_data);
253 return pcap_next_ex(p, pkt_header, pkt_data);
256 #define PCAP_OPENFLAG_PROMISCUOUS 1
258 pcap_t* CDECL wine_pcap_open(const char *source, int snaplen, int flags, int read_timeout,
259 void *auth, char *errbuf)
261 int promisc = flags & PCAP_OPENFLAG_PROMISCUOUS;
262 FIXME("(%s %i %i %i %p %p): partial stub\n", debugstr_a(source), snaplen, flags, read_timeout,
263 auth, errbuf);
264 return pcap_open_live(source, snaplen, promisc, read_timeout, errbuf);
267 pcap_t* CDECL wine_pcap_open_live(const char *source, int snaplen, int promisc, int to_ms,
268 char *errbuf)
270 TRACE("(%s %i %i %i %p)\n", debugstr_a(source), snaplen, promisc, to_ms, errbuf);
271 return pcap_open_live(source, snaplen, promisc, to_ms, errbuf);
274 int CDECL wine_pcap_parsesrcstr(const char *source, int *type, char *host, char *port, char *name, char *errbuf)
276 int t = PCAP_SRC_IFLOCAL;
277 const char *p = source;
279 FIXME("(%s %p %p %p %p %p): partial stub\n", debugstr_a(source), type, host, port, name, errbuf);
281 if (host)
282 *host = '\0';
283 if (port)
284 *port = '\0';
285 if (name)
286 *name = '\0';
288 if (!strncmp(p, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)))
289 p += strlen(PCAP_SRC_IF_STRING);
290 else if (!strncmp(p, PCAP_SRC_FILE_STRING, strlen(PCAP_SRC_FILE_STRING)))
292 p += strlen(PCAP_SRC_FILE_STRING);
293 t = PCAP_SRC_FILE;
296 if (type)
297 *type = t;
299 if (!*p)
301 if (errbuf)
302 sprintf(errbuf, "The name has not been specified in the source string.");
303 return -1;
306 if (name)
307 strcpy(name, p);
309 return 0;
312 int CDECL wine_pcap_sendpacket(pcap_t *p, const unsigned char *buf, int size)
314 TRACE("(%p %p %i)\n", p, buf, size);
315 return pcap_sendpacket(p, buf, size);
318 int CDECL wine_pcap_set_datalink(pcap_t *p, int dlt)
320 TRACE("(%p %i)\n", p, dlt);
321 return pcap_set_datalink(p, dlt);
324 int CDECL wine_pcap_setbuff(pcap_t * p, int dim)
326 FIXME("(%p %i) stub\n", p, dim);
327 return 0;
330 int CDECL wine_pcap_setfilter(pcap_t *p, struct bpf_program *fp)
332 TRACE("(%p %p)\n", p, fp);
333 return pcap_setfilter(p, fp);
336 int CDECL wine_pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
338 TRACE("(%p %i %p)\n", p, nonblock, errbuf);
339 return pcap_setnonblock(p, nonblock, errbuf);
342 int CDECL wine_pcap_snapshot(pcap_t *p)
344 TRACE("(%p)\n", p);
345 return pcap_snapshot(p);
348 int CDECL wine_pcap_stats(pcap_t *p, struct pcap_stat *ps)
350 TRACE("(%p %p)\n", p, ps);
351 return pcap_stats(p, ps);
354 int CDECL wine_wsockinit(void)
356 WSADATA wsadata;
357 TRACE("()\n");
358 if (WSAStartup(MAKEWORD(1,1), &wsadata)) return -1;
359 return 0;
362 pcap_dumper_t* CDECL wine_pcap_dump_open(pcap_t *p, const char *fname)
364 pcap_dumper_t *dumper;
365 WCHAR *fnameW = heap_strdupAtoW(fname);
366 char *unix_path;
368 TRACE("(%p %s)\n", p, debugstr_a(fname));
370 unix_path = wine_get_unix_file_name(fnameW);
371 heap_free(fnameW);
372 if(!unix_path)
373 return NULL;
375 TRACE("unix_path %s\n", debugstr_a(unix_path));
377 dumper = pcap_dump_open(p, unix_path);
378 heap_free(unix_path);
380 return dumper;
383 void CDECL wine_pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
385 TRACE("(%p %p %p)\n", user, h, sp);
386 return pcap_dump(user, h, sp);