]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/connect.c
ape: Add mkstemp to /sys/src/ape/lib/ap/gen/mkfile
[plan9front.git] / sys / src / ape / lib / bsd / connect.c
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <errno.h>
9
10 /* bsd extensions */
11 #include <sys/uio.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <sys/un.h>
15
16 #include "priv.h"
17
18 int
19 connect(int fd, void *a, int alen)
20 {
21         Rock *r;
22         int n, cfd, nfd;
23         char msg[8+256+1], file[8+256+1];
24         struct sockaddr *sa;
25         struct sockaddr_un *runix;
26         static int vers;
27
28         r = _sock_findrock(fd, 0);
29         if(r == 0){
30                 errno = ENOTSOCK;
31                 return -1;
32         }
33         sa = (struct sockaddr*)a;
34         if(sa->sa_family != r->domain){
35                 errno = EAFNOSUPPORT;
36                 return -1;
37         }
38         if(alen > sizeof(r->raddr)){
39                 errno = ENAMETOOLONG;
40                 return -1;
41         }
42         memmove(&r->raddr, a, alen);
43
44         switch(r->domain){
45         case PF_INET:
46         case PF_INET6:
47                 /* set up a tcp or udp connection */
48                 cfd = open(r->ctl, O_RDWR);
49                 if(cfd < 0)
50                         return -1;
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)),
54                                 _sock_inport(sa),
55                                 r->reserved ? "!r" : "",
56                                 _sock_inport(&r->addr));
57                 } else {
58                         snprintf(msg, sizeof msg, "connect %s!%d%s",
59                                 inet_ntop(sa->sa_family, _sock_inip(sa), file, sizeof(file)),
60                                 _sock_inport(sa),
61                                 r->reserved ? "!r" : "");
62                 }
63                 n = write(cfd, msg, strlen(msg));
64                 close(cfd);
65                 return (n < 0) ? -1 : 0;
66         case PF_UNIX:
67                 /* null terminate the address */
68                 if(alen == sizeof(r->raddr))
69                         alen--;
70                 *(((char*)&r->raddr)+alen) = 0;
71
72                 if(r->other < 0){
73                         errno = EGREG;
74                         return -1;
75                 }
76
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){
80                         r->other = -1;
81                         return -1;
82                 }
83                 r->other = -1;
84
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);
89                 if(nfd < 0){
90                         unlink(msg);
91                         return -1;
92                 }
93                 if(write(nfd, msg, strlen(msg)) < 0){
94                         close(nfd);
95                         unlink(msg);
96                         return -1;
97                 }
98                 close(nfd);
99
100                 /* wait for server to open it and then remove it */
101                 read(fd, file, sizeof(file));
102                 _sock_srvname(file, msg);
103                 unlink(file);
104                 return 0;
105         default:
106                 errno = EAFNOSUPPORT;
107                 return -1;
108         }
109 }