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.
11 * - Systems like FreeBSD don't seem to support the IP_TTL option and maybe others.
12 * But using IP_HDRINCL and building the IP header by hand might work.
13 * - Not all IP options are supported.
14 * - Are ICMP handles real handles, i.e. inheritable and all? There might be some
15 * more work to do here, including server side stuff with synchronization.
16 * - Is it correct to use malloc for the internal buffer, for allocating the
18 * - This API should probably be thread safe. Is it really?
19 * - Using the winsock functions has not been tested.
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
29 #include <netinet/in_systm.h>
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
39 #ifdef HAVE_ARPA_INET_H
40 # include <arpa/inet.h>
50 #include "wine/ipexport.h"
51 #include "wine/icmpapi.h"
52 #include "debugtools.h"
54 /* Set up endiannes macros for the ip and ip_icmp BSD headers */
56 #define BIG_ENDIAN 4321
59 #define LITTLE_ENDIAN 1234
62 #ifdef WORDS_BIGENDIAN
63 #define BYTE_ORDER BIG_ENDIAN
65 #define BYTE_ORDER LITTLE_ENDIAN
67 #endif /* BYTE_ORDER */
69 #define u_int16_t WORD
70 #define u_int32_t DWORD
72 /* These are BSD headers. We use these here because they are needed on
73 * libc5 Linux systems. On other platforms they are usually simply more
74 * complete than the native stuff, and cause less portability problems
75 * so we use them anyway.
81 DEFAULT_DEBUG_CHANNEL(icmp)
83 /* Define the following macro to use the winsock functions */
87 /* FIXME: should we include winsock.h ???*/
88 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol);
89 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags, struct sockaddr *to, INT tolen);
90 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf,INT len, INT flags, struct sockaddr *from, INT *fromlen32);
91 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how);
96 #define ISOCK_SOCKET SOCKET
97 #define ISOCK_ISVALID(a) ((a)!=INVALID_SOCKET)
98 #define ISOCK_getsockopt(a,b,c,d,e) WINSOCK_getsockopt(a,b,c,d,e)
99 #define ISOCK_recvfrom(a,b,c,d,e,f) WINSOCK_recvfrom(a,b,c,d,e,f)
100 #define ISOCK_select(a,b,c,d,e) WINSOCK_select(a,b,c,d,e)
101 #define ISOCK_sendto(a,b,c,d,e,f) WINSOCK_sendto(a,b,c,d,e,f)
102 #define ISOCK_setsockopt(a,b,c,d,e) WINSOCK_setsockopt(a,b,c,d,e)
103 #define ISOCK_shutdown(a,b) WINSOCK_shutdown(a,b)
104 #define ISOCK_socket(a,b,c) WINSOCK_socket(a,b,c)
106 #define ISOCK_SOCKET int
107 #define ISOCK_ISVALID(a) ((a)>=0)
108 #define ISOCK_getsockopt(a,b,c,d,e) getsockopt(a,b,c,d,e)
109 #define ISOCK_recvfrom(a,b,c,d,e,f) recvfrom(a,b,c,d,e,f)
110 #define ISOCK_select(a,b,c,d,e) select(a,b,c,d,e)
111 #define ISOCK_setsockopt(a,b,c,d,e) setsockopt(a,b,c,d,e)
112 #define ISOCK_sendto(a,b,c,d,e,f) sendto(a,b,c,d,e,f)
113 #define ISOCK_shutdown(a,b) shutdown(a,b)
114 #define ISOCK_socket(a,b,c) socket(a,b,c)
119 IP_OPTION_INFORMATION default_opts;
122 #define IP_OPTS_UNKNOWN 0
123 #define IP_OPTS_DEFAULT 1
124 #define IP_OPTS_CUSTOM 2
126 /* The sequence number is unique process wide, so that all threads
127 * have a distinct sequence number.
129 static LONG icmp_sequence=0;
131 static int in_cksum(u_short *addr, int len)
144 *(u_char *)(&answer) = *(u_char *)w;
148 sum = (sum >> 16) + (sum & 0xffff);
160 /***********************************************************************
163 HANDLE WINAPI IcmpCreateFile(VOID)
167 ISOCK_SOCKET sid=ISOCK_socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
168 if (!ISOCK_ISVALID(sid)) {
169 MESSAGE("WARNING: Trying to use ICMP will fail unless running as root\n");
170 SetLastError(ERROR_ACCESS_DENIED);
171 return INVALID_HANDLE_VALUE;
174 icp=malloc(sizeof(*icp));
176 SetLastError(IP_NO_RESOURCES);
177 return INVALID_HANDLE_VALUE;
180 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
185 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
187 icmp_t* icp=(icmp_t*)IcmpHandle;
188 if (IcmpHandle==INVALID_HANDLE_VALUE) {
189 /* FIXME: in fact win98 seems to ignore the handle value !!! */
190 SetLastError(ERROR_INVALID_HANDLE);
194 ISOCK_shutdown(icp->sid,2);
200 /***********************************************************************
203 DWORD WINAPI IcmpSendEcho(
205 IPAddr DestinationAddress,
208 PIP_OPTION_INFORMATION RequestOptions,
214 icmp_t* icp=(icmp_t*)IcmpHandle;
215 unsigned char* reqbuf;
218 struct icmp_echo_reply* ier;
219 struct ip* ip_header;
220 struct icmp* icmp_header;
225 struct timeval timeout,send_time,recv_time;
226 struct sockaddr_in addr;
228 unsigned short id,seq,cksum;
231 if (IcmpHandle==INVALID_HANDLE_VALUE) {
232 /* FIXME: in fact win98 seems to ignore the handle value !!! */
233 SetLastError(ERROR_INVALID_HANDLE);
237 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
238 SetLastError(IP_BUF_TOO_SMALL);
241 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
243 /* Prepare the request */
244 id=getpid() & 0xFFFF;
245 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
247 reqsize=ICMP_MINLEN+RequestSize;
248 reqbuf=malloc(reqsize);
250 SetLastError(ERROR_OUTOFMEMORY);
254 icmp_header=(struct icmp*)reqbuf;
255 icmp_header->icmp_type=ICMP_ECHO;
256 icmp_header->icmp_code=0;
257 icmp_header->icmp_cksum=0;
258 icmp_header->icmp_id=id;
259 icmp_header->icmp_seq=seq;
260 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
261 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
263 addr.sin_family=AF_INET;
264 addr.sin_addr.s_addr=DestinationAddress;
267 if (RequestOptions!=NULL) {
269 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
271 /* Before we mess with the options, get the default values */
273 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
274 icp->default_opts.Ttl=val;
277 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
278 icp->default_opts.Tos=val;
279 /* FIXME: missing: handling of IP 'flags', and all the other options */
282 val=RequestOptions->Ttl;
283 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
284 val=RequestOptions->Tos;
285 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
286 /* FIXME: missing: handling of IP 'flags', and all the other options */
288 icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
289 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
292 /* Restore the default options */
293 val=icp->default_opts.Ttl;
294 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
295 val=icp->default_opts.Tos;
296 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
297 /* FIXME: missing: handling of IP 'flags', and all the other options */
299 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
302 /* Get ready for receiving the reply
303 * Do it before we send the request to minimize the risk of introducing delays
306 FD_SET(icp->sid,&fdr);
307 timeout.tv_sec=Timeout/1000;
308 timeout.tv_usec=(Timeout % 1000)*1000;
309 addrlen=sizeof(addr);
311 ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
312 endbuf=(char *) ReplyBuffer+ReplySize;
313 maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
315 /* Send the packet */
316 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
319 unsigned char* buf=(unsigned char*)reqbuf;
321 printf("Output buffer:\n");
322 for (i=0;i<reqsize;i++)
323 printf("%2x,", buf[i]);
328 gettimeofday(&send_time,NULL);
329 res=ISOCK_sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
333 SetLastError(IP_PACKET_TOO_BIG);
337 SetLastError(IP_DEST_NET_UNREACHABLE);
340 SetLastError(IP_DEST_NET_UNREACHABLE);
343 TRACE("unknown error: errno=%d\n",errno);
344 SetLastError(ERROR_UNKNOWN);
351 ip_header_len=0; /* because gcc was complaining */
352 while ((res=ISOCK_select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
353 gettimeofday(&recv_time,NULL);
354 res=ISOCK_recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
355 TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
356 ier->Status=IP_REQ_TIMED_OUT;
358 /* Check whether we should ignore this packet */
359 if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
360 ip_header_len=ip_header->ip_hl << 2;
361 icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
362 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
363 if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
364 if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
365 ier->Status=IP_SUCCESS;
367 switch (icmp_header->icmp_type) {
369 switch (icmp_header->icmp_code) {
370 case ICMP_UNREACH_HOST:
371 #ifdef ICMP_UNREACH_HOST_UNKNOWN
372 case ICMP_UNREACH_HOST_UNKNOWN:
374 #ifdef ICMP_UNREACH_ISOLATED
375 case ICMP_UNREACH_ISOLATED:
377 #ifdef ICMP_UNREACH_HOST_PROHIB
378 case ICMP_UNREACH_HOST_PROHIB:
380 #ifdef ICMP_UNREACH_TOSHOST
381 case ICMP_UNREACH_TOSHOST:
383 ier->Status=IP_DEST_HOST_UNREACHABLE;
385 case ICMP_UNREACH_PORT:
386 ier->Status=IP_DEST_PORT_UNREACHABLE;
388 case ICMP_UNREACH_PROTOCOL:
389 ier->Status=IP_DEST_PROT_UNREACHABLE;
391 case ICMP_UNREACH_SRCFAIL:
392 ier->Status=IP_BAD_ROUTE;
395 ier->Status=IP_DEST_NET_UNREACHABLE;
399 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
400 ier->Status=IP_TTL_EXPIRED_REASSEM;
402 ier->Status=IP_TTL_EXPIRED_TRANSIT;
405 ier->Status=IP_PARAM_PROBLEM;
407 case ICMP_SOURCEQUENCH:
408 ier->Status=IP_SOURCE_QUENCH;
411 if (ier->Status!=IP_REQ_TIMED_OUT) {
412 struct ip* rep_ip_header;
413 struct icmp* rep_icmp_header;
414 /* The ICMP header size of all the packets we accept is the same */
415 rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
416 rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
418 /* Make sure that this is really a reply to our packet */
419 if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
420 ier->Status=IP_REQ_TIMED_OUT;
421 } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
422 (rep_icmp_header->icmp_code!=0) ||
423 (rep_icmp_header->icmp_id!=id) ||
424 (rep_icmp_header->icmp_seq!=seq) ||
425 (rep_icmp_header->icmp_cksum!=cksum)) {
426 /* This was not a reply to one of our packets after all */
427 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
428 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
429 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
430 rep_icmp_header->icmp_cksum);
431 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
434 ier->Status=IP_REQ_TIMED_OUT;
440 if (ier->Status==IP_REQ_TIMED_OUT) {
441 /* This packet was not for us.
442 * Decrease the timeout so that we don't enter an endless loop even
443 * if we get flooded with ICMP packets that are not for us.
445 timeout.tv_sec=Timeout/1000-(recv_time.tv_sec-send_time.tv_sec);
446 timeout.tv_usec=(Timeout % 1000)*1000+send_time.tv_usec-(recv_time.tv_usec-send_time.tv_usec);
447 if (timeout.tv_usec<0) {
448 timeout.tv_usec+=1000000;
453 /* This is a reply to our packet */
454 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
455 /* Status is already set */
456 ier->RoundTripTime=(recv_time.tv_sec-send_time.tv_sec)*1000+(recv_time.tv_usec-send_time.tv_usec)/1000;
457 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
459 ier->Data=endbuf-ier->DataSize;
460 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
461 ier->Options.Ttl=ip_header->ip_ttl;
462 ier->Options.Tos=ip_header->ip_tos;
463 ier->Options.Flags=ip_header->ip_off >> 13;
464 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
465 if (ier->Options.OptionsSize!=0) {
466 ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
467 /* FIXME: We are supposed to rearrange the option's 'source route' data */
468 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
469 endbuf=ier->Options.OptionsData;
471 ier->Options.OptionsData=NULL;
475 /* Prepare for the next packet */
477 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
478 maxlen=endbuf-(char*)ip_header;
480 /* Check out whether there is more but don't wait this time */
485 FD_SET(icp->sid,&fdr);
487 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
489 SetLastError(IP_REQ_TIMED_OUT);
490 TRACE("received %d replies\n",res);
495 * Copyright (c) 1989 The Regents of the University of California.
496 * All rights reserved.
498 * This code is derived from software contributed to Berkeley by
501 * Redistribution and use in source and binary forms, with or without
502 * modification, are permitted provided that the following conditions
504 * 1. Redistributions of source code must retain the above copyright
505 * notice, this list of conditions and the following disclaimer.
506 * 2. Redistributions in binary form must reproduce the above copyright
507 * notice, this list of conditions and the following disclaimer in the
508 * documentation and/or other materials provided with the distribution.
509 * 3. All advertising materials mentioning features or use of this software
510 * must display the following acknowledgement:
511 * This product includes software developed by the University of
512 * California, Berkeley and its contributors.
513 * 4. Neither the name of the University nor the names of its contributors
514 * may be used to endorse or promote products derived from this software
515 * without specific prior written permission.
517 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
518 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
519 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
520 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
521 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
522 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
523 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
524 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
525 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
526 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF