12 #include <sys/socket.h>
13 #include <netinet/in.h>
19 connect(int fd, void *a, int alen)
23 char msg[8+256+1], file[8+256+1];
25 struct sockaddr_un *runix;
28 r = _sock_findrock(fd, 0);
33 sa = (struct sockaddr*)a;
34 if(sa->sa_family != r->domain){
38 if(alen > sizeof(r->raddr)){
42 memmove(&r->raddr, a, alen);
47 /* set up a tcp or udp connection */
48 cfd = open(r->ctl, O_RDWR);
51 if(_sock_inport(&r->addr) > 0) {
52 snprintf(msg, sizeof msg, "connect %s!%d%s %d",
53 inet_ntop(sa->sa_family, _sock_inip(sa), file, sizeof(file)),
55 r->reserved ? "!r" : "",
56 _sock_inport(&r->addr));
58 snprintf(msg, sizeof msg, "connect %s!%d%s",
59 inet_ntop(sa->sa_family, _sock_inip(sa), file, sizeof(file)),
61 r->reserved ? "!r" : "");
63 n = write(cfd, msg, strlen(msg));
65 return (n < 0) ? -1 : 0;
67 /* null terminate the address */
68 if(alen == sizeof(r->raddr))
70 *(((char*)&r->raddr)+alen) = 0;
77 /* put far end of our pipe in /srv */
78 snprintf(msg, sizeof msg, "UD.%d.%d", getpid(), vers++);
79 if(_sock_srv(msg, r->other) < 0){
85 /* tell server the /srv file to open */
86 runix = (struct sockaddr_un*)&r->raddr;
87 _sock_srvname(file, runix->sun_path);
88 nfd = open(file, O_RDWR);
93 if(write(nfd, msg, strlen(msg)) < 0){
100 /* wait for server to open it and then remove it */
101 read(fd, file, sizeof(file));
102 _sock_srvname(file, msg);
106 errno = EAFNOSUPPORT;