-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.c
314 lines (270 loc) · 6.68 KB
/
net.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
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <argp.h>
#include <netdb.h>
#include <stdlib.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <asm/env.h>
#include <asm/eth.h>
const char *argp_program_version = "0.1";
const char *argp_program_bug_address = "[email protected]";
static char doc[] = "";
static char args_doc[] = "URL";
static struct argp_option options[] = {
{"interface", 'i', "string", 0, "native interface to use"},
{"mac", 'm', "mac address", 0, "MAC address for the lkl interface"},
{"address", 'a', "IPv4 address", 0, "IPv4 address for the lkl interface"},
{"netmask-length", 'n', "int", 0, "IPv4 netmask length for the lkl interface"},
{"gateway", 'g', "IPv4 address", 0, "IPv4 gateway for lkl"},
{"lkl", 'l', 0, 0, "Use LKL"},
{0},
};
typedef struct cl_args_ {
struct ether_addr *mac;
const char *iface, *request;
struct in_addr address, gateway, host;
unsigned int netmask_len, port, lkl;
} cl_args_t;
cl_args_t cla = {
.port = 80,
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
cl_args_t *cla = state->input;
switch (key) {
case 'm':
{
cla->mac=ether_aton(arg);
if (!cla->mac) {
printf("bad MAC address: %s\n", arg);
return -1;
}
break;
}
case 'a':
{
struct hostent *hostinfo =gethostbyname(arg);
if (!hostinfo) {
printf("unknown host %s\n", arg);
return -1;
}
cla->address=*(struct in_addr*)hostinfo->h_addr;
break;
}
case 'g':
{
struct hostent *hostinfo =gethostbyname(arg);
if (!hostinfo) {
printf("unknown host %s\n", arg);
return -1;
}
cla->gateway=*(struct in_addr*)hostinfo->h_addr;
break;
}
case 'n':
{
cla->netmask_len=atoi(arg);
if (cla->netmask_len <= 0 || cla->netmask_len >=31) {
printf("bad netmask length %d\n", cla->netmask_len);
return -1;
}
break;
}
case 'i':
{
if (if_nametoindex(arg) < 0) {
printf("invalid interface: %s\n", arg);
return -1;
}
cla->iface=arg;
break;
}
case 'l':
{
cla->lkl=1;
break;
}
case ARGP_KEY_ARG:
{
char *host, *request, *port;
struct hostent *hostinfo;
//URL: http://host:port/request
if (strncasecmp("http://", arg, sizeof("http://")-1) != 0) {
printf("bad url: %s\n", arg);
return -1;
}
host=arg+sizeof("http://")-1;
request=strchr(host, '/');
port=strchr(host, ':');
if (port && request && port < request) {
char tmp[request-port];
memcpy(tmp, port+1, sizeof(tmp));
tmp[sizeof(tmp)]=0;
cla->port=atoi(tmp);
}
if (!request)
cla->request="";
else
cla->request=request+1;
if (port)
*port=0;
else if (request)
*request=0;
hostinfo = gethostbyname(host);
if (!hostinfo) {
printf("unknown host %s\n", host);
return -1;
}
cla->host=*(struct in_addr*)hostinfo->h_addr;
break;
}
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
int do_connect(int fd, const struct sockaddr *saddr, socklen_t slen)
{
if (cla.lkl)
return lkl_sys_connect(fd, (struct sockaddr*)saddr, slen);
else
return connect(fd, saddr, slen);
}
int do_socket(int family, int type, int protocol)
{
if (cla.lkl)
return lkl_sys_socket(family, type, protocol);
else
return socket(family, type, protocol);
}
ssize_t do_read(int fd, void *buf, size_t len)
{
if (cla.lkl)
return lkl_sys_read(fd, buf, len);
else
return read(fd, buf, len);
}
ssize_t do_write(int fd, const void *buf, size_t len)
{
if (cla.lkl)
return lkl_sys_write(fd, buf, len);
else
return write(fd, buf, len);
}
#define get_error(err) (cla.lkl?-err:errno)
static int do_full_write(int fd, const char *buffer, int length)
{
int n, todo=length;
while (todo && (n=do_write(fd, buffer, todo)) >= 0) {
todo-=n;
buffer+=n;
}
if (!todo)
return length;
else
return n;
}
int main(int argc, char **argv)
{
int err, sock;
struct sockaddr_in saddr = {
.sin_family = AF_INET,
};
char req[1024], buffer[4096];
if (argp_parse(&argp, argc, argv, 0, 0, &cla) < 0)
return -1;
if (!cla.host.s_addr) {
printf("no url specified!\n");
return -1;
}
if (cla.lkl) {
int ifindex, sock;
struct ifreq ifr;
if (!cla.iface || !cla.mac || !cla.netmask_len ||
!cla.address.s_addr || !cla.gateway.s_addr) {
printf("lkl mode and no interface, mac, address, netmask length or gateway specified!\n");
return -1;
}
strncpy(ifr.ifr_name, cla.iface, IFNAMSIZ);
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
fprintf(stderr, "failed to create DGRAM INET socket: %s\n", strerror(errno));
return -1;
}
err = ioctl(sock, SIOCGIFFLAGS, (long)&ifr);
if (err) {
fprintf(stderr, "failed to get flags on %s: %s\n", cla.iface, strerror(errno));
return -1;
}
ifr.ifr_flags |= IFF_PROMISC;
err = ioctl(sock, SIOCSIFFLAGS, (long)&ifr);
if (err) {
fprintf(stderr, "failed to set %s in promisc mode\n", cla.iface);
return -1;
}
if (lkl_env_init(256*1024*1024) < 0)
return -1;
if ((ifindex=lkl_add_eth(cla.iface, (char*)cla.mac, 32)) == 0)
return -1;
if ((err=lkl_if_set_ipv4(ifindex, cla.address.s_addr,
cla.netmask_len)) < 0) {
printf("failed to set IP: %s/%d: %s\n",
inet_ntoa(cla.address), cla.netmask_len,
strerror(-err));
return -1;
}
if ((err=lkl_if_up(ifindex)) < 0) {
printf("failed to bring interface up: %s\n", strerror(-err));
return -1;
}
if ((err=lkl_set_gateway(cla.gateway.s_addr))) {
printf("failed to set gateway %s: %s\n",
inet_ntoa(cla.gateway), strerror(-err));
return -1;
}
}
if ((sock=do_socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("can't create socket: %s\n", strerror(get_error(sock)));
return -1;
}
saddr.sin_port = htons(cla.port);
saddr.sin_addr = cla.host;
if ((err=do_connect(sock, (struct sockaddr*)&saddr,
sizeof(saddr))) < 0) {
printf("can't connect to %s:%u: %s\n",
inet_ntoa(cla.host), ntohs(saddr.sin_port),
strerror(get_error(err)));
return -1;
}
snprintf(req, sizeof(req), "GET /%s HTTP/1.0\r\n\r\n", cla.request);
if ((err=do_full_write(sock, req, sizeof(req))) < 0) {
printf("can't write: %s\n", strerror(get_error(err)));
return -1;
}
#define SAMPLE_RATE 1
unsigned long total=0, last_time=time(NULL), last_bytes=0;
while ((err=do_read(sock, buffer, sizeof(buffer))) > 0) {
write(1, buffer, err);
total+=err;
if (time(NULL)-last_time >= SAMPLE_RATE) {
fprintf(stderr, "%ld %ld\n", total, (total-last_bytes)/SAMPLE_RATE);
last_time=time(NULL);
last_bytes=total;
}
}
if (cla.lkl)
lkl_sys_halt();
return 0;
}