commit 0f821852b2f05632429577c5b363a51017977f9b
parent 6b89363e6b79ecbf612306d42a8ef94a5a2f756a
Author: David Auer <david.auer@credativ.de>
Date: Thu, 25 Aug 2022 11:57:49 +0200
Merge branch 'interface'
Closes: https://github.com/cntools/cnping/pull/86
Diffstat:
5 files changed, 46 insertions(+), 12 deletions(-)
diff --git a/cnping.c b/cnping.c
@@ -652,6 +652,7 @@ int main( int argc, const char ** argv )
double LastFrameTime = OGGetAbsoluteTime();
double SecToWait;
double frameperiodseconds;
+ const char * device = NULL;
#ifdef WIN32
ShowWindow (GetConsoleWindow(), SW_HIDE);
@@ -704,6 +705,7 @@ int main( int argc, const char ** argv )
case 'y': GuiYScaleFactor = atof( nextargv ); break;
case 't': sprintf(title, "%s", nextargv); break;
case 'm': in_histogram_mode = 1; break;
+ case 'I': device = nextargv; break;
default: displayhelp = 1; break;
}
}
@@ -744,11 +746,18 @@ int main( int argc, const char ** argv )
" (-p) [period] -- period in seconds (optional), default 0.02 \n"
" (-s) [extra size] -- ping packet extra size (above 12), optional, default = 0 \n"
" (-y) [const y-axis scaling] -- use a fixed scaling factor instead of auto scaling (optional)\n"
- " (-t) [window title] -- the title of the window (optional)\n");
+ " (-t) [window title] -- the title of the window (optional)\n"
+ " (-I) [interface] -- Sets source interface (i.e. eth0)\n");
return -1;
}
#if defined( WIN32 ) || defined( WINDOWS )
+ if(device)
+ {
+ ERRM("Error: Device option is not implemented on your platform. PRs welcome.\n");
+ exit( -1 );
+ }
+
if( WSAStartup(MAKEWORD(2,2), &wsaData) )
{
ERRM( "Fault in WSAStartup\n" );
@@ -762,11 +771,11 @@ int main( int argc, const char ** argv )
if( memcmp( pinghost, "http://", 7 ) == 0 )
{
- StartHTTPing( pinghost+7, pingperiodseconds );
+ StartHTTPing( pinghost+7, pingperiodseconds, device );
}
else
{
- ping_setup();
+ ping_setup(device);
OGCreateThread( PingSend, 0 );
OGCreateThread( PingListen, 0 );
}
diff --git a/httping.c b/httping.c
@@ -36,9 +36,11 @@ void HTTPingCallbackStart( int seqno );
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 )
+void DoHTTPing( const char * addy, double minperiod, int * seqnoptr, volatile double * timeouttime, int * socketptr, volatile int * getting_host_by_name, const char * device)
{
#if defined(WIN32) || defined(WINDOWS)
+ (void) device; // option is not available for windows. Suppress unused warning.
+
WSADATA wsaData;
int r = WSAStartup(MAKEWORD(2,2), &wsaData);
if( r )
@@ -95,8 +97,19 @@ reconnect:
return;
}
+#if !defined( WIN32 ) && !defined( WINDOWS )
+ if(device)
+ {
+ if( setsockopt(httpsock, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) +1) != 0)
+ {
+ ERRM("Error: Failed to set Device option.\n");
+ exit( -1 );
+ }
+ }
+#endif
+
/* connect: create a connection with the server */
- if (connect(httpsock, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
+ if (connect(httpsock, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
{
ERRMB( "%s: ERROR connecting\n", hostname );
goto fail;
@@ -172,6 +185,7 @@ struct HTTPPingLaunch
{
const char * addy;
double minperiod;
+ const char * device;
volatile double timeout_time;
volatile int failed;
@@ -185,7 +199,7 @@ static void * DeployPing( void * v )
struct HTTPPingLaunch *hpl = (struct HTTPPingLaunch*)v;
hpl->socket = 0;
hpl->getting_host_by_name = 0;
- DoHTTPing( hpl->addy, hpl->minperiod, &hpl->seqno, &hpl->timeout_time, &hpl->socket, &hpl->getting_host_by_name );
+ DoHTTPing( hpl->addy, hpl->minperiod, &hpl->seqno, &hpl->timeout_time, &hpl->socket, &hpl->getting_host_by_name, hpl->device );
hpl->failed = 1;
return 0;
}
@@ -225,11 +239,12 @@ static void * PingRunner( void * v )
return 0;
}
-int StartHTTPing( const char * addy, double minperiod )
+int StartHTTPing( const char * addy, double minperiod, const char * device)
{
struct HTTPPingLaunch *hpl = malloc( sizeof( struct HTTPPingLaunch ) );
hpl->addy = addy;
hpl->minperiod = minperiod;
+ hpl->device = device;
OGCreateThread( PingRunner, hpl );
return 0;
}
diff --git a/httping.h b/httping.h
@@ -6,7 +6,7 @@ void HTTPingCallbackStart( int seqno );
void HTTPingCallbackGot( int seqno );
//addy should be google.com/blah or something like that. Do not include prefixing http://. Port numbers OK.
-int StartHTTPing( const char * addy, double minperiod );
+int StartHTTPing( const char * addy, double minperiod, const char * device);
#endif
diff --git a/ping.c b/ping.c
@@ -47,8 +47,9 @@ struct sockaddr_in psaddr;
static og_sema_t s_disp;
static og_sema_t s_ping;
-void ping_setup()
+void ping_setup(const char * device)
{
+
s_disp = OGCreateSema();
s_ping = OGCreateSema();
//This function is executed first.
@@ -163,7 +164,7 @@ void ping(struct sockaddr_in *addr )
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdint.h>
-#else
+#else // ! WIN32
#ifdef __FreeBSD__
#include <netinet/in.h>
#endif
@@ -364,7 +365,7 @@ void ping(struct sockaddr_in *addr )
//close( sd ); //Hacky, we don't close here because SD doesn't come from here, rather from ping_setup. We may want to run this multiple times.
}
-void ping_setup()
+void ping_setup(const char * device)
{
pid = getpid();
@@ -399,6 +400,15 @@ void ping_setup()
exit( -1 );
}
+ if(device)
+ {
+ if( setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) +1) != 0)
+ {
+ ERRM("Error: Failed to set Device option. Are you root? Or can do sock_raw sockets?\n");
+ exit( -1 );
+ }
+ }
+
#endif
if ( sd < 0 )
{
diff --git a/ping.h b/ping.h
@@ -22,7 +22,7 @@ void do_pinger( const char * strhost );
extern float pingperiodseconds;
extern int precise_ping; //if 0, use minimal CPU, but ping send-outs are only approximate, if 1, spinlock until precise time for ping is hit.
extern int ping_failed_to_send;
-void ping_setup();
+void ping_setup(const char * device);
#endif