add packet capture

This commit is contained in:
PoliEcho 2025-06-01 11:38:25 +02:00
parent ce9e9d0f3a
commit d48fb807de
5 changed files with 108 additions and 24 deletions

View File

@ -5,4 +5,6 @@
#include <wolfssl/ssl.h> #include <wolfssl/ssl.h>
extern WOLFSSL_CTX *ctx; extern WOLFSSL_CTX *ctx;
extern int sockfd; extern int sockfd;
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif #endif

View File

@ -0,0 +1,57 @@
#include <arpa/inet.h>
#include <cstring>
#include <ifaddrs.h>
#include <net/if.h>
#include <string>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
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;
}

View File

@ -0,0 +1,9 @@
#include <string>
#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

View File

@ -1,5 +1,6 @@
#include "../common/cleanup.h" #include "../common/cleanup.h"
#include "../common/const.h" #include "../common/const.h"
#include "IF_functions.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
@ -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 listen_for_ip_packets(const std::string interface_name, WOLFSSL *ssl) {
int raw_ip_scoketfd; int raw_ip_scoketfd;
unsigned char buffer[IP_PACKET_BUFFER_SIZE]; unsigned char buffer[IP_PACKET_BUFFER_SIZE];

View File

@ -3,11 +3,19 @@
#include "../common/global.h" #include "../common/global.h"
#include "../common/tls.h" #include "../common/tls.h"
#include "IF_functions.h"
#include "capture.h"
#include <cerrno>
#include <csignal> #include <csignal>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <thread>
#include <unistd.h> #include <unistd.h>
#include <wolfssl/options.h> #include <wolfssl/options.h>
#include <wolfssl/ssl.h> #include <wolfssl/ssl.h>
@ -15,7 +23,11 @@
WOLFSSL_CTX *ctx; WOLFSSL_CTX *ctx;
int sockfd; int sockfd;
int main() { int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <interface_name>" << std::endl;
return EINVAL;
}
std::signal(SIGTERM, safe_exit); std::signal(SIGTERM, safe_exit);
std::signal(SIGINT, safe_exit); std::signal(SIGINT, safe_exit);
std::signal(SIGQUIT, safe_exit); std::signal(SIGQUIT, safe_exit);
@ -65,7 +77,32 @@ int main() {
continue; 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_shutdown(ssl);
wolfSSL_free(ssl); wolfSSL_free(ssl);