xref: /plan9/sys/src/libsunrpc/net.c (revision fb7f0c934c48abaed6040d054ef636408c3c522d)
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <sunrpc.h>
5 
6 typedef struct Arg Arg;
7 struct Arg
8 {
9 	int fd;
10 	char adir[40];
11 	SunSrv *srv;
12 };
13 
14 static void
sunNetListen(void * v)15 sunNetListen(void *v)
16 {
17 	int fd, lcfd;
18 	char ldir[40];
19 	Arg *a = v;
20 
21 	for(;;){
22 		lcfd = listen(a->adir, ldir);
23 		if(lcfd < 0)
24 			break;
25 		fd = accept(lcfd, ldir);
26 		close(lcfd);
27 		if(fd < 0)
28 			continue;
29 		if(!sunSrvFd(a->srv, fd))
30 			close(fd);
31 	}
32 	free(a);
33 	close(a->fd);
34 }
35 
36 int
sunSrvNet(SunSrv * srv,char * addr)37 sunSrvNet(SunSrv *srv, char *addr)
38 {
39 	Arg *a;
40 
41 	a = emalloc(sizeof(Arg));
42 	if((a->fd = announce(addr, a->adir)) < 0)
43 		return -1;
44 	a->srv = srv;
45 
46 	proccreate(sunNetListen, a, SunStackSize);
47 	return 0;
48 }
49 
50 int
sunSrvAnnounce(SunSrv * srv,char * addr)51 sunSrvAnnounce(SunSrv *srv, char *addr)
52 {
53 	if(strstr(addr, "udp!"))
54 		return sunSrvUdp(srv, addr);
55 	else
56 		return sunSrvNet(srv, addr);
57 }
58