cnping

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

commit a19c77c0db292bf24a7e711f33b85ccbadd0b40b
parent afcc565771a37978eb0b54e185fa80482cdebe3a
Author: cnlohr <lohr85@gmail.com>
Date:   Mon, 23 Sep 2019 01:03:26 -0400

Merge branch 'attempt_without_admin'

Diffstat:
MCNFGWinDriver.c | 7+++----
MMakefile | 12++++++++----
Mcnping.c | 21++++++++++++++++-----
Mcnping.exe | 0
Mhttping.c | 32+++++++++++++++++++-------------
Mping.c | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mresources.rc | 2++
Muac.manifest | 1+
8 files changed, 200 insertions(+), 33 deletions(-)

diff --git a/CNFGWinDriver.c b/CNFGWinDriver.c @@ -8,7 +8,6 @@ #include <malloc.h> //for alloca static HBITMAP lsBitmap; -static HINSTANCE lhInstance; static HWND lsHWND; static HDC lsWindowHDC; static HDC lsHDC; @@ -63,8 +62,8 @@ void CNFGUpdateScreenWithBitmap( unsigned long * data, int w, int h ) { RECT r; - int a = SetBitmapBits(lsBitmap,w*h*4,data); - a = BitBlt(lsWindowHDC, 0, 0, w, h, lsHDC, 0, 0, SRCCOPY); + SetBitmapBits(lsBitmap,w*h*4,data); + BitBlt(lsWindowHDC, 0, 0, w, h, lsHDC, 0, 0, SRCCOPY); UpdateWindow( lsHWND ); int thisw, thish; @@ -212,7 +211,7 @@ void CNFGSetup( const char * name_of_window, int width, int height ) void CNFGHandleInput() { - int ldown = 0; + //int ldown = 0; MSG msg; while( PeekMessage( &msg, lsHWND, 0, 0xFFFF, 1 ) ) diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ all : cnping searchnet -CFLAGS:=$(CFLAGS) -g -Os -I/opt/X11/include -Wall +CFLAGS:=$(CFLAGS) -s -Os -I/opt/X11/include -Wall CXXFLAGS:=$(CFLAGS) -LDFLAGS:=-g -L/opt/X11/lib/ +LDFLAGS:=-s -L/opt/X11/lib/ #CFLAGS:=$(CFLAGS) -DCNFGOGL #LDFLAGS:=$(LDFLAGS) -lGL @@ -10,9 +10,13 @@ LDFLAGS:=-g -L/opt/X11/lib/ #MINGW32:=/usr/bin/i686-w64-mingw32- MINGW32:=i686-w64-mingw32- + +#If you don't need admin priveleges +ADMINFLAGS:= $(ADMINFLAGS) -DWIN_USE_NO_ADMIN_PING + cnping.exe : cnping.c CNFGFunctions.c CNFGWinDriver.c os_generic.c ping.c httping.c - $(MINGW32)windres resources.rc -o resources.o - $(MINGW32)gcc -g -fno-ident -mwindows -m32 $(CFLAGS) resources.o -o $@ $^ -lgdi32 -lws2_32 -s -D_WIN32_WINNT=0x0600 -DWIN32 + $(MINGW32)windres resources.rc -o resources.o $(ADMINFLAGS) + $(MINGW32)gcc -g -fno-ident -mwindows -m32 $(CFLAGS) -o $@ $^ -lgdi32 -lws2_32 -D_WIN32_WINNT=0x0600 -DWIN32 -liphlpapi -DMINGW_BUILD resources.o $(ADMINFLAGS) cnping : cnping.o CNFGFunctions.o CNFGXDriver.o os_generic.o ping.o httping.o gcc $(CFLAGS) -o $@ $^ -lX11 -lm -lpthread $(LDFLAGS) diff --git a/cnping.c b/cnping.c @@ -5,7 +5,7 @@ #include <math.h> #include <errno.h> #include <string.h> -#ifdef WIN32 +#if defined( WINDOWS ) || defined( WIN32 ) #ifdef _MSC_VER #define strdup _strdup #endif @@ -47,6 +47,11 @@ int ExtraPingSize; int in_histogram_mode, in_frame_mode = 1; void HandleGotPacket( int seqno, int timeout ); +#if defined( WINDOWS ) || defined( WIN32 ) +WSADATA wsaData; +#endif + + #define MAX_HISTO_MARKS (TIMEOUT*10000) uint64_t hist_counts[MAX_HISTO_MARKS]; @@ -171,7 +176,7 @@ void HandleKey( int keycode, int bDown ) { char lpFilename[1024]; char lpDirectory[1024]; - GetCurrentDirectory( lpDirectory, 1023 ); + GetCurrentDirectory( 1023, lpDirectory ); GetModuleFileNameA( GetModuleHandle(0), lpFilename, 1023 ); CreateProcessA( lpFilename, GetCommandLine(), 0, 0, 1, 0, 0, lpDirectory, 0, 0 ); @@ -507,7 +512,7 @@ int RegString( int write, char * data, DWORD len ) { if( write ) { - RegSetValueExA( hKey, "history", 0, REG_SZ, data, len ); + RegSetValueExA( hKey, "history", 0, REG_SZ, (uint8_t*)data, len ); return 0; } else @@ -616,8 +621,6 @@ int main( int argc, const char ** argv ) ShowWindow (GetConsoleWindow(), SW_HIDE); #endif - srand( (int)(OGGetAbsoluteTime()*100000) ); - for( i = 0; i < sizeof( pattern ); i++ ) { pattern[i] = rand(); @@ -710,6 +713,14 @@ int main( int argc, const char ** argv ) #endif return -1; } + +#if defined( WIN32 ) || defined( WINDOWS ) + if( WSAStartup(MAKEWORD(2,2), &wsaData) ) + { + ERRM( "Fault in WSAStartup\n" ); + exit( -2 ); + } +#endif CNFGSetup( title, 320, 155 ); diff --git a/cnping.exe b/cnping.exe Binary files differ. diff --git a/httping.c b/httping.c @@ -3,11 +3,19 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + +#ifndef TCC #include <unistd.h> #include <sys/types.h> +#else +#include "tccheader.h" +#endif -#ifdef WIN32 +#if defined( WIN32 ) || defined( WINDOWS ) + #ifdef TCC + #else #include <winsock2.h> + #endif #else #include <sys/socket.h> #include <netinet/in.h> @@ -30,6 +38,15 @@ void HTTPingCallbackGot( int seqno ); //Don't dynamically allocate resources here, since execution may be stopped arbitrarily. void DoHTTPing( const char * addy, double minperiod, int * seqnoptr, volatile double * timeouttime, int * socketptr, volatile int * getting_host_by_name ) { +#if defined(WIN32) || defined(WINDOWS) + WSADATA wsaData; + int r = WSAStartup(MAKEWORD(2,2), &wsaData); + if( r ) + { + ERRM( "Fault in WSAStartup\n" ); + exit( -2 ); + } +#endif struct sockaddr_in serveraddr; struct hostent *server; int httpsock; @@ -60,7 +77,7 @@ void DoHTTPing( const char * addy, double minperiod, int * seqnoptr, volatile do server = gethostbyname(hostname); *getting_host_by_name = 0; if (server == NULL) { - ERRMB("ERROR, no such host as %s\n", hostname); + ERRMB("ERROR, no such host as \"%s\"\n", hostname); goto fail; } @@ -210,17 +227,6 @@ static void * PingRunner( void * v ) int StartHTTPing( const char * addy, double minperiod ) { - -#ifdef WIN32 - WSADATA wsaData; - int r = WSAStartup(MAKEWORD(2,2), &wsaData); - if( r ) - { - ERRM( "Fault in WSAStartup\n" ); - exit( -2 ); - } -#endif - struct HTTPPingLaunch *hpl = malloc( sizeof( struct HTTPPingLaunch ) ); hpl->addy = addy; hpl->minperiod = minperiod; diff --git a/ping.c b/ping.c @@ -10,9 +10,145 @@ #include <stdlib.h> #include "ping.h" #include "os_generic.h" +#include "error_handling.h" + +#ifdef TCC +#include "tccheader.h" +#endif + +int ping_failed_to_send; +float pingperiodseconds; +int precise_ping; +struct sockaddr_in psaddr; + +#ifdef WIN_USE_NO_ADMIN_PING + +#ifdef TCC + //... +#else +#include <inaddr.h> +#include <winsock2.h> +#include <windows.h> +#include <ws2tcpip.h> +#include <ipexport.h> +#include <icmpapi.h> +#endif + + + +#define MAX_PING_SIZE 16384 +#define PINGTHREADS 100 + +#if !defined( MINGW_BUILD ) && !defined( TCC ) + #pragma comment(lib, "Ws2_32.lib") + #pragma comment(lib, "iphlpapi.lib") +#endif + +static og_sema_t s_disp; +static og_sema_t s_ping; + +void ping_setup() +{ + s_disp = OGCreateSema(); + s_ping = OGCreateSema(); + //This function is executed first. +} + +void listener() +{ + static uint8_t listth; + if( listth ) return; + listth = 1; + + OGUnlockSema( s_disp ); + //Normally needs to call display(buf + 28, bytes - 28 ); on successful ping. + //This function is executed as a thread after setup. + //Really, we just use the s_disp semaphore to make sure we only launch disp's at correct times. + + while(1) { OGSleep( 100000 ); } + return; +} + +static HANDLE pinghandles[PINGTHREADS]; + +static void * pingerthread( void * v ) +{ + uint8_t ping_payload[MAX_PING_SIZE]; + + HANDLE ih = *((HANDLE*)v); + + int timeout_ms = pingperiodseconds * (PINGTHREADS-1) * 1000; + while(1) + { + OGLockSema( s_ping ); + int rl = load_ping_packet( ping_payload, sizeof( ping_payload ) ); + struct repl_t + { + ICMP_ECHO_REPLY rply; + uint8_t err_data[16384]; + } repl; + + DWORD res = IcmpSendEcho( ih, + psaddr.sin_addr.s_addr, ping_payload, rl, + 0, &repl, sizeof( repl ), + timeout_ms ); + int err; + if( !res ) err = GetLastError(); + OGLockSema( s_disp ); + + if( !res ) + { + if( err == 11050 ) + { + printf( "GENERAL FAILURE\n" ); + } + else + { + printf( "ERROR: %d\n", err ); + } + } + if( res ) + { + display( ping_payload, rl ); + } + OGUnlockSema( s_disp ); + } + return 0; +} + +void ping(struct sockaddr_in *addr ) +{ + int i; + //Launch pinger threads + for( i = 0; i < PINGTHREADS; i++ ) + { + HANDLE ih = pinghandles[i] = IcmpCreateFile(); + if( ih == INVALID_HANDLE_VALUE ) + { + ERRM( "Cannot create ICMP thread %d\n", i ); + exit( 0 ); + } + + OGCreateThread( pingerthread, &pinghandles[i] ); + } + //This function is executed as a thread after setup. + + while(1) + { + if( i >= PINGTHREADS-1 ) i = 0; + else i++; + OGUnlockSema( s_ping ); + OGUSleep( (int)(pingperiodseconds * 1000000) ); + } +} + + + +#else + +//The normal way to do it - only problem is Windows needs admin privs. -#include "error_handling.h" #ifdef WIN32 #include <winsock2.h> @@ -21,7 +157,9 @@ #define ICMP_ECHO 8 #define IP_TTL 2 #define O_NONBLOCK 04000 +#ifndef MINGW_BUILD #pragma comment(lib, "Ws2_32.lib") +#endif #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> @@ -62,8 +200,6 @@ struct icmphdr }; #endif -float pingperiodseconds; -int precise_ping; #define PACKETSIZE 65536 @@ -75,8 +211,6 @@ struct packet int sd; int pid=-1; -int ping_failed_to_send; -struct sockaddr_in psaddr; uint16_t checksum( const unsigned char * start, uint16_t len ) { @@ -140,7 +274,8 @@ void listener() goto keep_retry_quick; } - ERRM( "Fault on listen.\n" ); + + ERRM( "Fault on listen().\n" ); exit( 0 ); } @@ -250,10 +385,19 @@ void ping_setup() } +#endif + + + void do_pinger( const char * strhost ) { struct hostent *hname; hname = gethostbyname(strhost); + if( hname == 0 ) + { + ERRM( "Error: cannot find host %s\n", strhost ); + return; + } memset(&psaddr, 0, sizeof(psaddr)); psaddr.sin_family = hname->h_addrtype; @@ -262,6 +406,6 @@ void do_pinger( const char * strhost ) ping(&psaddr ); } -char errbuffer[1024]; +char errbuffer[1024]; diff --git a/resources.rc b/resources.rc @@ -1,4 +1,6 @@ +#ifndef WIN_USE_NO_ADMIN_PING 1 24 "uac.manifest" +#endif #include <windows.h> diff --git a/uac.manifest b/uac.manifest @@ -1,4 +1,5 @@ <?xml version="1.0" encoding="utf-8" standalone="yes"?> +<!-- This is no longer needed if you compile with WIN_USE_NO_ADMIN_PING. It is only required for raw packet access --> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="cnping" type="win32"/> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">