t2embed: Add stub dll.
[wine/multimedia.git] / dlls / iphlpapi / icmp.c
blobca965fb0a5b17bf8dc0fef24134b0102d5591977
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 #include "windef.h"
67 #include "winbase.h"
68 #include "winerror.h"
69 #include "winternl.h"
70 #include "ipexport.h"
71 #include "icmpapi.h"
72 #include "wine/debug.h"
74 /* Set up endianness macros for the ip and ip_icmp BSD headers */
75 #ifndef BIG_ENDIAN
76 #define BIG_ENDIAN 4321
77 #endif
78 #ifndef LITTLE_ENDIAN
79 #define LITTLE_ENDIAN 1234
80 #endif
81 #ifndef BYTE_ORDER
82 #ifdef WORDS_BIGENDIAN
83 #define BYTE_ORDER BIG_ENDIAN
84 #else
85 #define BYTE_ORDER LITTLE_ENDIAN
86 #endif
87 #endif /* BYTE_ORDER */
89 #define u_int16_t WORD
90 #define u_int32_t DWORD
92 /* These are BSD headers. We use these here because they are needed on
93 * libc5 Linux systems. On other platforms they are usually simply more
94 * complete than the native stuff, and cause less portability problems
95 * so we use them anyway.
97 #include "ip.h"
98 #include "ip_icmp.h"
101 WINE_DEFAULT_DEBUG_CHANNEL(icmp);
104 typedef struct {
105 int sid;
106 IP_OPTION_INFORMATION default_opts;
107 } icmp_t;
109 #define IP_OPTS_UNKNOWN 0
110 #define IP_OPTS_DEFAULT 1
111 #define IP_OPTS_CUSTOM 2
113 /* The sequence number is unique process wide, so that all threads
114 * have a distinct sequence number.
116 static LONG icmp_sequence=0;
118 static int in_cksum(u_short *addr, int len)
120 int nleft=len;
121 u_short *w = addr;
122 int sum = 0;
123 u_short answer = 0;
125 while (nleft > 1) {
126 sum += *w++;
127 nleft -= 2;
130 if (nleft == 1) {
131 *(u_char *)(&answer) = *(u_char *)w;
132 sum += answer;
135 sum = (sum >> 16) + (sum & 0xffff);
136 sum += (sum >> 16);
137 answer = ~sum;
138 return(answer);
144 * Exported Routines.
147 /***********************************************************************
148 * IcmpCreateFile (IPHLPAPI.@)
150 HANDLE WINAPI IcmpCreateFile(VOID)
152 icmp_t* icp;
154 int sid=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
155 if (sid < 0) {
156 MESSAGE("WARNING: Trying to use ICMP (network ping) will fail unless running as root\n");
157 SetLastError(ERROR_ACCESS_DENIED);
158 return INVALID_HANDLE_VALUE;
161 icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
162 if (icp==NULL) {
163 SetLastError(IP_NO_RESOURCES);
164 return INVALID_HANDLE_VALUE;
166 icp->sid=sid;
167 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
168 return (HANDLE)icp;
172 /***********************************************************************
173 * IcmpCloseHandle (IPHLPAPI.@)
175 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
177 icmp_t* icp=(icmp_t*)IcmpHandle;
178 if (IcmpHandle==INVALID_HANDLE_VALUE) {
179 /* FIXME: in fact win98 seems to ignore the handle value !!! */
180 SetLastError(ERROR_INVALID_HANDLE);
181 return FALSE;
184 shutdown(icp->sid,2);
185 HeapFree(GetProcessHeap (), 0, icp);
186 return TRUE;
190 /***********************************************************************
191 * IcmpSendEcho (IPHLPAPI.@)
193 DWORD WINAPI IcmpSendEcho(
194 HANDLE IcmpHandle,
195 IPAddr DestinationAddress,
196 LPVOID RequestData,
197 WORD RequestSize,
198 PIP_OPTION_INFORMATION RequestOptions,
199 LPVOID ReplyBuffer,
200 DWORD ReplySize,
201 DWORD Timeout
204 icmp_t* icp=(icmp_t*)IcmpHandle;
205 unsigned char* reqbuf;
206 int reqsize;
208 struct icmp_echo_reply* ier;
209 struct ip* ip_header;
210 struct icmp* icmp_header;
211 char* endbuf;
212 int ip_header_len;
213 int maxlen;
214 struct pollfd fdr;
215 DWORD send_time,recv_time;
216 struct sockaddr_in addr;
217 unsigned int addrlen;
218 unsigned short id,seq,cksum;
219 int res;
221 if (IcmpHandle==INVALID_HANDLE_VALUE) {
222 /* FIXME: in fact win98 seems to ignore the handle value !!! */
223 SetLastError(ERROR_INVALID_HANDLE);
224 return 0;
227 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
228 SetLastError(IP_BUF_TOO_SMALL);
229 return 0;
231 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
233 /* Prepare the request */
234 id=getpid() & 0xFFFF;
235 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
237 reqsize=ICMP_MINLEN+RequestSize;
238 reqbuf=HeapAlloc(GetProcessHeap(), 0, reqsize);
239 if (reqbuf==NULL) {
240 SetLastError(ERROR_OUTOFMEMORY);
241 return 0;
244 icmp_header=(struct icmp*)reqbuf;
245 icmp_header->icmp_type=ICMP_ECHO;
246 icmp_header->icmp_code=0;
247 icmp_header->icmp_cksum=0;
248 icmp_header->icmp_id=id;
249 icmp_header->icmp_seq=seq;
250 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
251 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
253 addr.sin_family=AF_INET;
254 addr.sin_addr.s_addr=DestinationAddress;
255 addr.sin_port=0;
257 if (RequestOptions!=NULL) {
258 int val;
259 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
260 unsigned int len;
261 /* Before we mess with the options, get the default values */
262 len=sizeof(val);
263 getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
264 icp->default_opts.Ttl=val;
266 len=sizeof(val);
267 getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
268 icp->default_opts.Tos=val;
269 /* FIXME: missing: handling of IP 'flags', and all the other options */
272 val=RequestOptions->Ttl;
273 setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
274 val=RequestOptions->Tos;
275 setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
276 /* FIXME: missing: handling of IP 'flags', and all the other options */
278 icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
279 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
280 int val;
282 /* Restore the default options */
283 val=icp->default_opts.Ttl;
284 setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
285 val=icp->default_opts.Tos;
286 setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
287 /* FIXME: missing: handling of IP 'flags', and all the other options */
289 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
292 /* Get ready for receiving the reply
293 * Do it before we send the request to minimize the risk of introducing delays
295 fdr.fd = icp->sid;
296 fdr.events = POLLIN;
297 addrlen=sizeof(addr);
298 ier=ReplyBuffer;
299 ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
300 endbuf=(char *) ReplyBuffer+ReplySize;
301 maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
303 /* Send the packet */
304 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
305 #if 0
306 if (TRACE_ON(icmp)){
307 unsigned char* buf=(unsigned char*)reqbuf;
308 int i;
309 printf("Output buffer:\n");
310 for (i=0;i<reqsize;i++)
311 printf("%2x,", buf[i]);
312 printf("\n");
314 #endif
316 send_time = GetTickCount();
317 res=sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
318 HeapFree(GetProcessHeap (), 0, reqbuf);
319 if (res<0) {
320 if (errno==EMSGSIZE)
321 SetLastError(IP_PACKET_TOO_BIG);
322 else {
323 switch (errno) {
324 case ENETUNREACH:
325 SetLastError(IP_DEST_NET_UNREACHABLE);
326 break;
327 case EHOSTUNREACH:
328 SetLastError(IP_DEST_HOST_UNREACHABLE);
329 break;
330 default:
331 TRACE("unknown error: errno=%d\n",errno);
332 SetLastError(IP_GENERAL_FAILURE);
335 return 0;
338 /* Get the reply */
339 ip_header_len=0; /* because gcc was complaining */
340 while (poll(&fdr,1,Timeout)>0) {
341 recv_time = GetTickCount();
342 res=recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
343 TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
344 ier->Status=IP_REQ_TIMED_OUT;
346 /* Check whether we should ignore this packet */
347 if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
348 ip_header_len=ip_header->ip_hl << 2;
349 icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
350 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
351 if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
352 if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
353 ier->Status=IP_SUCCESS;
354 } else {
355 switch (icmp_header->icmp_type) {
356 case ICMP_UNREACH:
357 switch (icmp_header->icmp_code) {
358 case ICMP_UNREACH_HOST:
359 #ifdef ICMP_UNREACH_HOST_UNKNOWN
360 case ICMP_UNREACH_HOST_UNKNOWN:
361 #endif
362 #ifdef ICMP_UNREACH_ISOLATED
363 case ICMP_UNREACH_ISOLATED:
364 #endif
365 #ifdef ICMP_UNREACH_HOST_PROHIB
366 case ICMP_UNREACH_HOST_PROHIB:
367 #endif
368 #ifdef ICMP_UNREACH_TOSHOST
369 case ICMP_UNREACH_TOSHOST:
370 #endif
371 ier->Status=IP_DEST_HOST_UNREACHABLE;
372 break;
373 case ICMP_UNREACH_PORT:
374 ier->Status=IP_DEST_PORT_UNREACHABLE;
375 break;
376 case ICMP_UNREACH_PROTOCOL:
377 ier->Status=IP_DEST_PROT_UNREACHABLE;
378 break;
379 case ICMP_UNREACH_SRCFAIL:
380 ier->Status=IP_BAD_ROUTE;
381 break;
382 default:
383 ier->Status=IP_DEST_NET_UNREACHABLE;
385 break;
386 case ICMP_TIMXCEED:
387 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
388 ier->Status=IP_TTL_EXPIRED_REASSEM;
389 else
390 ier->Status=IP_TTL_EXPIRED_TRANSIT;
391 break;
392 case ICMP_PARAMPROB:
393 ier->Status=IP_PARAM_PROBLEM;
394 break;
395 case ICMP_SOURCEQUENCH:
396 ier->Status=IP_SOURCE_QUENCH;
397 break;
399 if (ier->Status!=IP_REQ_TIMED_OUT) {
400 struct ip* rep_ip_header;
401 struct icmp* rep_icmp_header;
402 /* The ICMP header size of all the packets we accept is the same */
403 rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
404 rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
406 /* Make sure that this is really a reply to our packet */
407 if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
408 ier->Status=IP_REQ_TIMED_OUT;
409 } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
410 (rep_icmp_header->icmp_code!=0) ||
411 (rep_icmp_header->icmp_id!=id) ||
412 /* windows doesn't check this checksum, else tracert */
413 /* behind a Linux 2.2 masquerading firewall would fail*/
414 /* (rep_icmp_header->icmp_cksum!=cksum) || */
415 (rep_icmp_header->icmp_seq!=seq)) {
416 /* This was not a reply to one of our packets after all */
417 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
418 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
419 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
420 rep_icmp_header->icmp_cksum);
421 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
422 id,seq,
423 cksum);
424 ier->Status=IP_REQ_TIMED_OUT;
430 if (ier->Status==IP_REQ_TIMED_OUT) {
431 /* This packet was not for us.
432 * Decrease the timeout so that we don't enter an endless loop even
433 * if we get flooded with ICMP packets that are not for us.
435 DWORD t = (recv_time - send_time);
436 if (Timeout > t) Timeout -= t;
437 else Timeout = 0;
438 continue;
439 } else {
440 /* This is a reply to our packet */
441 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
442 /* Status is already set */
443 ier->RoundTripTime= recv_time - send_time;
444 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
445 ier->Reserved=0;
446 ier->Data=endbuf-ier->DataSize;
447 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
448 ier->Options.Ttl=ip_header->ip_ttl;
449 ier->Options.Tos=ip_header->ip_tos;
450 ier->Options.Flags=ip_header->ip_off >> 13;
451 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
452 if (ier->Options.OptionsSize!=0) {
453 ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
454 /* FIXME: We are supposed to rearrange the option's 'source route' data */
455 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
456 endbuf=(char*)ier->Options.OptionsData;
457 } else {
458 ier->Options.OptionsData=NULL;
459 endbuf=ier->Data;
462 /* Prepare for the next packet */
463 ier++;
464 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
465 maxlen=endbuf-(char*)ip_header;
467 /* Check out whether there is more but don't wait this time */
468 Timeout=0;
471 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
472 if (res==0)
473 SetLastError(IP_REQ_TIMED_OUT);
474 TRACE("received %d replies\n",res);
475 return res;
478 /***********************************************************************
479 * IcmpSendEcho2 (IPHLPAPI.@)
481 DWORD WINAPI IcmpSendEcho2(
482 HANDLE IcmpHandle,
483 HANDLE Event,
484 PIO_APC_ROUTINE ApcRoutine,
485 PVOID ApcContext,
486 IPAddr DestinationAddress,
487 LPVOID RequestData,
488 WORD RequestSize,
489 PIP_OPTION_INFORMATION RequestOptions,
490 LPVOID ReplyBuffer,
491 DWORD ReplySize,
492 DWORD Timeout
495 TRACE("(%p, %p, %p, %p, %08x, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle,
496 Event, ApcRoutine, ApcContext, DestinationAddress, RequestData,
497 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
499 if (Event)
501 FIXME("unsupported for events\n");
502 return 0;
504 if (ApcRoutine)
506 FIXME("unsupported for APCs\n");
507 return 0;
509 return IcmpSendEcho(IcmpHandle, DestinationAddress, RequestData,
510 RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
514 * Copyright (c) 1989 The Regents of the University of California.
515 * All rights reserved.
517 * This code is derived from software contributed to Berkeley by
518 * Mike Muuss.
520 * Redistribution and use in source and binary forms, with or without
521 * modification, are permitted provided that the following conditions
522 * are met:
523 * 1. Redistributions of source code must retain the above copyright
524 * notice, this list of conditions and the following disclaimer.
525 * 2. Redistributions in binary form must reproduce the above copyright
526 * notice, this list of conditions and the following disclaimer in the
527 * documentation and/or other materials provided with the distribution.
528 * 3. Neither the name of the University nor the names of its contributors
529 * may be used to endorse or promote products derived from this software
530 * without specific prior written permission.
532 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
533 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
534 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
536 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
537 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
538 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
539 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
540 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
541 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
542 * SUCH DAMAGE.