diff --git a/src/common/global.h b/src/common/global.h index 163f27f..c6670e7 100644 --- a/src/common/global.h +++ b/src/common/global.h @@ -5,4 +5,6 @@ #include extern WOLFSSL_CTX *ctx; extern int sockfd; + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif \ No newline at end of file diff --git a/src/server/IF_functions.cpp b/src/server/IF_functions.cpp new file mode 100644 index 0000000..1a170cf --- /dev/null +++ b/src/server/IF_functions.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +std::string getInterfaceIP(const std::string &interfaceName) { + int sockfd; + struct ifreq ifr; + + // Create socket + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + return nullptr; + } + + // Set interface name + strncpy(ifr.ifr_name, interfaceName.c_str(), IFNAMSIZ - 1); + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; + + // Get IP address + if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) { + close(sockfd); + return nullptr; + } + + close(sockfd); + + // Convert to string + struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr; + return std::string(inet_ntoa(addr->sin_addr)); +} + +int get_interface_index(const char *interface_name) { + struct ifaddrs *ifaddr, *ifa; + int interface_index = -1; + + if (getifaddrs(&ifaddr) == -1) { + return -1; + } + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) + continue; + + if (strcmp(ifa->ifa_name, interface_name) == 0) { + interface_index = if_nametoindex(interface_name); + break; + } + } + + freeifaddrs(ifaddr); + return interface_index; +} \ No newline at end of file diff --git a/src/server/IF_functions.h b/src/server/IF_functions.h new file mode 100644 index 0000000..cd4bb75 --- /dev/null +++ b/src/server/IF_functions.h @@ -0,0 +1,9 @@ +#include + +#ifndef IF_FUNCTIONS_H +#define IF_FUNCTIONS_H + +std::string getInterfaceIP(const std::string &interfaceName); +int get_interface_index(const char *interface_name); + +#endif // IF_FUNCTIONS_H \ No newline at end of file diff --git a/src/server/capture.cpp b/src/server/capture.cpp index 0a79f8b..a59c62b 100644 --- a/src/server/capture.cpp +++ b/src/server/capture.cpp @@ -1,5 +1,6 @@ #include "../common/cleanup.h" #include "../common/const.h" +#include "IF_functions.h" #include #include #include @@ -30,28 +31,6 @@ void process_incoming_ip_packet(unsigned char *buffer, int packet_size, } } -int get_interface_index(const char *interface_name) { - struct ifaddrs *ifaddr, *ifa; - int interface_index = -1; - - if (getifaddrs(&ifaddr) == -1) { - return -1; - } - - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) - continue; - - if (strcmp(ifa->ifa_name, interface_name) == 0) { - interface_index = if_nametoindex(interface_name); - break; - } - } - - freeifaddrs(ifaddr); - return interface_index; -} - int listen_for_ip_packets(const std::string interface_name, WOLFSSL *ssl) { int raw_ip_scoketfd; unsigned char buffer[IP_PACKET_BUFFER_SIZE]; diff --git a/src/server/main.cpp b/src/server/main.cpp index c95ad34..8ec4112 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -3,11 +3,19 @@ #include "../common/global.h" #include "../common/tls.h" +#include "IF_functions.h" +#include "capture.h" + +#include #include #include #include +#include #include +#include +#include #include +#include #include #include #include @@ -15,7 +23,11 @@ WOLFSSL_CTX *ctx; int sockfd; -int main() { +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return EINVAL; + } std::signal(SIGTERM, safe_exit); std::signal(SIGINT, safe_exit); std::signal(SIGQUIT, safe_exit); @@ -65,7 +77,32 @@ int main() { continue; } - handle_connection(ssl); + { // send intreface ip to client + std::string interface_ip = getInterfaceIP(argv[1]); + if (interface_ip.empty()) { + std::cerr << "Failed to get IP address for interface: " << argv[1] + << std::endl; + wolfSSL_free(ssl); + close(client_fd); + continue; + } + + interface_ip.insert(0, 1, IP_PACKET_TYPE); + + int bytes_sent = + wolfSSL_write(ssl, interface_ip.c_str(), interface_ip.size()); + if (bytes_sent <= 0) { + int ssl_error = wolfSSL_get_error(ssl, bytes_sent); + std::cerr << "wolfSSL_write failed with error: " << ssl_error + << std::endl; + wolfSSL_free(ssl); + close(client_fd); + continue; + } + } + + // handle_connection(ssl); + std::thread IP_capture_thread(listen_for_ip_packets, argv[1], ssl); wolfSSL_shutdown(ssl); wolfSSL_free(ssl);