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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
36 #define WIN32_NO_STATUS
38 #define COM_NO_WINDOWS_H
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
49 #ifdef HAVE_NETINET_IN_SYSTM_H
50 # include <netinet/in_systm.h>
52 #ifdef HAVE_NETINET_IN_H
53 # include <netinet/in.h>
56 #ifdef HAVE_SYS_TIME_H
57 # include <sys/time.h>
65 #ifdef HAVE_ARPA_INET_H
66 # include <arpa/inet.h>
71 //#include "winerror.h"
73 //#include <ws2tcpip.h>
74 //#include "icmpapi.h"
75 #include <wine/debug.h>
77 /* Set up endiannes macros for the ip and ip_icmp BSD headers */
79 #define BIG_ENDIAN 4321
82 #define LITTLE_ENDIAN 1234
85 #ifdef WORDS_BIGENDIAN
86 #define BYTE_ORDER BIG_ENDIAN
88 #define BYTE_ORDER LITTLE_ENDIAN
90 #endif /* BYTE_ORDER */
92 #define u_int16_t WORD
93 #define u_int32_t DWORD
95 /* These are BSD headers. We use these here because they are needed on
96 * libc5 Linux systems. On other platforms they are usually simply more
97 * complete than the native stuff, and cause less portability problems
98 * so we use them anyway.
104 WINE_DEFAULT_DEBUG_CHANNEL(icmp
);
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 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
155 case DLL_PROCESS_ATTACH
:
156 WSAStartup(MAKEWORD(2, 2), &wsaData
);
159 case DLL_PROCESS_DETACH
:
166 /***********************************************************************
167 * IcmpCreateFile (ICMP.@)
169 HANDLE WINAPI
IcmpCreateFile(VOID
)
173 int sid
=socket(AF_INET
,SOCK_RAW
,IPPROTO_ICMP
);
175 MESSAGE("WARNING: Trying to use ICMP (network ping) will fail unless running as root\n");
176 SetLastError(ERROR_ACCESS_DENIED
);
177 return INVALID_HANDLE_VALUE
;
180 icp
=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp
));
183 SetLastError(IP_NO_RESOURCES
);
184 return INVALID_HANDLE_VALUE
;
187 icp
->default_opts
.OptionsSize
=IP_OPTS_UNKNOWN
;
192 /***********************************************************************
193 * IcmpCloseHandle (ICMP.@)
195 BOOL WINAPI
IcmpCloseHandle(HANDLE IcmpHandle
)
197 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
198 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
199 /* FIXME: in fact win98 seems to ignore the handle value !!! */
200 SetLastError(ERROR_INVALID_HANDLE
);
204 shutdown(icp
->sid
,2);
205 HeapFree(GetProcessHeap (), 0, icp
);
210 /***********************************************************************
211 * IcmpSendEcho (ICMP.@)
213 DWORD WINAPI
IcmpSendEcho(
215 IPAddr DestinationAddress
,
218 PIP_OPTION_INFORMATION RequestOptions
,
224 icmp_t
* icp
=(icmp_t
*)IcmpHandle
;
225 unsigned char* reqbuf
;
228 struct icmp_echo_reply
* ier
;
229 struct ip
* ip_header
;
230 struct icmp
* icmp_header
;
235 struct timeval timeout
;
236 DWORD send_time
,recv_time
;
237 struct sockaddr_in addr
;
238 unsigned int addrlen
;
239 unsigned short id
,seq
,cksum
;
242 if (IcmpHandle
==INVALID_HANDLE_VALUE
) {
243 /* FIXME: in fact win98 seems to ignore the handle value !!! */
244 SetLastError(ERROR_INVALID_HANDLE
);
248 if (ReplySize
<sizeof(ICMP_ECHO_REPLY
)+ICMP_MINLEN
) {
249 SetLastError(IP_BUF_TOO_SMALL
);
252 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
254 /* Prepare the request */
255 id
=GetCurrentProcessId() & 0xFFFF;
256 seq
=InterlockedIncrement(&icmp_sequence
) & 0xFFFF;
258 reqsize
=ICMP_MINLEN
+RequestSize
;
259 reqbuf
=HeapAlloc(GetProcessHeap(), 0, reqsize
);
261 SetLastError(ERROR_OUTOFMEMORY
);
265 icmp_header
=(struct icmp
*)reqbuf
;
266 icmp_header
->icmp_type
=ICMP_ECHO
;
267 icmp_header
->icmp_code
=0;
268 icmp_header
->icmp_cksum
=0;
269 icmp_header
->icmp_id
=id
;
270 icmp_header
->icmp_seq
=seq
;
271 memcpy(reqbuf
+ICMP_MINLEN
, RequestData
, RequestSize
);
272 icmp_header
->icmp_cksum
=cksum
=in_cksum((u_short
*)reqbuf
,reqsize
);
274 addr
.sin_family
=AF_INET
;
275 addr
.sin_addr
.s_addr
=DestinationAddress
;
278 if (RequestOptions
!=NULL
) {
280 if (icp
->default_opts
.OptionsSize
==IP_OPTS_UNKNOWN
) {
282 /* Before we mess with the options, get the default values */
284 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,&len
);
285 icp
->default_opts
.Ttl
=val
;
288 getsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,&len
);
289 icp
->default_opts
.Tos
=val
;
290 /* FIXME: missing: handling of IP 'flags', and all the other options */
293 val
=RequestOptions
->Ttl
;
294 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
295 val
=RequestOptions
->Tos
;
296 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_CUSTOM
;
300 } else if (icp
->default_opts
.OptionsSize
==IP_OPTS_CUSTOM
) {
303 /* Restore the default options */
304 val
=icp
->default_opts
.Ttl
;
305 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TTL
,(char *)&val
,sizeof(val
));
306 val
=icp
->default_opts
.Tos
;
307 setsockopt(icp
->sid
,IPPROTO_IP
,IP_TOS
,(char *)&val
,sizeof(val
));
308 /* FIXME: missing: handling of IP 'flags', and all the other options */
310 icp
->default_opts
.OptionsSize
=IP_OPTS_DEFAULT
;
313 /* Get ready for receiving the reply
314 * Do it before we send the request to minimize the risk of introducing delays
317 FD_SET(icp
->sid
,&fdr
);
318 timeout
.tv_sec
=Timeout
/1000;
319 timeout
.tv_usec
=(Timeout
% 1000)*1000;
320 addrlen
=sizeof(addr
);
322 ip_header
=(struct ip
*) ((char *) ReplyBuffer
+sizeof(ICMP_ECHO_REPLY
));
323 endbuf
=(char *) ReplyBuffer
+ReplySize
;
324 maxlen
=ReplySize
-sizeof(ICMP_ECHO_REPLY
);
326 /* Send the packet */
327 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize
, RequestSize
, inet_ntoa(addr
.sin_addr
));
330 unsigned char* buf
=(unsigned char*)reqbuf
;
332 printf("Output buffer:\n");
333 for (i
=0;i
<reqsize
;i
++)
334 printf("%2x,", buf
[i
]);
339 send_time
= GetTickCount();
340 res
=sendto(icp
->sid
, (const char*)reqbuf
, reqsize
, 0, (struct sockaddr
*)&addr
, sizeof(addr
));
341 HeapFree(GetProcessHeap (), 0, reqbuf
);
343 if (WSAGetLastError()==WSAEMSGSIZE
)
344 SetLastError(IP_PACKET_TOO_BIG
);
346 switch (WSAGetLastError()) {
348 SetLastError(IP_DEST_NET_UNREACHABLE
);
350 case WSAEHOSTUNREACH
:
351 SetLastError(IP_DEST_HOST_UNREACHABLE
);
354 TRACE("unknown error: errno=%d\n",WSAGetLastError());
355 SetLastError(IP_GENERAL_FAILURE
);
362 ip_header_len
=0; /* because gcc was complaining */
363 while ((res
=select(icp
->sid
+1,&fdr
,NULL
,NULL
,&timeout
))>0) {
364 recv_time
= GetTickCount();
365 res
=recvfrom(icp
->sid
, (char*)ip_header
, maxlen
, 0, (struct sockaddr
*)&addr
,(int*)&addrlen
);
366 TRACE("received %d bytes from %s\n",res
, inet_ntoa(addr
.sin_addr
));
367 ier
->Status
=IP_REQ_TIMED_OUT
;
369 /* Check whether we should ignore this packet */
370 if ((ip_header
->ip_p
==IPPROTO_ICMP
) && (res
>=sizeof(struct ip
)+ICMP_MINLEN
)) {
371 ip_header_len
=ip_header
->ip_hl
<< 2;
372 icmp_header
=(struct icmp
*)(((char*)ip_header
)+ip_header_len
);
373 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header
->icmp_type
,icmp_header
->icmp_code
);
374 if (icmp_header
->icmp_type
==ICMP_ECHOREPLY
) {
375 if ((icmp_header
->icmp_id
==id
) && (icmp_header
->icmp_seq
==seq
))
376 ier
->Status
=IP_SUCCESS
;
378 switch (icmp_header
->icmp_type
) {
380 switch (icmp_header
->icmp_code
) {
381 case ICMP_UNREACH_HOST
:
382 #ifdef ICMP_UNREACH_HOST_UNKNOWN
383 case ICMP_UNREACH_HOST_UNKNOWN
:
385 #ifdef ICMP_UNREACH_ISOLATED
386 case ICMP_UNREACH_ISOLATED
:
388 #ifdef ICMP_UNREACH_HOST_PROHIB
389 case ICMP_UNREACH_HOST_PROHIB
:
391 #ifdef ICMP_UNREACH_TOSHOST
392 case ICMP_UNREACH_TOSHOST
:
394 ier
->Status
=IP_DEST_HOST_UNREACHABLE
;
396 case ICMP_UNREACH_PORT
:
397 ier
->Status
=IP_DEST_PORT_UNREACHABLE
;
399 case ICMP_UNREACH_PROTOCOL
:
400 ier
->Status
=IP_DEST_PROT_UNREACHABLE
;
402 case ICMP_UNREACH_SRCFAIL
:
403 ier
->Status
=IP_BAD_ROUTE
;
406 ier
->Status
=IP_DEST_NET_UNREACHABLE
;
410 if (icmp_header
->icmp_code
==ICMP_TIMXCEED_REASS
)
411 ier
->Status
=IP_TTL_EXPIRED_REASSEM
;
413 ier
->Status
=IP_TTL_EXPIRED_TRANSIT
;
416 ier
->Status
=IP_PARAM_PROBLEM
;
418 case ICMP_SOURCEQUENCH
:
419 ier
->Status
=IP_SOURCE_QUENCH
;
422 if (ier
->Status
!=IP_REQ_TIMED_OUT
) {
423 struct ip
* rep_ip_header
;
424 struct icmp
* rep_icmp_header
;
425 /* The ICMP header size of all the packets we accept is the same */
426 rep_ip_header
=(struct ip
*)(((char*)icmp_header
)+ICMP_MINLEN
);
427 rep_icmp_header
=(struct icmp
*)(((char*)rep_ip_header
)+(rep_ip_header
->ip_hl
<< 2));
429 /* Make sure that this is really a reply to our packet */
430 if (ip_header_len
+ICMP_MINLEN
+(rep_ip_header
->ip_hl
<< 2)+ICMP_MINLEN
>ip_header
->ip_len
) {
431 ier
->Status
=IP_REQ_TIMED_OUT
;
432 } else if ((rep_icmp_header
->icmp_type
!=ICMP_ECHO
) ||
433 (rep_icmp_header
->icmp_code
!=0) ||
434 (rep_icmp_header
->icmp_id
!=id
) ||
435 /* windows doesn't check this checksum, else tracert */
436 /* behind a Linux 2.2 masquerading firewall would fail*/
437 /* (rep_icmp_header->icmp_cksum!=cksum) || */
438 (rep_icmp_header
->icmp_seq
!=seq
)) {
439 /* This was not a reply to one of our packets after all */
440 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
441 rep_icmp_header
->icmp_type
,rep_icmp_header
->icmp_code
,
442 rep_icmp_header
->icmp_id
,rep_icmp_header
->icmp_seq
,
443 rep_icmp_header
->icmp_cksum
);
444 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
447 ier
->Status
=IP_REQ_TIMED_OUT
;
453 if (ier
->Status
==IP_REQ_TIMED_OUT
) {
454 /* This packet was not for us.
455 * Decrease the timeout so that we don't enter an endless loop even
456 * if we get flooded with ICMP packets that are not for us.
458 int t
= Timeout
- (recv_time
- send_time
);
460 timeout
.tv_sec
= t
/ 1000;
461 timeout
.tv_usec
= (t
% 1000) * 1000;
464 /* This is a reply to our packet */
465 memcpy(&ier
->Address
,&ip_header
->ip_src
,sizeof(IPAddr
));
466 /* Status is already set */
467 ier
->RoundTripTime
= recv_time
- send_time
;
468 ier
->DataSize
=res
-ip_header_len
-ICMP_MINLEN
;
470 ier
->Data
=endbuf
-ier
->DataSize
;
471 memmove(ier
->Data
,((char*)ip_header
)+ip_header_len
+ICMP_MINLEN
,ier
->DataSize
);
472 ier
->Options
.Ttl
=ip_header
->ip_ttl
;
473 ier
->Options
.Tos
=ip_header
->ip_tos
;
474 ier
->Options
.Flags
=ip_header
->ip_off
>> 13;
475 ier
->Options
.OptionsSize
=ip_header_len
-sizeof(struct ip
);
476 if (ier
->Options
.OptionsSize
!=0) {
477 ier
->Options
.OptionsData
=(unsigned char *) ier
->Data
-ier
->Options
.OptionsSize
;
478 /* FIXME: We are supposed to rearrange the option's 'source route' data */
479 memmove(ier
->Options
.OptionsData
,((char*)ip_header
)+ip_header_len
,ier
->Options
.OptionsSize
);
480 endbuf
=(char*)ier
->Options
.OptionsData
;
482 ier
->Options
.OptionsData
=NULL
;
486 /* Prepare for the next packet */
488 ip_header
=(struct ip
*)(((char*)ip_header
)+sizeof(ICMP_ECHO_REPLY
));
489 maxlen
=endbuf
-(char*)ip_header
;
491 /* Check out whether there is more but don't wait this time */
496 FD_SET(icp
->sid
,&fdr
);
498 res
=ier
-(ICMP_ECHO_REPLY
*)ReplyBuffer
;
500 SetLastError(IP_REQ_TIMED_OUT
);
501 TRACE("received %d replies\n",res
);
506 * Copyright (c) 1989 The Regents of the University of California.
507 * All rights reserved.
509 * This code is derived from software contributed to Berkeley by
512 * Redistribution and use in source and binary forms, with or without
513 * modification, are permitted provided that the following conditions
515 * 1. Redistributions of source code must retain the above copyright
516 * notice, this list of conditions and the following disclaimer.
517 * 2. Redistributions in binary form must reproduce the above copyright
518 * notice, this list of conditions and the following disclaimer in the
519 * documentation and/or other materials provided with the distribution.
520 * 3. Neither the name of the University nor the names of its contributors
521 * may be used to endorse or promote products derived from this software
522 * without specific prior written permission.
524 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
525 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
526 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
527 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
528 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
529 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
530 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
531 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
532 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
533 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF