refactor: extract packet type parameter handling

This commit is contained in:
Glenn Y. Rolland 2023-12-26 22:47:28 +01:00
parent b0678332d7
commit 31bc5f3263
4 changed files with 124 additions and 61 deletions

View file

@ -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)

32
src/ipg_types.h Normal file
View file

@ -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 <sys/types.h>
/* 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 */

View file

@ -2,21 +2,21 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
/* missing type definitions (depending on system) */
typedef unsigned char unchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
#include <libnet.h>
#include <netinet/igmp.h>
#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 <interface> Specify the network interface (e.g., eth0)\n");
fprintf(stderr, " -t <type> Specify the IGMP packet type and version (e.g., 1.query)\n");
fprintf(stderr, " -g <group> Specify the IGMP group (e.g., 224.0.0.1)\n");
fprintf(stderr, " -s <source> Specify the source IP and port (e.g., 192.168.1.1:1234)\n");
fprintf(stderr, " -d <destination> Specify the destination IP and port (e.g., 224.0.0.2:5678)\n");
fprintf(stderr, " -n <number> 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

30
src/usage.c Normal file
View file

@ -0,0 +1,30 @@
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#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 <interface> Specify the network interface (e.g., eth0)\n");
fprintf(stderr, " -t <type> Specify the IGMP packet type and version (e.g., 1.query)\n");
fprintf(stderr, " -g <group> Specify the destination IGMP group (e.g., 224.0.0.1)\n");
fprintf(stderr, " -s <source> Specify the source IP and port (e.g., 192.168.1.1:1234)\n");
fprintf(stderr, " -d <destination> Specify the destination IP and port (e.g., 224.0.0.2:5678)\n");
fprintf(stderr, " -n <number> 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");
}