]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/bind.c
Import sources from 2011-03-30 iso image
[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;
28         Rock *r;
29         char msg[128];
30         struct sockaddr_in *lip;
31
32         /* assign the address */
33         r = _sock_findrock(fd, 0);
34         if(r == 0){
35                 errno = ENOTSOCK;
36                 return -1;
37         }
38         if(alen > sizeof(r->addr)){
39                 errno = ENAMETOOLONG;
40                 return -1;
41         }
42         memmove(&r->addr, a, alen);
43
44         /* the rest is IP sepecific */
45         if (r->domain != PF_INET)
46                 return 0;
47
48         cfd = open(r->ctl, O_RDWR);
49         if(cfd < 0){
50                 errno = EBADF;
51                 return -1;
52         }
53         lip = (struct sockaddr_in*)&r->addr;
54         if(lip->sin_port > 0)
55                 snprintf(msg, sizeof msg, "bind %d", ntohs(lip->sin_port));
56         else
57                 strcpy(msg, "bind *");
58         n = write(cfd, msg, strlen(msg));
59         if(n < 0){
60                 errno = EOPNOTSUPP;     /* Improve error reporting!!! */
61                 close(cfd);
62                 return -1;
63         }
64         close(cfd);
65
66         if(lip->sin_port <= 0)
67                 _sock_ingetaddr(r, lip, &len, "local");
68
69         return 0;
70 }