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)
45 struct sockaddr_in sa;
46 struct sockaddr_un sun;
52 if(_p9dialparse(buf, &net, &unix, &host, &port) < 0){
56 if(strcmp(net, "tcp") == 0)
58 else if(strcmp(net, "udp") == 0)
60 else if(strcmp(net, "unix") == 0)
63 werrstr("can only handle tcp, udp, and unix: not %s", net);
69 memset(&sa, 0, sizeof sa);
70 memmove(&sa.sin_addr, &host, 4);
71 sa.sin_family = AF_INET;
72 sa.sin_port = htons(port);
73 if((s = socket(AF_INET, proto, 0)) < 0)
76 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
79 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
81 if(bind(s, (struct sockaddr*)&sa, sizeof sa) < 0){
85 if(proto == SOCK_STREAM){
92 memset(&sun, 0, sizeof sun);
93 sun.sun_family = AF_UNIX;
94 strcpy(sun.sun_path, unix);
95 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
98 if(bind(s, (struct sockaddr*)&sun, sizeof sun) < 0){
99 if(errno == EADDRINUSE
100 && connect(s, (struct sockaddr*)&sun, sizeof sun) < 0
101 && errno == ECONNREFUSED){
102 /* dead socket, so remove it */
105 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
107 if(bind(s, (struct sockaddr*)&sun, sizeof sun) >= 0)
120 p9listen(char *dir, char *newdir)
124 if((fd = _p9netfd(dir)) < 0){
125 werrstr("bad 'directory' in listen: %s", dir);
129 if((fd = accept(fd, nil, nil)) < 0)
137 p9accept(int cfd, char *dir)
141 if((fd = _p9netfd(dir)) < 0){
142 werrstr("bad 'directory' in accept");
145 /* need to dup because the listen fd will be closed */