1999-11-07 22:22:17 +01:00
|
|
|
/*
|
|
|
|
* ICMP
|
|
|
|
*
|
|
|
|
* Francois Gouget, 1999, based on the work of
|
2002-06-01 01:06:46 +02:00
|
|
|
* RW Hall, 1999, based on public domain code PING.C by Mike Muus (1983)
|
|
|
|
* and later works (c) 1989 Regents of Univ. of California - see copyright
|
1999-11-07 22:22:17 +01:00
|
|
|
* notice at end of source-code.
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-11-07 22:22:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Future work:
|
|
|
|
* - Systems like FreeBSD don't seem to support the IP_TTL option and maybe others.
|
|
|
|
* But using IP_HDRINCL and building the IP header by hand might work.
|
|
|
|
* - Not all IP options are supported.
|
2002-06-01 01:06:46 +02:00
|
|
|
* - Are ICMP handles real handles, i.e. inheritable and all? There might be some
|
1999-11-07 22:22:17 +01:00
|
|
|
* more work to do here, including server side stuff with synchronization.
|
|
|
|
* - This API should probably be thread safe. Is it really?
|
|
|
|
* - Using the winsock functions has not been tested.
|
|
|
|
*/
|
|
|
|
|
1999-11-10 21:21:37 +01:00
|
|
|
#include "config.h"
|
1999-11-07 22:22:17 +01:00
|
|
|
|
1999-11-10 21:21:37 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2000-11-26 05:00:53 +01:00
|
|
|
#ifdef HAVE_NETDB_H
|
2000-12-02 00:53:46 +01:00
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_SYSTM_H
|
|
|
|
# include <netinet/in_systm.h>
|
2000-11-26 05:00:53 +01:00
|
|
|
#endif
|
1999-11-10 21:21:37 +01:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
1999-11-14 00:02:06 +01:00
|
|
|
|
2002-08-17 02:43:16 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-11-07 22:22:17 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2002-08-17 02:43:16 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
1999-11-10 21:21:37 +01:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
2006-11-06 07:29:27 +01:00
|
|
|
#ifdef HAVE_SYS_POLL_H
|
|
|
|
# include <sys/poll.h>
|
|
|
|
#endif
|
1999-11-07 22:22:17 +01:00
|
|
|
|
2011-12-09 12:00:39 +01:00
|
|
|
#define USE_WS_PREFIX
|
|
|
|
|
1999-11-07 22:22:17 +01:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2008-07-17 18:40:22 +02:00
|
|
|
#include "winternl.h"
|
2002-12-03 23:08:14 +01:00
|
|
|
#include "ipexport.h"
|
|
|
|
#include "icmpapi.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-11-07 22:22:17 +01:00
|
|
|
|
2008-04-03 19:10:30 +02:00
|
|
|
/* Set up endianness macros for the ip and ip_icmp BSD headers */
|
1999-11-14 00:02:06 +01:00
|
|
|
#ifndef BIG_ENDIAN
|
|
|
|
#define BIG_ENDIAN 4321
|
|
|
|
#endif
|
|
|
|
#ifndef LITTLE_ENDIAN
|
|
|
|
#define LITTLE_ENDIAN 1234
|
|
|
|
#endif
|
|
|
|
#ifndef BYTE_ORDER
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
#define BYTE_ORDER BIG_ENDIAN
|
|
|
|
#else
|
|
|
|
#define BYTE_ORDER LITTLE_ENDIAN
|
|
|
|
#endif
|
|
|
|
#endif /* BYTE_ORDER */
|
|
|
|
|
1999-11-21 01:48:06 +01:00
|
|
|
#define u_int16_t WORD
|
|
|
|
#define u_int32_t DWORD
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* These are BSD headers. We use these here because they are needed on
|
|
|
|
* libc5 Linux systems. On other platforms they are usually simply more
|
|
|
|
* complete than the native stuff, and cause less portability problems
|
1999-11-14 00:02:06 +01:00
|
|
|
* so we use them anyway.
|
|
|
|
*/
|
|
|
|
#include "ip.h"
|
|
|
|
#include "ip_icmp.h"
|
|
|
|
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(icmp);
|
2009-12-08 14:14:21 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(winediag);
|
1999-11-07 22:22:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2003-04-04 21:33:07 +02:00
|
|
|
int sid;
|
1999-11-07 22:22:17 +01:00
|
|
|
IP_OPTION_INFORMATION default_opts;
|
|
|
|
} icmp_t;
|
|
|
|
|
|
|
|
#define IP_OPTS_UNKNOWN 0
|
|
|
|
#define IP_OPTS_DEFAULT 1
|
|
|
|
#define IP_OPTS_CUSTOM 2
|
|
|
|
|
2018-08-05 05:04:43 +02:00
|
|
|
#define MAXIPLEN 60
|
|
|
|
#define MAXICMPLEN 76
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* The sequence number is unique process wide, so that all threads
|
1999-11-07 22:22:17 +01:00
|
|
|
* have a distinct sequence number.
|
|
|
|
*/
|
|
|
|
static LONG icmp_sequence=0;
|
|
|
|
|
|
|
|
static int in_cksum(u_short *addr, int len)
|
|
|
|
{
|
|
|
|
int nleft=len;
|
|
|
|
u_short *w = addr;
|
|
|
|
int sum = 0;
|
|
|
|
u_short answer = 0;
|
|
|
|
|
|
|
|
while (nleft > 1) {
|
|
|
|
sum += *w++;
|
|
|
|
nleft -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nleft == 1) {
|
|
|
|
*(u_char *)(&answer) = *(u_char *)w;
|
|
|
|
sum += answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = (sum >> 16) + (sum & 0xffff);
|
|
|
|
sum += (sum >> 16);
|
|
|
|
answer = ~sum;
|
|
|
|
return(answer);
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:47:41 +01:00
|
|
|
/* Receive a reply (IPv4); this function uses, takes ownership of and will always free `buffer` */
|
|
|
|
static DWORD icmp_get_reply(int sid, unsigned char *buffer, DWORD send_time, void *reply_buf, DWORD reply_size, DWORD timeout)
|
|
|
|
{
|
|
|
|
int repsize = MAXIPLEN + MAXICMPLEN + min(65535, reply_size);
|
|
|
|
struct icmp *icmp_header = (struct icmp*)buffer;
|
|
|
|
char *endbuf = (char*)reply_buf + reply_size;
|
|
|
|
struct ip *ip_header = (struct ip*)buffer;
|
|
|
|
struct icmp_echo_reply *ier = reply_buf;
|
|
|
|
unsigned short id, seq, cksum;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int ip_header_len = 0;
|
|
|
|
socklen_t addrlen;
|
|
|
|
struct pollfd fdr;
|
|
|
|
DWORD recv_time;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
id = icmp_header->icmp_id;
|
|
|
|
seq = icmp_header->icmp_seq;
|
|
|
|
cksum = icmp_header->icmp_cksum;
|
|
|
|
fdr.fd = sid;
|
|
|
|
fdr.events = POLLIN;
|
|
|
|
addrlen = sizeof(addr);
|
|
|
|
|
|
|
|
while (poll(&fdr,1,timeout)>0) {
|
|
|
|
recv_time = GetTickCount();
|
|
|
|
res=recvfrom(sid, buffer, repsize, 0, (struct sockaddr*)&addr, &addrlen);
|
|
|
|
TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
|
|
|
|
ier->Status=IP_REQ_TIMED_OUT;
|
|
|
|
|
|
|
|
/* Check whether we should ignore this packet */
|
|
|
|
if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
|
|
|
|
ip_header_len=ip_header->ip_hl << 2;
|
|
|
|
icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
|
|
|
|
TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
|
|
|
|
if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
|
|
|
|
if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
|
|
|
|
{
|
|
|
|
ier->Status=IP_SUCCESS;
|
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (icmp_header->icmp_type) {
|
|
|
|
case ICMP_UNREACH:
|
|
|
|
switch (icmp_header->icmp_code) {
|
|
|
|
case ICMP_UNREACH_HOST:
|
|
|
|
#ifdef ICMP_UNREACH_HOST_UNKNOWN
|
|
|
|
case ICMP_UNREACH_HOST_UNKNOWN:
|
|
|
|
#endif
|
|
|
|
#ifdef ICMP_UNREACH_ISOLATED
|
|
|
|
case ICMP_UNREACH_ISOLATED:
|
|
|
|
#endif
|
|
|
|
#ifdef ICMP_UNREACH_HOST_PROHIB
|
|
|
|
case ICMP_UNREACH_HOST_PROHIB:
|
|
|
|
#endif
|
|
|
|
#ifdef ICMP_UNREACH_TOSHOST
|
|
|
|
case ICMP_UNREACH_TOSHOST:
|
|
|
|
#endif
|
|
|
|
ier->Status=IP_DEST_HOST_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_PORT:
|
|
|
|
ier->Status=IP_DEST_PORT_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_PROTOCOL:
|
|
|
|
ier->Status=IP_DEST_PROT_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_SRCFAIL:
|
|
|
|
ier->Status=IP_BAD_ROUTE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ier->Status=IP_DEST_NET_UNREACHABLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICMP_TIMXCEED:
|
|
|
|
if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
|
|
|
|
ier->Status=IP_TTL_EXPIRED_REASSEM;
|
|
|
|
else
|
|
|
|
ier->Status=IP_TTL_EXPIRED_TRANSIT;
|
|
|
|
break;
|
|
|
|
case ICMP_PARAMPROB:
|
|
|
|
ier->Status=IP_PARAM_PROBLEM;
|
|
|
|
break;
|
|
|
|
case ICMP_SOURCEQUENCH:
|
|
|
|
ier->Status=IP_SOURCE_QUENCH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ier->Status!=IP_REQ_TIMED_OUT) {
|
|
|
|
struct ip* rep_ip_header;
|
|
|
|
struct icmp* rep_icmp_header;
|
|
|
|
/* The ICMP header size of all the packets we accept is the same */
|
|
|
|
rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
|
|
|
|
rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
|
|
|
|
|
|
|
|
/* Make sure that this is really a reply to our packet */
|
|
|
|
if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
|
|
|
|
ier->Status=IP_REQ_TIMED_OUT;
|
|
|
|
} else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
|
|
|
|
(rep_icmp_header->icmp_code!=0) ||
|
|
|
|
(rep_icmp_header->icmp_id!=id) ||
|
|
|
|
/* windows doesn't check this checksum, else tracert */
|
|
|
|
/* behind a Linux 2.2 masquerading firewall would fail*/
|
|
|
|
/* (rep_icmp_header->icmp_cksum!=cksum) || */
|
|
|
|
(rep_icmp_header->icmp_seq!=seq)) {
|
|
|
|
/* This was not a reply to one of our packets after all */
|
|
|
|
TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
|
|
|
|
rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
|
|
|
|
rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
|
|
|
|
rep_icmp_header->icmp_cksum);
|
|
|
|
TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
|
|
|
|
id,seq,
|
|
|
|
cksum);
|
|
|
|
ier->Status=IP_REQ_TIMED_OUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ier->Status==IP_REQ_TIMED_OUT) {
|
|
|
|
/* This packet was not for us.
|
|
|
|
* Decrease the timeout so that we don't enter an endless loop even
|
|
|
|
* if we get flooded with ICMP packets that are not for us.
|
|
|
|
*/
|
|
|
|
DWORD t = (recv_time - send_time);
|
|
|
|
if (timeout > t) timeout -= t;
|
|
|
|
else timeout = 0;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Check free space, should be large enough for an ICMP_ECHO_REPLY and remainning icmp data */
|
|
|
|
if (endbuf-(char *)ier < sizeof(struct icmp_echo_reply)+(res-ip_header_len-ICMP_MINLEN)) {
|
|
|
|
res=ier-(ICMP_ECHO_REPLY *)reply_buf;
|
|
|
|
SetLastError(IP_GENERAL_FAILURE);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* This is a reply to our packet */
|
|
|
|
memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
|
|
|
|
/* Status is already set */
|
|
|
|
ier->RoundTripTime= recv_time - send_time;
|
|
|
|
ier->DataSize=res-ip_header_len-ICMP_MINLEN;
|
|
|
|
ier->Reserved=0;
|
|
|
|
ier->Data=endbuf-ier->DataSize;
|
|
|
|
memcpy(ier->Data, ((char *)ip_header)+ip_header_len+ICMP_MINLEN, ier->DataSize);
|
|
|
|
ier->Options.Ttl=ip_header->ip_ttl;
|
|
|
|
ier->Options.Tos=ip_header->ip_tos;
|
|
|
|
ier->Options.Flags=ip_header->ip_off >> 13;
|
|
|
|
ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
|
|
|
|
if (ier->Options.OptionsSize!=0) {
|
|
|
|
ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
|
|
|
|
/* FIXME: We are supposed to rearrange the option's 'source route' data */
|
|
|
|
memcpy(ier->Options.OptionsData, ((char *)ip_header)+ip_header_len, ier->Options.OptionsSize);
|
|
|
|
endbuf=(char*)ier->Options.OptionsData;
|
|
|
|
} else {
|
|
|
|
ier->Options.OptionsData=NULL;
|
|
|
|
endbuf=ier->Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare for the next packet */
|
|
|
|
ier++;
|
|
|
|
|
|
|
|
/* Check out whether there is more but don't wait this time */
|
|
|
|
timeout=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res=ier-(ICMP_ECHO_REPLY*)reply_buf;
|
|
|
|
if (res==0)
|
|
|
|
SetLastError(IP_REQ_TIMED_OUT);
|
|
|
|
done:
|
2020-11-23 14:47:44 +01:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
/* Move the data so there's no gap between it and the ICMP_ECHO_REPLY array */
|
|
|
|
DWORD gap_size = endbuf - (char*)ier;
|
|
|
|
|
|
|
|
if (gap_size)
|
|
|
|
{
|
|
|
|
memmove(ier, endbuf, ((char*)reply_buf + reply_size) - endbuf);
|
|
|
|
|
|
|
|
/* Fix the pointers */
|
|
|
|
while (ier-- != reply_buf)
|
|
|
|
{
|
|
|
|
ier->Data = (char*)ier->Data - gap_size;
|
|
|
|
if (ier->Options.OptionsData)
|
|
|
|
ier->Options.OptionsData -= gap_size;
|
|
|
|
}
|
2020-11-23 14:47:45 +01:00
|
|
|
|
|
|
|
/* According to MSDN, the reply buffer needs to hold space for a IO_STATUS_BLOCK,
|
|
|
|
found at the very end of the reply. This is confirmed on Windows XP, but Vista
|
|
|
|
and later do not store it anywhere and in fact don't even require it at all.
|
|
|
|
|
|
|
|
However, in case old apps analyze this IO_STATUS_BLOCK and expect it, we mimic
|
|
|
|
it and write it out if there's enough space available in the buffer. */
|
|
|
|
if (gap_size >= sizeof(IO_STATUS_BLOCK))
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK *io = (IO_STATUS_BLOCK*)((char*)reply_buf + reply_size - sizeof(IO_STATUS_BLOCK));
|
|
|
|
|
|
|
|
io->Pointer = NULL; /* Always NULL or STATUS_SUCCESS */
|
|
|
|
io->Information = reply_size - gap_size;
|
|
|
|
}
|
2020-11-23 14:47:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:47:41 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
|
|
|
TRACE("received %d replies\n",res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-11-07 22:22:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exported Routines.
|
|
|
|
*/
|
|
|
|
|
2014-09-07 19:03:20 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Icmp6CreateFile (IPHLPAPI.@)
|
|
|
|
*/
|
|
|
|
HANDLE WINAPI Icmp6CreateFile(VOID)
|
|
|
|
{
|
|
|
|
icmp_t* icp;
|
|
|
|
|
|
|
|
int sid=socket(AF_INET6,SOCK_RAW,IPPROTO_ICMPV6);
|
|
|
|
if (sid < 0)
|
|
|
|
{
|
2020-08-03 15:15:52 +02:00
|
|
|
/* Some systems (e.g. Linux 3.0+ and Mac OS X) support
|
|
|
|
non-privileged ICMP via SOCK_DGRAM type. */
|
2014-09-07 19:03:20 +02:00
|
|
|
sid=socket(AF_INET6,SOCK_DGRAM,IPPROTO_ICMPV6);
|
|
|
|
}
|
|
|
|
if (sid < 0) {
|
|
|
|
ERR_(winediag)("Failed to use ICMPV6 (network ping), this requires special permissions.\n");
|
|
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
|
|
|
|
if (icp==NULL) {
|
|
|
|
close(sid);
|
|
|
|
SetLastError(IP_NO_RESOURCES);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
icp->sid=sid;
|
|
|
|
icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
|
|
|
|
return (HANDLE)icp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-07 19:05:04 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Icmp6SendEcho2 (IPHLPAPI.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI Icmp6SendEcho2(
|
|
|
|
HANDLE IcmpHandle,
|
|
|
|
HANDLE Event,
|
|
|
|
PIO_APC_ROUTINE ApcRoutine,
|
|
|
|
PVOID ApcContext,
|
|
|
|
struct sockaddr_in6* SourceAddress,
|
|
|
|
struct sockaddr_in6* DestinationAddress,
|
|
|
|
LPVOID RequestData,
|
|
|
|
WORD RequestSize,
|
|
|
|
PIP_OPTION_INFORMATION RequestOptions,
|
|
|
|
LPVOID ReplyBuffer,
|
|
|
|
DWORD ReplySize,
|
|
|
|
DWORD Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
FIXME("(%p, %p, %p, %p, %p, %p, %p, %d, %p, %p, %d, %d): stub\n", IcmpHandle, Event,
|
|
|
|
ApcRoutine, ApcContext, SourceAddress, DestinationAddress, RequestData,
|
|
|
|
RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-24 21:46:04 +01:00
|
|
|
/***********************************************************************
|
2008-07-17 18:39:29 +02:00
|
|
|
* IcmpCreateFile (IPHLPAPI.@)
|
2000-03-24 21:46:04 +01:00
|
|
|
*/
|
1999-11-07 22:22:17 +01:00
|
|
|
HANDLE WINAPI IcmpCreateFile(VOID)
|
|
|
|
{
|
|
|
|
icmp_t* icp;
|
|
|
|
|
2003-04-04 21:33:07 +02:00
|
|
|
int sid=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
|
2011-02-11 23:02:02 +01:00
|
|
|
if (sid < 0)
|
|
|
|
{
|
2020-08-03 15:15:52 +02:00
|
|
|
/* Some systems (e.g. Linux 3.0+ and Mac OS X) support
|
|
|
|
non-privileged ICMP via SOCK_DGRAM type. */
|
2011-02-11 23:02:02 +01:00
|
|
|
sid=socket(AF_INET,SOCK_DGRAM,IPPROTO_ICMP);
|
|
|
|
}
|
2003-04-04 21:33:07 +02:00
|
|
|
if (sid < 0) {
|
2009-12-08 14:14:21 +01:00
|
|
|
ERR_(winediag)("Failed to use ICMP (network ping), this requires special permissions.\n");
|
1999-11-07 22:22:17 +01:00
|
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2000-05-19 05:37:21 +02:00
|
|
|
icp=HeapAlloc(GetProcessHeap(), 0, sizeof(*icp));
|
1999-11-07 22:22:17 +01:00
|
|
|
if (icp==NULL) {
|
2012-10-25 21:29:06 +02:00
|
|
|
close(sid);
|
1999-11-07 22:22:17 +01:00
|
|
|
SetLastError(IP_NO_RESOURCES);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
icp->sid=sid;
|
|
|
|
icp->default_opts.OptionsSize=IP_OPTS_UNKNOWN;
|
|
|
|
return (HANDLE)icp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-15 22:44:21 +02:00
|
|
|
/***********************************************************************
|
2008-07-17 18:39:29 +02:00
|
|
|
* IcmpCloseHandle (IPHLPAPI.@)
|
2000-04-15 22:44:21 +02:00
|
|
|
*/
|
1999-11-07 22:22:17 +01:00
|
|
|
BOOL WINAPI IcmpCloseHandle(HANDLE IcmpHandle)
|
|
|
|
{
|
|
|
|
icmp_t* icp=(icmp_t*)IcmpHandle;
|
1999-12-25 23:46:22 +01:00
|
|
|
if (IcmpHandle==INVALID_HANDLE_VALUE) {
|
|
|
|
/* FIXME: in fact win98 seems to ignore the handle value !!! */
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
1999-11-07 22:22:17 +01:00
|
|
|
|
2011-05-30 17:22:27 +02:00
|
|
|
close( icp->sid );
|
2000-05-19 05:37:21 +02:00
|
|
|
HeapFree(GetProcessHeap (), 0, icp);
|
1999-11-07 22:22:17 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-24 21:46:04 +01:00
|
|
|
/***********************************************************************
|
2008-07-17 18:39:29 +02:00
|
|
|
* IcmpSendEcho (IPHLPAPI.@)
|
2000-03-24 21:46:04 +01:00
|
|
|
*/
|
1999-11-07 22:22:17 +01:00
|
|
|
DWORD WINAPI IcmpSendEcho(
|
|
|
|
HANDLE IcmpHandle,
|
|
|
|
IPAddr DestinationAddress,
|
|
|
|
LPVOID RequestData,
|
|
|
|
WORD RequestSize,
|
|
|
|
PIP_OPTION_INFORMATION RequestOptions,
|
|
|
|
LPVOID ReplyBuffer,
|
|
|
|
DWORD ReplySize,
|
|
|
|
DWORD Timeout
|
|
|
|
)
|
2020-11-23 14:47:42 +01:00
|
|
|
{
|
|
|
|
return IcmpSendEcho2Ex(IcmpHandle, NULL, NULL, NULL, 0, DestinationAddress,
|
|
|
|
RequestData, RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* IcmpSendEcho2 (IPHLPAPI.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI IcmpSendEcho2(
|
|
|
|
HANDLE IcmpHandle,
|
|
|
|
HANDLE Event,
|
|
|
|
PIO_APC_ROUTINE ApcRoutine,
|
|
|
|
PVOID ApcContext,
|
|
|
|
IPAddr DestinationAddress,
|
|
|
|
LPVOID RequestData,
|
|
|
|
WORD RequestSize,
|
|
|
|
PIP_OPTION_INFORMATION RequestOptions,
|
|
|
|
LPVOID ReplyBuffer,
|
|
|
|
DWORD ReplySize,
|
|
|
|
DWORD Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return IcmpSendEcho2Ex(IcmpHandle, Event, ApcRoutine, ApcContext, 0,
|
|
|
|
DestinationAddress, RequestData, RequestSize, RequestOptions,
|
|
|
|
ReplyBuffer, ReplySize, Timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* IcmpSendEcho2Ex (IPHLPAPI.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI IcmpSendEcho2Ex(
|
|
|
|
HANDLE IcmpHandle,
|
|
|
|
HANDLE Event,
|
|
|
|
PIO_APC_ROUTINE ApcRoutine,
|
|
|
|
PVOID ApcContext,
|
|
|
|
IPAddr SourceAddress,
|
|
|
|
IPAddr DestinationAddress,
|
|
|
|
LPVOID RequestData,
|
|
|
|
WORD RequestSize,
|
|
|
|
PIP_OPTION_INFORMATION RequestOptions,
|
|
|
|
LPVOID ReplyBuffer,
|
|
|
|
DWORD ReplySize,
|
|
|
|
DWORD Timeout
|
|
|
|
)
|
1999-11-07 22:22:17 +01:00
|
|
|
{
|
|
|
|
icmp_t* icp=(icmp_t*)IcmpHandle;
|
|
|
|
struct icmp* icmp_header;
|
|
|
|
struct sockaddr_in addr;
|
2020-11-23 14:47:41 +01:00
|
|
|
unsigned short id, seq;
|
|
|
|
unsigned char *buffer;
|
|
|
|
int reqsize, repsize;
|
|
|
|
DWORD send_time;
|
1999-11-07 22:22:17 +01:00
|
|
|
|
2020-11-23 14:47:42 +01:00
|
|
|
TRACE("(%p, %p, %p, %p, %08x, %08x, %p, %d, %p, %p, %d, %d)\n", IcmpHandle,
|
|
|
|
Event, ApcRoutine, ApcContext, SourceAddress, DestinationAddress, RequestData,
|
|
|
|
RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
|
|
|
|
|
1999-12-25 23:46:22 +01:00
|
|
|
if (IcmpHandle==INVALID_HANDLE_VALUE) {
|
|
|
|
/* FIXME: in fact win98 seems to ignore the handle value !!! */
|
2018-08-05 05:05:16 +02:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReplyBuffer||!ReplySize) {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
1999-12-25 23:46:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:05:16 +02:00
|
|
|
if (ReplySize<sizeof(ICMP_ECHO_REPLY)) {
|
1999-11-07 22:22:17 +01:00
|
|
|
SetLastError(IP_BUF_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* check the request size against SO_MAX_MSG_SIZE using getsockopt */
|
|
|
|
|
2014-01-09 23:45:37 +01:00
|
|
|
if (!DestinationAddress) {
|
|
|
|
SetLastError(ERROR_INVALID_NETNAME);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:47:42 +01:00
|
|
|
if (Event)
|
|
|
|
{
|
|
|
|
FIXME("unsupported for events\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ApcRoutine)
|
|
|
|
{
|
|
|
|
FIXME("unsupported for APCs\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (SourceAddress)
|
|
|
|
{
|
|
|
|
FIXME("unsupported for source addresses\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-11-07 22:22:17 +01:00
|
|
|
/* Prepare the request */
|
|
|
|
id=getpid() & 0xFFFF;
|
|
|
|
seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
|
|
|
|
|
|
|
|
reqsize=ICMP_MINLEN+RequestSize;
|
2018-08-05 05:04:43 +02:00
|
|
|
/* max ip header + max icmp header and error data + reply size(max 65535 on Windows) */
|
|
|
|
/* FIXME: request size of 65535 is not supported yet because max buffer size of raw socket on linux is 32767 */
|
|
|
|
repsize = MAXIPLEN + MAXICMPLEN + min( 65535, ReplySize );
|
|
|
|
buffer = HeapAlloc(GetProcessHeap(), 0, max( repsize, reqsize ));
|
|
|
|
if (buffer == NULL) {
|
1999-11-07 22:22:17 +01:00
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:04:43 +02:00
|
|
|
icmp_header=(struct icmp*)buffer;
|
1999-11-07 22:22:17 +01:00
|
|
|
icmp_header->icmp_type=ICMP_ECHO;
|
|
|
|
icmp_header->icmp_code=0;
|
|
|
|
icmp_header->icmp_cksum=0;
|
|
|
|
icmp_header->icmp_id=id;
|
|
|
|
icmp_header->icmp_seq=seq;
|
2018-08-05 05:04:43 +02:00
|
|
|
memcpy(buffer+ICMP_MINLEN, RequestData, RequestSize);
|
2020-11-23 14:47:41 +01:00
|
|
|
icmp_header->icmp_cksum=in_cksum((u_short*)buffer,reqsize);
|
1999-11-07 22:22:17 +01:00
|
|
|
|
|
|
|
addr.sin_family=AF_INET;
|
|
|
|
addr.sin_addr.s_addr=DestinationAddress;
|
|
|
|
addr.sin_port=0;
|
|
|
|
|
|
|
|
if (RequestOptions!=NULL) {
|
|
|
|
int val;
|
|
|
|
if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
|
2013-09-19 16:39:22 +02:00
|
|
|
socklen_t len;
|
1999-11-07 22:22:17 +01:00
|
|
|
/* Before we mess with the options, get the default values */
|
|
|
|
len=sizeof(val);
|
2003-04-04 21:33:07 +02:00
|
|
|
getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
|
1999-11-07 22:22:17 +01:00
|
|
|
icp->default_opts.Ttl=val;
|
|
|
|
|
|
|
|
len=sizeof(val);
|
2003-04-04 21:33:07 +02:00
|
|
|
getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
|
1999-11-07 22:22:17 +01:00
|
|
|
icp->default_opts.Tos=val;
|
|
|
|
/* FIXME: missing: handling of IP 'flags', and all the other options */
|
|
|
|
}
|
|
|
|
|
|
|
|
val=RequestOptions->Ttl;
|
2003-04-04 21:33:07 +02:00
|
|
|
setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
|
1999-11-07 22:22:17 +01:00
|
|
|
val=RequestOptions->Tos;
|
2003-04-04 21:33:07 +02:00
|
|
|
setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
|
1999-11-07 22:22:17 +01:00
|
|
|
/* FIXME: missing: handling of IP 'flags', and all the other options */
|
|
|
|
|
|
|
|
icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
|
|
|
|
} else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
|
|
|
|
int val;
|
|
|
|
|
|
|
|
/* Restore the default options */
|
|
|
|
val=icp->default_opts.Ttl;
|
2003-04-04 21:33:07 +02:00
|
|
|
setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
|
1999-11-07 22:22:17 +01:00
|
|
|
val=icp->default_opts.Tos;
|
2003-04-04 21:33:07 +02:00
|
|
|
setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
|
1999-11-07 22:22:17 +01:00
|
|
|
/* FIXME: missing: handling of IP 'flags', and all the other options */
|
|
|
|
|
|
|
|
icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the packet */
|
|
|
|
TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
|
|
|
|
#if 0
|
|
|
|
if (TRACE_ON(icmp)){
|
|
|
|
int i;
|
|
|
|
printf("Output buffer:\n");
|
|
|
|
for (i=0;i<reqsize;i++)
|
2018-08-05 05:04:43 +02:00
|
|
|
printf("%2x,", buffer[i]);
|
1999-11-07 22:22:17 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-29 23:33:05 +02:00
|
|
|
send_time = GetTickCount();
|
2020-11-23 14:47:41 +01:00
|
|
|
if (sendto(icp->sid, buffer, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
1999-11-07 22:22:17 +01:00
|
|
|
if (errno==EMSGSIZE)
|
|
|
|
SetLastError(IP_PACKET_TOO_BIG);
|
|
|
|
else {
|
|
|
|
switch (errno) {
|
|
|
|
case ENETUNREACH:
|
|
|
|
SetLastError(IP_DEST_NET_UNREACHABLE);
|
|
|
|
break;
|
|
|
|
case EHOSTUNREACH:
|
2004-04-13 01:15:11 +02:00
|
|
|
SetLastError(IP_DEST_HOST_UNREACHABLE);
|
1999-11-07 22:22:17 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TRACE("unknown error: errno=%d\n",errno);
|
2004-04-13 01:15:11 +02:00
|
|
|
SetLastError(IP_GENERAL_FAILURE);
|
1999-11-07 22:22:17 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-05 05:04:43 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, buffer);
|
1999-11-07 22:22:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:47:41 +01:00
|
|
|
return icmp_get_reply(icp->sid, buffer, send_time, ReplyBuffer, ReplySize, Timeout);
|
1999-11-07 22:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Mike Muuss.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2002-03-10 00:29:33 +01:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-11-07 22:22:17 +01:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|