Project

General

Profile

Bug #252 ยป rawsend_massive.c

Sven Eckelmann, 05/15/2016 01:48 PM

 
1
#include <stdio.h>
2
#include <sys/ioctl.h>
3
#include <net/if.h> 
4
#include <sys/types.h>
5
#include <sys/socket.h>
6
#include <netpacket/packet.h>
7
#include <net/ethernet.h>
8
#include <stdint.h>
9
#include <string.h>
10
#include <arpa/inet.h>
11
#include <unistd.h>
12

    
13
int getmac(const char *src, unsigned char *dst)
14
{
15
	unsigned int n[6];
16
	int i;
17
	if (sscanf(src, "%x:%x:%x:%x:%x:%x",
18
	    &n[0], &n[1], &n[2], &n[3], &n[4], &n[5]) != 6) {
19
		if(sscanf(src, "%2x%2x.%2x%2x.%2x%2x",
20
		   &n[0], &n[1], &n[2], &n[3], &n[4], &n[5]) != 6) {
21
			return 1;
22
		}
23
	}
24

    
25
	for (i = 0; i < 6; i++) {
26
		if (n[i] <= 255)
27
			dst[i] = (unsigned char)n[i];
28
		else
29
			return 1;
30
	}
31

    
32
	return 0;
33
}
34

    
35
int main(int argc, char *argv[])
36
{
37
	int rawsock;
38
	uint16_t type = 0x1337;
39
	struct ifreq req;
40
	struct sockaddr_ll addr;
41
	struct iovec vector[2];
42
	char buf[256];
43
	size_t size = 256;
44
	struct ether_header header;
45
	uint32_t x;
46

    
47
	if (argc != 2 && argc != 3) {
48
		fprintf(stderr, "Usage: %s DEVICE [DST-MAC]\n", argv[0]);
49
		return 1;
50
	}
51

    
52
	if ((rawsock = socket(PF_PACKET, SOCK_RAW, htons(type))) < 0 ) {
53
			fprintf(stderr, "Could not open raw socket\n");
54
			return 1;
55
	}
56

    
57
	strncpy(req.ifr_name, argv[1], IFNAMSIZ);
58
	if (ioctl(rawsock, SIOCGIFINDEX, &req) < 0) {
59
		fprintf(stderr, "Could not find device %s\n", argv[1]);
60
		return 1;
61
	}
62

    
63
	addr.sll_family = AF_PACKET;
64
	addr.sll_protocol = htons(type);
65
	addr.sll_ifindex  = req.ifr_ifindex;
66

    
67
	if (bind(rawsock, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) {
68
		fprintf(stderr, "Could not bind to device %s\n", argv[1]);
69
		return 1;
70
	}
71

    
72
	strncpy(req.ifr_name, argv[1], IFNAMSIZ);
73
	if (ioctl(rawsock, SIOCGIFHWADDR, &req) < 0) {
74
		fprintf(stderr, "Could not get hwaddr of device %s\n", argv[1]);
75
		return 1;
76
	}
77

    
78
	if (argc == 2) {
79
		vector[0].iov_base = &header;
80
		vector[0].iov_len  = sizeof(struct ether_header);
81
		vector[1].iov_base = buf;
82
		vector[1].iov_len  = size;
83

    
84
		if (readv(rawsock, vector, 2) < 0) {
85
			close(rawsock);
86
			fprintf(stderr, "Could not receive message\n");
87
			return 1;
88
		}
89
		printf("Received message\n");
90
	} else if (argc == 3) {
91
		for (x = 1; x < 3000; x++) {
92
			vector[0].iov_base = &header;
93
			vector[0].iov_len  = sizeof(struct ether_header);
94
			vector[1].iov_base = buf;
95
			vector[1].iov_len  = size;
96

    
97
			header.ether_type = htons(type);
98

    
99
			header.ether_shost[0] = 0;
100
			header.ether_shost[1] = 0;
101
			header.ether_shost[2] = (x >> 24) & 0xff;
102
			header.ether_shost[3] = (x >> 16) & 0xff;
103
			header.ether_shost[4] = (x >> 8) & 0xff;
104
			header.ether_shost[5] = (x) & 0xff;
105

    
106
			if (getmac(argv[2], header.ether_dhost)) {
107
				close(rawsock);
108
				fprintf(stderr, "Could not parse mac address %s\n", argv[2]);
109
				return 1;
110
			}
111
			if (writev(rawsock, vector, 2) < 0) {
112
				close(rawsock);
113
				fprintf(stderr, "Could not send message\n");
114
				return 1;
115
			}
116
		}
117
	}
118
	close(rawsock);
119
	return 0;
120
}
    (1-1/1)