aboutsummaryrefslogtreecommitdiff
path: root/src/libsunrpc/net.c
blob: a8b917e02360ebd27ffaf989e37394bf0355987e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <thread.h>
#include <sunrpc.h>

typedef struct Arg Arg;
struct Arg
{
	int fd;
	char adir[40];
	SunSrv *srv;
};

static void
sunnetlisten(void *v)
{
	int fd, lcfd;
	char ldir[40];
	uchar ip[IPaddrlen];
	int port;
	Arg *a = v;
	NetConnInfo *nci;

	for(;;){
		lcfd = listen(a->adir, ldir);
		if(lcfd < 0)
			break;
		fd = accept(lcfd, ldir);
		close(lcfd);
		if(fd < 0)
			continue;
		if(a->srv->ipokay){
			if((nci = getnetconninfo(nil, fd)) == nil){
				close(fd);
				continue;
			}
			port = atoi(nci->rserv);
			parseip(ip, nci->raddr);
			freenetconninfo(nci);
			if(!a->srv->ipokay(ip, port)){
				close(fd);
				continue;
			}
		}
		if(!sunsrvfd(a->srv, fd))
			close(fd);
	}
	free(a);
	close(a->fd);
}

int
sunsrvnet(SunSrv *srv, char *addr)
{
	Arg *a;

	a = emalloc(sizeof(Arg));
	if((a->fd = announce(addr, a->adir)) < 0)
		return -1;
	a->srv = srv;

	proccreate(sunnetlisten, a, SunStackSize);
	return 0;
}

int
sunsrvannounce(SunSrv *srv, char *addr)
{
	if(strstr(addr, "udp!"))
		return sunsrvudp(srv, addr);
	else
		return sunsrvnet(srv, addr);
}