Updates for move of net apps to rosapps

svn path=/trunk/; revision=2445
This commit is contained in:
Steven Edwards 2001-12-30 04:12:47 +00:00
parent 174bb8c3d3
commit f355097ad2
5 changed files with 689 additions and 2780 deletions

View file

@ -1,389 +0,0 @@
/* acconfig.h
This file is in the public domain.
Descriptive text for the C preprocessor macros that
the distributed Autoconf macros can define.
No software package will use all of them; autoheader copies the ones
your configure.in uses into your configuration header file templates.
The entries are in sort -df order: alphabetical, case insensitive,
ignoring punctuation (such as underscores). Although this order
can split up related entries, it makes it easier to check whether
a given entry is in the file.
Leave the following blank line there!! Autoheader needs it. */
/* Define if on AIX 3.
System headers sometimes define this.
We just want to avoid a redefinition error message. */
#ifndef _ALL_SOURCE
#undef _ALL_SOURCE
#endif
#undef CAN_USE_SYS_SELECT_H
/* Define if using alloca.c. */
#undef C_ALLOCA
/* Define if type char is unsigned and you are not using gcc. */
#ifndef __CHAR_UNSIGNED__
#undef __CHAR_UNSIGNED__
#endif
/* Define if the closedir function returns void instead of int. */
#undef CLOSEDIR_VOID
/* Define to empty if the keyword does not work. */
#undef const
/* Define to one of _getb67, GETB67, getb67 for Cray-2 and Cray-YMP systems.
This function is required for alloca.c support on those systems. */
#undef CRAY_STACKSEG_END
/* Define for DGUX with <sys/dg_sys_info.h>. */
#undef DGUX
/* Define if you have <dirent.h>. */
#undef DIRENT
/* Define to the type of elements in the array set by `getgroups'.
Usually this is either `int' or `gid_t'. */
#undef GETGROUPS_T
/* Define if the `getloadavg' function needs to be run setuid or setgid. */
#undef GETLOADAVG_PRIVILEGED
/* Define if the `getpgrp' function takes no argument. */
#undef GETPGRP_VOID
/* Define to `int' if <sys/types.h> doesn't define. */
#undef gid_t
/* Define if you have alloca, as a function or macro. */
#undef HAVE_ALLOCA
/* Define if you have <alloca.h> and it should be used (not on Ultrix). */
#undef HAVE_ALLOCA_H
/* Define if your curses library has this functionality. */
#undef HAVE_BEEP
#undef HAVE_CURSES_H
/* Define if you don't have vprintf but do have _doprnt. */
#undef HAVE_DOPRNT
/* Define if your system has a working fnmatch function. */
#undef HAVE_FNMATCH
/* Define if your curses library has this functionality. */
#undef HAVE_GETBEGX
/* Define if your system has its own `getloadavg' function. */
#undef HAVE_GETLOADAVG
/* Define if your curses library has this functionality. */
#undef HAVE_GETMAXX
/* Define if your curses library has this functionality. */
#undef HAVE_GETMAXYX
/* Define if you have the getmntent function. */
#undef HAVE_GETMNTENT
/* Define if you have <hpsecurity.h>. */
#undef HAVE_HPSECURITY_H
/* Define if you have the curses library. */
#undef HAVE_LIBCURSES
/* Define if you want to use the Hpwd library, and you also have it's corresponding database library (such as *dbm). */
#undef HAVE_LIBHPWD
/* Define if you have the ncurses library. */
#undef HAVE_LIBNCURSES
/* Define if you have the readline library, version 2.0 or higher. */
#undef HAVE_LIBREADLINE
/* Define if the `long double' type works. */
#undef HAVE_LONG_DOUBLE
/* Define if you support file names longer than 14 characters. */
#undef HAVE_LONG_FILE_NAMES
/* Define if your compiler supports the "long long" integral type. */
#undef HAVE_LONG_LONG
/* Most system's curses library uses a _maxx field instead of maxx. */
#undef HAVE__MAXX
/* Define if you have a working `mmap' system call. */
#undef HAVE_MMAP
#undef HAVE_MSGHDR_ACCRIGHTS
#undef HAVE_MSGHDR_CONTROL
#undef HAVE_PR_PASSWD_FG_OLDCRYPT
/* Define if you have a _res global variable used by resolve routines. */
#undef HAVE__RES_DEFDNAME
/* Define if system calls automatically restart after interruption
by a signal. */
#undef HAVE_RESTARTABLE_SYSCALLS
/* Define if you have sigsetjmp and siglongjmp. */
#undef HAVE_SIGSETJMP
#undef HAVE_SOCKADDR_UN_SUN_LEN
#undef HAVE_STATFS_F_BAVAIL
/* Define if your struct stat has st_blksize. */
#undef HAVE_ST_BLKSIZE
/* Define if your struct stat has st_blocks. */
#undef HAVE_ST_BLOCKS
/* Define if you have the strcoll function and it is properly defined. */
#undef HAVE_STRCOLL
/* Define if your struct stat has st_rdev. */
#undef HAVE_ST_RDEV
/* Define if you have the strftime function. */
#undef HAVE_STRFTIME
/* Define if you have the ANSI # stringizing operator in cpp. */
#undef HAVE_STRINGIZE
#undef HAVE_STRUCT_CMSGDHR
#undef HAVE_STRUCT_STAT64
/* Define if you have <sys/wait.h> that is POSIX.1 compatible. */
#undef HAVE_SYS_WAIT_H
/* Define if your curses library has this functionality. */
#undef HAVE_TOUCHWIN
/* Define if your struct tm has tm_zone. */
#undef HAVE_TM_ZONE
/* Define if you don't have tm_zone but do have the external array
tzname. */
#undef HAVE_TZNAME
/* Define if you have <unistd.h>. */
#undef HAVE_UNISTD_H
/* Define if utime(file, NULL) sets file's timestamp to the present. */
#undef HAVE_UTIME_NULL
/* Define if you have a ut_host field in your struct utmp. */
#undef HAVE_UTMP_UT_HOST
#undef HAVE_UTMP_UT_NAME
#undef HAVE_UTMP_UT_USER
#undef HAVE_UTMP_UT_PID
#undef HAVE_UTMP_UT_TIME
#undef HAVE_UTMPX_UT_SYSLEN
/* Define if you have <vfork.h>. */
#undef HAVE_VFORK_H
/* Define if you have the vprintf function. */
#undef HAVE_VPRINTF
/* Define if you have the wait3 system call. */
#undef HAVE_WAIT3
/* Define as __inline if that's what the C compiler calls it. */
#undef inline
/* Define if chown is promiscuous (regular user can give away ownership) */
#undef INSECURE_CHOWN
/* Define if int is 16 bits instead of 32. */
#undef INT_16_BITS
/* Define if long int is 64 bits. */
#undef LONG_64_BITS
/* Define if major, minor, and makedev are declared in <mkdev.h>. */
#undef MAJOR_IN_MKDEV
/* Define if major, minor, and makedev are declared in <sysmacros.h>. */
#undef MAJOR_IN_SYSMACROS
/* Define if on MINIX. */
#undef _MINIX
/* Define to `int' if <sys/types.h> doesn't define. */
#undef mode_t
/* Define if you don't have <dirent.h>, but have <ndir.h>. */
#undef NDIR
/* Define if you have <memory.h>, and <string.h> doesn't declare the
mem* functions. */
#undef NEED_MEMORY_H
/* Define if your struct nlist has an n_un member. */
#undef NLIST_NAME_UNION
/* Define if you have <nlist.h>. */
#undef NLIST_STRUCT
/* Define if your C compiler doesn't accept -c and -o together. */
#undef NO_MINUS_C_MINUS_O
/* Define if your Fortran 77 compiler doesn't accept -c and -o together. */
#undef F77_NO_MINUS_C_MINUS_O
/* Define to `long' if <sys/types.h> doesn't define. */
#undef off_t
#undef OS
/* Define to `int' if <sys/types.h> doesn't define. */
#undef pid_t
/* Define if the system does not provide POSIX.1 features except
with this defined. */
#undef _POSIX_1_SOURCE
/* Define if you need to in order for stat and other things to work. */
#undef _POSIX_SOURCE
/* Format string for the printf() family for 64 bit integers. */
#undef PRINTF_LONG_LONG
/* Define if printing a "long long" with "%lld" works . */
#undef PRINTF_LONG_LONG_LLD
/* Define if printing a "long long" with "%qd" works . */
#undef PRINTF_LONG_LONG_QD
/* Define if your C compiler supports ANSI C function prototyping. */
#undef PROTOTYPES
/* Define as the return type of signal handlers (int or void). */
#undef RETSIGTYPE
/* Format string for the scanf() family for 64 bit integers. */
#undef SCANF_LONG_LONG
/* Define if scanning a "long long" with "%lld" works. */
#undef SCANF_LONG_LONG_LLD
/* Define if scanning a "long long" with "%qd" works. */
#undef SCANF_LONG_LONG_QD
/* Define to the type of arg1 for select(). */
#undef SELECT_TYPE_ARG1
/* Define to the type of args 2, 3 and 4 for select(). */
#undef SELECT_TYPE_ARG234
/* Define to the type of arg5 for select(). */
#undef SELECT_TYPE_ARG5
/* Define if the `setpgrp' function takes no argument. */
#undef SETPGRP_VOID
/* Define if the setvbuf function takes the buffering type as its second
argument and the buffer pointer as the third, as on System V
before release 3. */
#undef SETVBUF_REVERSED
/* Define to `int' if <sys/signal.h> doesn't define. */
#undef sig_atomic_t
/* Define to `unsigned' if <sys/types.h> doesn't define. */
#undef size_t
#undef SNPRINTF_TERMINATES
#undef SPRINTF_RETURNS_PTR
/* If using the C implementation of alloca, define if you know the
direction of stack growth for your system; otherwise it will be
automatically deduced at run-time.
STACK_DIRECTION > 0 => grows toward higher addresses
STACK_DIRECTION < 0 => grows toward lower addresses
STACK_DIRECTION = 0 => direction of growth unknown
*/
#undef STACK_DIRECTION
/* Define if the `S_IS*' macros in <sys/stat.h> do not work properly. */
#undef STAT_MACROS_BROKEN
/* Define if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Define on System V Release 4. */
#undef SVR4
/* Define if you don't have <dirent.h>, but have <sys/dir.h>. */
#undef SYSDIR
/* Define if you don't have <dirent.h>, but have <sys/ndir.h>. */
#undef SYSNDIR
/* Define if `sys_siglist' is declared by <signal.h>. */
#undef SYS_SIGLIST_DECLARED
/* Define to the full path of the Tar program, if you have it. */
#undef TAR
/* Define if you can safely include both <sys/time.h> and <time.h>. */
#undef TIME_WITH_SYS_TIME
/* Define if your <sys/time.h> declares struct tm. */
#undef TM_IN_SYS_TIME
/* Define to `int' if <sys/types.h> doesn't define. */
#undef uid_t
/* Define for Encore UMAX. */
#undef UMAX
/* Result of "uname -a" */
#undef UNAME
/* Define for Encore UMAX 4.3 that has <inq_status/cpustats.h>
instead of <sys/cpustats.h>. */
#undef UMAX4_3
/* Define if you do not have <strings.h>, index, bzero, etc.. */
#undef USG
/* Define vfork as fork if vfork does not work. */
#undef vfork
/* Define if the closedir function returns void instead of int. */
#undef VOID_CLOSEDIR
/* Define if your processor stores words with the most significant
byte first (like Motorola and SPARC, unlike Intel and VAX). */
#undef WORDS_BIGENDIAN
/* Define if the X Window System is missing or not being used. */
#undef X_DISPLAY_MISSING
/* Define if lex declares yytext as a char * by default, not a char[]. */
#undef YYTEXT_POINTER
/* Leave that blank line there!! Autoheader needs it.
If you're adding to this file, keep in mind:
The entries are in sort -df order: alphabetical, case insensitive,
ignoring punctuation (such as underscores). */

