summaryrefslogtreecommitdiff
path: root/src/user/app/netstack/netstack.c
blob: 55b716a8df85aab60e557e06df6cce95068d0d79 (plain)
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
#include "proto.h"
#include "util.h"
#include <camellia.h>
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <thread.h>

struct net_state state = {
	// TODO dynamically get mac
	.mac = {0x52, 0x54, 0x00, 0xCA, 0x77, 0x1A},
};

void network_thread(void *arg) { (void)arg;
	const size_t buflen = 4096;
	char *buf = malloc(buflen);
	for (;;) {
		long ret = _sys_read(state.raw_h, buf, buflen, -1);
		if (ret < 0) break;
		ether_parse((void*)buf, ret);
	}
	free(buf);
}

void fs_thread(void *arg);

int main(int argc, char **argv) {
	if (argc < 4) {
		eprintf("usage: netstack iface ip gateway");
		return 1;
	}
	state.raw_h = camellia_open(argv[1], OPEN_RW);
	if (state.raw_h < 0) {
		eprintf("couldn't open %s", argv[1]);
		return 1;
	}
	if (ip_parse(argv[2], &state.ip) < 0) {
		eprintf("invalid ip");
		return -1;
	}
	if (ip_parse(argv[3], &state.gateway) < 0) {
		eprintf("invalid gateway");
		return -1;
	}
	arp_request(state.gateway);
	thread_create(0, network_thread, NULL);
	thread_create(0, fs_thread, NULL);
	_sys_await();
	return 0;
}