5 #include <sys/socket.h>
6 #include <netinet/in.h>
11 extern int _p9dialparse(char*, char**, char**, u32int*, int*);
18 if(strncmp(dir, "/dev/fd/", 8) != 0)
20 fd = strtol(dir+8, &dir, 0);
27 putfd(char *dir, int fd)
29 snprint(dir, NETPATHLEN, "/dev/fd/%d", fd);
35 p9announce(char *addr, char *dir)
43 struct sockaddr_in sa;
44 struct sockaddr_un sun;
50 if(_p9dialparse(buf, &net, &unix, &host, &port) < 0){
54 if(strcmp(net, "tcp") == 0)
56 else if(strcmp(net, "udp") == 0)
58 else if(strcmp(net, "unix") == 0)
61 werrstr("can only handle tcp, udp, and unix: not %s", net);
67 memset(&sa, 0, sizeof sa);
68 memmove(&sa.sin_addr, &host, 4);
69 sa.sin_family = AF_INET;
70 sa.sin_port = htons(port);
71 if((s = socket(AF_INET, proto, 0)) < 0)
74 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&n, &sn) >= 0
77 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
79 if(bind(s, (struct sockaddr*)&sa, sizeof sa) < 0){
83 if(proto == SOCK_STREAM){
86 print("announce dir: %s\n", dir);
91 memset(&sun, 0, sizeof sun);
92 sun.sun_family = AF_UNIX;
93 strcpy(sun.sun_path, unix);
94 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
97 if(bind(s, (struct sockaddr*)&sun, sizeof sun) < 0){
107 p9listen(char *dir, char *newdir)
111 if((fd = getfd(dir)) < 0){
112 werrstr("bad 'directory' in listen: %s", dir);
116 print("accept %d", fd);
117 if((fd = accept(fd, nil, nil)) < 0)
119 print(" -> %d\n", fd);
122 print("listen dir: %s\n", newdir);
127 p9accept(int cfd, char *dir)
131 if((fd = getfd(dir)) < 0){
132 werrstr("bad 'directory' in accept");
135 /* need to dup because the listen fd will be closed */