cnping

Minimal Graphical Ping Tool
git clone https://git.sinitax.com/cnlohr/cnping
Log | Files | Refs | Submodules | README | LICENSE | sfeed.txt

commit a4f65f0e0d115c521856101c6bc71712d827a832
parent ccb43508a3475fa0d1388e89d4ac2f0b3567e243
Author: cnlohr <lohr85@gmail.com>
Date:   Tue,  1 Dec 2015 00:10:11 -0500

Cleanup - make window version of windows file.

Diffstat:
MMakefile | 2+-
MREADME.md | 6+++++-
Mcnping.c | 114++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mcnping.exe | 0
Acnping.png | 0
Mping.c | 16++++++++++------
Mping.h | 11+++++++++++
7 files changed, 138 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,7 +7,7 @@ LDFLAGS:=-g MINGW32:=/usr/bin/i686-w64-mingw32- cnping.exe : cnping.c DrawFunctions.c WinDriver.c os_generic.c ping.c - $(MINGW32)gcc -g -m32 $(CFLAGS) -o $@ $^ -lgdi32 -lws2_32 + $(MINGW32)gcc -g -mwindows -m32 $(CFLAGS) -o $@ $^ -lgdi32 -lws2_32 -s # Rasterizer.o cnping : cnping.o DrawFunctions.o XDriver.o os_generic.o ping.o diff --git a/README.md b/README.md @@ -1,4 +1,8 @@ cnping ====== -Minimal Graphical IPV4 Ping Tool. (also comes with searchnet, like nmap but smaller and simpler). It uses rawdraw so it is (Almost) OS independent. Someone would have to update ping.h/ping.c. It takes two parameters... address and time between pings in seconds. The second is optional. +Minimal Graphical IPV4 Ping Tool. (also comes with searchnet, like nmap but smaller and simpler). It uses rawdraw so it is OS independent. + +Usage: cnping [IP address] [Speed to ping at] [additional size to increase] + +<IMG SRC=cnping.png> diff --git a/cnping.c b/cnping.c @@ -186,7 +186,11 @@ void DrawFrame( void ) OGUSleep( 1000 ); } +#ifdef WIN32 +int mymain( int argc, const char ** argv ) +#else int main( int argc, const char ** argv ) +#endif { char title[1024]; int i, x, y, r; @@ -214,7 +218,7 @@ int main( int argc, const char ** argv ) if( argc < 2 ) { - fprintf( stderr, "Usage: cnping [host] [ping period in seconds (optional) default 0.02] [ping packet extra size (above 12), default = 0]\n" ); + ERRM( "Usage: cnping [host] [ping period in seconds (optional) default 0.02] [ping packet extra size (above 12), default = 0]\n" ); return -1; } @@ -261,8 +265,8 @@ int main( int argc, const char ** argv ) LastFPSTime+=1; } - SecToWait = .016 - ( ThisTime - LastFrameTime ); - LastFrameTime += .016; + SecToWait = .030 - ( ThisTime - LastFrameTime ); + LastFrameTime += .030; if( SecToWait > 0 ) OGUSleep( (int)( SecToWait * 1000000 ) ); } @@ -270,3 +274,107 @@ int main( int argc, const char ** argv ) return(0); } +#ifdef WIN32 + +//from: http://alter.org.ua/docs/win/args/ + PCHAR* + CommandLineToArgvA( + PCHAR CmdLine, + int* _argc + ) + { + PCHAR* argv; + PCHAR _argv; + ULONG len; + ULONG argc; + CHAR a; + ULONG i, j; + + BOOLEAN in_QM; + BOOLEAN in_TEXT; + BOOLEAN in_SPACE; + + len = strlen(CmdLine); + i = ((len+2)/2)*sizeof(PVOID) + sizeof(PVOID); + + argv = (PCHAR*)GlobalAlloc(GMEM_FIXED, + i + (len+2)*sizeof(CHAR)); + + _argv = (PCHAR)(((PUCHAR)argv)+i); + + argc = 0; + argv[argc] = _argv; + in_QM = FALSE; + in_TEXT = FALSE; + in_SPACE = TRUE; + i = 0; + j = 0; + + while( a = CmdLine[i] ) { + if(in_QM) { + if(a == '\"') { + in_QM = FALSE; + } else { + _argv[j] = a; + j++; + } + } else { + switch(a) { + case '\"': + in_QM = TRUE; + in_TEXT = TRUE; + if(in_SPACE) { + argv[argc] = _argv+j; + argc++; + } + in_SPACE = FALSE; + break; + case ' ': + case '\t': + case '\n': + case '\r': + if(in_TEXT) { + _argv[j] = '\0'; + j++; + } + in_TEXT = FALSE; + in_SPACE = TRUE; + break; + default: + in_TEXT = TRUE; + if(in_SPACE) { + argv[argc] = _argv+j; + argc++; + } + _argv[j] = a; + j++; + in_SPACE = FALSE; + break; + } + } + i++; + } + _argv[j] = '\0'; + argv[argc] = NULL; + + (*_argc) = argc; + return argv; + } + + +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + int argc; + char cts[8192]; + sprintf( cts, "%s %s", GetCommandLine(), lpCmdLine ); + + ShowWindow (GetConsoleWindow(), SW_HIDE); + char ** argv = CommandLineToArgvA( + cts, + &argc + ); +// MessageBox( 0, argv[1], "X", 0 ); + return mymain( argc-1, (const char**)argv ); +} + +#endif diff --git a/cnping.exe b/cnping.exe Binary files differ. diff --git a/cnping.png b/cnping.png Binary files differ. diff --git a/ping.c b/ping.c @@ -27,6 +27,7 @@ #include <fcntl.h> #include <errno.h> #include <strings.h> +#include "ping.h" #ifdef WIN32 #include <winsock2.h> #define SOL_IP 0 @@ -110,7 +111,7 @@ void listener(void) if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) { - perror("Set TTL option"); + ERRM("Erro: could not set TTL option\n"); exit( -1 ); } @@ -162,7 +163,7 @@ void ping(struct sockaddr_in *addr, float pingperiod) } #else if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) - perror("Request nonblocking I/O"); + ERRM("Warning: Request nonblocking I/O failed."); #endif do @@ -206,7 +207,7 @@ void ping_setup() int r = WSAStartup(MAKEWORD(2,2), &wsaData); if( r ) { - fprintf( stderr, "Fault!\n" ); + ERRM( "Fault in WSAStartup\n" ); exit( -2 ); } #endif @@ -228,14 +229,14 @@ void ping_setup() if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) { - perror("Set TTL option"); - exit( -1 ); + ERRM("Error: Failed to set TTL option\n"); + exit( -1 ); } #endif if ( sd < 0 ) { - perror("socket"); + ERRM("Error: Could not create raw socket\n"); exit(0); } @@ -253,3 +254,6 @@ void do_pinger( const char * strhost, float _ping_period ) ping(&psaddr, _ping_period); } +char errbuffer[1024]; + + diff --git a/ping.h b/ping.h @@ -20,5 +20,16 @@ void do_pinger( const char * strhost, float _ping_period ); //If pingperiod = -1 void ping_setup(); +#ifdef WIN32 + +extern char errbuffer[1024]; +#define ERRM(x...) { sprintf( errbuffer, x ); MessageBox( 0, errbuffer, "cnping", 0 ); } + +#else + +#define ERRM(x...) fprintf( stderr, x ); + +#endif + #endif