File diff suppressed because it is too large Load diff

21
rosapps/net/ping/makefile Normal file
View file

@ -0,0 +1,21 @@
# $Id: makefile,v 1.1 2001/12/30 04:09:38 sedwards Exp $
PATH_TO_TOP=../../../reactos
ROS_DIR=../../../reactos
TARGET_TYPE = program
TARGET_APPTYPE = console
TARGET_NAME = ping
TARGET_SDKLIBS = ntdll.a ws2_32.a
TARGET_OBJECTS = $(TARGET_NAME).o
include $(PATH_TO_TOP)/rules.mak
include $(TOOLS_PATH)/helper.mk
# EOF

629
rosapps/net/ping/ping.c Normal file
View file

@ -0,0 +1,629 @@
/* $Id: ping.c,v 1.1 2001/12/30 04:09:38 sedwards Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS ping utility
* FILE: apps/net/ping/ping.c
* PURPOSE: Network test utility
* PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
* REVISIONS:
* CSH 01/09/2000 Created
*/
#include <winsock2.h>
#include <tchar.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#ifndef _MSC_VER
//#define DBG
/* FIXME: Where should this be? */
#define CopyMemory(Destination, Source, Length) memcpy(Destination, Source, Length);
/* Should be in the header files somewhere (exported by ntdll.dll) */
long atol(const char *str);
#ifndef __int64
typedef long long __int64;
#endif
char * _i64toa(__int64 value, char *string, int radix);
#endif
/* General ICMP constants */
#define ICMP_MINSIZE 8 /* Minimum ICMP packet size */
#define ICMP_MAXSIZE 65535 /* Maximum ICMP packet size */
/* ICMP message types */
#define ICMPMSG_ECHOREQUEST 8 /* ICMP ECHO request message */
#define ICMPMSG_ECHOREPLY 0 /* ICMP ECHO reply message */
#pragma pack(4)
/* IPv4 header structure */
typedef struct _IPv4_HEADER {
unsigned char IHL:4;
unsigned char Version:4;
unsigned char TOS;
unsigned short Length;
unsigned short Id;
unsigned short FragFlags;
unsigned char TTL;
unsigned char Protocol;
unsigned short Checksum;
unsigned int SrcAddress;
unsigned int DstAddress;
} IPv4_HEADER, *PIPv4_HEADER;
/* ICMP echo request/reply header structure */
typedef struct _ICMP_HEADER {
unsigned char Type;
unsigned char Code;
unsigned short Checksum;
unsigned short Id;
unsigned short SeqNum;
} ICMP_HEADER, *PICMP_HEADER;
typedef struct _ICMP_ECHO_PACKET {
ICMP_HEADER Icmp;
LARGE_INTEGER Timestamp;
} ICMP_ECHO_PACKET, *PICMP_ECHO_PACKET;
#pragma pack(1)
BOOL InvalidOption;
BOOL NeverStop;
BOOL ResolveAddresses;
UINT PingCount;
UINT DataSize; /* ICMP echo request data size */
BOOL DontFragment;
ULONG TTLValue;
ULONG TOSValue;
ULONG Timeout;
CHAR TargetName[256];
SOCKET IcmpSock;
SOCKADDR_IN Target;
LPSTR TargetIP;
FD_SET Fds;
TIMEVAL Timeval;
UINT CurrentSeqNum;
UINT SentCount;
UINT LostCount;
BOOL MinRTTSet;
LARGE_INTEGER MinRTT; /* Minimum round trip time in microseconds */
LARGE_INTEGER MaxRTT;
LARGE_INTEGER SumRTT;
LARGE_INTEGER AvgRTT;
LARGE_INTEGER TicksPerMs; /* Ticks per millisecond */
LARGE_INTEGER TicksPerUs; /* Ticks per microsecond */
BOOL UsePerformanceCounter;
/* Display the contents of a buffer */
VOID DisplayBuffer(
PVOID Buffer,
DWORD Size)
{
UINT i;
PCHAR p;
printf("Buffer (0x%X) Size (0x%X).\n", Buffer, Size);
p = (PCHAR)Buffer;
for (i = 0; i < Size; i++) {
if (i % 16 == 0) {
printf("\n");
}
printf("%02X ", (p[i]) & 0xFF);
}
}
/* Display usage information on screen */
VOID Usage(VOID)
{
printf("\nUsage: ping [-t] [-n count] [-l size] [-w timeout] destination-host\n\n");
printf("Options:\n");
printf(" -t Ping the specified host until stopped.\n");
printf(" To stop - type Control-C.\n");
printf(" -n count Number of echo requests to send.\n");
printf(" -l size Send buffer size.\n");
printf(" -w timeout Timeout in milliseconds to wait for each reply.\n\n");
}
/* Reset configuration to default values */
VOID Reset(VOID)
{
LARGE_INTEGER PerformanceCounterFrequency;
NeverStop = FALSE;
ResolveAddresses = FALSE;
PingCount = 4;
DataSize = 32;
DontFragment = FALSE;
TTLValue = 128;
TOSValue = 0;
Timeout = 1000;
UsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);
if (UsePerformanceCounter) {
/* Performance counters may return incorrect results on some multiprocessor
platforms so we restrict execution on the first processor. This may fail
on Windows NT so we fall back to GetCurrentTick() for timing */
if (SetThreadAffinityMask (GetCurrentThread(), 1) == 0) {
UsePerformanceCounter = FALSE;
}
/* Convert frequency to ticks per millisecond */
TicksPerMs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000;
/* And to ticks per microsecond */
TicksPerUs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000000;
}
if (!UsePerformanceCounter) {
/* 1 tick per millisecond for GetCurrentTick() */
TicksPerMs.QuadPart = 1;
/* GetCurrentTick() cannot handle microseconds */
TicksPerUs.QuadPart = 1;
}
}
/* Return ULONG in a string */
ULONG GetULONG(LPSTR String)
{
UINT i, Length;
ULONG Value;
i = 0;
Length = strlen(String);
while ((i < Length) && ((String[i] < '0') || (String[i] > '9'))) i++;
if ((i >= Length) || ((String[i] < '0') || (String[i] > '9'))) {
InvalidOption = TRUE;
return 0;
}
Value = (ULONG)atol(&String[i]);
return Value;
}
/* Return ULONG in a string. Try next paramter if not successful */
ULONG GetULONG2(LPSTR String1, LPSTR String2, PINT i)
{
ULONG Value;
Value = GetULONG(String1);
if (InvalidOption) {
InvalidOption = FALSE;
if (String2[0] != '-') {
Value = GetULONG(String2);
if (!InvalidOption)
*i += 1;
}
}
return Value;
}
/* Parse command line parameters */
BOOL ParseCmdline(int argc, char* argv[])
{
INT i;
BOOL ShowUsage;
BOOL FoundTarget;
#if 1
lstrcpy(TargetName, "127.0.0.1");
PingCount = 1;
return TRUE;
#endif
if (argc < 2) {
ShowUsage = TRUE;
} else {
ShowUsage = FALSE;
}
FoundTarget = FALSE;
InvalidOption = FALSE;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 't': NeverStop = TRUE; break;
case 'a': ResolveAddresses = TRUE; break;
case 'n': PingCount = GetULONG2(&argv[i][2], argv[i + 1], &i); break;
case 'l':
DataSize = GetULONG2(&argv[i][2], argv[i + 1], &i);
if ((DataSize < 0) || (DataSize > ICMP_MAXSIZE - sizeof(ICMP_ECHO_PACKET))) {
printf("Bad value for option -l, valid range is from 0 to %d.\n",
ICMP_MAXSIZE - sizeof(ICMP_ECHO_PACKET));
return FALSE;
}
break;
case 'f': DontFragment = TRUE; break;
case 'i': TTLValue = GetULONG2(&argv[i][2], argv[i + 1], &i); break;
case 'v': TOSValue = GetULONG2(&argv[i][2], argv[i + 1], &i); break;
case 'w': Timeout = GetULONG2(&argv[i][2], argv[i + 1], &i); break;
default:
printf("Bad option %s.\n", argv[i]);
Usage();
return FALSE;
}
if (InvalidOption) {
printf("Bad option format %s.\n", argv[i]);
return FALSE;
}
} else {
if (FoundTarget) {
printf("Bad parameter %s.\n", argv[i]);
return FALSE;
} else {
lstrcpy(TargetName, argv[i]);
FoundTarget = TRUE;
}
}
}
if ((!ShowUsage) && (!FoundTarget)) {
printf("Name or IP address of destination host must be specified.\n");
return FALSE;
}
if (ShowUsage) {
Usage();
return FALSE;
}
return TRUE;
}
/* Calculate checksum of data */
WORD Checksum(PUSHORT data, UINT size)
{
ULONG sum = 0;
while (size > 1) {
sum += *data++;
size -= sizeof(USHORT);
}
if (size)
sum += *(UCHAR*)data;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return (USHORT)(~sum);
}
/* Prepare to ping target */
BOOL Setup(VOID)
{
WORD wVersionRequested;
WSADATA WsaData;
INT Status;
ULONG Addr;
PHOSTENT phe;
wVersionRequested = MAKEWORD(2, 2);
Status = WSAStartup(wVersionRequested, &WsaData);
if (Status != 0) {
printf("Could not initialize winsock dll.\n");
return FALSE;
}
IcmpSock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
if (IcmpSock == INVALID_SOCKET) {
printf("Could not create socket (#%d).\n", WSAGetLastError());
return FALSE;
}
ZeroMemory(&Target, sizeof(Target));
phe = NULL;
Addr = inet_addr(TargetName);
if (Addr == INADDR_NONE) {
phe = gethostbyname(TargetName);
if (phe == NULL) {
printf("Unknown host %s.\n", TargetName);
return FALSE;
}
}
if (phe != NULL) {
CopyMemory(&Target.sin_addr, phe->h_addr_list, phe->h_length);
} else {
Target.sin_addr.s_addr = Addr;
}
if (phe != NULL) {
Target.sin_family = phe->h_addrtype;
} else {
Target.sin_family = AF_INET;
}
TargetIP = inet_ntoa(Target.sin_addr);
CurrentSeqNum = 0;
SentCount = 0;
LostCount = 0;
MinRTT.QuadPart = 0;
MaxRTT.QuadPart = 0;
SumRTT.QuadPart = 0;
MinRTTSet = FALSE;
return TRUE;
}
/* Close socket */
VOID Cleanup(VOID)
{
if (IcmpSock != INVALID_SOCKET)
closesocket(IcmpSock);
WSACleanup();
}
VOID QueryTime(PLARGE_INTEGER Time)
{
if (UsePerformanceCounter) {
if (QueryPerformanceCounter(Time) == 0) {
/* This should not happen, but we fall
back to GetCurrentTick() if it does */
Time->u.LowPart = (ULONG)GetTickCount();
Time->u.HighPart = 0;
/* 1 tick per millisecond for GetCurrentTick() */
TicksPerMs.QuadPart = 1;
/* GetCurrentTick() cannot handle microseconds */
TicksPerUs.QuadPart = 1;
UsePerformanceCounter = FALSE;
}
} else {
Time->u.LowPart = (ULONG)GetTickCount();
Time->u.HighPart = 0;
}
}
VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
{
UINT i, Length;
CHAR Convstr[40];
LARGE_INTEGER LargeTime;
LargeTime.QuadPart = Time.QuadPart / TicksPerMs.QuadPart;
_i64toa(LargeTime.QuadPart, Convstr, 10);
strcpy(String, Convstr);
strcat(String, ",");
LargeTime.QuadPart = (Time.QuadPart % TicksPerMs.QuadPart) / TicksPerUs.QuadPart;
_i64toa(LargeTime.QuadPart, Convstr, 10);
Length = strlen(Convstr);
if (Length < 4) {
for (i = 0; i < 4 - Length; i++)
strcat(String, "0");
}
strcat(String, Convstr);
strcat(String, "ms");
}
/* Locate the ICMP data and print it. Returns TRUE if the packet was good,
FALSE if not */
BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from)
{
PIPv4_HEADER IpHeader;
PICMP_ECHO_PACKET Icmp;
UINT IphLength;
CHAR Time[100];
LARGE_INTEGER RelativeTime;
LARGE_INTEGER LargeTime;
IpHeader = (PIPv4_HEADER)buffer;
IphLength = IpHeader->IHL * 4;
if (size < IphLength + ICMP_MINSIZE) {
#ifdef DBG
printf("Bad size (0x%X < 0x%X)\n", size, IphLength + ICMP_MINSIZE);
#endif DBG
return FALSE;
}
Icmp = (PICMP_ECHO_PACKET)(buffer + IphLength);
if (Icmp->Icmp.Type != ICMPMSG_ECHOREPLY) {
#ifdef DBG
printf("Bad ICMP type (0x%X should be 0x%X)\n", Icmp->Icmp.Type, ICMPMSG_ECHOREPLY);
#endif DBG
return FALSE;
}
if (Icmp->Icmp.Id != (USHORT)GetCurrentProcessId()) {
#ifdef DBG
printf("Bad ICMP id (0x%X should be 0x%X)\n", Icmp->Icmp.Id, (USHORT)GetCurrentProcessId());
#endif DBG
return FALSE;
}
QueryTime(&LargeTime);
RelativeTime.QuadPart = (LargeTime.QuadPart - Icmp->Timestamp.QuadPart);
TimeToMsString(Time, RelativeTime);
printf("Reply from %s: bytes=%d time=%s TTL=%d\n", inet_ntoa(from->sin_addr),
size - IphLength - sizeof(ICMP_ECHO_PACKET), Time, IpHeader->TTL);
if (RelativeTime.QuadPart < MinRTT.QuadPart) {
MinRTT.QuadPart = RelativeTime.QuadPart;
MinRTTSet = TRUE;
}
if (RelativeTime.QuadPart > MaxRTT.QuadPart)
MaxRTT.QuadPart = RelativeTime.QuadPart;
SumRTT.QuadPart += RelativeTime.QuadPart;
return TRUE;
}
/* Send and receive one ping */
BOOL Ping(VOID)
{
INT Status;
SOCKADDR From;
UINT Length;
PVOID Buffer;
UINT Size;
PICMP_ECHO_PACKET Packet;
/* Account for extra space for IP header when packet is received */
Size = DataSize + 128;
Buffer = GlobalAlloc(0, Size);
if (!Buffer) {
printf("Not enough free resources available.\n");
return FALSE;
}
ZeroMemory(Buffer, Size);
Packet = (PICMP_ECHO_PACKET)Buffer;
/* Assemble ICMP echo request packet */
Packet->Icmp.Type = ICMPMSG_ECHOREQUEST;
Packet->Icmp.Code = 0;
Packet->Icmp.Id = (USHORT)GetCurrentProcessId();
Packet->Icmp.SeqNum = (USHORT)CurrentSeqNum;
Packet->Icmp.Checksum = 0;
/* Timestamp is part of data area */
QueryTime(&Packet->Timestamp);
CopyMemory(Buffer, &Packet->Icmp, sizeof(ICMP_ECHO_PACKET) + DataSize);
/* Calculate checksum for ICMP header and data area */
Packet->Icmp.Checksum = Checksum((PUSHORT)&Packet->Icmp, sizeof(ICMP_ECHO_PACKET) + DataSize);
CurrentSeqNum++;
/* Send ICMP echo request */
FD_ZERO(&Fds);
FD_SET(IcmpSock, &Fds);
Timeval.tv_sec = Timeout / 1000;
Timeval.tv_usec = Timeout % 1000;
Status = select(0, NULL, &Fds, NULL, &Timeval);
if ((Status != SOCKET_ERROR) && (Status != 0)) {
#ifdef DBG
printf("Sending packet\n");
DisplayBuffer(Buffer, sizeof(ICMP_ECHO_PACKET) + DataSize);
printf("\n");
#endif DBG
Status = sendto(IcmpSock, Buffer, sizeof(ICMP_ECHO_PACKET) + DataSize,
0, (SOCKADDR*)&Target, sizeof(Target));
SentCount++;
}
if (Status == SOCKET_ERROR) {
if (WSAGetLastError() == WSAEHOSTUNREACH) {
printf("Destination host unreachable.\n");
} else {
printf("Could not transmit data (%d).\n", WSAGetLastError());
}
GlobalFree(Buffer);
return FALSE;
}
/* Expect to receive ICMP echo reply */
FD_ZERO(&Fds);
FD_SET(IcmpSock, &Fds);
Timeval.tv_sec = Timeout / 1000;
Timeval.tv_usec = Timeout % 1000;
Status = select(0, &Fds, NULL, NULL, &Timeval);
if ((Status != SOCKET_ERROR) && (Status != 0)) {
Length = sizeof(From);
Status = recvfrom(IcmpSock, Buffer, Size, 0, &From, &Length);
#ifdef DBG
printf("Received packet\n");
DisplayBuffer(Buffer, Status);
printf("\n");
#endif DBG
}
if (Status == SOCKET_ERROR) {
if (WSAGetLastError() != WSAETIMEDOUT) {
printf("Could not receive data (%d).\n", WSAGetLastError());
GlobalFree(Buffer);
return FALSE;
}
Status = 0;
}
if (Status == 0) {
printf("Request timed out.\n");
LostCount++;
GlobalFree(Buffer);
return TRUE;
}
if (!DecodeResponse(Buffer, Status, (PSOCKADDR_IN)&From)) {
/* FIXME: Wait again as it could be another ICMP message type */
printf("Request timed out (incomplete datagram received).\n");
LostCount++;
}
GlobalFree(Buffer);
return TRUE;
}
/* Program entry point */
int main(int argc, char* argv[])
{
UINT Count;
CHAR MinTime[20];
CHAR MaxTime[20];
CHAR AvgTime[20];
Reset();
if ((ParseCmdline(argc, argv)) && (Setup())) {
printf("\nPinging %s [%s] with %d bytes of data:\n\n",
TargetName, TargetIP, DataSize);
Count = 0;
while ((NeverStop) || (Count < PingCount)) {
Ping();
Sleep(Timeout);
Count++;
};
Cleanup();
/* Calculate avarage round trip time */
if ((SentCount - LostCount) > 0) {
AvgRTT.QuadPart = SumRTT.QuadPart / (SentCount - LostCount);
} else {
AvgRTT.QuadPart = 0;
}
/* Calculate loss percent */
if (LostCount > 0) {
Count = (SentCount * 100) / LostCount;
} else {
Count = 0;
}
if (!MinRTTSet)
MinRTT = MaxRTT;
TimeToMsString(MinTime, MinRTT);
TimeToMsString(MaxTime, MaxRTT);
TimeToMsString(AvgTime, AvgRTT);
/* Print statistics */
printf("\nPing statistics for %s:\n", TargetIP);
printf(" Packets: Sent = %d, Received = %d, Lost = %d (%d%% loss),\n",
SentCount, SentCount - LostCount, LostCount, Count);
printf("Approximate round trip times in milli-seconds:\n");
printf(" Minimum = %s, Maximum = %s, Average = %s\n",
MinTime, MaxTime, AvgTime);
}
return 0;
}
/* EOF */

39
rosapps/net/ping/ping.rc Normal file
View file

@ -0,0 +1,39 @@
#include <defines.h>
#include <reactos/resource.h>
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
VS_VERSION_INFO VERSIONINFO
FILEVERSION RES_UINT_FV_MAJOR,RES_UINT_FV_MINOR,RES_UINT_FV_REVISION,RES_UINT_FV_BUILD
PRODUCTVERSION RES_UINT_PV_MAJOR,RES_UINT_PV_MINOR,RES_UINT_PV_REVISION,RES_UINT_PV_BUILD
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", RES_STR_COMPANY_NAME
VALUE "FileDescription", "ReactOS TCP/IPv4 Win32 Ping\0"
VALUE "FileVersion", RES_STR_FILE_VERSION
VALUE "InternalName", "ping\0"
VALUE "LegalCopyright", RES_STR_LEGAL_COPYRIGHT
VALUE "OriginalCopyright", "Casper S. Hornstrup (chorns@users.sourceforge.net)\0"
VALUE "OriginalFilename", "ping.exe\0"
VALUE "ProductName", RES_STR_PRODUCT_NAME
VALUE "ProductVersion", RES_STR_PRODUCT_VERSION
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END