From 41ca93fc94cfef39a146f94e4ba3e6fa25497d53 Mon Sep 17 00:00:00 2001 From: Glenn Date: Tue, 26 Dec 2023 11:13:13 +0100 Subject: [PATCH] feat: implement basic packet creation --- src/igmpgen.c | 76 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/src/igmpgen.c b/src/igmpgen.c index 855d108..6081e39 100644 --- a/src/igmpgen.c +++ b/src/igmpgen.c @@ -21,6 +21,7 @@ struct igmp_extra { { 2, "query", 0x11, 1, "224.0.0.1", 0 }, { 2, "report", 0x16, 1, "224.0.0.2", 1 }, { 2, "leave", 0x17, 1, "224.0.0.2", 0 }, + /* Note: end of list (please keep) */ { 0, 0, 0, 0 }, }; @@ -32,17 +33,22 @@ void usage(char *name) int main(int argc, char **argv) { - /* ip addresses */ u_int32_t ip_src = 0; char *ip_src_str = NULL; - /* libnet stuff */ char neterr[LIBNET_ERRBUF_SIZE]; libnet_t *netcontext = NULL; /* misc */ - int c; + int c; char *device = NULL; + u_int8_t igmp_version = 0; + char *cp = NULL; + int found = 0; + struct igmp_extra *pkt_ptr; + u_char igmp_type = 0; + u_char igmp_code = 0; + printf("IGMP packet generator\n\n"); printf("Parsing command line...\n"); @@ -55,8 +61,27 @@ int main(int argc, char **argv) device = optarg; break; case 't': - /* Packet version/type argument handling */ - printf(" Packet version/type = [%s]\n", optarg); + if (!(cp = strrchr(optarg, '.'))) { + usage(argv[0]); + exit(1); + } + *cp++ = 0; + igmp_version = (u_short)atoi(cp); + pkt_ptr = g_igmp_pkts; + while (pkt_ptr->igmp_version || pkt_ptr->igmp_tag) { + if ((strcasecmp(pkt_ptr->igmp_tag, optarg) == 0) + && (igmp_version == pkt_ptr->igmp_version)) { + found = 1; + igmp_type = pkt_ptr->igmp_type; + igmp_code = pkt_ptr->igmp_code; + break; + } + pkt_ptr++; + } + if (!found) { + usage(argv[0]); + exit(1); + } break; default: usage(argv[0]); @@ -72,22 +97,49 @@ int main(int argc, char **argv) printf("done\n"); /* Memory initialization */ - printf("Initializing libnet context..."); + printf("Initializing libnet context...\n"); netcontext = libnet_init(LIBNET_RAW4, device, neterr); - if (!netcontext){ - fprintf(stderr, neterr); + if (!netcontext) { + fprintf(stderr, "%s\n", neterr); exit(1); } libnet_clear_packet(netcontext); - if (!ip_src_str) - { + + if (!ip_src_str) { ip_src = libnet_get_ipaddr4(netcontext); ip_src_str = libnet_addr2name4(ip_src, LIBNET_DONT_RESOLVE); } - printf("done\n"); + printf("Packet construction...\n"); + + /* Packet construction: IGMP */ + printf(" Building IGMP content...\n"); + libnet_ptag_t ptag; + ptag = libnet_build_igmp( + igmp_type, // IGMP type + igmp_code, // IGMP code + 0, // checksum + 0, // group address + NULL, // payload + 0, // payload size + netcontext, // libnet context + 0 // ptag + ); + if (ptag == -1) { + fprintf(stderr, "Error building IGMP header: %s\n", libnet_geterror(netcontext)); + libnet_destroy(netcontext); + exit(EXIT_FAILURE); + } + + printf(" done\n"); + + /* Send packet */ + if (libnet_write(netcontext) == -1) { + fprintf(stderr, "Error sending packet: %s\n", libnet_geterror(netcontext)); + } else { + printf("Packet sent successfully.\n"); + } - /* Clean up and exit */ libnet_destroy(netcontext); return 0; }