Ignore the LPSURFACE bit if ALLOCONLOAD is set.
[wine/multimedia.git] / dlls / icmp / icmp_main.c
blobc9b1af05fab6f24f7be49842c3db443e363d6021
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.
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
24 /* Future work:
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
31 * handle's structure?
32 * - This API should probably be thread safe. Is it really?
33 * - Using the winsock functions has not been tested.
36 #include "config.h"
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
41 #endif
42 #ifdef HAVE_NETDB_H
43 # include <netdb.h>
44 #endif
45 #ifdef HAVE_NETINET_IN_SYSTM_H
46 # include <netinet/in_systm.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
50 #endif
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
54 #endif
55 #include <string.h>
56 #include <errno.h>
57 #ifdef HAVE_UNISTD_H
58 # include <unistd.h>
59 #endif
60 #ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
62 #endif
64 #include "windef.h"
65 #include "winbase.h"
66 #ifdef ICMP_WIN
67 #include "winsock2.h"
68 #endif
70 #include "winerror.h"
71 #include "ipexport.h"
72 #include "icmpapi.h"
73 #include "wine/debug.h"
75 /* Set up endiannes macros for the ip and ip_icmp BSD headers */
76 #ifndef BIG_ENDIAN
77 #define BIG_ENDIAN 4321
78 #endif
79 #ifndef LITTLE_ENDIAN
80 #define LITTLE_ENDIAN 1234
81 #endif
82 #ifndef BYTE_ORDER
83 #ifdef WORDS_BIGENDIAN
84 #define BYTE_ORDER BIG_ENDIAN
85 #else
86 #define BYTE_ORDER LITTLE_ENDIAN
87 #endif
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.
98 #include "ip.h"
99 #include "ip_icmp.h"
102 WINE_DEFAULT_DEBUG_CHANNEL(icmp);
104 /* Define the following macro to use the winsock functions */
105 /*#define ICMP_WIN*/
107 #ifdef ICMP_WIN
108 /* FIXME: should we include winsock.h ???*/
109 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol);
110 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags, struct sockaddr *to, INT tolen);
111 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf,INT len, INT flags, struct sockaddr *from, INT *fromlen32);
112 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how);
113 #endif
116 #ifdef ICMP_WIN
117 #define ISOCK_SOCKET SOCKET
118 #define ISOCK_ISVALID(a) ((a)!=INVALID_SOCKET)
119 #define ISOCK_getsockopt(a,b,c,d,e) WINSOCK_getsockopt(a,b,c,d,e)
120 #define ISOCK_recvfrom(a,b,c,d,e,f) WINSOCK_recvfrom(a,b,c,d,e,f)
121 #define ISOCK_select(a,b,c,d,e) WINSOCK_select(a,b,c,d,e)
122 #define ISOCK_sendto(a,b,c,d,e,f) WINSOCK_sendto(a,b,c,d,e,f)
123 #define ISOCK_setsockopt(a,b,c,d,e) WINSOCK_setsockopt(a,b,c,d,e)
124 #define ISOCK_shutdown(a,b) WINSOCK_shutdown(a,b)
125 #define ISOCK_socket(a,b,c) WINSOCK_socket(a,b,c)
126 #else
127 #define ISOCK_SOCKET int
128 #define ISOCK_ISVALID(a) ((a)>=0)
129 #define ISOCK_getsockopt(a,b,c,d,e) getsockopt(a,b,c,d,e)
130 #define ISOCK_recvfrom(a,b,c,d,e,f) recvfrom(a,b,c,d,e,f)
131 #define ISOCK_select(a,b,c,d,e) select(a,b,c,d,e)
132 #define ISOCK_setsockopt(a,b,c,d,e) setsockopt(a,b,c,d,e)
133 #define ISOCK_sendto(a,b,c,d,e,f) sendto(a,b,c,d,e,f)
134 #define ISOCK_shutdown(a,b) shutdown(a,b)
135 #define ISOCK_socket(a,b,c) socket(a,b,c)
136 #endif
138 typedef struct {
139 ISOCK_SOCKET sid;
140 IP_OPTION_INFORMATION default_opts;
141 } icmp_t;
143 #define IP_OPTS_UNKNOWN 0
144 #define IP_OPTS_DEFAULT 1
145 #define IP_OPTS_CUSTOM 2
147 /* The sequence number is unique process wide, so that all threads
148 * have a distinct sequence number.
150 static LONG icmp_sequence=0;
152 static int in_cksum(u_short *addr, int len)
154 int nleft=len;
155 u_short *w = addr;
156 int sum = 0;
157 u_short answer = 0;
159 while (nleft > 1) {
160 sum += *w++;
161 nleft -= 2;
164 if (nleft == 1) {
165 *(u_char *)(&answer) = *(u_char *)w;
166 sum += answer;
169 sum = (sum >> 16) + (sum & 0xffff);
170 sum += (sum >> 16);
171 answer = ~sum;
172 return(answer);
178 * Exported Routines.
181 /***********************************************************************
182 * IcmpCreateFile (ICMP.@)
184 HANDLE WINAPI IcmpCreateFile(VOID)
186 icmp_t* icp;
188 ISOCK_SOCKET sid=ISOCK_socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
189 if (!ISOCK_ISVALID(sid)) {
190 MESSAGE("WARNING: Trying to use ICMP (network ping) will fail unless running as root\n");
191 SetLastError(ERROR_ACCESS_DENIED);
192 return INVALID_HANDLE_VALUE;
195 icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
196 if (icp==NULL) {
197 SetLastError(IP_NO_RESOURCES);
198 return INVALID_HANDLE_VALUE;
200 icp->sid=sid;
201 icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
202 return (HANDLE)icp;
206 /***********************************************************************
207 * IcmpCloseHandle (ICMP.@)
209 BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
211 icmp_t* icp=(icmp_t*)IcmpHandle;
212 if (IcmpHandle==INVALID_HANDLE_VALUE) {
213 /* FIXME: in fact win98 seems to ignore the handle value !!! */
214 SetLastError(ERROR_INVALID_HANDLE);
215 return FALSE;
218 ISOCK_shutdown(icp->sid,2);
219 HeapFree(GetProcessHeap (), 0, icp);
220 return TRUE;
224 /***********************************************************************
225 * IcmpSendEcho (ICMP.@)
227 DWORD WINAPI IcmpSendEcho(
228 HANDLE IcmpHandle,
229 IPAddr DestinationAddress,
230 LPVOID RequestData,
231 WORD RequestSize,
232 PIP_OPTION_INFORMATION RequestOptions,
233 LPVOID ReplyBuffer,
234 DWORD ReplySize,
235 DWORD Timeout
238 icmp_t* icp=(icmp_t*)IcmpHandle;
239 unsigned char* reqbuf;
240 int reqsize;
242 struct icmp_echo_reply* ier;
243 struct ip* ip_header;
244 struct icmp* icmp_header;
245 char* endbuf;
246 int ip_header_len;
247 int maxlen;
248 fd_set fdr;
249 struct timeval timeout,send_time,recv_time;
250 struct sockaddr_in addr;
251 int addrlen;
252 unsigned short id,seq,cksum;
253 int res;
255 if (IcmpHandle==INVALID_HANDLE_VALUE) {
256 /* FIXME: in fact win98 seems to ignore the handle value !!! */
257 SetLastError(ERROR_INVALID_HANDLE);
258 return 0;
261 if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
262 SetLastError(IP_BUF_TOO_SMALL);
263 return 0;
265 /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
267 /* Prepare the request */
268 id=getpid() & 0xFFFF;
269 seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
271 reqsize=ICMP_MINLEN+RequestSize;
272 reqbuf=HeapAlloc(GetProcessHeap(), 0, reqsize);
273 if (reqbuf==NULL) {
274 SetLastError(ERROR_OUTOFMEMORY);
275 return 0;
278 icmp_header=(struct icmp*)reqbuf;
279 icmp_header->icmp_type=ICMP_ECHO;
280 icmp_header->icmp_code=0;
281 icmp_header->icmp_cksum=0;
282 icmp_header->icmp_id=id;
283 icmp_header->icmp_seq=seq;
284 memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
285 icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
287 addr.sin_family=AF_INET;
288 addr.sin_addr.s_addr=DestinationAddress;
289 addr.sin_port=0;
291 if (RequestOptions!=NULL) {
292 int val;
293 if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
294 int len;
295 /* Before we mess with the options, get the default values */
296 len=sizeof(val);
297 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
298 icp->default_opts.Ttl=val;
300 len=sizeof(val);
301 ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
302 icp->default_opts.Tos=val;
303 /* FIXME: missing: handling of IP 'flags', and all the other options */
306 val=RequestOptions->Ttl;
307 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
308 val=RequestOptions->Tos;
309 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
310 /* FIXME: missing: handling of IP 'flags', and all the other options */
312 icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
313 } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
314 int val;
316 /* Restore the default options */
317 val=icp->default_opts.Ttl;
318 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
319 val=icp->default_opts.Tos;
320 ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
321 /* FIXME: missing: handling of IP 'flags', and all the other options */
323 icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
326 /* Get ready for receiving the reply
327 * Do it before we send the request to minimize the risk of introducing delays
329 FD_ZERO(&fdr);
330 FD_SET(icp->sid,&fdr);
331 timeout.tv_sec=Timeout/1000;
332 timeout.tv_usec=(Timeout % 1000)*1000;
333 addrlen=sizeof(addr);
334 ier=ReplyBuffer;
335 ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
336 endbuf=(char *) ReplyBuffer+ReplySize;
337 maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
339 /* Send the packet */
340 TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
341 #if 0
342 if (TRACE_ON(icmp)){
343 unsigned char* buf=(unsigned char*)reqbuf;
344 int i;
345 printf("Output buffer:\n");
346 for (i=0;i<reqsize;i++)
347 printf("%2x,", buf[i]);
348 printf("\n");
350 #endif
352 gettimeofday(&send_time,NULL);
353 res=ISOCK_sendto(icp->sid, reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
354 HeapFree(GetProcessHeap (), 0, reqbuf);
355 if (res<0) {
356 if (errno==EMSGSIZE)
357 SetLastError(IP_PACKET_TOO_BIG);
358 else {
359 switch (errno) {
360 case ENETUNREACH:
361 SetLastError(IP_DEST_NET_UNREACHABLE);
362 break;
363 case EHOSTUNREACH:
364 SetLastError(IP_DEST_NET_UNREACHABLE);
365 break;
366 default:
367 TRACE("unknown error: errno=%d\n",errno);
368 SetLastError(ERROR_UNKNOWN);
371 return 0;
374 /* Get the reply */
375 ip_header_len=0; /* because gcc was complaining */
376 while ((res=ISOCK_select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
377 gettimeofday(&recv_time,NULL);
378 res=ISOCK_recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
379 TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
380 ier->Status=IP_REQ_TIMED_OUT;
382 /* Check whether we should ignore this packet */
383 if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
384 ip_header_len=ip_header->ip_hl << 2;
385 icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
386 TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
387 if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
388 if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
389 ier->Status=IP_SUCCESS;
390 } else {
391 switch (icmp_header->icmp_type) {
392 case ICMP_UNREACH:
393 switch (icmp_header->icmp_code) {
394 case ICMP_UNREACH_HOST:
395 #ifdef ICMP_UNREACH_HOST_UNKNOWN
396 case ICMP_UNREACH_HOST_UNKNOWN:
397 #endif
398 #ifdef ICMP_UNREACH_ISOLATED
399 case ICMP_UNREACH_ISOLATED:
400 #endif
401 #ifdef ICMP_UNREACH_HOST_PROHIB
402 case ICMP_UNREACH_HOST_PROHIB:
403 #endif
404 #ifdef ICMP_UNREACH_TOSHOST
405 case ICMP_UNREACH_TOSHOST:
406 #endif
407 ier->Status=IP_DEST_HOST_UNREACHABLE;
408 break;
409 case ICMP_UNREACH_PORT:
410 ier->Status=IP_DEST_PORT_UNREACHABLE;
411 break;
412 case ICMP_UNREACH_PROTOCOL:
413 ier->Status=IP_DEST_PROT_UNREACHABLE;
414 break;
415 case ICMP_UNREACH_SRCFAIL:
416 ier->Status=IP_BAD_ROUTE;
417 break;
418 default:
419 ier->Status=IP_DEST_NET_UNREACHABLE;
421 break;
422 case ICMP_TIMXCEED:
423 if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
424 ier->Status=IP_TTL_EXPIRED_REASSEM;
425 else
426 ier->Status=IP_TTL_EXPIRED_TRANSIT;
427 break;
428 case ICMP_PARAMPROB:
429 ier->Status=IP_PARAM_PROBLEM;
430 break;
431 case ICMP_SOURCEQUENCH:
432 ier->Status=IP_SOURCE_QUENCH;
433 break;
435 if (ier->Status!=IP_REQ_TIMED_OUT) {
436 struct ip* rep_ip_header;
437 struct icmp* rep_icmp_header;
438 /* The ICMP header size of all the packets we accept is the same */
439 rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
440 rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
442 /* Make sure that this is really a reply to our packet */
443 if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
444 ier->Status=IP_REQ_TIMED_OUT;
445 } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
446 (rep_icmp_header->icmp_code!=0) ||
447 (rep_icmp_header->icmp_id!=id) ||
448 /* windows doesn't check this checksum, else tracert */
449 /* behind a Linux 2.2 masquerading firewall would fail*/
450 /* (rep_icmp_header->icmp_cksum!=cksum) || */
451 (rep_icmp_header->icmp_seq!=seq)) {
452 /* This was not a reply to one of our packets after all */
453 TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
454 rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
455 rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
456 rep_icmp_header->icmp_cksum);
457 TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
458 id,seq,
459 cksum);
460 ier->Status=IP_REQ_TIMED_OUT;
466 if (ier->Status==IP_REQ_TIMED_OUT) {
467 /* This packet was not for us.
468 * Decrease the timeout so that we don't enter an endless loop even
469 * if we get flooded with ICMP packets that are not for us.
471 timeout.tv_sec=Timeout/1000-(recv_time.tv_sec-send_time.tv_sec);
472 timeout.tv_usec=(Timeout % 1000)*1000+send_time.tv_usec-(recv_time.tv_usec-send_time.tv_usec);
473 if (timeout.tv_usec<0) {
474 timeout.tv_usec+=1000000;
475 timeout.tv_sec--;
477 continue;
478 } else {
479 /* This is a reply to our packet */
480 memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
481 /* Status is already set */
482 ier->RoundTripTime=(recv_time.tv_sec-send_time.tv_sec)*1000+(recv_time.tv_usec-send_time.tv_usec)/1000;
483 ier->DataSize=res-ip_header_len-ICMP_MINLEN;
484 ier->Reserved=0;
485 ier->Data=endbuf-ier->DataSize;
486 memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
487 ier->Options.Ttl=ip_header->ip_ttl;
488 ier->Options.Tos=ip_header->ip_tos;
489 ier->Options.Flags=ip_header->ip_off >> 13;
490 ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
491 if (ier->Options.OptionsSize!=0) {
492 ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
493 /* FIXME: We are supposed to rearrange the option's 'source route' data */
494 memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
495 endbuf=ier->Options.OptionsData;
496 } else {
497 ier->Options.OptionsData=NULL;
498 endbuf=ier->Data;
501 /* Prepare for the next packet */
502 ier++;
503 ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
504 maxlen=endbuf-(char*)ip_header;
506 /* Check out whether there is more but don't wait this time */
507 timeout.tv_sec=0;
508 timeout.tv_usec=0;
510 FD_ZERO(&fdr);
511 FD_SET(icp->sid,&fdr);
513 res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
514 if (res==0)
515 SetLastError(IP_REQ_TIMED_OUT);
516 TRACE("received %d replies\n",res);
517 return res;
521 * Copyright (c) 1989 The Regents of the University of California.
522 * All rights reserved.
524 * This code is derived from software contributed to Berkeley by
525 * Mike Muuss.
527 * Redistribution and use in source and binary forms, with or without
528 * modification, are permitted provided that the following conditions
529 * are met:
530 * 1. Redistributions of source code must retain the above copyright
531 * notice, this list of conditions and the following disclaimer.
532 * 2. Redistributions in binary form must reproduce the above copyright
533 * notice, this list of conditions and the following disclaimer in the
534 * documentation and/or other materials provided with the distribution.
535 * 3. Neither the name of the University nor the names of its contributors
536 * may be used to endorse or promote products derived from this software
537 * without specific prior written permission.
539 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
540 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
541 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
542 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
543 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
544 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
545 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
546 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
547 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
548 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
549 * SUCH DAMAGE.