any interface is not supported because of missing ethernet header

This commit is contained in:
2025-03-26 07:36:41 +01:00
parent 39a8b7c016
commit 038a6de68e

28
main.c
View File

@@ -7,7 +7,9 @@ Copyright (c) 2025, Matthias Cramer, cramer@freestone.net
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pcap.h> #include <pcap.h>
#include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <string.h> #include <string.h>
@@ -20,7 +22,7 @@ Copyright (c) 2025, Matthias Cramer, cramer@freestone.net
#define DEFAULT_DEST_PORT 37008 // Default TZSP port #define DEFAULT_DEST_PORT 37008 // Default TZSP port
#define TZSP_ENCAP_LEN 4 // Length of TZSP encapsulation header #define TZSP_ENCAP_LEN 4 // Length of TZSP encapsulation header
#define TZSP_TAGGED_LEN 1 // Length of TZSP tagged field header (type) #define TZSP_TAGGED_LEN 1 // Length of TZSP tagged field header (type)
#define ETHERNET_HEADER_LENGTH 14 #define ETHERNET_HEADER_LENGTH 14 // Assuming Ethernet header is 14 bytes
// TZSP Header Structure // TZSP Header Structure
struct tzsp_header { struct tzsp_header {
@@ -140,6 +142,12 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
// Check that interface is not any
if (dev_name != NULL && strcmp(dev_name, "any") == 0) {
fprintf(stderr, "Error: Interface 'any' is not supported.\n");
return 1;
}
// Resolve the destination address // Resolve the destination address
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6 hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6
@@ -271,33 +279,29 @@ int main(int argc, char *argv[]) {
if (packet == NULL) if (packet == NULL)
continue; continue;
// Assuming Ethernet header is 14 bytes if (verbose) {
// Check IP version // Check IP version
ip_header = (struct ip*)(packet + ETHERNET_HEADER_LENGTH); ip_header = (struct ip*)(packet + ETHERNET_HEADER_LENGTH);
ip_protocol = ip_header->ip_v; ip_protocol = ip_header->ip_v & 0x0F; // Get IP version
if (ip_protocol == 4) { if (ip_protocol == 4) {
// IPv4 inet_ntop(AF_INET, &(ip_header->ip_src.s_addr), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &(ip_header->ip_src), source_ip_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET, &(ip_header->ip_dst.s_addr), dest_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &(ip_header->ip_dst), dest_ip_str, INET6_ADDRSTRLEN);
if (verbose) {
printf("IPv4 Packet: %s -> %s, IP Protocol: %d\n", printf("IPv4 Packet: %s -> %s, IP Protocol: %d\n",
source_ip_str, dest_ip_str, ip_header->ip_p); source_ip_str, dest_ip_str, ip_header->ip_p);
}
} else if (ip_protocol == 6) { } else if (ip_protocol == 6) {
// IPv6 // IPv6
ip6_header = (struct ip6_hdr*)(packet + ETHERNET_HEADER_LENGTH); ip6_header = (struct ip6_hdr*)(packet + ETHERNET_HEADER_LENGTH);
inet_ntop(AF_INET6, &(ip6_header->ip6_src), source_ip_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &(ip6_header->ip6_src), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &(ip6_header->ip6_dst), dest_ip_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &(ip6_header->ip6_dst), dest_ip_str, INET6_ADDRSTRLEN);
if (verbose) {
printf("IPv6 Packet: %s -> %s, Next Header: %d\n", printf("IPv6 Packet: %s -> %s, Next Header: %d\n",
source_ip_str, dest_ip_str, ip6_header->ip6_nxt); source_ip_str, dest_ip_str, ip6_header->ip6_nxt);
}
} else { } else {
printf("Non-IP Packet\n"); printf("Non-IP Packet, Protocol: %i\n", ip_protocol);
continue; }
} }
// Create TZSP Header // Create TZSP Header