From 31bc5f32633af7a6bda1dde756f57869848ded83 Mon Sep 17 00:00:00 2001 From: Glenn Date: Tue, 26 Dec 2023 22:47:28 +0100 Subject: [PATCH] refactor: extract packet type parameter handling --- CMakeLists.txt | 9 ++- src/ipg_types.h | 32 +++++++++++ src/{igmpgen.c => main.c} | 114 ++++++++++++++++++-------------------- src/usage.c | 30 ++++++++++ 4 files changed, 124 insertions(+), 61 deletions(-) create mode 100644 src/ipg_types.h rename src/{igmpgen.c => main.c} (80%) create mode 100644 src/usage.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e06c029..3b17979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,15 @@ project(igmpgen VERSION 1.0) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED True) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -Wall -Werror -D_GNU_SOURCE -pedantic") + + # Add the executable -add_executable(igmpgen src/igmpgen.c) +add_executable( + igmpgen + src/main.c + src/usage.c +) # Detect the net library find_library(LIBNET_LIBRARY NAMES net libnet libnet1 REQUIRED) diff --git a/src/ipg_types.h b/src/ipg_types.h new file mode 100644 index 0000000..9047feb --- /dev/null +++ b/src/ipg_types.h @@ -0,0 +1,32 @@ + +#ifndef IGP_TYPES_H +#define IGP_TYPES_H + +#define IPOPT_RA 148 /* router alert */ +#define IGMP_V3_MEMBERSHIP_REPORT 0x22 + +#include + +/* Structure to represent IGMP extra information */ +typedef struct { + u_int8_t igmp_version; + char *igmp_tag; + u_int8_t igmp_type; /* IGMP type */ + u_int8_t igmp_code; /* routing code */ + char *igmp_dst; + int group_override_dst; +} ipg_igmp_extra_t; + +typedef struct { + char *device; + char *igmp_type_version; // Combined type and version + char *igmp_group; + char *source_ip_port; // Combined source IP and port + char *destination_ip_port; // Combined destination IP and port + int delay_between_packets; // Optional, can be 0 if not specified +} ipg_cmdline_options_t; + +extern ipg_igmp_extra_t g_igmp_pkts[]; + +#endif /* IGP_TYPES_H */ + diff --git a/src/igmpgen.c b/src/main.c similarity index 80% rename from src/igmpgen.c rename to src/main.c index 333287c..1e013df 100644 --- a/src/igmpgen.c +++ b/src/main.c @@ -2,21 +2,21 @@ #include #include +#include + +/* missing type definitions (depending on system) */ +typedef unsigned char unchar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; + #include #include -#define IPOPT_RA 148 /* router alert */ -#define IGMP_V3_MEMBERSHIP_REPORT 0x22 +#include "ipg_types.h" -/* Structure to represent IGMP extra information */ -struct igmp_extra { - u_int8_t igmp_version; - char *igmp_tag; - u_int8_t igmp_type; /* IGMP type */ - u_int8_t igmp_code; /* routing code */ - char *igmp_dst; - int group_override_dst; -} g_igmp_pkts[] = { +/* global table of IGMP packet types*/ +ipg_igmp_extra_t g_igmp_pkts[] = { /* name, type (or version+type), code */ { 1, "query", 0x11, 0, "224.0.0.1", 0 }, { 1, "report", 0x12, 0, "224.0.0.1", 1 }, @@ -27,11 +27,26 @@ struct igmp_extra { { 3, "report", 0x22, 1, "224.0.0.22", 0 }, /* Note: end of list (please keep) */ - { 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0 }, }; void usage(char *); +ipg_igmp_extra_t * igmp_packet_type_find(u_int8_t igmp_version, char *igmp_request) { + ipg_igmp_extra_t *pkt_ptr; + + pkt_ptr = g_igmp_pkts; + while(pkt_ptr->igmp_version || pkt_ptr->igmp_tag){ + // if ((strcasecmp(pkt_ptr->igmp_tag, optarg) == 0) + if ((strcasecmp(pkt_ptr->igmp_tag, igmp_request) == 0) + && (igmp_version == pkt_ptr->igmp_version)){ + return pkt_ptr; + } + pkt_ptr++; + } + return NULL; +} + int main(int argc, char **argv) { u_int32_t ip_src = 0; @@ -40,17 +55,17 @@ int main(int argc, char **argv) char *igmp_group_str = NULL; char *ip_src_str = NULL; char *ip_dst_str = NULL; + u_char igmp_type = 0; + u_char igmp_code = 0; + ipg_igmp_extra_t *pkt_ptr; + int igmp_override; /* ports */ u_short src_prt = 3141; u_short dst_prt = 5926; /* igmp stuff */ - u_char igmp_type = 0; - u_char igmp_code = 0; u_int8_t igmp_version = 0; - int igmp_override; - struct igmp_extra *pkt_ptr; int ip_src_test = 1; int ip_dst_test = 1; @@ -65,9 +80,9 @@ int main(int argc, char **argv) char *device = "eth0"; int c; char *cp = NULL; - int found=0; printf("IGMP packet generator\n\n"); + printf("Detected options:\n"); /* Parsing command line */ while((c = getopt(argc, argv, "i:t:g:d:s:")) != EOF) @@ -86,30 +101,20 @@ int main(int argc, char **argv) } *cp++ = 0; - // igmp_version = (u_short)atoi(cp); igmp_version = (u_short)atoi(optarg); - pkt_ptr = g_igmp_pkts; - while(pkt_ptr->igmp_version || pkt_ptr->igmp_tag){ - // if ((strcasecmp(pkt_ptr->igmp_tag, optarg) == 0) - if ((strcasecmp(pkt_ptr->igmp_tag, cp) == 0) - && (igmp_version == pkt_ptr->igmp_version)){ - found = 1; - igmp_type = pkt_ptr->igmp_type; - igmp_code = pkt_ptr->igmp_code; - ip_dst_str = pkt_ptr->igmp_dst; - igmp_override = pkt_ptr->group_override_dst; - - break; - } - pkt_ptr++; - } - if (found){ - printf(" Packet = [%s] version [%d]\n", pkt_ptr->igmp_tag, igmp_version); - } else { + pkt_ptr = igmp_packet_type_find(igmp_version, cp); + if (!pkt_ptr) { + printf("ERROR: Packet type %d.%s not found!\n", igmp_version, cp); usage(argv[0]); exit(1); } + + igmp_type = pkt_ptr->igmp_type; + igmp_code = pkt_ptr->igmp_code; + ip_dst_str = pkt_ptr->igmp_dst; + igmp_override = pkt_ptr->group_override_dst; + printf(" Packet = [%s] version [%d]\n", pkt_ptr->igmp_tag, igmp_version); break; case 'g': @@ -122,13 +127,14 @@ int main(int argc, char **argv) // delay between packets in sec break; + case 'd': /* - * We expect the input to be of the form `ip.ip.ip.ip.port`. We + * We expect the input to be of the form `ip.ip.ip.ip:port`. We * point cp to the last dot of the IP address/port string and * then seperate them with a NULL byte. The optarg now points to * just the IP address, and cp points to the port. */ - case 'd': + if ((cp = strrchr(optarg, ':'))) { *cp++ = 0; @@ -157,9 +163,18 @@ int main(int argc, char **argv) exit(1); } } - if (!igmp_group_str || !device) + + if (!igmp_group_str) { usage(argv[0]); + fprintf(stderr, "ERROR: missing destination IGMP group\n"); + exit(EXIT_FAILURE); + } + + if (!device) + { + usage(argv[0]); + fprintf(stderr, "ERROR: missing device\n"); exit(EXIT_FAILURE); } @@ -354,25 +369,4 @@ int main(int argc, char **argv) return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS); } -void usage(char *name) -{ - struct igmp_extra *pkt_ptr; - - fprintf(stderr, "Usage: %s [options]\n", name); - fprintf(stderr, "\nOptions:\n"); - fprintf(stderr, " -i Specify the network interface (e.g., eth0)\n"); - fprintf(stderr, " -t Specify the IGMP packet type and version (e.g., 1.query)\n"); - fprintf(stderr, " -g Specify the IGMP group (e.g., 224.0.0.1)\n"); - fprintf(stderr, " -s Specify the source IP and port (e.g., 192.168.1.1:1234)\n"); - fprintf(stderr, " -d Specify the destination IP and port (e.g., 224.0.0.2:5678)\n"); - fprintf(stderr, " -n Specify delay between packets in seconds (optional)\n"); - - fprintf(stderr, "\nAvailable IGMP packet types:\n"); - pkt_ptr = g_igmp_pkts; - while(pkt_ptr->igmp_version || pkt_ptr->igmp_tag){ - fprintf(stderr," - %d.%s\n", pkt_ptr->igmp_version, pkt_ptr->igmp_tag); - pkt_ptr++; - } -} - // vim: ts=4 sts=4 sw=4 et diff --git a/src/usage.c b/src/usage.c new file mode 100644 index 0000000..b1f6212 --- /dev/null +++ b/src/usage.c @@ -0,0 +1,30 @@ + +#include +#include +#include + +#include "ipg_types.h" + +void usage(const char *name) +{ + ipg_igmp_extra_t *pkt_ptr; + + fprintf(stderr, "Usage: %s [options]\n", basename(name)); + fprintf(stderr, "\nOptions:\n"); + fprintf(stderr, " -i Specify the network interface (e.g., eth0)\n"); + fprintf(stderr, " -t Specify the IGMP packet type and version (e.g., 1.query)\n"); + fprintf(stderr, " -g Specify the destination IGMP group (e.g., 224.0.0.1)\n"); + fprintf(stderr, " -s Specify the source IP and port (e.g., 192.168.1.1:1234)\n"); + fprintf(stderr, " -d Specify the destination IP and port (e.g., 224.0.0.2:5678)\n"); + fprintf(stderr, " -n Specify delay between packets in seconds (optional)\n"); + + fprintf(stderr, "\nAvailable IGMP packet types:\n"); + + pkt_ptr = g_igmp_pkts; + while(pkt_ptr->igmp_version || pkt_ptr->igmp_tag){ + fprintf(stderr," - %d.%s\n", pkt_ptr->igmp_version, pkt_ptr->igmp_tag); + pkt_ptr++; + } + fprintf(stderr, "\n"); +} +