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>
33 #include <netinet/ip.h>
34 #include <netinet/ip_icmp.h>
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
51 #include "wine/ipexport.h"
52 #include "wine/icmpapi.h"
53 #include "debugtools.h"
55 DEFAULT_DEBUG_CHANNEL(icmp)
57 /* Define the following macro to use the winsock functions */
61 /* FIXME: should we include winsock.h ???*/
62 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol);
63 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags, struct sockaddr *to, INT tolen);
64 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf,INT len, INT flags, struct sockaddr *from, INT *fromlen32);
65 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how);
70 #define ISOCK_SOCKET SOCKET
71 #define ISOCK_ISVALID(a) ((a)!=INVALID_SOCKET)
72 #define ISOCK_getsockopt(a,b,c,d,e) WINSOCK_getsockopt(a,b,c,d,e)
73 #define ISOCK_recvfrom(a,b,c,d,e,f) WINSOCK_recvfrom(a,b,c,d,e,f)
74 #define ISOCK_select(a,b,c,d,e) WINSOCK_select(a,b,c,d,e)
75 #define ISOCK_sendto(a,b,c,d,e,f) WINSOCK_sendto(a,b,c,d,e,f)
76 #define ISOCK_setsockopt(a,b,c,d,e) WINSOCK_setsockopt(a,b,c,d,e)
77 #define ISOCK_shutdown(a,b) WINSOCK_shutdown(a,b)
78 #define ISOCK_socket(a,b,c) WINSOCK_socket(a,b,c)
80 #define ISOCK_SOCKET int
81 #define ISOCK_ISVALID(a) ((a)>=0)
82 #define ISOCK_getsockopt(a,b,c,d,e) getsockopt(a,b,c,d,e)
83 #define ISOCK_recvfrom(a,b,c,d,e,f) recvfrom(a,b,c,d,e,f)
84 #define ISOCK_select(a,b,c,d,e) select(a,b,c,d,e)
85 #define ISOCK_setsockopt(a,b,c,d,e) setsockopt(a,b,c,d,e)
86 #define ISOCK_sendto(a,b,c,d,e,f) sendto(a,b,c,d,e,f)
87 #define ISOCK_shutdown(a,b) shutdown(a,b)
88 #define ISOCK_socket(a,b,c) socket(a,b,c)
93 IP_OPTION_INFORMATION default_opts;
96 #define IP_OPTS_UNKNOWN 0
97 #define IP_OPTS_DEFAULT 1
98 #define IP_OPTS_CUSTOM 2
100 /* The sequence number is unique process wide, so that all threads
101 * have a distinct sequence number.
103 static LONG icmp_sequence=0;
105 static int in_cksum(u_short *addr, int len)
118 *(u_char *)(&answer) = *(u_char *)w;
122 sum = (sum >> 16) + (sum & 0xffff);
134 HANDLE WINAPI IcmpCreateFile(VOID)
138 ISOCK_SOCKET sid=ISOCK_socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
139 if (!ISOCK_ISVALID(sid)) {
140 MESSAGE("WARNING: Trying to use ICMP will fail unless running as root\n");
141 SetLastError(ERROR_ACCESS_DENIED);
142 return INVALID_HANDLE_VALUE;
145 icp=malloc(sizeof(*icp));
147 SetLastError(IP_NO_RESOURCES);
148 return INVALID_HANDLE_VALUE;
151 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
156 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
158 icmp_t* icp=(icmp_t*)IcmpHandle;
160 ISOCK_shutdown(icp->sid,2);
166 DWORD WINAPI IcmpSendEcho(
168 IPAddr DestinationAddress,
171 PIP_OPTION_INFORMATION RequestOptions,
177 icmp_t* icp=(icmp_t*)IcmpHandle;
178 unsigned char* reqbuf;
181 struct icmp_echo_reply* ier;
182 struct ip* ip_header;
183 struct icmp* icmp_header;
188 struct timeval timeout,send_time,recv_time;
189 struct sockaddr_in addr;
191 unsigned short id,seq,cksum;
194 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
195 SetLastError(IP_BUF_TOO_SMALL);
198 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
200 /* Prepare the request */
201 id=getpid() & 0xFFFF;
202 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
204 reqsize=ICMP_MINLEN+RequestSize;
205 reqbuf=malloc(reqsize);
207 SetLastError(ERROR_OUTOFMEMORY);
211 icmp_header=(struct icmp*)reqbuf;
212 icmp_header->icmp_type=ICMP_ECHO;
213 icmp_header->icmp_code=0;
214 icmp_header->icmp_cksum=0;
215 icmp_header->icmp_id=id;
216 icmp_header->icmp_seq=seq;
217 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
218 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
220 addr.sin_family=AF_INET;
221 addr.sin_addr.s_addr=DestinationAddress;
224 if (RequestOptions!=NULL) {
226 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
228 /* Before we mess with the options, get the default values */
230 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
231 icp->default_opts.Ttl=val;
234 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
235 icp->default_opts.Tos=val;
236 /* FIXME: missing: handling of IP 'flags', and all the other options */
239 val=RequestOptions->Ttl;
240 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
241 val=RequestOptions->Tos;
242 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
243 /* FIXME: missing: handling of IP 'flags', and all the other options */
245 icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
246 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
249 /* Restore the default options */
250 val=icp->default_opts.Ttl;
251 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
252 val=icp->default_opts.Tos;
253 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
254 /* FIXME: missing: handling of IP 'flags', and all the other options */
256 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
259 /* Get ready for receiving the reply
260 * Do it before we send the request to minimize the risk of introducing delays
263 FD_SET(icp->sid,&fdr);
264 timeout.tv_sec=Timeout/1000;
265 timeout.tv_usec=(Timeout % 1000)*1000;
266 addrlen=sizeof(addr);
268 ip_header=ReplyBuffer+sizeof(ICMP_ECHO_REPLY);
269 endbuf=ReplyBuffer+ReplySize;
270 maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
272 /* Send the packet */
273 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
276 unsigned char* buf=(unsigned char*)reqbuf;
278 printf("Output buffer:\n");
279 for (i=0;i<reqsize;i++)
280 printf("%2x,", buf[i]);
285 gettimeofday(&send_time,NULL);
286 res=ISOCK_sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
290 SetLastError(IP_PACKET_TOO_BIG);
294 SetLastError(IP_DEST_NET_UNREACHABLE);
297 SetLastError(IP_DEST_NET_UNREACHABLE);
300 TRACE("unknown error: errno=%d\n",errno);
301 SetLastError(ERROR_UNKNOWN);
308 ip_header_len=0; /* because gcc was complaining */
309 while ((res=ISOCK_select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
310 gettimeofday(&recv_time,NULL);
311 res=ISOCK_recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
312 TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
313 ier->Status=IP_REQ_TIMED_OUT;
315 /* Check whether we should ignore this packet */
316 if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
317 ip_header_len=ip_header->ip_hl << 2;
318 icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
319 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
320 if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
321 if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
322 ier->Status=IP_SUCCESS;
324 switch (icmp_header->icmp_type) {
326 switch (icmp_header->icmp_code) {
327 case ICMP_UNREACH_HOST:
328 #ifdef ICMP_UNREACH_HOST_UNKNOWN
329 case ICMP_UNREACH_HOST_UNKNOWN:
331 #ifdef ICMP_UNREACH_ISOLATED
332 case ICMP_UNREACH_ISOLATED:
334 #ifdef ICMP_UNREACH_HOST_PROHIB
335 case ICMP_UNREACH_HOST_PROHIB:
337 #ifdef ICMP_UNREACH_TOSHOST
338 case ICMP_UNREACH_TOSHOST:
340 ier->Status=IP_DEST_HOST_UNREACHABLE;
342 case ICMP_UNREACH_PORT:
343 ier->Status=IP_DEST_PORT_UNREACHABLE;
345 case ICMP_UNREACH_PROTOCOL:
346 ier->Status=IP_DEST_PROT_UNREACHABLE;
348 case ICMP_UNREACH_SRCFAIL:
349 ier->Status=IP_BAD_ROUTE;
352 ier->Status=IP_DEST_NET_UNREACHABLE;
356 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
357 ier->Status=IP_TTL_EXPIRED_REASSEM;
359 ier->Status=IP_TTL_EXPIRED_TRANSIT;
362 ier->Status=IP_PARAM_PROBLEM;
364 case ICMP_SOURCEQUENCH:
365 ier->Status=IP_SOURCE_QUENCH;
368 if (ier->Status!=IP_REQ_TIMED_OUT) {
369 struct ip* rep_ip_header;
370 struct icmp* rep_icmp_header;
371 /* The ICMP header size of all the packets we accept is the same */
372 rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
373 rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
375 /* Make sure that this is really a reply to our packet */
376 if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
377 ier->Status=IP_REQ_TIMED_OUT;
378 } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
379 (rep_icmp_header->icmp_code!=0) ||
380 (rep_icmp_header->icmp_id!=id) ||
381 (rep_icmp_header->icmp_seq!=seq) ||
382 (rep_icmp_header->icmp_cksum!=cksum)) {
383 /* This was not a reply to one of our packets after all */
384 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
385 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
386 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
387 rep_icmp_header->icmp_cksum);
388 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
391 ier->Status=IP_REQ_TIMED_OUT;
397 if (ier->Status==IP_REQ_TIMED_OUT) {
398 /* This packet was not for us.
399 * Decrease the timeout so that we don't enter an endless loop even
400 * if we get flooded with ICMP packets that are not for us.
402 timeout.tv_sec=Timeout/1000-(recv_time.tv_sec-send_time.tv_sec);
403 timeout.tv_usec=(Timeout % 1000)*1000+send_time.tv_usec-(recv_time.tv_usec-send_time.tv_usec);
404 if (timeout.tv_usec<0) {
405 timeout.tv_usec+=1000000;
410 /* This is a reply to our packet */
411 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
412 /* Status is already set */
413 ier->RoundTripTime=(recv_time.tv_sec-send_time.tv_sec)*1000+(recv_time.tv_usec-send_time.tv_usec)/1000;
414 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
416 ier->Data=endbuf-ier->DataSize;
417 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
418 ier->Options.Ttl=ip_header->ip_ttl;
419 ier->Options.Tos=ip_header->ip_tos;
420 ier->Options.Flags=ip_header->ip_off >> 13;
421 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
422 if (ier->Options.OptionsSize!=0) {
423 ier->Options.OptionsData=ier->Data-ier->Options.OptionsSize;
424 /* FIXME: We are supposed to rearrange the option's 'source route' data */
425 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
426 endbuf=ier->Options.OptionsData;
428 ier->Options.OptionsData=NULL;
432 /* Prepare for the next packet */
434 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
435 maxlen=endbuf-(char*)ip_header;
437 /* Check out whether there is more but don't wait this time */
442 FD_SET(icp->sid,&fdr);
444 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
446 SetLastError(IP_REQ_TIMED_OUT);
447 TRACE("received %d replies\n",res);
452 * Copyright (c) 1989 The Regents of the University of California.
453 * All rights reserved.
455 * This code is derived from software contributed to Berkeley by
458 * Redistribution and use in source and binary forms, with or without
459 * modification, are permitted provided that the following conditions
461 * 1. Redistributions of source code must retain the above copyright
462 * notice, this list of conditions and the following disclaimer.
463 * 2. Redistributions in binary form must reproduce the above copyright
464 * notice, this list of conditions and the following disclaimer in the
465 * documentation and/or other materials provided with the distribution.
466 * 3. All advertising materials mentioning features or use of this software
467 * must display the following acknowledgement:
468 * This product includes software developed by the University of
469 * California, Berkeley and its contributors.
470 * 4. Neither the name of the University nor the names of its contributors
471 * may be used to endorse or promote products derived from this software
472 * without specific prior written permission.
474 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
475 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
476 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
477 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
478 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
479 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
480 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
481 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
482 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
483 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF