5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
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);
37 p9announce(char *addr, char *dir)
46 struct sockaddr_in sa;
47 struct sockaddr_un sun;
53 if(p9dialparse(buf, &net, &unix, &host, &port) < 0){
57 if(strcmp(net, "tcp") == 0)
59 else if(strcmp(net, "udp") == 0)
61 else if(strcmp(net, "unix") == 0)
64 werrstr("can only handle tcp, udp, and unix: not %s", net);
70 memset(&sa, 0, sizeof sa);
71 memmove(&sa.sin_addr, &host, 4);
72 sa.sin_family = AF_INET;
73 sa.sin_port = htons(port);
74 if((s = socket(AF_INET, proto, 0)) < 0)
77 if(port && getsockopt(s, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0
80 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
82 if(bind(s, (struct sockaddr*)&sa, sizeof sa) < 0){
86 if(proto == SOCK_STREAM){
93 memset(&sun, 0, sizeof sun);
94 sun.sun_family = AF_UNIX;
95 strcpy(sun.sun_path, unix);
96 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
99 if(bind(s, (struct sockaddr*)&sun, sizeof sun) < 0){
100 if(errno == EADDRINUSE
101 && connect(s, (struct sockaddr*)&sun, sizeof sun) < 0
102 && errno == ECONNREFUSED){
103 /* dead socket, so remove it */
106 if((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
108 if(bind(s, (struct sockaddr*)&sun, sizeof sun) >= 0)
121 p9listen(char *dir, char *newdir)
125 if((fd = _p9netfd(dir)) < 0){
126 werrstr("bad 'directory' in listen: %s", dir);
130 if((fd = accept(fd, nil, nil)) < 0)
134 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
141 p9accept(int cfd, char *dir)
145 if((fd = _p9netfd(dir)) < 0){
146 werrstr("bad 'directory' in accept");
149 /* need to dup because the listen fd will be closed */