-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
149 lines (135 loc) · 3.42 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* This file is part of pgen, a packet generator tool.
* Copyright (C) 2013 Balakumaran Kannan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pgen.h"
/**
* It starts exactly here
*/
int32_t main(int32_t argc, char **argv) {
FILE *fp;
int32_t buff_size;
char if_name[IFNAMSIZ];
char dst_mac[CHAR_MAC_LEN];
char *cp_buff = NULL;
char *cp_cur = NULL;
char option[MAX_OPTION_LEN];
char value[MAX_VALUE_LEN];
char conf_file[PATH_MAX];
int32_t len;
/* Choose the conf file */
if (argc < 2)
strcpy(conf_file, DEF_PGEN_CONF);
else if (argc == 2)
strcpy(conf_file, argv[1]);
else {
usage();
goto err;
}
/* Open Conf file */
fp = fopen(conf_file, "r");
if (!fp) {
PGEN_ERROR("fopen error", errno);
return -1;
}
/* Get buffer size */
if (pgen_parse_option(fp, option, value))
goto err;
if (strcmp(option, "BUFF_SIZE"))
goto err;
if (pgen_store_num(&buff_size, value))
goto err;
/* Get interface name */
if (pgen_parse_option(fp, option, value))
goto err;
if (strcmp(option, "IF_NAME"))
goto err;
if (!strcpy(if_name, value))
goto err;
if (validate_if(if_name))
goto err;
/* Get recipient mac address */
if (pgen_parse_option(fp, option, value))
goto err;
if (strcmp(option, "PK_DST_MAC"))
goto err;
if (!strcpy(dst_mac, value))
goto err;
/* Allocating the packet itself ;) */
cp_buff = malloc(buff_size);
if (!cp_buff) {
PGEN_ERROR("malloc failed", errno);
goto err;
}
memset(cp_buff, 0, buff_size);
cp_cur = cp_buff;
/* write data in the packet buffer */
while (pgen_parse_option(fp, option, value) != EOF) {
if (!strcmp(option, "ETHER_HEADER")) {
cp_cur = pgen_ethr_hdr_writer(fp, cp_cur);
}
/* L2 */
else if (!strcmp(option, "ARP")) {
cp_cur = pgen_arp_writer(fp, cp_cur);
}
/* L3 */
else if (!strcmp(option, "IPV6")) {
cp_cur = pgen_ipv6_writer(fp, cp_cur);
}
/* L4 */
else if (!strcmp(option, "ICMP6")) {
cp_cur = pgen_icmp6_writer(fp, cp_cur);
}
else if (!strcmp(option, "UDP")) {
cp_cur = pgen_udp_writer(fp, cp_cur);
}
else if (!strcmp(option, "RAW")) {
len = raw_data_writer(fp, cp_cur);
if (len < 0)
goto err;
cp_cur += len;
}
else {
PGEN_INFO("Packet type not yet supported");
PGEN_PRINT_DATA("%s\t%s\n", option, value);
goto err;
}
if (!cp_cur)
goto err;
}
/* Cut the extra bytes */
buff_size = cp_cur - cp_buff;
/* Heap might grow in either direction */
if (buff_size < 0)
buff_size *= -1;
cp_buff = realloc((void *)cp_buff, buff_size);
/* Send the packet in wire */
if (send_packet(if_name, dst_mac, cp_buff, buff_size)) {
PGEN_INFO("send_packet failed");
goto err;
}
PGEN_INFO("SUCCESS");
fclose(fp);
/* free will accept NULL also */
free(cp_buff);
return 0;
err:
PGEN_INFO("ERROR CASE");
fclose(fp);
/* free will accept NULL also */
free(cp_buff);
return -1;
}