summaryrefslogtreecommitdiff
path: root/net9p.c
blob: 99d3d720a974b3f76d593e6acd91bbaabee183df (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
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "net9p.h"
#include "coroutine.h"

void net9p_listen_cr(void *_stack) {
	static union {
		struct sockaddr_in in;
		struct sockaddr gen;
	} addr = {0};
	if (!addr.in.sin_family) {
		addr.in.sin_family = AF_INET;
		addr.in.sin_port = 0x2823;
	}
	net9p_listen_stack_t *stack = _stack;

	cr_begin();
	
	stack->fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
	bind(stack->fd, &addr.gen, sizeof addr);
	listen(stack->fd, 5);

	for (;;) {
		int conn = accept(stack->fd, NULL, NULL);
		if (conn >= 0) {
			net9p_worker_stack_t *connstack = malloc(sizeof(net9p_worker_stack_t));
			connstack->fd = conn;
			coroutine_add(net9p_worker_cr, connstack);
		}
		cr_yield();
	}

	cr_end();
}

void net9p_worker_cr(void *_stack) {
	net9p_worker_stack_t *stack = _stack;
}