Skip to content

Commit

Permalink
fix windows compile; add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbesen committed Feb 21, 2024
1 parent 722ec94 commit b97025b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cnping.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ struct PingHost
struct PingHost* next;
};

unsigned frames = 0;
unsigned long iframeno = 0;
short screenx, screeny;
float GuiYScaleFactor;
int GuiYscaleFactorIsConstant;
Expand All @@ -74,6 +72,8 @@ uint8_t pattern[8] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
#define TIMEOUT 4

struct PingData * PingData = NULL;

// count of hosts to ping - represents size of PingData and number of elements in pinghostList
unsigned int pinghostListSize = 0;


Expand Down Expand Up @@ -692,6 +692,8 @@ int main( int argc, const char ** argv )
double SecToWait;
double frameperiodseconds;
const char * device = NULL;

// linked list of all hosts that should be pinged
struct PingHost * pinghostList = NULL;
pinghostListSize = 0;

Expand Down Expand Up @@ -864,6 +866,8 @@ int main( int argc, const char ** argv )

frameperiodseconds = fmin(.2, fmax(.03, pingperiodseconds) );

unsigned frames = 0;
unsigned long iframeno = 0;
while(1)
{
iframeno++;
Expand Down
34 changes: 24 additions & 10 deletions ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ int using_regular_ping;

#include "rawdraw/os_generic.h"

// used to pack arguments for windows ping threads
struct WindowsPingArgs
{
struct PreparedPing* pp;
HANDLE icmpHandle;
};


#define MAX_PING_SIZE 16384
Expand Down Expand Up @@ -84,27 +90,29 @@ void listener( struct PreparedPing* pp )
return;
}

static HANDLE pinghandles[PINGTHREADS];

static void * pingerthread( void * v )
{
uint8_t ping_payload[MAX_PING_SIZE];

HANDLE ih = *((HANDLE*)v);
// copy arguments and free struct
struct WindowsPingArgs* args = (struct WindowsPingArgs*) v;
struct PreparedPing* pp = args->pp;
HANDLE ih = args->icmpHandle;
free(args);

int timeout_ms = pingperiodseconds * (PINGTHREADS-1) * 1000;
while(1)
{
OGLockSema( s_ping );
int rl = load_ping_packet( ping_payload, sizeof( ping_payload ) );
int rl = load_ping_packet( ping_payload, sizeof( ping_payload ), PingData + pp->pingHostId );
struct repl_t
{
ICMP_ECHO_REPLY rply;
uint8_t err_data[16384];
} repl;

DWORD res = IcmpSendEcho( ih,
((struct sockaddr_in*) &psaddr)->sin_addr.s_addr, ping_payload, rl,
((struct sockaddr_in*) &pp->psaddr)->sin_addr.s_addr, ping_payload, rl,
0, &repl, sizeof( repl ),
timeout_ms );
int err;
Expand All @@ -124,7 +132,7 @@ static void * pingerthread( void * v )
}
if( res )
{
display( repl.rply.Data, rl );
display( repl.rply.Data, rl, pp->pingHostId );
}
OGUnlockSema( s_disp );
}
Expand All @@ -149,14 +157,17 @@ void singleping( struct PreparedPing* pp )
//Launch pinger threads
for( i = 0; i < PINGTHREADS; i++ )
{
HANDLE ih = pinghandles[i] = IcmpCreateFile();
HANDLE ih = IcmpCreateFile();
if( ih == INVALID_HANDLE_VALUE )
{
ERRM( "Cannot create ICMP thread %d\n", i );
exit( 0 );
}

OGCreateThread( pingerthread, &pinghandles[i] );
struct WindowsPingArgs* args = (struct WindowsPingArgs*) malloc(sizeof(struct WindowsPingArgs));
args->pp = pp;
args->icmpHandle = ih;
OGCreateThread( pingerthread, args );
}
}
//This function is executed as a thread after setup.
Expand All @@ -182,14 +193,17 @@ void ping( struct PreparedPing* pp )
//Launch pinger threads
for( i = 0; i < PINGTHREADS; i++ )
{
HANDLE ih = pinghandles[i] = IcmpCreateFile();
HANDLE ih = IcmpCreateFile();
if( ih == INVALID_HANDLE_VALUE )
{
ERRM( "Cannot create ICMP thread %d\n", i );
exit( 0 );
}

OGCreateThread( pingerthread, &pinghandles[i] );
struct WindowsPingArgs* args = (struct WindowsPingArgs*) malloc(sizeof(struct WindowsPingArgs));
args->pp = pp;
args->icmpHandle = ih;
OGCreateThread( pingerthread, args );
}
//This function is executed as a thread after setup.

Expand Down
13 changes: 13 additions & 0 deletions ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@

struct PreparedPing
{
// socket for sending the pings
int fd;

// the number of this host -> used to access the PingData array
int pingHostId;

// address to send the pings to
struct sockaddr_in6 psaddr;
// size of psaddr
socklen_t psaddr_len;
};

// meassured ping data of one host. This is the meassured data cnping displays
struct PingData
{
double PingSendTimes [PINGCYCLEWIDTH];
Expand All @@ -39,9 +46,13 @@ struct PingData
double globinterval, globlast;
uint64_t globalrx;
uint64_t globallost;

// pointer to the related prepared ping so it can be freed at exit
struct PreparedPing* pp;
};

// captured ping data - This is an array that get allocated with the size of pinghostListSize
// each Ping thread should use pingHostId as offset into this array
extern struct PingData * PingData;

unsigned short checksum(const unsigned char *b, uint16_t len);
Expand All @@ -61,6 +72,8 @@ void singleping( struct PreparedPing* pp ); // If using this, must call ping_set
//If pingperiodseconds = -1, run ping/do_pinger once and exit.
extern float pingperiodseconds;
extern int ping_failed_to_send;

// resolvs the hostname and prepares the socket. Might return NULL on error
struct PreparedPing* ping_setup(const char * strhost, const char * device);


Expand Down

0 comments on commit b97025b

Please sign in to comment.