plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

net.c (1117B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <ip.h>
      4 #include <thread.h>
      5 #include <sunrpc.h>
      6 
      7 typedef struct Arg Arg;
      8 struct Arg
      9 {
     10 	int fd;
     11 	char adir[40];
     12 	SunSrv *srv;
     13 };
     14 
     15 static void
     16 sunnetlisten(void *v)
     17 {
     18 	int fd, lcfd;
     19 	char ldir[40];
     20 	uchar ip[IPaddrlen];
     21 	int port;
     22 	Arg *a = v;
     23 	NetConnInfo *nci;
     24 
     25 	for(;;){
     26 		lcfd = listen(a->adir, ldir);
     27 		if(lcfd < 0)
     28 			break;
     29 		fd = accept(lcfd, ldir);
     30 		close(lcfd);
     31 		if(fd < 0)
     32 			continue;
     33 		if(a->srv->ipokay){
     34 			if((nci = getnetconninfo(nil, fd)) == nil){
     35 				close(fd);
     36 				continue;
     37 			}
     38 			port = atoi(nci->rserv);
     39 			parseip(ip, nci->raddr);
     40 			freenetconninfo(nci);
     41 			if(!a->srv->ipokay(ip, port)){
     42 				close(fd);
     43 				continue;
     44 			}
     45 		}
     46 		if(!sunsrvfd(a->srv, fd))
     47 			close(fd);
     48 	}
     49 	free(a);
     50 	close(a->fd);
     51 }
     52 
     53 int
     54 sunsrvnet(SunSrv *srv, char *addr)
     55 {
     56 	Arg *a;
     57 
     58 	a = emalloc(sizeof(Arg));
     59 	if((a->fd = announce(addr, a->adir)) < 0)
     60 		return -1;
     61 	a->srv = srv;
     62 
     63 	proccreate(sunnetlisten, a, SunStackSize);
     64 	return 0;
     65 }
     66 
     67 int
     68 sunsrvannounce(SunSrv *srv, char *addr)
     69 {
     70 	if(strstr(addr, "udp!"))
     71 		return sunsrvudp(srv, addr);
     72 	else
     73 		return sunsrvnet(srv, addr);
     74 }