/
send_ether.c
427 lines (348 loc) · 9.33 KB
/
send_ether.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* FileName: send_ether.c
* Author: Fasion Chan
* @contact: fasionchan@gmail.com
* @version: $Id$
*
* Description:
*
* Changelog:
*
**/
#include <argp.h>
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX_ETHERNET_FRAME_SIZE 1514
#define MAX_ETHERNET_DATA_SIZE 1500
#define ETHERNET_HEADER_SIZE 14
#define ETHERNET_DST_ADDR_OFFSET 0
#define ETHERNET_SRC_ADDR_OFFSET 6
#define ETHERNET_TYPE_OFFSET 12
#define ETHERNET_DATA_OFFSET 14
#define MAC_BYTES 6
/**
* Convert readable MAC address to binary format.
*
* Arguments
* a: buffer for readable format, like "08:00:27:c8:04:83".
*
* n: buffer for binary format, 6 bytes at least.
*
* Returns
* 0 if success, -1 if error.
**/
int mac_aton(const char *a, unsigned char *n) {
int matches = sscanf(a, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", n, n+1, n+2,
n+3, n+4, n+5);
return (6 == matches ? 0 : -1);
}
/**
* Fetch MAC address of given iface.
*
* Arguments
* iface: name of given iface.
*
* mac: buffer for binary MAC address, 6 bytes at least.
*
* s: socket for ioctl, optional.
*
* Returns
* 0 if success, -1 if error.
**/
int fetch_iface_mac(char const *iface, unsigned char *mac, int s) {
// value to return, 0 for success, -1 for error
int value_to_return = -1;
// create socket if needed(s is not given)
bool create_socket = (s < 0);
if (create_socket) {
s = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == s) {
return value_to_return;
}
}
// fill iface name to struct ifreq
struct ifreq ifr;
strncpy(ifr.ifr_name, iface, 15);
// call ioctl to get hardware address
int ret = ioctl(s, SIOCGIFHWADDR, &ifr);
if (-1 == ret) {
goto cleanup;
}
// copy MAC address to given buffer
memcpy(mac, ifr.ifr_hwaddr.sa_data, MAC_BYTES);
// success, set return value to 0
value_to_return = 0;
cleanup:
// close socket if created here
if (create_socket) {
close(s);
}
return value_to_return;
}
/**
* Fetch index of given iface.
*
* Arguments
* iface: name of given iface.
*
* s: socket for ioctl, optional.
*
* Returns
* Iface index(which is greater than 0) if success, -1 if error.
**/
int fetch_iface_index(char const *iface, int s) {
// iface index to return, -1 means error
int if_index = -1;
// create socket if needed(s is not given)
bool create_socket = (s < 0);
if (create_socket) {
s = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == s) {
return if_index;
}
}
// fill iface name to struct ifreq
struct ifreq ifr;
strncpy(ifr.ifr_name, iface, 15);
// call ioctl system call to fetch iface index
int ret = ioctl(s, SIOCGIFINDEX, &ifr);
if (-1 == ret) {
goto cleanup;
}
if_index = ifr.ifr_ifindex;
cleanup:
// close socket if created here
if (create_socket) {
close(s);
}
return if_index;
}
/**
* Bind socket with given iface.
*
* Arguments
* s: given socket.
*
* iface: name of given iface.
*
* Returns
* 0 if success, -1 if error.
**/
int bind_iface(int s, char const *iface) {
// fetch iface index
int if_index = fetch_iface_index(iface, s);
if (-1 == if_index) {
return -1;
}
// fill iface index to struct sockaddr_ll for binding
struct sockaddr_ll sll;
bzero(&sll, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = if_index;
sll.sll_pkttype = PACKET_HOST;
// call bind system call to bind socket with iface
int ret = bind(s, (struct sockaddr *)&sll, sizeof(sll));
if (-1 == ret) {
return -1;
}
return 0;
}
/**
* struct for an ethernet frame
**/
struct ethernet_frame {
// destination MAC address, 6 bytes
unsigned char dst_addr[6];
// source MAC address, 6 bytes
unsigned char src_addr[6];
// type, in network byte order
unsigned short type;
// data
unsigned char data[MAX_ETHERNET_DATA_SIZE];
};
/**
* Send data through given iface by ethernet protocol, using raw socket.
*
* Arguments
* iface: name of iface for sending.
*
* to: destination MAC address, in binary format.
*
* type: protocol type.
*
* data: data to send, ends with '\0'.
*
* s: socket for ioctl, optional.
*
* Returns
* 0 if success, -1 if error.
**/
int send_ether(char const *iface, unsigned char const *to, short type,
char const *data, int s) {
// value to return, 0 for success, -1 for error
int value_to_return = -1;
// create socket if needed(s is not given)
bool create_socket = (s < 0);
if (create_socket) {
s = socket(PF_PACKET, SOCK_RAW | SOCK_CLOEXEC, 0);
if (-1 == s) {
return value_to_return;
}
}
// bind socket with iface
int ret = bind_iface(s, iface);
if (-1 == ret) {
goto cleanup;
}
// fetch MAC address of given iface, which is the source address
unsigned char fr[6];
ret = fetch_iface_mac(iface, fr, s);
if (-1 == ret) {
goto cleanup;
}
// construct ethernet frame, which can be 1514 bytes at most
struct ethernet_frame frame;
// fill destination MAC address
memcpy(frame.dst_addr, to, MAC_BYTES);
// fill source MAC address
memcpy(frame.src_addr, fr, MAC_BYTES);
// fill type
frame.type = htons(type);
// truncate if data is to long
int data_size = strlen(data);
if (data_size > MAX_ETHERNET_DATA_SIZE) {
data_size = MAX_ETHERNET_DATA_SIZE;
}
// fill data
memcpy(frame.data, data, data_size);
int frame_size = ETHERNET_HEADER_SIZE + data_size;
ret = sendto(s, &frame, frame_size, 0, NULL, 0);
if (-1 == ret) {
goto cleanup;
}
// set return value to 0 if success
value_to_return = 0;
cleanup:
// close socket if created here
if (create_socket) {
close(s);
}
return value_to_return;
}
/**
* struct for storing command line arguments.
**/
struct arguments {
// name of iface through which data is sent
char const *iface;
// destination MAC address
char const *to;
// data type
unsigned short type;
// data to send
char const *data;
};
/**
* opt_handler function for GNU argp.
**/
static error_t opt_handler(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch(key) {
case 'd':
arguments->data = arg;
break;
case 'i':
arguments->iface = arg;
break;
case 'T':
if (sscanf(arg, "%hx", &arguments->type) != 1) {
return ARGP_ERR_UNKNOWN;
}
break;
case 't':
arguments->to = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/**
* Parse command line arguments given by argc, argv.
*
* Arguments
* argc: the same with main function.
*
* argv: the same with main function.
*
* Returns
* Pointer to struct arguments if success, NULL if error.
**/
static struct arguments const *parse_arguments(int argc, char *argv[]) {
// docs for program and options
static char const doc[] = "send_ether: send data through ethernet frame";
static char const args_doc[] = "";
// command line options
static struct argp_option const options[] = {
// Option -i --iface: name of iface through which data is sent
{"iface", 'i', "IFACE", 0, "name of iface for sending"},
// Option -t --to: destination MAC address
{"to", 't', "TO", 0, "destination mac address"},
// Option -T --type: data type
{"type", 'T', "TYPE", 0, "data type"},
// Option -d --data: data to send, optional since default value is set
{"data", 'd', "DATA", 0, "data to send"},
{ 0 }
};
static struct argp const argp = {
options,
opt_handler,
args_doc,
doc,
0,
0,
0,
};
// for storing results
static struct arguments arguments = {
.iface = NULL,
.to = NULL,
//default data type: 0x0900
.type = 0x0900,
// default data, 46 bytes string of 'a'
// since for ethernet frame data is 46 bytes at least
.data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
};
argp_parse(&argp, argc, argv, 0, 0, &arguments);
return &arguments;
}
int main(int argc, char *argv[]) {
// parse command line options to struct arguments
struct arguments const *arguments = parse_arguments(argc, argv);
if (NULL == arguments) {
fprintf(stderr, "Bad command line options given\n");
return 1;
}
// convert destinaction MAC address to binary format
unsigned char to[6];
int ret = mac_aton(arguments->to, to);
if (0 != ret) {
fprintf(stderr, "Bad MAC address given: %s\n", arguments->to);
return 2;
}
// send data
ret = send_ether(arguments->iface, to, arguments->type,
arguments->data, -1);
if (-1 == ret) {
perror("Fail to send ethernet frame: ");
return 3;
}
return 0;
}