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
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 * - Is it correct to use malloc for the internal buffer, for allocating the
32 * - This API should probably be thread safe. Is it really?
33 * - Using the winsock functions has not been tested.
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
45 #ifdef HAVE_NETINET_IN_SYSTM_H
46 # include <netinet/in_systm.h>
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
61 #ifdef HAVE_ARPA_INET_H
62 # include <arpa/inet.h>
64 #ifdef HAVE_SYS_POLL_H
65 # include <sys/poll.h>
73 #include "wine/debug.h"
75 /* Set up endiannes macros for the ip and ip_icmp BSD headers */
77 #define BIG_ENDIAN 4321
80 #define LITTLE_ENDIAN 1234
83 #ifdef WORDS_BIGENDIAN
84 #define BYTE_ORDER BIG_ENDIAN
86 #define BYTE_ORDER LITTLE_ENDIAN
88 #endif /* BYTE_ORDER */
90 #define u_int16_t WORD
91 #define u_int32_t DWORD
93 /* These are BSD headers. We use these here because they are needed on
94 * libc5 Linux systems. On other platforms they are usually simply more
95 * complete than the native stuff, and cause less portability problems
96 * so we use them anyway.
102 WINE_DEFAULT_DEBUG_CHANNEL(icmp
);
107 IP_OPTION_INFORMATION default_opts
;
110 #define IP_OPTS_UNKNOWN 0
111 #define IP_OPTS_DEFAULT 1
112 #define IP_OPTS_CUSTOM 2
114 /* The sequence number is unique process wide, so that all threads
115 * have a distinct sequence number.
117 static LONG icmp_sequence
=0;
119 static int in_cksum(u_short
*addr
, int len
)
132 *(u_char
*)(&answer
) = *(u_char
*)w
;
136 sum
= (sum
>> 16) + (sum
& 0xffff);
148 /***********************************************************************
149 * IcmpCreateFile (ICMP.@)
151 HANDLE WINAPI
IcmpCreateFile(VOID
)
155 int sid
=socket(AF_INET
,SOCK_RAW
,IPPROTO_ICMP
);
157 MESSAGE("WARNING: Trying to use ICMP (network ping) will fail unless running as root\n");
158 SetLastError(ERROR_ACCESS_DENIED
);
159 return INVALID_HANDLE_VALUE
;
162 icp
=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp
));
164 SetLastError(IP_NO_RESOURCES
);
165 return INVALID_HANDLE_VALUE
;
168 icp
->default_opts
.OptionsSize
=IP_OPTS_UNKNOWN
;
173 /***********************************************************************
174 * IcmpCloseHandle (ICMP.@)
176 BOOL WINAPI
IcmpCloseHandle(HANDLE IcmpHandle
)
178 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
179 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
180 /* FIXME: in fact win98 seems to ignore the handle value !!! */
181 SetLastError(ERROR_INVALID_HANDLE
);
185 shutdown(icp
->sid
,2);
186 HeapFree(GetProcessHeap (), 0, icp
);
191 /***********************************************************************
192 * IcmpSendEcho (ICMP.@)
194 DWORD WINAPI
IcmpSendEcho(
196 IPAddr DestinationAddress
,
199 PIP_OPTION_INFORMATION RequestOptions
,
205 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
206 unsigned char* reqbuf
;
209 struct icmp_echo_reply
* ier
;
210 struct ip
* ip_header
;
211 struct icmp
* icmp_header
;
216 DWORD send_time
,recv_time
;
217 struct sockaddr_in addr
;
218 unsigned int addrlen
;
219 unsigned short id
,seq
,cksum
;
222 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
223 /* FIXME: in fact win98 seems to ignore the handle value !!! */
224 SetLastError(ERROR_INVALID_HANDLE
);
228 if (ReplySize
<sizeof(ICMP_ECHO_REPLY
)+ICMP_MINLEN
) {
229 SetLastError(IP_BUF_TOO_SMALL
);
232 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
234 /* Prepare the request */
235 id
=getpid() & 0xFFFF;
236 seq
=InterlockedIncrement(&icmp_sequence
) & 0xFFFF;
238 reqsize
=ICMP_MINLEN
+RequestSize
;
239 reqbuf
=HeapAlloc(GetProcessHeap(), 0, reqsize
);
241 SetLastError(ERROR_OUTOFMEMORY
);
245 icmp_header
=(struct icmp
*)reqbuf
;
246 icmp_header
->icmp_type
=ICMP_ECHO
;
247 icmp_header
->icmp_code
=0;
248 icmp_header
->icmp_cksum
=0;
249 icmp_header
->icmp_id
=id
;
250 icmp_header
->icmp_seq
=seq
;
251 memcpy(reqbuf
+ICMP_MINLEN
, RequestData
, RequestSize
);
252 icmp_header
->icmp_cksum
=cksum
=in_cksum((u_short
*)reqbuf
,reqsize
);
254 addr
.sin_family
=AF_INET
;
255 addr
.sin_addr
.s_addr
=DestinationAddress
;
258 if (RequestOptions
!=NULL
) {
260 if (icp
->default_opts
.OptionsSize
==IP_OPTS_UNKNOWN
) {
262 /* Before we mess with the options, get the default values */
264 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,&len
);
265 icp
->default_opts
.Ttl
=val
;
268 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,&len
);
269 icp
->default_opts
.Tos
=val
;
270 /* FIXME: missing: handling of IP 'flags', and all the other options */
273 val
=RequestOptions
->Ttl
;
274 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
275 val
=RequestOptions
->Tos
;
276 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,sizeof(val
));
277 /* FIXME: missing: handling of IP 'flags', and all the other options */
279 icp
->default_opts
.OptionsSize
=IP_OPTS_CUSTOM
;
280 } else if (icp
->default_opts
.OptionsSize
==IP_OPTS_CUSTOM
) {
283 /* Restore the default options */
284 val
=icp
->default_opts
.Ttl
;
285 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
286 val
=icp
->default_opts
.Tos
;
287 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,sizeof(val
));
288 /* FIXME: missing: handling of IP 'flags', and all the other options */
290 icp
->default_opts
.OptionsSize
=IP_OPTS_DEFAULT
;
293 /* Get ready for receiving the reply
294 * Do it before we send the request to minimize the risk of introducing delays
298 addrlen
=sizeof(addr
);
300 ip_header
=(struct ip
*) ((char *) ReplyBuffer
+sizeof(ICMP_ECHO_REPLY
));
301 endbuf
=(char *) ReplyBuffer
+ReplySize
;
302 maxlen
=ReplySize
-sizeof(ICMP_ECHO_REPLY
);
304 /* Send the packet */
305 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize
, RequestSize
, inet_ntoa(addr
.sin_addr
));
308 unsigned char* buf
=(unsigned char*)reqbuf
;
310 printf("Output buffer:\n");
311 for (i
=0;i
<reqsize
;i
++)
312 printf("%2x,", buf
[i
]);
317 send_time
= GetTickCount();
318 res
=sendto(icp
->sid
, reqbuf
, reqsize
, 0, (struct sockaddr
*)&addr
, sizeof(addr
));
319 HeapFree(GetProcessHeap (), 0, reqbuf
);
322 SetLastError(IP_PACKET_TOO_BIG
);
326 SetLastError(IP_DEST_NET_UNREACHABLE
);
329 SetLastError(IP_DEST_HOST_UNREACHABLE
);
332 TRACE("unknown error: errno=%d\n",errno
);
333 SetLastError(IP_GENERAL_FAILURE
);
340 ip_header_len
=0; /* because gcc was complaining */
341 while ((res
=poll(&fdr
,1,Timeout
))>0) {
342 recv_time
= GetTickCount();
343 res
=recvfrom(icp
->sid
, (char*)ip_header
, maxlen
, 0, (struct sockaddr
*)&addr
,&addrlen
);
344 TRACE("received %d bytes from %s\n",res
, inet_ntoa(addr
.sin_addr
));
345 ier
->Status
=IP_REQ_TIMED_OUT
;
347 /* Check whether we should ignore this packet */
348 if ((ip_header
->ip_p
==IPPROTO_ICMP
) && (res
>=sizeof(struct ip
)+ICMP_MINLEN
)) {
349 ip_header_len
=ip_header
->ip_hl
<< 2;
350 icmp_header
=(struct icmp
*)(((char*)ip_header
)+ip_header_len
);
351 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header
->icmp_type
,icmp_header
->icmp_code
);
352 if (icmp_header
->icmp_type
==ICMP_ECHOREPLY
) {
353 if ((icmp_header
->icmp_id
==id
) && (icmp_header
->icmp_seq
==seq
))
354 ier
->Status
=IP_SUCCESS
;
356 switch (icmp_header
->icmp_type
) {
358 switch (icmp_header
->icmp_code
) {
359 case ICMP_UNREACH_HOST
:
360 #ifdef ICMP_UNREACH_HOST_UNKNOWN
361 case ICMP_UNREACH_HOST_UNKNOWN
:
363 #ifdef ICMP_UNREACH_ISOLATED
364 case ICMP_UNREACH_ISOLATED
:
366 #ifdef ICMP_UNREACH_HOST_PROHIB
367 case ICMP_UNREACH_HOST_PROHIB
:
369 #ifdef ICMP_UNREACH_TOSHOST
370 case ICMP_UNREACH_TOSHOST
:
372 ier
->Status
=IP_DEST_HOST_UNREACHABLE
;
374 case ICMP_UNREACH_PORT
:
375 ier
->Status
=IP_DEST_PORT_UNREACHABLE
;
377 case ICMP_UNREACH_PROTOCOL
:
378 ier
->Status
=IP_DEST_PROT_UNREACHABLE
;
380 case ICMP_UNREACH_SRCFAIL
:
381 ier
->Status
=IP_BAD_ROUTE
;
384 ier
->Status
=IP_DEST_NET_UNREACHABLE
;
388 if (icmp_header
->icmp_code
==ICMP_TIMXCEED_REASS
)
389 ier
->Status
=IP_TTL_EXPIRED_REASSEM
;
391 ier
->Status
=IP_TTL_EXPIRED_TRANSIT
;
394 ier
->Status
=IP_PARAM_PROBLEM
;
396 case ICMP_SOURCEQUENCH
:
397 ier
->Status
=IP_SOURCE_QUENCH
;
400 if (ier
->Status
!=IP_REQ_TIMED_OUT
) {
401 struct ip
* rep_ip_header
;
402 struct icmp
* rep_icmp_header
;
403 /* The ICMP header size of all the packets we accept is the same */
404 rep_ip_header
=(struct ip
*)(((char*)icmp_header
)+ICMP_MINLEN
);
405 rep_icmp_header
=(struct icmp
*)(((char*)rep_ip_header
)+(rep_ip_header
->ip_hl
<< 2));
407 /* Make sure that this is really a reply to our packet */
408 if (ip_header_len
+ICMP_MINLEN
+(rep_ip_header
->ip_hl
<< 2)+ICMP_MINLEN
>ip_header
->ip_len
) {
409 ier
->Status
=IP_REQ_TIMED_OUT
;
410 } else if ((rep_icmp_header
->icmp_type
!=ICMP_ECHO
) ||
411 (rep_icmp_header
->icmp_code
!=0) ||
412 (rep_icmp_header
->icmp_id
!=id
) ||
413 /* windows doesn't check this checksum, else tracert */
414 /* behind a Linux 2.2 masquerading firewall would fail*/
415 /* (rep_icmp_header->icmp_cksum!=cksum) || */
416 (rep_icmp_header
->icmp_seq
!=seq
)) {
417 /* This was not a reply to one of our packets after all */
418 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
419 rep_icmp_header
->icmp_type
,rep_icmp_header
->icmp_code
,
420 rep_icmp_header
->icmp_id
,rep_icmp_header
->icmp_seq
,
421 rep_icmp_header
->icmp_cksum
);
422 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
425 ier
->Status
=IP_REQ_TIMED_OUT
;
431 if (ier
->Status
==IP_REQ_TIMED_OUT
) {
432 /* This packet was not for us.
433 * Decrease the timeout so that we don't enter an endless loop even
434 * if we get flooded with ICMP packets that are not for us.
436 Timeout
-= (recv_time
- send_time
);
437 if (Timeout
< 0) Timeout
= 0;
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
;
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
;
458 ier
->Options
.OptionsData
=NULL
;
462 /* Prepare for the next packet */
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 */
471 res
=ier
-(ICMP_ECHO_REPLY
*)ReplyBuffer
;
473 SetLastError(IP_REQ_TIMED_OUT
);
474 TRACE("received %d replies\n",res
);
479 * Copyright (c) 1989 The Regents of the University of California.
480 * All rights reserved.
482 * This code is derived from software contributed to Berkeley by
485 * Redistribution and use in source and binary forms, with or without
486 * modification, are permitted provided that the following conditions
488 * 1. Redistributions of source code must retain the above copyright
489 * notice, this list of conditions and the following disclaimer.
490 * 2. Redistributions in binary form must reproduce the above copyright
491 * notice, this list of conditions and the following disclaimer in the
492 * documentation and/or other materials provided with the distribution.
493 * 3. Neither the name of the University nor the names of its contributors
494 * may be used to endorse or promote products derived from this software
495 * without specific prior written permission.
497 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
498 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
499 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
500 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
501 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
502 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
503 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
504 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
505 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
506 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF