5 #include <sys/socket.h>
6 #include <netinet/in.h>
12 extern int _p9dialparse(char*, char**, char**, u32int*, int*);
19 if(strncmp(dir, "/dev/fd/", 8) != 0)
21 fd = strtol(dir+8, &dir, 0);
28 putfd(char *dir, int fd)
30 snprint(dir, NETPATHLEN, "/dev/fd/%d", fd);
36 p9announce(char *addr, char *dir)
44 struct sockaddr_in sa;
45 struct sockaddr_un sun;
51 if(_p9dialparse(buf, &net, &unix, &host, &port) < 0){
55 if(strcmp(net, "tcp") == 0)
57 else if(strcmp(net, "udp") == 0)
59 else if(strcmp(net, "unix") == 0)
62 werrstr("can only handle tcp, udp, and unix: not %s", net);
68 memset(&sa, 0, sizeof sa);
69 memmove(&sa.sin_addr, &host, 4);
70 sa.sin_family = AF_INET;
71 sa.sin_port = htons(port);
72 if((s = socket(AF_INET, proto, 0)) < 0)
75 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&n, &sn) >= 0
78 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
80 if(bind(s, (struct sockaddr*)&sa, sizeof sa) < 0){
84 if(proto == SOCK_STREAM){
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){
98 if(errno == EADDRINUSE
99 && connect(s, (struct sockaddr*)&sun, sizeof sun) < 0
100 && errno == ECONNREFUSED){
101 /* dead socket, so remove it */
104 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
106 if(bind(s, (struct sockaddr*)&sun, sizeof sun) >= 0)
119 p9listen(char *dir, char *newdir)
123 if((fd = _p9netfd(dir)) < 0){
124 werrstr("bad 'directory' in listen: %s", dir);
128 if((fd = accept(fd, nil, nil)) < 0)
136 p9accept(int cfd, char *dir)
140 if((fd = _p9netfd(dir)) < 0){
141 werrstr("bad 'directory' in accept");
144 /* need to dup because the listen fd will be closed */