2009-09-03 21:18:43 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2009-09-04 22:54:06 +02:00
|
|
|
|
2009-09-04 21:34:49 +02:00
|
|
|
#include <libnet.h>
|
|
|
|
#include <netinet/igmp.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[] = {
|
|
|
|
/* name, type (or version+type), code */
|
|
|
|
{ 1, "query", 0x11, 0, "224.0.0.1", 0 },
|
|
|
|
{ 1, "report", 0x12, 0, "224.0.0.1", 1 },
|
|
|
|
{ 1, "dvmrp", 0x13, 0, "224.0.0.1", 0 },
|
|
|
|
{ 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 },
|
2009-09-07 22:54:51 +02:00
|
|
|
|
2009-09-07 21:47:11 +02:00
|
|
|
/* Note: end of list (please keep) */
|
2009-09-04 21:34:49 +02:00
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
};
|
2009-09-03 21:18:43 +02:00
|
|
|
|
|
|
|
void usage(char *name)
|
|
|
|
{
|
2009-09-07 21:47:11 +02:00
|
|
|
fprintf(stderr, "usage: %s -i ethdevice -t packet.version\n", name);
|
2009-09-03 21:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2009-09-04 22:54:06 +02:00
|
|
|
u_int32_t ip_src = 0;
|
|
|
|
char *ip_src_str = NULL;
|
|
|
|
/* libnet stuff */
|
|
|
|
char neterr[LIBNET_ERRBUF_SIZE];
|
|
|
|
libnet_t *netcontext = NULL;
|
|
|
|
|
2009-09-03 21:18:43 +02:00
|
|
|
/* misc */
|
2009-09-07 22:54:51 +02:00
|
|
|
int c;
|
2009-09-03 21:18:43 +02:00
|
|
|
char *device = NULL;
|
2009-09-07 22:54:51 +02:00
|
|
|
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;
|
|
|
|
|
2009-09-03 21:18:43 +02:00
|
|
|
printf("IGMP packet generator\n\n");
|
|
|
|
|
|
|
|
printf("Parsing command line...\n");
|
2009-09-07 21:47:11 +02:00
|
|
|
while((c = getopt(argc, argv, "i:t:")) != EOF)
|
2009-09-03 21:18:43 +02:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
printf(" Net interface = [%s]\n", optarg);
|
|
|
|
device = optarg;
|
|
|
|
break;
|
2009-09-07 21:47:11 +02:00
|
|
|
case 't':
|
2009-09-07 22:54:51 +02:00
|
|
|
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);
|
|
|
|
}
|
2009-09-07 21:47:11 +02:00
|
|
|
break;
|
2009-09-03 21:18:43 +02:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!device)
|
|
|
|
{
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-09-04 21:34:49 +02:00
|
|
|
|
2009-09-03 21:18:43 +02:00
|
|
|
printf("done\n");
|
|
|
|
|
2009-09-07 21:47:11 +02:00
|
|
|
/* Memory initialization */
|
2009-09-07 22:54:51 +02:00
|
|
|
printf("Initializing libnet context...\n");
|
2009-09-04 22:54:06 +02:00
|
|
|
netcontext = libnet_init(LIBNET_RAW4, device, neterr);
|
2009-09-07 22:54:51 +02:00
|
|
|
if (!netcontext) {
|
|
|
|
fprintf(stderr, "%s\n", neterr);
|
2009-09-04 22:54:06 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
libnet_clear_packet(netcontext);
|
2009-09-07 22:54:51 +02:00
|
|
|
|
|
|
|
if (!ip_src_str) {
|
2009-09-04 22:54:06 +02:00
|
|
|
ip_src = libnet_get_ipaddr4(netcontext);
|
|
|
|
ip_src_str = libnet_addr2name4(ip_src, LIBNET_DONT_RESOLVE);
|
|
|
|
}
|
|
|
|
|
2009-09-07 22:54:51 +02:00
|
|
|
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");
|
|
|
|
}
|
2009-09-04 22:54:06 +02:00
|
|
|
|
2009-09-07 21:47:11 +02:00
|
|
|
libnet_destroy(netcontext);
|
2009-09-03 21:18:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|