]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/bind.c
ape: Add mkstemp to /sys/src/ape/lib/ap/gen/mkfile
[plan9front.git] / sys / src / ape / lib / bsd / bind.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 #include <sys/stat.h>
10 #include <signal.h>
11
12 /* socket extensions */
13 #include <sys/uio.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <sys/un.h>
17
18 /* plan 9 */
19 #include "lib.h"
20 #include "sys9.h"
21
22 #include "priv.h"
23
24 int
25 bind(int fd, void *a, int alen)
26 {
27         int n, len, cfd, port;
28         struct sockaddr *sa;
29         Rock *r;
30         char msg[128];
31
32         /* assign the address */
33         r = _sock_findrock(fd, 0);
34         if(r == 0){
35                 errno = ENOTSOCK;
36                 return -1;
37         }
38         sa = (struct sockaddr*)a;
39         if(sa->sa_family != r->domain){
40                 errno = EAFNOSUPPORT;
41                 return -1;
42         }
43         if(alen > sizeof(r->addr)){
44                 errno = ENAMETOOLONG;
45                 return -1;
46         }
47         memmove(&r->addr, a, alen);
48
49         /* the rest is IP sepecific */
50         if (r->domain != PF_INET && r->domain != PF_INET6)
51                 return 0;
52
53         cfd = open(r->ctl, O_RDWR);
54         if(cfd < 0){
55                 errno = EBADF;
56                 return -1;
57         }
58         port = _sock_inport(&r->addr);
59         if(port > 0)
60                 snprintf(msg, sizeof msg, "bind %d", port);
61         else
62                 strcpy(msg, "bind *");
63         n = write(cfd, msg, strlen(msg));
64         if(n < 0){
65                 errno = EOPNOTSUPP;     /* Improve error reporting!!! */
66                 close(cfd);
67                 return -1;
68         }
69         close(cfd);
70         if(port <= 0)
71                 _sock_ingetaddr(r, &r->addr, 0, "local");
72
73         return 0;
74 }