| 1 | /*
|
|---|
| 2 | * ICMP
|
|---|
| 3 | *
|
|---|
| 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 | */
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include <sys/types.h>
|
|---|
| 23 | #include <time.h>
|
|---|
| 24 | #include <malloc.h>
|
|---|
| 25 | #include <string.h>
|
|---|
| 26 | #include <errno.h>
|
|---|
| 27 |
|
|---|
| 28 | #include "windef.h"
|
|---|
| 29 | #include "winbase.h"
|
|---|
| 30 | #include "winsock.h"
|
|---|
| 31 | #include "winerror.h"
|
|---|
| 32 |
|
|---|
| 33 | #include <netinet/in_systm.h>
|
|---|
| 34 | #include <netinet/ip.h>
|
|---|
| 35 |
|
|---|
| 36 | #include <process.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <ipexport.h>
|
|---|
| 39 | #include <icmpapi.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "icmp.h"
|
|---|
| 42 |
|
|---|
| 43 | #include <misc.h>
|
|---|
| 44 |
|
|---|
| 45 | #define ISOCK_SOCKET SOCKET
|
|---|
| 46 | #define ISOCK_ISVALID(a) ((a)!=INVALID_SOCKET)
|
|---|
| 47 | #define ISOCK_getsockopt(a,b,c,d,e) getsockopt(a,b,c,d,e)
|
|---|
| 48 | #define ISOCK_recvfrom(a,b,c,d,e,f) recvfrom(a,b,c,d,e,f)
|
|---|
| 49 | #define ISOCK_select(a,b,c,d,e) select(a,b,c,d,e)
|
|---|
| 50 | #define ISOCK_sendto(a,b,c,d,e,f) sendto(a,b,c,d,e,f)
|
|---|
| 51 | #define ISOCK_setsockopt(a,b,c,d,e) setsockopt(a,b,c,d,e)
|
|---|
| 52 | #define ISOCK_shutdown(a,b) shutdown(a,b)
|
|---|
| 53 | #define ISOCK_socket(a,b,c) socket(a,b,c)
|
|---|
| 54 |
|
|---|
| 55 | typedef struct {
|
|---|
| 56 | ISOCK_SOCKET sid;
|
|---|
| 57 | IP_OPTION_INFORMATION default_opts;
|
|---|
| 58 | } icmp_t;
|
|---|
| 59 |
|
|---|
| 60 | #define IP_OPTS_UNKNOWN 0
|
|---|
| 61 | #define IP_OPTS_DEFAULT 1
|
|---|
| 62 | #define IP_OPTS_CUSTOM 2
|
|---|
| 63 |
|
|---|
| 64 | /* The sequence number is unique process wide, so that all threads
|
|---|
| 65 | * have a distinct sequence number.
|
|---|
| 66 | */
|
|---|
| 67 | static LONG icmp_sequence=0;
|
|---|
| 68 |
|
|---|
| 69 | /* Odin specific - private def of gettimeofday func */
|
|---|
| 70 | void gettimeofday(struct timeval *t,struct timezone *z)
|
|---|
| 71 | {
|
|---|
| 72 | t->tv_sec=clock();
|
|---|
| 73 | t->tv_usec=0;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static int in_cksum(u_short *addr, int len)
|
|---|
| 77 | {
|
|---|
| 78 | int nleft=len;
|
|---|
| 79 | u_short *w = addr;
|
|---|
| 80 | int sum = 0;
|
|---|
| 81 | u_short answer = 0;
|
|---|
| 82 |
|
|---|
| 83 | while (nleft > 1) {
|
|---|
| 84 | sum += *w++;
|
|---|
| 85 | nleft -= 2;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (nleft == 1) {
|
|---|
| 89 | *(u_char *)(&answer) = *(u_char *)w;
|
|---|
| 90 | sum += answer;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | sum = (sum >> 16) + (sum & 0xffff);
|
|---|
| 94 | sum += (sum >> 16);
|
|---|
| 95 | answer = ~sum;
|
|---|
| 96 | return(answer);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * Exported Routines.
|
|---|
| 103 | */
|
|---|
| 104 |
|
|---|
| 105 | /***********************************************************************
|
|---|
| 106 | * IcmpCreateFile
|
|---|
| 107 | */
|
|---|
| 108 | HANDLE WINAPI IcmpCreateFile(VOID)
|
|---|
| 109 | {
|
|---|
| 110 | icmp_t* icp;
|
|---|
| 111 |
|
|---|
| 112 | ISOCK_SOCKET sid=ISOCK_socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
|
|---|
| 113 | if (!ISOCK_ISVALID(sid)) {
|
|---|
| 114 | SetLastError(ERROR_ACCESS_DENIED);
|
|---|
| 115 | return INVALID_HANDLE_VALUE;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | icp=(icmp_t *)malloc(sizeof(*icp));
|
|---|
| 119 | if (icp==NULL) {
|
|---|
| 120 | SetLastError(IP_NO_RESOURCES);
|
|---|
| 121 | return INVALID_HANDLE_VALUE;
|
|---|
| 122 | }
|
|---|
| 123 | icp->sid=sid;
|
|---|
| 124 | icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
|
|---|
| 125 | return (HANDLE)icp;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | /***********************************************************************
|
|---|
| 130 | * IcmpCloseHandle
|
|---|
| 131 | */
|
|---|
| 132 | BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
|
|---|
| 133 | {
|
|---|
| 134 | icmp_t* icp=(icmp_t*)IcmpHandle;
|
|---|
| 135 | if (IcmpHandle==INVALID_HANDLE_VALUE) {
|
|---|
| 136 | /* FIXME: in fact win98 seems to ignore the handle value !!! */
|
|---|
| 137 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 138 | return FALSE;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | ISOCK_shutdown(icp->sid,2);
|
|---|
| 142 | free(icp);
|
|---|
| 143 | return TRUE;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | /***********************************************************************
|
|---|
| 148 | * IcmpSendEcho
|
|---|
| 149 | */
|
|---|
| 150 | DWORD WINAPI IcmpSendEcho(
|
|---|
| 151 | HANDLE IcmpHandle,
|
|---|
| 152 | IPAddr DestinationAddress,
|
|---|
| 153 | LPVOID RequestData,
|
|---|
| 154 | WORD RequestSize,
|
|---|
| 155 | PIP_OPTION_INFORMATION RequestOptions,
|
|---|
| 156 | LPVOID ReplyBuffer,
|
|---|
| 157 | DWORD ReplySize,
|
|---|
| 158 | DWORD Timeout
|
|---|
| 159 | )
|
|---|
| 160 | {
|
|---|
| 161 | icmp_t* icp=(icmp_t*)IcmpHandle;
|
|---|
| 162 | unsigned char* reqbuf;
|
|---|
| 163 | int reqsize;
|
|---|
| 164 |
|
|---|
| 165 | struct icmp_echo_reply* ier;
|
|---|
| 166 | struct ip* ip_header;
|
|---|
| 167 | struct icmp* icmp_header;
|
|---|
| 168 | char* endbuf;
|
|---|
| 169 | int ip_header_len;
|
|---|
| 170 | int maxlen;
|
|---|
| 171 | fd_set fdr;
|
|---|
| 172 | struct timeval timeout,send_time,recv_time;
|
|---|
| 173 | struct sockaddr_in addr;
|
|---|
| 174 | int addrlen;
|
|---|
| 175 | unsigned short id,seq,cksum;
|
|---|
| 176 | int res;
|
|---|
| 177 |
|
|---|
| 178 | if (IcmpHandle==INVALID_HANDLE_VALUE) {
|
|---|
| 179 | /* FIXME: in fact win98 seems to ignore the handle value !!! */
|
|---|
| 180 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 181 | return 0;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
|
|---|
| 185 | SetLastError(IP_BUF_TOO_SMALL);
|
|---|
| 186 | return 0;
|
|---|
| 187 | }
|
|---|
| 188 | /* check the request size against SO_MAX_MSG_SIZE using getsockopt */
|
|---|
| 189 |
|
|---|
| 190 | /* Prepare the request */
|
|---|
| 191 | id=getpid() & 0xFFFF;
|
|---|
| 192 | seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
|
|---|
| 193 |
|
|---|
| 194 | reqsize=ICMP_MINLEN+RequestSize;
|
|---|
| 195 | reqbuf=(unsigned char *)malloc(reqsize);
|
|---|
| 196 | if (reqbuf==NULL) {
|
|---|
| 197 | SetLastError(ERROR_OUTOFMEMORY);
|
|---|
| 198 | return 0;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | icmp_header=(struct icmp*)reqbuf;
|
|---|
| 202 | icmp_header->icmp_type=ICMP_ECHO;
|
|---|
| 203 | icmp_header->icmp_code=0;
|
|---|
| 204 | icmp_header->icmp_cksum=0;
|
|---|
| 205 | icmp_header->icmp_id=id;
|
|---|
| 206 | icmp_header->icmp_seq=seq;
|
|---|
| 207 | memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
|
|---|
| 208 | icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
|
|---|
| 209 |
|
|---|
| 210 | addr.sin_family=AF_INET;
|
|---|
| 211 | addr.sin_addr.s_addr=DestinationAddress;
|
|---|
| 212 | addr.sin_port=0;
|
|---|
| 213 |
|
|---|
| 214 | if (RequestOptions!=NULL) {
|
|---|
| 215 | int val;
|
|---|
| 216 | if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
|
|---|
| 217 | int len;
|
|---|
| 218 | /* Before we mess with the options, get the default values */
|
|---|
| 219 | len=sizeof(val);
|
|---|
| 220 | ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
|
|---|
| 221 | icp->default_opts.Ttl=val;
|
|---|
| 222 |
|
|---|
| 223 | len=sizeof(val);
|
|---|
| 224 | ISOCK_getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
|
|---|
| 225 | icp->default_opts.Tos=val;
|
|---|
| 226 | /* FIXME: missing: handling of IP 'flags', and all the other options */
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | val=RequestOptions->Ttl;
|
|---|
| 230 | ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
|
|---|
| 231 | val=RequestOptions->Tos;
|
|---|
| 232 | ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
|
|---|
| 233 | /* FIXME: missing: handling of IP 'flags', and all the other options */
|
|---|
| 234 |
|
|---|
| 235 | icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
|
|---|
| 236 | } else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
|
|---|
| 237 | int val;
|
|---|
| 238 |
|
|---|
| 239 | /* Restore the default options */
|
|---|
| 240 | val=icp->default_opts.Ttl;
|
|---|
| 241 | ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
|
|---|
| 242 | val=icp->default_opts.Tos;
|
|---|
| 243 | ISOCK_setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
|
|---|
| 244 | /* FIXME: missing: handling of IP 'flags', and all the other options */
|
|---|
| 245 |
|
|---|
| 246 | icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /* Get ready for receiving the reply
|
|---|
| 250 | * Do it before we send the request to minimize the risk of introducing delays
|
|---|
| 251 | */
|
|---|
| 252 | FD_ZERO(&fdr);
|
|---|
| 253 | FD_SET(icp->sid,&fdr);
|
|---|
| 254 | timeout.tv_sec=Timeout/1000;
|
|---|
| 255 | timeout.tv_usec=(Timeout % 1000)*1000;
|
|---|
| 256 | addrlen=sizeof(addr);
|
|---|
| 257 | ier=(struct icmp_echo_reply *)ReplyBuffer;
|
|---|
| 258 | ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
|
|---|
| 259 | endbuf=(char *) ReplyBuffer+ReplySize;
|
|---|
| 260 | maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
|
|---|
| 261 |
|
|---|
| 262 | /* Send the packet */
|
|---|
| 263 | dprintf(("ICMP: Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr)));
|
|---|
| 264 |
|
|---|
| 265 | gettimeofday(&send_time,NULL);
|
|---|
| 266 | res=ISOCK_sendto(icp->sid, (const char *)reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
|
|---|
| 267 | free(reqbuf);
|
|---|
| 268 | if (res<0) {
|
|---|
| 269 | if (errno==WSAEMSGSIZE)
|
|---|
| 270 | SetLastError(IP_PACKET_TOO_BIG);
|
|---|
| 271 | else {
|
|---|
| 272 | switch (errno) {
|
|---|
| 273 | case WSAENETUNREACH:
|
|---|
| 274 | SetLastError(IP_DEST_NET_UNREACHABLE);
|
|---|
| 275 | break;
|
|---|
| 276 | case WSAEHOSTUNREACH:
|
|---|
| 277 | SetLastError(IP_DEST_NET_UNREACHABLE);
|
|---|
| 278 | break;
|
|---|
| 279 | default:
|
|---|
| 280 | dprintf(("ICMP: unknown error: errno=%d\n",errno));
|
|---|
| 281 | SetLastError(ERROR_UNKNOWN);
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 | return 0;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /* Get the reply */
|
|---|
| 288 | ip_header_len=0; /* because gcc was complaining */
|
|---|
| 289 | while ((res=ISOCK_select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
|
|---|
| 290 | gettimeofday(&recv_time,NULL);
|
|---|
| 291 | res=ISOCK_recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,&addrlen);
|
|---|
| 292 | dprintf(("ICMP: received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr)));
|
|---|
| 293 | ier->Status=IP_REQ_TIMED_OUT;
|
|---|
| 294 |
|
|---|
| 295 | /* Check whether we should ignore this packet */
|
|---|
| 296 | if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
|
|---|
| 297 | ip_header_len=ip_header->ip_hl << 2;
|
|---|
| 298 | icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
|
|---|
| 299 | dprintf(("ICMP: received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code));
|
|---|
| 300 | if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
|
|---|
| 301 | if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
|
|---|
| 302 | ier->Status=IP_SUCCESS;
|
|---|
| 303 | } else {
|
|---|
| 304 | switch (icmp_header->icmp_type) {
|
|---|
| 305 | case ICMP_UNREACH:
|
|---|
| 306 | switch (icmp_header->icmp_code) {
|
|---|
| 307 | case ICMP_UNREACH_HOST:
|
|---|
| 308 | #ifdef ICMP_UNREACH_HOST_UNKNOWN
|
|---|
| 309 | case ICMP_UNREACH_HOST_UNKNOWN:
|
|---|
| 310 | #endif
|
|---|
| 311 | #ifdef ICMP_UNREACH_ISOLATED
|
|---|
| 312 | case ICMP_UNREACH_ISOLATED:
|
|---|
| 313 | #endif
|
|---|
| 314 | #ifdef ICMP_UNREACH_HOST_PROHIB
|
|---|
| 315 | case ICMP_UNREACH_HOST_PROHIB:
|
|---|
| 316 | #endif
|
|---|
| 317 | #ifdef ICMP_UNREACH_TOSHOST
|
|---|
| 318 | case ICMP_UNREACH_TOSHOST:
|
|---|
| 319 | #endif
|
|---|
| 320 | ier->Status=IP_DEST_HOST_UNREACHABLE;
|
|---|
| 321 | break;
|
|---|
| 322 | case ICMP_UNREACH_PORT:
|
|---|
| 323 | ier->Status=IP_DEST_PORT_UNREACHABLE;
|
|---|
| 324 | break;
|
|---|
| 325 | case ICMP_UNREACH_PROTOCOL:
|
|---|
| 326 | ier->Status=IP_DEST_PROT_UNREACHABLE;
|
|---|
| 327 | break;
|
|---|
| 328 | case ICMP_UNREACH_SRCFAIL:
|
|---|
| 329 | ier->Status=IP_BAD_ROUTE;
|
|---|
| 330 | break;
|
|---|
| 331 | default:
|
|---|
| 332 | ier->Status=IP_DEST_NET_UNREACHABLE;
|
|---|
| 333 | }
|
|---|
| 334 | break;
|
|---|
| 335 | case ICMP_TIMXCEED:
|
|---|
| 336 | if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
|
|---|
| 337 | ier->Status=IP_TTL_EXPIRED_REASSEM;
|
|---|
| 338 | else
|
|---|
| 339 | ier->Status=IP_TTL_EXPIRED_TRANSIT;
|
|---|
| 340 | break;
|
|---|
| 341 | case ICMP_PARAMPROB:
|
|---|
| 342 | ier->Status=IP_PARAM_PROBLEM;
|
|---|
| 343 | break;
|
|---|
| 344 | case ICMP_SOURCEQUENCH:
|
|---|
| 345 | ier->Status=IP_SOURCE_QUENCH;
|
|---|
| 346 | break;
|
|---|
| 347 | }
|
|---|
| 348 | if (ier->Status!=IP_REQ_TIMED_OUT) {
|
|---|
| 349 | struct ip* rep_ip_header;
|
|---|
| 350 | struct icmp* rep_icmp_header;
|
|---|
| 351 | /* The ICMP header size of all the packets we accept is the same */
|
|---|
| 352 | rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
|
|---|
| 353 | rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
|
|---|
| 354 |
|
|---|
| 355 | /* Make sure that this is really a reply to our packet */
|
|---|
| 356 | if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
|
|---|
| 357 | ier->Status=IP_REQ_TIMED_OUT;
|
|---|
| 358 | } else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
|
|---|
| 359 | (rep_icmp_header->icmp_code!=0) ||
|
|---|
| 360 | (rep_icmp_header->icmp_id!=id) ||
|
|---|
| 361 | (rep_icmp_header->icmp_seq!=seq) ||
|
|---|
| 362 | (rep_icmp_header->icmp_cksum!=cksum)) {
|
|---|
| 363 | /* This was not a reply to one of our packets after all */
|
|---|
| 364 | dprintf(("ICMP: skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
|
|---|
| 365 | rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
|
|---|
| 366 | rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
|
|---|
| 367 | rep_icmp_header->icmp_cksum));
|
|---|
| 368 | dprintf(("ICMP: expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
|
|---|
| 369 | id,seq,
|
|---|
| 370 | cksum));
|
|---|
| 371 | ier->Status=IP_REQ_TIMED_OUT;
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | if (ier->Status==IP_REQ_TIMED_OUT) {
|
|---|
| 378 | /* This packet was not for us.
|
|---|
| 379 | * Decrease the timeout so that we don't enter an endless loop even
|
|---|
| 380 | * if we get flooded with ICMP packets that are not for us.
|
|---|
| 381 | */
|
|---|
| 382 | timeout.tv_sec=Timeout/1000-(recv_time.tv_sec-send_time.tv_sec);
|
|---|
| 383 | timeout.tv_usec=(Timeout % 1000)*1000+send_time.tv_usec-(recv_time.tv_usec-send_time.tv_usec);
|
|---|
| 384 | if (timeout.tv_usec<0) {
|
|---|
| 385 | timeout.tv_usec+=1000000;
|
|---|
| 386 | timeout.tv_sec--;
|
|---|
| 387 | }
|
|---|
| 388 | continue;
|
|---|
| 389 | } else {
|
|---|
| 390 | /* This is a reply to our packet */
|
|---|
| 391 | memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
|
|---|
| 392 | /* Status is already set */
|
|---|
| 393 | ier->RoundTripTime=(recv_time.tv_sec-send_time.tv_sec)*1000+(recv_time.tv_usec-send_time.tv_usec)/1000;
|
|---|
| 394 | ier->DataSize=res-ip_header_len-ICMP_MINLEN;
|
|---|
| 395 | ier->Reserved=0;
|
|---|
| 396 | ier->Data=endbuf-ier->DataSize;
|
|---|
| 397 | memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
|
|---|
| 398 | ier->Options.Ttl=ip_header->ip_ttl;
|
|---|
| 399 | ier->Options.Tos=ip_header->ip_tos;
|
|---|
| 400 | ier->Options.Flags=ip_header->ip_off >> 13;
|
|---|
| 401 | ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
|
|---|
| 402 | if (ier->Options.OptionsSize!=0) {
|
|---|
| 403 | ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
|
|---|
| 404 | /* FIXME: We are supposed to rearrange the option's 'source route' data */
|
|---|
| 405 | memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
|
|---|
| 406 | endbuf=(char *)ier->Options.OptionsData;
|
|---|
| 407 | } else {
|
|---|
| 408 | ier->Options.OptionsData=NULL;
|
|---|
| 409 | endbuf=(char *)ier->Data;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /* Prepare for the next packet */
|
|---|
| 413 | ier++;
|
|---|
| 414 | ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
|
|---|
| 415 | maxlen=endbuf-(char*)ip_header;
|
|---|
| 416 |
|
|---|
| 417 | /* Check out whether there is more but don't wait this time */
|
|---|
| 418 | timeout.tv_sec=0;
|
|---|
| 419 | timeout.tv_usec=0;
|
|---|
| 420 | }
|
|---|
| 421 | FD_ZERO(&fdr);
|
|---|
| 422 | FD_SET(icp->sid,&fdr);
|
|---|
| 423 | }
|
|---|
| 424 | res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
|
|---|
| 425 | if (res==0)
|
|---|
| 426 | SetLastError(IP_REQ_TIMED_OUT);
|
|---|
| 427 | dprintf(("ICMP: received %d replies\n",res));
|
|---|
| 428 | return res;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /*
|
|---|
| 432 | * Copyright (c) 1989 The Regents of the University of California.
|
|---|
| 433 | * All rights reserved.
|
|---|
| 434 | *
|
|---|
| 435 | * This code is derived from software contributed to Berkeley by
|
|---|
| 436 | * Mike Muuss.
|
|---|
| 437 | *
|
|---|
| 438 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 439 | * modification, are permitted provided that the following conditions
|
|---|
| 440 | * are met:
|
|---|
| 441 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 442 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 443 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 444 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 445 | * documentation and/or other materials provided with the distribution.
|
|---|
| 446 | * 3. All advertising materials mentioning features or use of this software
|
|---|
| 447 | * must display the following acknowledgement:
|
|---|
| 448 | * This product includes software developed by the University of
|
|---|
| 449 | * California, Berkeley and its contributors.
|
|---|
| 450 | * 4. Neither the name of the University nor the names of its contributors
|
|---|
| 451 | * may be used to endorse or promote products derived from this software
|
|---|
| 452 | * without specific prior written permission.
|
|---|
| 453 | *
|
|---|
| 454 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 455 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 456 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 457 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 458 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 459 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 460 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 461 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 462 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 463 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 464 | * SUCH DAMAGE.
|
|---|
| 465 | *
|
|---|
| 466 | */
|
|---|