commit dc7cd8a254926625815a4284e949c1e885799834
parent 6b89363e6b79ecbf612306d42a8ef94a5a2f756a
Author: mrbesen <y.g.2@gmx.de>
Date: Thu, 18 Aug 2022 11:13:02 +0200
Add option to bind to specific network interface
Diffstat:
3 files changed, 17 insertions(+), 4 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,7 +746,8 @@ 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;
}
@@ -766,7 +769,7 @@ int main( int argc, const char ** argv )
}
else
{
- ping_setup();
+ ping_setup(device);
OGCreateThread( PingSend, 0 );
OGCreateThread( PingListen, 0 );
}
diff --git a/ping.c b/ping.c
@@ -8,6 +8,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/socket.h>
#include "ping.h"
#include "error_handling.h"
@@ -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