-
Notifications
You must be signed in to change notification settings - Fork 0
/
eg_cmd_inject.c
157 lines (147 loc) · 3.61 KB
/
eg_cmd_inject.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
/**
* @file
* Egress command main for eg inject
*/
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pkttools/defines.h"
#include "pkttools/lib.h"
#include "pkttools/pcap.h"
#include "pcap.h"
#include "eg_enc.h"
/**
* print usage
*/
static void usage()
{
printf("usage: eg inject [-q] [-w <usec>] -i <device>\n");
printf("\n");
printf(" -q quiet mode\n");
printf(" -w <usec> sending interval\n");
printf(" -i <device> interface to inject\n");
}
enum {
EG_FILETYPE_PCAP = 0, /* pcap format (default) */
EG_FILETYPE_RAW = 1, /* raw frame */
};
/**
* print hexadecimal dump
*/
void print_hexdump(const char *hexbin, int len) {
int i;
const char *p;
p = hexbin;
for (i = 0; i < len; i++) {
if (i != 0 && i % 32 == 0) {
printf("\n");
}
if ((i % 8) == 0) {
printf(" ");
}
printf("%02x", (unsigned char)*p);
p++;
}
printf("\n");
return;
}
/**
* eg_inject main
*
* @param[in] argc arguments count
* @param[in] argv arguments value
*/
int eg_inject_main(int argc, char *argv[])
{
static char buf[2000];
unsigned long sendflags = 0;
char *ifname = NULL;
char *infile = NULL;
int filetype = EG_FILETYPE_PCAP;
int interval = 0;
int qflag = 0;
int c;
FILE *in;
pktif_t out;
int len;
struct timeval tv;
while ((c = getopt(argc, argv, "cw:t:r:i:qh?")) != -1) {
switch (c) {
case 'c':
fprintf(stderr, "warning: -c option has been deprecated.\n");
sendflags |= PKT_SEND_FLAG_COMPLETE;
break;
case 'w':
interval = atoi(optarg);
break;
case 't':
if (strncasecmp("PCAP", optarg, strlen(optarg)) == 0) {
filetype = EG_FILETYPE_PCAP;
} else if (strncasecmp("RAW", optarg, strlen(optarg)) == 0) {
filetype = EG_FILETYPE_RAW;
} else {
fprintf(stderr, "invalid file type: %s\n", optarg);
usage();
exit(EXIT_SUCCESS);
}
break;
case 'r':
infile = optarg;
break;
case 'i':
ifname = optarg;
break;
case 'q':
qflag = 1;
break;
case 'h':
case '?':
usage();
exit(EXIT_SUCCESS);
default:
usage();
exit(EXIT_FAILURE);
}
}
if (ifname == NULL) {
usage();
exit(EXIT_FAILURE);
}
if (infile != NULL) {
in = fopen(infile, "r");
if (!in) {
fprintf(stderr, "Failed to open: %s\n", infile);
}
} else {
in = stdin;
}
out = pkthandler.open_send(ifname, sendflags);
if (filetype == EG_FILETYPE_PCAP) {
while (pkt_pcap_read(in, buf, sizeof(buf), &len, NULL, &tv) > 0) {
pkthandler.send(out, buf, len);
if (!qflag) {
printf("\n---> %d bytes to %s\n", len, ifname);
print_hexdump(buf, len);
}
if (interval) {
usleep(interval * 1000);
}
}
} else {
while ((len = fread(buf, 1, sizeof(buf), in)) > 0) {
pkthandler.send(out, buf, len);
if (!qflag) {
printf("\n---> %d bytes to %s\n", len, ifname);
print_hexdump(buf, len);
}
if (interval) {
usleep(interval * 1000);
}
}
}
exit(EXIT_SUCCESS);
}