shell32: Use shared IUnknown implementation for both vtables.
[wine/multimedia.git] / dlls / iphlpapi / icmp.c
blobfb87fe290047a7277d7aa596de1797c08cf08632
1 /*
2 * ICMP
4 * Francois Gouget, 1999, based on the work of
5 * RW Hall, 1999, based on public domain code PING.C by Mike Muus (1983)
6 * and later works (c) 1989 Regents of Univ. of California - see copyright
7 * notice at end of source-code.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /* Future work:
25 * - Systems like FreeBSD don't seem to support the IP_TTL option and maybe others.
26 * But using IP_HDRINCL and building the IP header by hand might work.
27 * - Not all IP options are supported.
28 * - Are ICMP handles real handles, i.e. inheritable and all? There might be some
29 * more work to do here, including server side stuff with synchronization.
30 * - This API should probably be thread safe. Is it really?
31 * - Using the winsock functions has not been tested.
34 #include "config.h"
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_NETDB_H
41 # include <netdb.h>
42 #endif
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 # include <netinet/in_systm.h>
45 #endif
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
48 #endif
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 #endif
53 #include <stdarg.h>
54 #include <string.h>
55 #include <errno.h>
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h>
61 #endif
62 #ifdef HAVE_SYS_POLL_H
63 # include <sys/poll.h>
64 #endif
66 #define USE_WS_PREFIX
68 #include "windef.h"
69 #include "winbase.h"
70 #include "winerror.h"
71 #include "winternl.h"
72 #include "ipexport.h"
73 #include "icmpapi.h"
74 #include "wine/debug.h"
76 /* Set up endianness macros for the ip and ip_icmp BSD headers */
77 #ifndef BIG_ENDIAN
78 #define BIG_ENDIAN 4321
79 #endif
80 #ifndef LITTLE_ENDIAN
81 #define LITTLE_ENDIAN 1234
82 #endif
83 #ifndef BYTE_ORDER
84 #ifdef WORDS_BIGENDIAN
85 #define BYTE_ORDER BIG_ENDIAN
86 #else
87 #define BYTE_ORDER LITTLE_ENDIAN
88 #endif
89 #endif /* BYTE_ORDER */
91 #define u_int16_t WORD
92 #define u_int32_t DWORD
94 /* These are BSD headers. We use these here because they are needed on
95 * libc5 Linux systems. On other platforms they are usually simply more
96 * complete than the native stuff, and cause less portability problems
97 * so we use them anyway.
99 #include "ip.h"
100 #include "ip_icmp.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(icmp);
104 WINE_DECLARE_DEBUG_CHANNEL(winediag);
107 typedef struct {
108 int sid;
109 IP_OPTION_INFORMATION default_opts;
110 } icmp_t;
112 #define IP_OPTS_UNKNOWN 0
113 #define IP_OPTS_DEFAULT 1
114 #define IP_OPTS_CUSTOM 2
116 /* The sequence number is unique process wide, so that all threads
117 * have a distinct sequence number.
119 static LONG icmp_sequence=0;
121 static int in_cksum(u_short *addr, int len)
123 int nleft=len;
124 u_short *w = addr;
125 int sum = 0;
126 u_short answer = 0;
128 while (nleft > 1) {
129 sum += *w++;
130 nleft -= 2;
133 if (nleft == 1) {
134 *(u_char *)(&answer) = *(u_char *)w;
135 sum += answer;
138 sum = (sum >> 16) + (sum & 0xffff);
139 sum += (sum >> 16);
140 answer = ~sum;
141 return(answer);
147 * Exported Routines.
150 /***********************************************************************
151 * IcmpCreateFile (IPHLPAPI.@)
153 HANDLE WINAPI IcmpCreateFile(VOID)
155 icmp_t* icp;
157 int sid=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
158 if (sid < 0)
160 /* Mac OS X supports non-privileged ICMP via SOCK_DGRAM type. */
161 sid=socket(AF_INET,SOCK_DGRAM,IPPROTO_ICMP);
163 if (sid < 0) {
164 ERR_(winediag)("Failed to use ICMP (network ping), this requires special permissions.\n");
165 SetLastError(ERROR_ACCESS_DENIED);
166 return INVALID_HANDLE_VALUE;
169 icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
170 if (icp==NULL) {
171 SetLastError(IP_NO_RESOURCES);
172 return INVALID_HANDLE_VALUE;
174 icp->sid=sid;
175 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
176 return (HANDLE)icp;
180 /***********************************************************************
181 * IcmpCloseHandle (IPHLPAPI.@)
183 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
185 icmp_t* icp=(icmp_t*)IcmpHandle;
186 if (IcmpHandle==INVALID_HANDLE_VALUE) {
187 /* FIXME: in fact win98 seems to ignore the handle value !!! */
188 SetLastError(ERROR_INVALID_HANDLE);
189 return FALSE;
192 close( icp->sid );
193 HeapFree(GetProcessHeap (), 0, icp);
194 return TRUE;
198 /***********************************************************************
199 * IcmpSendEcho (IPHLPAPI.@)
201 DWORD WINAPI IcmpSendEcho(
202 HANDLE IcmpHandle,
203 IPAddr DestinationAddress,
204 LPVOID RequestData,
205 WORD RequestSize,
206 PIP_OPTION_INFORMATION RequestOptions,
207 LPVOID ReplyBuffer,
208 DWORD ReplySize,
209 DWORD Timeout
212 icmp_t* icp=(icmp_t*)IcmpHandle;
213 unsigned char* reqbuf;
214 int reqsize;
216 struct icmp_echo_reply* ier;
217 struct ip* ip_header;
218 struct icmp* icmp_header;
219 char* endbuf;
220 int ip_header_len;
221 int maxlen;
222 struct pollfd fdr;
223 DWORD send_time,recv_time;
224 struct sockaddr_in addr;
225 unsigned int addrlen;
226 unsigned short id,seq,cksum;
227 int res;
229 if (IcmpHandle==INVALID_HANDLE_VALUE) {
230 /* FIXME: in fact win98 seems to ignore the handle value !!! */
231 SetLastError(ERROR_INVALID_HANDLE);
232 return 0;
235 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
236 SetLastError(IP_BUF_TOO_SMALL);
237 return 0;
239 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
241 /* Prepare the request */
242 id=getpid() & 0xFFFF;
243 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
245 reqsize=ICMP_MINLEN+RequestSize;
246 reqbuf=HeapAlloc(GetProcessHeap(), 0, reqsize);
247 if (reqbuf==NULL) {
248 SetLastError(ERROR_OUTOFMEMORY);
249 return 0;
252 icmp_header=(struct icmp*)reqbuf;
253 icmp_header->icmp_type=ICMP_ECHO;
254 icmp_header->icmp_code=0;
255 icmp_header->icmp_cksum=0;
256 icmp_header->icmp_id=id;
257 icmp_header->icmp_seq=seq;
258 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
259 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
261 addr.sin_family=AF_INET;
262 addr.sin_addr.s_addr=DestinationAddress;
263 addr.sin_port=0;
265 if (RequestOptions!=NULL) {
266 int val;
267 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
268 unsigned int len;
269 /* Before we mess with the options, get the default values */
270 len=sizeof(val);
271 getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
272 icp->default_opts.Ttl=val;
274 len=sizeof(val);
275 getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
276 icp->default_opts.Tos=val;
277 /* FIXME: missing: handling of IP 'flags', and all the other options */
280 val=RequestOptions->Ttl;
281 setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
282 val=RequestOptions->Tos;
283 setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
284 /* FIXME: missing: handling of IP 'flags', and all the other options */
286 icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
287 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
288 int val;
290 /* Restore the default options */
291 val=icp->default_opts.Ttl;
292 setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
293 val=icp->default_opts.Tos;
294 setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
295 /* FIXME: missing: handling of IP 'flags', and all the other options */
297 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
300 /* Get ready for receiving the reply
301 * Do it before we send the request to minimize the risk of introducing delays
303 fdr.fd = icp->sid;
304 fdr.events = POLLIN;
305 addrlen=sizeof(addr);
306 ier=ReplyBuffer;
307 ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
308 endbuf=(char *) ReplyBuffer+ReplySize;
309 maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
311 /* Send the packet */
312 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
313 #if 0
314 if (TRACE_ON(icmp)){
315 unsigned char* buf=(unsigned char*)reqbuf;
316 int i;
317 printf("Output buffer:\n");
318 for (i=0;i<reqsize;i++)
319 printf("%2x,", buf[i]);
320 printf("\n");
322 #endif
324 send_time = GetTickCount();
325 res=sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
326 HeapFree(GetProcessHeap (), 0, reqbuf);
327 if (res<0) {
328 if (errno==EMSGSIZE)
329 SetLastError(IP_PACKET_TOO_BIG);
330 else {
331 switch (errno) {
332 case ENETUNREACH:
333 SetLastError(IP_DEST_NET_UNREACHABLE);
334 break;
335 case EHOSTUNREACH:
336 SetLastError(IP_DEST_HOST_UNREACHABLE);
337 break;
338 default:
339 TRACE("unknown error: errno=%d\n",errno);
340 SetLastError(IP_GENERAL_FAILURE);
343 return 0;
346 /* Get the reply */
347 ip_header_len=0; /* because gcc was complaining */
348 while (poll(&fdr,1,Timeout)>0) {
349 recv_time = GetTickCount();
350 res=recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
351 TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
352 ier->Status=IP_REQ_TIMED_OUT;
354 /* Check whether we should ignore this packet */
355 if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
356 ip_header_len=ip_header->ip_hl << 2;
357 icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
358 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
359 if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
360 if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
361 ier->Status=IP_SUCCESS;
362 } else {
363 switch (icmp_header->icmp_type) {
364 case ICMP_UNREACH:
365 switch (icmp_header->icmp_code) {
366 case ICMP_UNREACH_HOST:
367 #ifdef ICMP_UNREACH_HOST_UNKNOWN
368 case ICMP_UNREACH_HOST_UNKNOWN:
369 #endif
370 #ifdef ICMP_UNREACH_ISOLATED
371 case ICMP_UNREACH_ISOLATED:
372 #endif
373 #ifdef ICMP_UNREACH_HOST_PROHIB
374 case ICMP_UNREACH_HOST_PROHIB:
375 #endif
376 #ifdef ICMP_UNREACH_TOSHOST
377 case ICMP_UNREACH_TOSHOST:
378 #endif
379 ier->Status=IP_DEST_HOST_UNREACHABLE;
380 break;
381 case ICMP_UNREACH_PORT:
382 ier->Status=IP_DEST_PORT_UNREACHABLE;
383 break;
384 case ICMP_UNREACH_PROTOCOL:
385 ier->Status=IP_DEST_PROT_UNREACHABLE;
386 break;
387 case ICMP_UNREACH_SRCFAIL:
388 ier->Status=IP_BAD_ROUTE;
389 break;
390 default:
391 ier->Status=IP_DEST_NET_UNREACHABLE;
393 break;
394 case ICMP_TIMXCEED:
395 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
396 ier->Status=IP_TTL_EXPIRED_REASSEM;
397 else
398 ier->Status=IP_TTL_EXPIRED_TRANSIT;
399 break;
400 case ICMP_PARAMPROB:
401 ier->Status=IP_PARAM_PROBLEM;
402 break;
403 case ICMP_SOURCEQUENCH:
404 ier->Status=IP_SOURCE_QUENCH;
405 break;
407 if (ier->Status!=IP_REQ_TIMED_OUT) {
408 struct ip* rep_ip_header;
409 struct icmp* rep_icmp_header;
410 /* The ICMP header size of all the packets we accept is the same */
411 rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
412 rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
414 /* Make sure that this is really a reply to our packet */
415 if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
416 ier->Status=IP_REQ_TIMED_OUT;
417 } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
418 (rep_icmp_header->icmp_code!=0) ||
419 (rep_icmp_header->icmp_id!=id) ||
420 /* windows doesn't check this checksum, else tracert */
421 /* behind a Linux 2.2 masquerading firewall would fail*/
422 /* (rep_icmp_header->icmp_cksum!=cksum) || */
423 (rep_icmp_header->icmp_seq!=seq)) {
424 /* This was not a reply to one of our packets after all */
425 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
426 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
427 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
428 rep_icmp_header->icmp_cksum);
429 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
430 id,seq,
431 cksum);
432 ier->Status=IP_REQ_TIMED_OUT;
438 if (ier->Status==IP_REQ_TIMED_OUT) {
439 /* This packet was not for us.
440 * Decrease the timeout so that we don't enter an endless loop even
441 * if we get flooded with ICMP packets that are not for us.
443 DWORD t = (recv_time - send_time);
444 if (Timeout > t) Timeout -= t;
445 else Timeout = 0;
446 continue;
447 } else {
448 /* This is a reply to our packet */
449 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
450 /* Status is already set */
451 ier->RoundTripTime= recv_time - send_time;
452 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
453 ier->Reserved=0;
454 ier->Data=endbuf-ier->DataSize;
455 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
456 ier->Options.Ttl=ip_header->ip_ttl;
457 ier->Options.Tos=ip_header->ip_tos;
458 ier->Options.Flags=ip_header->ip_off >> 13;
459 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
460 if (ier->Options.OptionsSize!=0) {
461 ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
462 /* FIXME: We are supposed to rearrange the option's 'source route' data */
463 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
464 endbuf=(char*)ier->Options.OptionsData;
465 } else {
466 ier->Options.OptionsData=NULL;
467 endbuf=ier->Data;
470 /* Prepare for the next packet */
471 ier++;
472 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
473 maxlen=endbuf-(char*)ip_header;
475 /* Check out whether there is more but don't wait this time */
476 Timeout=0;
479 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
480 if (res==0)
481 SetLastError(IP_REQ_TIMED_OUT);
482 TRACE("received %d replies\n",res);
483 return res;
486 /***********************************************************************
487 * IcmpSendEcho2 (IPHLPAPI.@)
489 DWORD WINAPI IcmpSendEcho2(
490 HANDLE IcmpHandle,
491 HANDLE Event,
492 PIO_APC_ROUTINE ApcRoutine,
493 PVOID ApcContext,
494 IPAddr DestinationAddress,
495 LPVOID RequestData,
496 WORD RequestSize,
497 PIP_OPTION_INFORMATION RequestOptions,
498 LPVOID ReplyBuffer,
499 DWORD ReplySize,
500 DWORD Timeout
503 TRACE("(%p, %p, %p, %p, %08x, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle,
504 Event, ApcRoutine, ApcContext, DestinationAddress, RequestData,
505 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
507 if (Event)
509 FIXME("unsupported for events\n");
510 return 0;
512 if (ApcRoutine)
514 FIXME("unsupported for APCs\n");
515 return 0;
517 return IcmpSendEcho(IcmpHandle, DestinationAddress, RequestData,
518 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
521 /***********************************************************************
522 * IcmpSendEcho2Ex (IPHLPAPI.@)
524 DWORD WINAPI IcmpSendEcho2Ex(
525 HANDLE IcmpHandle,
526 HANDLE Event,
527 PIO_APC_ROUTINE ApcRoutine,
528 PVOID ApcContext,
529 IPAddr SourceAddress,
530 IPAddr DestinationAddress,
531 LPVOID RequestData,
532 WORD RequestSize,
533 PIP_OPTION_INFORMATION RequestOptions,
534 LPVOID ReplyBuffer,
535 DWORD ReplySize,
536 DWORD Timeout
539 TRACE("(%p, %p, %p, %p, %08x, %08x, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle,
540 Event, ApcRoutine, ApcContext, SourceAddress, DestinationAddress, RequestData,
541 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
543 if (Event)
545 FIXME("unsupported for events\n");
546 return 0;
548 if (ApcRoutine)
550 FIXME("unsupported for APCs\n");
551 return 0;
553 if (SourceAddress)
555 FIXME("unsupported for source addresses\n");
556 return 0;
559 return IcmpSendEcho(IcmpHandle, DestinationAddress, RequestData,
560 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
564 * Copyright (c) 1989 The Regents of the University of California.
565 * All rights reserved.
567 * This code is derived from software contributed to Berkeley by
568 * Mike Muuss.
570 * Redistribution and use in source and binary forms, with or without
571 * modification, are permitted provided that the following conditions
572 * are met:
573 * 1. Redistributions of source code must retain the above copyright
574 * notice, this list of conditions and the following disclaimer.
575 * 2. Redistributions in binary form must reproduce the above copyright
576 * notice, this list of conditions and the following disclaimer in the
577 * documentation and/or other materials provided with the distribution.
578 * 3. Neither the name of the University nor the names of its contributors
579 * may be used to endorse or promote products derived from this software
580 * without specific prior written permission.
582 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
583 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
584 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
585 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
586 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
587 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
588 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
589 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
590 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
591 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
592 * SUCH DAMAGE.