5 #include <sys/socket.h>
6 #include <netinet/in.h>
9 extern int _p9dialparse(char*, char**, char**, u32int*, int*);
16 if(strncmp(dir, "/dev/fd/", 8) != 0)
18 fd = strtol(dir+8, &dir, 0);
25 putfd(char *dir, int fd)
27 snprint(dir, NETPATHLEN, "/dev/fd/%d", fd);
33 p9announce(char *addr, char *dir)
41 struct sockaddr_in sa;
42 struct sockaddr_un sun;
48 if(_p9dialparse(buf, &net, &unix, &host, &port) < 0){
52 if(strcmp(net, "tcp") == 0)
54 else if(strcmp(net, "udp") == 0)
56 else if(strcmp(net, "unix") == 0)
59 werrstr("can only handle tcp, udp, and unix: not %s", net);
65 memset(&sa, 0, sizeof sa);
66 memmove(&sa.sin_addr, &host, 4);
67 sa.sin_family = AF_INET;
68 sa.sin_port = htons(port);
69 if((s = socket(AF_INET, proto, 0)) < 0)
72 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&n, &sn) >= 0
75 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
77 if(bind(s, (struct sockaddr*)&sa, sizeof sa) < 0){
81 if(proto == SOCK_STREAM){
84 print("announce dir: %s\n", dir);
89 memset(&sun, 0, sizeof sun);
90 sun.sun_family = AF_UNIX;
91 sun.sun_len = sizeof sun;
92 strcpy(sun.sun_path, unix);
93 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
96 if(bind(s, (struct sockaddr*)&sun, sizeof sun) < 0){
106 p9listen(char *dir, char *newdir)
110 if((fd = getfd(dir)) < 0){
111 werrstr("bad 'directory' in listen: %s", dir);
115 print("accept %d", fd);
116 if((fd = accept(fd, nil, nil)) < 0)
118 print(" -> %d\n", fd);
121 print("listen dir: %s\n", newdir);
126 p9accept(int cfd, char *dir)
130 if((fd = getfd(dir)) < 0){
131 werrstr("bad 'directory' in accept");
134 /* need to dup because the listen fd will be closed */