DGA compile fix.
[wine/gsoc_dplay.git] / dlls / icmp / icmp_main.c
blob8b86f5123f17ba5641806faec56ee5479aac9047
1 /*
2 * ICMP
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.
8 */
10 /* Future work:
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
17 * handle's structure?
18 * - This API should probably be thread safe. Is it really?
19 * - Using the winsock functions has not been tested.
22 #include "config.h"
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
27 #endif
28 #include <netdb.h>
29 #include <netinet/in_systm.h>
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
32 #endif
34 #include <sys/time.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #ifdef HAVE_ARPA_INET_H
39 # include <arpa/inet.h>
40 #endif
42 #include "windef.h"
43 #include "winbase.h"
44 #ifdef ICMP_WIN
45 #include "winsock.h"
46 #endif
48 #include "winerror.h"
49 #include "wine/ipexport.h"
50 #include "wine/icmpapi.h"
51 #include "debugtools.h"
53 /* Set up endiannes macros for the ip and ip_icmp BSD headers */
54 #ifndef BIG_ENDIAN
55 #define BIG_ENDIAN 4321
56 #endif
57 #ifndef LITTLE_ENDIAN
58 #define LITTLE_ENDIAN 1234
59 #endif
60 #ifndef BYTE_ORDER
61 #ifdef WORDS_BIGENDIAN
62 #define BYTE_ORDER BIG_ENDIAN
63 #else
64 #define BYTE_ORDER LITTLE_ENDIAN
65 #endif
66 #endif /* BYTE_ORDER */
68 #define u_int16_t WORD
69 #define u_int32_t DWORD
71 /* These are BSD headers. We use these here because they are needed on
72 * libc5 Linux systems. On other platforms they are usually simply more
73 * complete than the native stuff, and cause less portability problems
74 * so we use them anyway.
76 #include "ip.h"
77 #include "ip_icmp.h"
80 DEFAULT_DEBUG_CHANNEL(icmp)
82 /* Define the following macro to use the winsock functions */
83 /*#define ICMP_WIN*/
85 #ifdef ICMP_WIN
86 /* FIXME: should we include winsock.h ???*/
87 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol);
88 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags, struct sockaddr *to, INT tolen);
89 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf,INT len, INT flags, struct sockaddr *from, INT *fromlen32);
90 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how);
91 #endif
94 #ifdef ICMP_WIN
95 #define ISOCK_SOCKET SOCKET
96 #define ISOCK_ISVALID(a) ((a)!=INVALID_SOCKET)
97 #define ISOCK_getsockopt(a,b,c,d,e) WINSOCK_getsockopt(a,b,c,d,e)
98 #define ISOCK_recvfrom(a,b,c,d,e,f) WINSOCK_recvfrom(a,b,c,d,e,f)
99 #define ISOCK_select(a,b,c,d,e) WINSOCK_select(a,b,c,d,e)
100 #define ISOCK_sendto(a,b,c,d,e,f) WINSOCK_sendto(a,b,c,d,e,f)
101 #define ISOCK_setsockopt(a,b,c,d,e) WINSOCK_setsockopt(a,b,c,d,e)
102 #define ISOCK_shutdown(a,b) WINSOCK_shutdown(a,b)
103 #define ISOCK_socket(a,b,c) WINSOCK_socket(a,b,c)
104 #else
105 #define ISOCK_SOCKET int
106 #define ISOCK_ISVALID(a) ((a)>=0)
107 #define ISOCK_getsockopt(a,b,c,d,e) getsockopt(a,b,c,d,e)
108 #define ISOCK_recvfrom(a,b,c,d,e,f) recvfrom(a,b,c,d,e,f)
109 #define ISOCK_select(a,b,c,d,e) select(a,b,c,d,e)
110 #define ISOCK_setsockopt(a,b,c,d,e) setsockopt(a,b,c,d,e)
111 #define ISOCK_sendto(a,b,c,d,e,f) sendto(a,b,c,d,e,f)
112 #define ISOCK_shutdown(a,b) shutdown(a,b)
113 #define ISOCK_socket(a,b,c) socket(a,b,c)
114 #endif
116 typedef struct {
117 ISOCK_SOCKET sid;
118 IP_OPTION_INFORMATION default_opts;
119 } icmp_t;
121 #define IP_OPTS_UNKNOWN 0
122 #define IP_OPTS_DEFAULT 1
123 #define IP_OPTS_CUSTOM 2
125 /* The sequence number is unique process wide, so that all threads
126 * have a distinct sequence number.
128 static LONG icmp_sequence=0;
130 static int in_cksum(u_short *addr, int len)
132 int nleft=len;
133 u_short *w = addr;
134 int sum = 0;
135 u_short answer = 0;
137 while (nleft > 1) {
138 sum += *w++;
139 nleft -= 2;
142 if (nleft == 1) {
143 *(u_char *)(&answer) = *(u_char *)w;
144 sum += answer;
147 sum = (sum >> 16) + (sum & 0xffff);
148 sum += (sum >> 16);
149 answer = ~sum;
150 return(answer);
156 * Exported Routines.
159 /***********************************************************************
160 * IcmpCreateFile
162 HANDLE WINAPI IcmpCreateFile(VOID)
164 icmp_t* icp;
166 ISOCK_SOCKET sid=ISOCK_socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
167 if (!ISOCK_ISVALID(sid)) {
168 MESSAGE("WARNING: Trying to use ICMP will fail unless running as root\n");
169 SetLastError(ERROR_ACCESS_DENIED);
170 return INVALID_HANDLE_VALUE;
173 icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
174 if (icp==NULL) {
175 SetLastError(IP_NO_RESOURCES);
176 return INVALID_HANDLE_VALUE;
178 icp->sid=sid;
179 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
180 return (HANDLE)icp;
184 /***********************************************************************
185 * IcmpCloseHandle
187 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
189 icmp_t* icp=(icmp_t*)IcmpHandle;
190 if (IcmpHandle==INVALID_HANDLE_VALUE) {
191 /* FIXME: in fact win98 seems to ignore the handle value !!! */
192 SetLastError(ERROR_INVALID_HANDLE);
193 return FALSE;
196 ISOCK_shutdown(icp->sid,2);
197 HeapFree(GetProcessHeap (), 0, icp);
198 return TRUE;
202 /***********************************************************************
203 * IcmpSendEcho
205 DWORD WINAPI IcmpSendEcho(
206 HANDLE IcmpHandle,
207 IPAddr DestinationAddress,
208 LPVOID RequestData,
209 WORD RequestSize,
210 PIP_OPTION_INFORMATION RequestOptions,
211 LPVOID ReplyBuffer,
212 DWORD ReplySize,
213 DWORD Timeout
216 icmp_t* icp=(icmp_t*)IcmpHandle;
217 unsigned char* reqbuf;
218 int reqsize;
220 struct icmp_echo_reply* ier;
221 struct ip* ip_header;
222 struct icmp* icmp_header;
223 char* endbuf;
224 int ip_header_len;
225 int maxlen;
226 fd_set fdr;
227 struct timeval timeout,send_time,recv_time;
228 struct sockaddr_in addr;
229 int addrlen;
230 unsigned short id,seq,cksum;
231 int res;
233 if (IcmpHandle==INVALID_HANDLE_VALUE) {
234 /* FIXME: in fact win98 seems to ignore the handle value !!! */
235 SetLastError(ERROR_INVALID_HANDLE);
236 return 0;
239 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
240 SetLastError(IP_BUF_TOO_SMALL);
241 return 0;
243 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
245 /* Prepare the request */
246 id=getpid() & 0xFFFF;
247 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
249 reqsize=ICMP_MINLEN+RequestSize;
250 reqbuf=HeapAlloc(GetProcessHeap(), 0, reqsize);
251 if (reqbuf==NULL) {
252 SetLastError(ERROR_OUTOFMEMORY);
253 return 0;
256 icmp_header=(struct icmp*)reqbuf;
257 icmp_header->icmp_type=ICMP_ECHO;
258 icmp_header->icmp_code=0;
259 icmp_header->icmp_cksum=0;
260 icmp_header->icmp_id=id;
261 icmp_header->icmp_seq=seq;
262 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
263 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
265 addr.sin_family=AF_INET;
266 addr.sin_addr.s_addr=DestinationAddress;
267 addr.sin_port=0;
269 if (RequestOptions!=NULL) {
270 int val;
271 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
272 int len;
273 /* Before we mess with the options, get the default values */
274 len=sizeof(val);
275 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
276 icp->default_opts.Ttl=val;
278 len=sizeof(val);
279 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
280 icp->default_opts.Tos=val;
281 /* FIXME: missing: handling of IP 'flags', and all the other options */
284 val=RequestOptions->Ttl;
285 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
286 val=RequestOptions->Tos;
287 ISOCK_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_CUSTOM;
291 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
292 int val;
294 /* Restore the default options */
295 val=icp->default_opts.Ttl;
296 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
297 val=icp->default_opts.Tos;
298 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
299 /* FIXME: missing: handling of IP 'flags', and all the other options */
301 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
304 /* Get ready for receiving the reply
305 * Do it before we send the request to minimize the risk of introducing delays
307 FD_ZERO(&fdr);
308 FD_SET(icp->sid,&fdr);
309 timeout.tv_sec=Timeout/1000;
310 timeout.tv_usec=(Timeout % 1000)*1000;
311 addrlen=sizeof(addr);
312 ier=ReplyBuffer;
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));
319 #if 0
320 if (TRACE_ON(icmp)){
321 unsigned char* buf=(unsigned char*)reqbuf;
322 int i;
323 printf("Output buffer:\n");
324 for (i=0;i<reqsize;i++)
325 printf("%2x,", buf[i]);
326 printf("\n");
328 #endif
330 gettimeofday(&send_time,NULL);
331 res=ISOCK_sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
332 HeapFree(GetProcessHeap (), 0, reqbuf);
333 if (res<0) {
334 if (errno==EMSGSIZE)
335 SetLastError(IP_PACKET_TOO_BIG);
336 else {
337 switch (errno) {
338 case ENETUNREACH:
339 SetLastError(IP_DEST_NET_UNREACHABLE);
340 break;
341 case EHOSTUNREACH:
342 SetLastError(IP_DEST_NET_UNREACHABLE);
343 break;
344 default:
345 TRACE("unknown error: errno=%d\n",errno);
346 SetLastError(ERROR_UNKNOWN);
349 return 0;
352 /* Get the reply */
353 ip_header_len=0; /* because gcc was complaining */
354 while ((res=ISOCK_select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
355 gettimeofday(&recv_time,NULL);
356 res=ISOCK_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;
368 } else {
369 switch (icmp_header->icmp_type) {
370 case ICMP_UNREACH:
371 switch (icmp_header->icmp_code) {
372 case ICMP_UNREACH_HOST:
373 #ifdef ICMP_UNREACH_HOST_UNKNOWN
374 case ICMP_UNREACH_HOST_UNKNOWN:
375 #endif
376 #ifdef ICMP_UNREACH_ISOLATED
377 case ICMP_UNREACH_ISOLATED:
378 #endif
379 #ifdef ICMP_UNREACH_HOST_PROHIB
380 case ICMP_UNREACH_HOST_PROHIB:
381 #endif
382 #ifdef ICMP_UNREACH_TOSHOST
383 case ICMP_UNREACH_TOSHOST:
384 #endif
385 ier->Status=IP_DEST_HOST_UNREACHABLE;
386 break;
387 case ICMP_UNREACH_PORT:
388 ier->Status=IP_DEST_PORT_UNREACHABLE;
389 break;
390 case ICMP_UNREACH_PROTOCOL:
391 ier->Status=IP_DEST_PROT_UNREACHABLE;
392 break;
393 case ICMP_UNREACH_SRCFAIL:
394 ier->Status=IP_BAD_ROUTE;
395 break;
396 default:
397 ier->Status=IP_DEST_NET_UNREACHABLE;
399 break;
400 case ICMP_TIMXCEED:
401 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
402 ier->Status=IP_TTL_EXPIRED_REASSEM;
403 else
404 ier->Status=IP_TTL_EXPIRED_TRANSIT;
405 break;
406 case ICMP_PARAMPROB:
407 ier->Status=IP_PARAM_PROBLEM;
408 break;
409 case ICMP_SOURCEQUENCH:
410 ier->Status=IP_SOURCE_QUENCH;
411 break;
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 (rep_icmp_header->icmp_seq!=seq) ||
427 (rep_icmp_header->icmp_cksum!=cksum)) {
428 /* This was not a reply to one of our packets after all */
429 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
430 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
431 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
432 rep_icmp_header->icmp_cksum);
433 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
434 id,seq,
435 cksum);
436 ier->Status=IP_REQ_TIMED_OUT;
442 if (ier->Status==IP_REQ_TIMED_OUT) {
443 /* This packet was not for us.
444 * Decrease the timeout so that we don't enter an endless loop even
445 * if we get flooded with ICMP packets that are not for us.
447 timeout.tv_sec=Timeout/1000-(recv_time.tv_sec-send_time.tv_sec);
448 timeout.tv_usec=(Timeout % 1000)*1000+send_time.tv_usec-(recv_time.tv_usec-send_time.tv_usec);
449 if (timeout.tv_usec<0) {
450 timeout.tv_usec+=1000000;
451 timeout.tv_sec--;
453 continue;
454 } else {
455 /* This is a reply to our packet */
456 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
457 /* Status is already set */
458 ier->RoundTripTime=(recv_time.tv_sec-send_time.tv_sec)*1000+(recv_time.tv_usec-send_time.tv_usec)/1000;
459 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
460 ier->Reserved=0;
461 ier->Data=endbuf-ier->DataSize;
462 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
463 ier->Options.Ttl=ip_header->ip_ttl;
464 ier->Options.Tos=ip_header->ip_tos;
465 ier->Options.Flags=ip_header->ip_off >> 13;
466 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
467 if (ier->Options.OptionsSize!=0) {
468 ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
469 /* FIXME: We are supposed to rearrange the option's 'source route' data */
470 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
471 endbuf=ier->Options.OptionsData;
472 } else {
473 ier->Options.OptionsData=NULL;
474 endbuf=ier->Data;
477 /* Prepare for the next packet */
478 ier++;
479 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
480 maxlen=endbuf-(char*)ip_header;
482 /* Check out whether there is more but don't wait this time */
483 timeout.tv_sec=0;
484 timeout.tv_usec=0;
486 FD_ZERO(&fdr);
487 FD_SET(icp->sid,&fdr);
489 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
490 if (res==0)
491 SetLastError(IP_REQ_TIMED_OUT);
492 TRACE("received %d replies\n",res);
493 return res;
497 * Copyright (c) 1989 The Regents of the University of California.
498 * All rights reserved.
500 * This code is derived from software contributed to Berkeley by
501 * Mike Muuss.
503 * Redistribution and use in source and binary forms, with or without
504 * modification, are permitted provided that the following conditions
505 * are met:
506 * 1. Redistributions of source code must retain the above copyright
507 * notice, this list of conditions and the following disclaimer.
508 * 2. Redistributions in binary form must reproduce the above copyright
509 * notice, this list of conditions and the following disclaimer in the
510 * documentation and/or other materials provided with the distribution.
511 * 3. All advertising materials mentioning features or use of this software
512 * must display the following acknowledgement:
513 * This product includes software developed by the University of
514 * California, Berkeley and its contributors.
515 * 4. Neither the name of the University nor the names of its contributors
516 * may be used to endorse or promote products derived from this software
517 * without specific prior written permission.
519 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
520 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
521 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
522 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
523 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
524 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
525 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
526 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
527 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
528 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
529 * SUCH DAMAGE.