cnping

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

commit ccb43508a3475fa0d1388e89d4ac2f0b3567e243
parent 6508627af672bf7b1fd47cac77e29b6bdfafbc9f
Author: cnlohr <lohr85@gmail.com>
Date:   Tue, 24 Nov 2015 11:10:58 -0500

Fix Linux, using separate sockets, for some reason using the same socket reduces performance.

Diffstat:
Mcnping.c | 3++-
Mcnping.exe | 0
Mping.c | 112+++++++++++++++++++++++++++++++------------------------------------------------
3 files changed, 46 insertions(+), 69 deletions(-)

diff --git a/cnping.c b/cnping.c @@ -214,7 +214,8 @@ int main( int argc, const char ** argv ) if( argc < 2 ) { - fprintf( stderr, "Usage: cnping [host] [ping period in seconds (optional)]\n" ); + fprintf( stderr, "Usage: cnping [host] [ping period in seconds (optional) default 0.02] [ping packet extra size (above 12), default = 0]\n" ); + return -1; } if( argc > 3 ) diff --git a/cnping.exe b/cnping.exe Binary files differ. diff --git a/ping.c b/ping.c @@ -74,6 +74,7 @@ struct packet char msg[PACKETSIZE-sizeof(struct icmphdr)]; }; +int sd; int pid=-1; struct protoent *proto=NULL; struct sockaddr_in psaddr; @@ -96,60 +97,27 @@ unsigned short checksum(void *b, int len) return result; } -/*--------------------------------------------------------------------*/ -/*--- display - present echo info ---*/ -/*--------------------------------------------------------------------*/ -/*void display(void *buf, int bytes) -{ int i; - struct iphdr *ip = buf; - struct icmphdr *icmp = buf+ip->ihl*4; - printf("----------------\n"); - for ( i = 0; i < bytes; i++ ) - { - if ( !(i & 15) ) printf("\nX: ", i); - printf("X ", ((unsigned char*)buf)[i]); - } - printf("\n"); - printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ", - ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol, - ip->ttl, inet_ntoa(ip->saddr)); - printf("dst=%s\n", inet_ntoa(ip->daddr)); - if ( icmp->un.echo.id == pid ) - { - printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n", - icmp->type, icmp->code, ntohs(icmp->checksum), - icmp->un.echo.id, icmp->un.echo.sequence); - } -} -*/ /*--------------------------------------------------------------------*/ /*--- listener - separate process to listen for and collect messages--*/ /*--------------------------------------------------------------------*/ void listener(void) { - int sd; - struct sockaddr_in addr; - unsigned char buf[1024]; +#ifndef WIN32 + const int val=255; -#ifdef WIN32 - sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0); + int sd = socket(PF_INET, SOCK_RAW, proto->p_proto); + + if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) { - int lttl = 0xff; - if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&lttl, - sizeof(lttl)) == SOCKET_ERROR) { - printf( "Warning: No IP_TTL.\n" ); - } + perror("Set TTL option"); + exit( -1 ); } -#else - sd = socket(PF_INET, SOCK_RAW, proto->p_proto); #endif - if ( sd < 0 ) - { - perror("socket"); - exit(0); - } + + struct sockaddr_in addr; + unsigned char buf[1024]; for (;;) { @@ -158,7 +126,6 @@ void listener(void) bzero(buf, sizeof(buf)); bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); - if( buf[20] != 0 ) continue; //Make sure ping response. if( buf[9] != 1 ) continue; //ICMP if( addr.sin_addr.s_addr != psaddr.sin_addr.s_addr ) continue; @@ -183,34 +150,11 @@ void ping(struct sockaddr_in *addr, float pingperiod) #else const int val=255; #endif - int i, sd, cnt=1; + int i, cnt=1; struct packet pckt; struct sockaddr_in r_addr; #ifdef WIN32 - sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0); - { - int lttl = 0xff; - if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&lttl, - sizeof(lttl)) == SOCKET_ERROR) { - printf( "Error with sockets.\n" ); - exit( -1 ); - } - } -#else - sd = socket(PF_INET, SOCK_RAW, proto->p_proto); -#endif - - if ( sd < 0 ) - { - perror("socket"); - return; - } - - if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) - perror("Set TTL option"); - -#ifdef WIN32 { //this /was/ recommended. unsigned long iMode = 1; @@ -254,8 +198,12 @@ void ping_setup() proto = getprotobyname("ICMP"); #ifdef WIN32 +/* WSADATA wsaData; int r = WSAStartup(0x0202, &wsaData ); +*/ + WSADATA wsaData; + int r = WSAStartup(MAKEWORD(2,2), &wsaData); if( r ) { fprintf( stderr, "Fault!\n" ); @@ -263,6 +211,34 @@ void ping_setup() } #endif + +#ifdef WIN32 + sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0); + { + int lttl = 0xff; + if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&lttl, + sizeof(lttl)) == SOCKET_ERROR) { + printf( "Warning: No IP_TTL.\n" ); + } + } +#else + const int val=255; + + sd = socket(PF_INET, SOCK_RAW, proto->p_proto); + + if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) + { + perror("Set TTL option"); + exit( -1 ); + } + +#endif + if ( sd < 0 ) + { + perror("socket"); + exit(0); + } + } void do_pinger( const char * strhost, float _ping_period )