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 * - This API should probably be thread safe. Is it really?
31 * - Using the winsock functions has not been tested.
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 # include <netinet/in_systm.h>
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
59 #ifdef HAVE_ARPA_INET_H
60 # include <arpa/inet.h>
62 #ifdef HAVE_SYS_POLL_H
63 # include <sys/poll.h>
74 #include "wine/debug.h"
76 /* Set up endianness macros for the ip and ip_icmp BSD headers */
78 #define BIG_ENDIAN 4321
81 #define LITTLE_ENDIAN 1234
84 #ifdef WORDS_BIGENDIAN
85 #define BYTE_ORDER BIG_ENDIAN
87 #define BYTE_ORDER LITTLE_ENDIAN
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.
103 WINE_DEFAULT_DEBUG_CHANNEL(icmp
);
104 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
109 IP_OPTION_INFORMATION default_opts
;
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
)
134 *(u_char
*)(&answer
) = *(u_char
*)w
;
138 sum
= (sum
>> 16) + (sum
& 0xffff);
150 /***********************************************************************
151 * IcmpCreateFile (IPHLPAPI.@)
153 HANDLE WINAPI
IcmpCreateFile(VOID
)
157 int sid
=socket(AF_INET
,SOCK_RAW
,IPPROTO_ICMP
);
160 /* Mac OS X supports non-privileged ICMP via SOCK_DGRAM type. */
161 sid
=socket(AF_INET
,SOCK_DGRAM
,IPPROTO_ICMP
);
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
));
172 SetLastError(IP_NO_RESOURCES
);
173 return INVALID_HANDLE_VALUE
;
176 icp
->default_opts
.OptionsSize
=IP_OPTS_UNKNOWN
;
181 /***********************************************************************
182 * IcmpCloseHandle (IPHLPAPI.@)
184 BOOL WINAPI
IcmpCloseHandle(HANDLE IcmpHandle
)
186 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
187 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
188 /* FIXME: in fact win98 seems to ignore the handle value !!! */
189 SetLastError(ERROR_INVALID_HANDLE
);
194 HeapFree(GetProcessHeap (), 0, icp
);
199 /***********************************************************************
200 * IcmpSendEcho (IPHLPAPI.@)
202 DWORD WINAPI
IcmpSendEcho(
204 IPAddr DestinationAddress
,
207 PIP_OPTION_INFORMATION RequestOptions
,
213 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
214 unsigned char* reqbuf
;
217 struct icmp_echo_reply
* ier
;
218 struct ip
* ip_header
;
219 struct icmp
* icmp_header
;
224 DWORD send_time
,recv_time
;
225 struct sockaddr_in addr
;
227 unsigned short id
,seq
,cksum
;
230 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
231 /* FIXME: in fact win98 seems to ignore the handle value !!! */
232 SetLastError(ERROR_INVALID_HANDLE
);
236 if (ReplySize
<sizeof(ICMP_ECHO_REPLY
)+ICMP_MINLEN
) {
237 SetLastError(IP_BUF_TOO_SMALL
);
240 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
242 if (!DestinationAddress
) {
243 SetLastError(ERROR_INVALID_NETNAME
);
247 /* Prepare the request */
248 id
=getpid() & 0xFFFF;
249 seq
=InterlockedIncrement(&icmp_sequence
) & 0xFFFF;
251 reqsize
=ICMP_MINLEN
+RequestSize
;
252 reqbuf
=HeapAlloc(GetProcessHeap(), 0, reqsize
);
254 SetLastError(ERROR_OUTOFMEMORY
);
258 icmp_header
=(struct icmp
*)reqbuf
;
259 icmp_header
->icmp_type
=ICMP_ECHO
;
260 icmp_header
->icmp_code
=0;
261 icmp_header
->icmp_cksum
=0;
262 icmp_header
->icmp_id
=id
;
263 icmp_header
->icmp_seq
=seq
;
264 memcpy(reqbuf
+ICMP_MINLEN
, RequestData
, RequestSize
);
265 icmp_header
->icmp_cksum
=cksum
=in_cksum((u_short
*)reqbuf
,reqsize
);
267 addr
.sin_family
=AF_INET
;
268 addr
.sin_addr
.s_addr
=DestinationAddress
;
271 if (RequestOptions
!=NULL
) {
273 if (icp
->default_opts
.OptionsSize
==IP_OPTS_UNKNOWN
) {
275 /* Before we mess with the options, get the default values */
277 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,&len
);
278 icp
->default_opts
.Ttl
=val
;
281 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,&len
);
282 icp
->default_opts
.Tos
=val
;
283 /* FIXME: missing: handling of IP 'flags', and all the other options */
286 val
=RequestOptions
->Ttl
;
287 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
288 val
=RequestOptions
->Tos
;
289 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,sizeof(val
));
290 /* FIXME: missing: handling of IP 'flags', and all the other options */
292 icp
->default_opts
.OptionsSize
=IP_OPTS_CUSTOM
;
293 } else if (icp
->default_opts
.OptionsSize
==IP_OPTS_CUSTOM
) {
296 /* Restore the default options */
297 val
=icp
->default_opts
.Ttl
;
298 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
299 val
=icp
->default_opts
.Tos
;
300 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,sizeof(val
));
301 /* FIXME: missing: handling of IP 'flags', and all the other options */
303 icp
->default_opts
.OptionsSize
=IP_OPTS_DEFAULT
;
306 /* Get ready for receiving the reply
307 * Do it before we send the request to minimize the risk of introducing delays
311 addrlen
=sizeof(addr
);
313 ip_header
=(struct ip
*) ((char *) ReplyBuffer
+sizeof(ICMP_ECHO_REPLY
));
314 endbuf
=(char *) ReplyBuffer
+ReplySize
;
315 maxlen
=ReplySize
-sizeof(ICMP_ECHO_REPLY
);
317 /* Send the packet */
318 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize
, RequestSize
, inet_ntoa(addr
.sin_addr
));
321 unsigned char* buf
=(unsigned char*)reqbuf
;
323 printf("Output buffer:\n");
324 for (i
=0;i
<reqsize
;i
++)
325 printf("%2x,", buf
[i
]);
330 send_time
= GetTickCount();
331 res
=sendto(icp
->sid
, reqbuf
, reqsize
, 0, (struct sockaddr
*)&addr
, sizeof(addr
));
332 HeapFree(GetProcessHeap (), 0, reqbuf
);
335 SetLastError(IP_PACKET_TOO_BIG
);
339 SetLastError(IP_DEST_NET_UNREACHABLE
);
342 SetLastError(IP_DEST_HOST_UNREACHABLE
);
345 TRACE("unknown error: errno=%d\n",errno
);
346 SetLastError(IP_GENERAL_FAILURE
);
353 ip_header_len
=0; /* because gcc was complaining */
354 while (poll(&fdr
,1,Timeout
)>0) {
355 recv_time
= GetTickCount();
356 res
=recvfrom(icp
->sid
, (char*)ip_header
, maxlen
, 0, (struct sockaddr
*)&addr
,&addrlen
);
357 TRACE("received %d bytes from %s\n",res
, inet_ntoa(addr
.sin_addr
));
358 ier
->Status
=IP_REQ_TIMED_OUT
;
360 /* Check whether we should ignore this packet */
361 if ((ip_header
->ip_p
==IPPROTO_ICMP
) && (res
>=sizeof(struct ip
)+ICMP_MINLEN
)) {
362 ip_header_len
=ip_header
->ip_hl
<< 2;
363 icmp_header
=(struct icmp
*)(((char*)ip_header
)+ip_header_len
);
364 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header
->icmp_type
,icmp_header
->icmp_code
);
365 if (icmp_header
->icmp_type
==ICMP_ECHOREPLY
) {
366 if ((icmp_header
->icmp_id
==id
) && (icmp_header
->icmp_seq
==seq
))
367 ier
->Status
=IP_SUCCESS
;
369 switch (icmp_header
->icmp_type
) {
371 switch (icmp_header
->icmp_code
) {
372 case ICMP_UNREACH_HOST
:
373 #ifdef ICMP_UNREACH_HOST_UNKNOWN
374 case ICMP_UNREACH_HOST_UNKNOWN
:
376 #ifdef ICMP_UNREACH_ISOLATED
377 case ICMP_UNREACH_ISOLATED
:
379 #ifdef ICMP_UNREACH_HOST_PROHIB
380 case ICMP_UNREACH_HOST_PROHIB
:
382 #ifdef ICMP_UNREACH_TOSHOST
383 case ICMP_UNREACH_TOSHOST
:
385 ier
->Status
=IP_DEST_HOST_UNREACHABLE
;
387 case ICMP_UNREACH_PORT
:
388 ier
->Status
=IP_DEST_PORT_UNREACHABLE
;
390 case ICMP_UNREACH_PROTOCOL
:
391 ier
->Status
=IP_DEST_PROT_UNREACHABLE
;
393 case ICMP_UNREACH_SRCFAIL
:
394 ier
->Status
=IP_BAD_ROUTE
;
397 ier
->Status
=IP_DEST_NET_UNREACHABLE
;
401 if (icmp_header
->icmp_code
==ICMP_TIMXCEED_REASS
)
402 ier
->Status
=IP_TTL_EXPIRED_REASSEM
;
404 ier
->Status
=IP_TTL_EXPIRED_TRANSIT
;
407 ier
->Status
=IP_PARAM_PROBLEM
;
409 case ICMP_SOURCEQUENCH
:
410 ier
->Status
=IP_SOURCE_QUENCH
;
413 if (ier
->Status
!=IP_REQ_TIMED_OUT
) {
414 struct ip
* rep_ip_header
;
415 struct icmp
* rep_icmp_header
;
416 /* The ICMP header size of all the packets we accept is the same */
417 rep_ip_header
=(struct ip
*)(((char*)icmp_header
)+ICMP_MINLEN
);
418 rep_icmp_header
=(struct icmp
*)(((char*)rep_ip_header
)+(rep_ip_header
->ip_hl
<< 2));
420 /* Make sure that this is really a reply to our packet */
421 if (ip_header_len
+ICMP_MINLEN
+(rep_ip_header
->ip_hl
<< 2)+ICMP_MINLEN
>ip_header
->ip_len
) {
422 ier
->Status
=IP_REQ_TIMED_OUT
;
423 } else if ((rep_icmp_header
->icmp_type
!=ICMP_ECHO
) ||
424 (rep_icmp_header
->icmp_code
!=0) ||
425 (rep_icmp_header
->icmp_id
!=id
) ||
426 /* windows doesn't check this checksum, else tracert */
427 /* behind a Linux 2.2 masquerading firewall would fail*/
428 /* (rep_icmp_header->icmp_cksum!=cksum) || */
429 (rep_icmp_header
->icmp_seq
!=seq
)) {
430 /* This was not a reply to one of our packets after all */
431 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
432 rep_icmp_header
->icmp_type
,rep_icmp_header
->icmp_code
,
433 rep_icmp_header
->icmp_id
,rep_icmp_header
->icmp_seq
,
434 rep_icmp_header
->icmp_cksum
);
435 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
438 ier
->Status
=IP_REQ_TIMED_OUT
;
444 if (ier
->Status
==IP_REQ_TIMED_OUT
) {
445 /* This packet was not for us.
446 * Decrease the timeout so that we don't enter an endless loop even
447 * if we get flooded with ICMP packets that are not for us.
449 DWORD t
= (recv_time
- send_time
);
450 if (Timeout
> t
) Timeout
-= t
;
454 /* This is a reply to our packet */
455 memcpy(&ier
->Address
,&ip_header
->ip_src
,sizeof(IPAddr
));
456 /* Status is already set */
457 ier
->RoundTripTime
= recv_time
- send_time
;
458 ier
->DataSize
=res
-ip_header_len
-ICMP_MINLEN
;
460 ier
->Data
=endbuf
-ier
->DataSize
;
461 memmove(ier
->Data
,((char*)ip_header
)+ip_header_len
+ICMP_MINLEN
,ier
->DataSize
);
462 ier
->Options
.Ttl
=ip_header
->ip_ttl
;
463 ier
->Options
.Tos
=ip_header
->ip_tos
;
464 ier
->Options
.Flags
=ip_header
->ip_off
>> 13;
465 ier
->Options
.OptionsSize
=ip_header_len
-sizeof(struct ip
);
466 if (ier
->Options
.OptionsSize
!=0) {
467 ier
->Options
.OptionsData
=(unsigned char *) ier
->Data
-ier
->Options
.OptionsSize
;
468 /* FIXME: We are supposed to rearrange the option's 'source route' data */
469 memmove(ier
->Options
.OptionsData
,((char*)ip_header
)+ip_header_len
,ier
->Options
.OptionsSize
);
470 endbuf
=(char*)ier
->Options
.OptionsData
;
472 ier
->Options
.OptionsData
=NULL
;
476 /* Prepare for the next packet */
478 ip_header
=(struct ip
*)(((char*)ip_header
)+sizeof(ICMP_ECHO_REPLY
));
479 maxlen
=endbuf
-(char*)ip_header
;
481 /* Check out whether there is more but don't wait this time */
485 res
=ier
-(ICMP_ECHO_REPLY
*)ReplyBuffer
;
487 SetLastError(IP_REQ_TIMED_OUT
);
488 TRACE("received %d replies\n",res
);
492 /***********************************************************************
493 * IcmpSendEcho2 (IPHLPAPI.@)
495 DWORD WINAPI
IcmpSendEcho2(
498 PIO_APC_ROUTINE ApcRoutine
,
500 IPAddr DestinationAddress
,
503 PIP_OPTION_INFORMATION RequestOptions
,
509 TRACE("(%p, %p, %p, %p, %08x, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle
,
510 Event
, ApcRoutine
, ApcContext
, DestinationAddress
, RequestData
,
511 RequestSize
, RequestOptions
, ReplyBuffer
, ReplySize
, Timeout
);
515 FIXME("unsupported for events\n");
520 FIXME("unsupported for APCs\n");
523 return IcmpSendEcho(IcmpHandle
, DestinationAddress
, RequestData
,
524 RequestSize
, RequestOptions
, ReplyBuffer
, ReplySize
, Timeout
);
527 /***********************************************************************
528 * IcmpSendEcho2Ex (IPHLPAPI.@)
530 DWORD WINAPI
IcmpSendEcho2Ex(
533 PIO_APC_ROUTINE ApcRoutine
,
535 IPAddr SourceAddress
,
536 IPAddr DestinationAddress
,
539 PIP_OPTION_INFORMATION RequestOptions
,
545 TRACE("(%p, %p, %p, %p, %08x, %08x, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle
,
546 Event
, ApcRoutine
, ApcContext
, SourceAddress
, DestinationAddress
, RequestData
,
547 RequestSize
, RequestOptions
, ReplyBuffer
, ReplySize
, Timeout
);
551 FIXME("unsupported for events\n");
556 FIXME("unsupported for APCs\n");
561 FIXME("unsupported for source addresses\n");
565 return IcmpSendEcho(IcmpHandle
, DestinationAddress
, RequestData
,
566 RequestSize
, RequestOptions
, ReplyBuffer
, ReplySize
, Timeout
);
570 * Copyright (c) 1989 The Regents of the University of California.
571 * All rights reserved.
573 * This code is derived from software contributed to Berkeley by
576 * Redistribution and use in source and binary forms, with or without
577 * modification, are permitted provided that the following conditions
579 * 1. Redistributions of source code must retain the above copyright
580 * notice, this list of conditions and the following disclaimer.
581 * 2. Redistributions in binary form must reproduce the above copyright
582 * notice, this list of conditions and the following disclaimer in the
583 * documentation and/or other materials provided with the distribution.
584 * 3. Neither the name of the University nor the names of its contributors
585 * may be used to endorse or promote products derived from this software
586 * without specific prior written permission.
588 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
589 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
590 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
591 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
592 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
593 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
594 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
595 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
596 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
597 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF