summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/bsd/bind.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/bsd/bind.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/bsd/bind.c')
-rwxr-xr-xsys/src/ape/lib/bsd/bind.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/sys/src/ape/lib/bsd/bind.c b/sys/src/ape/lib/bsd/bind.c
new file mode 100755
index 000000000..040c62fa0
--- /dev/null
+++ b/sys/src/ape/lib/bsd/bind.c
@@ -0,0 +1,70 @@
+/* posix */
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <signal.h>
+
+/* socket extensions */
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+
+/* plan 9 */
+#include "lib.h"
+#include "sys9.h"
+
+#include "priv.h"
+
+int
+bind(int fd, void *a, int alen)
+{
+ int n, len, cfd;
+ Rock *r;
+ char msg[128];
+ struct sockaddr_in *lip;
+
+ /* assign the address */
+ r = _sock_findrock(fd, 0);
+ if(r == 0){
+ errno = ENOTSOCK;
+ return -1;
+ }
+ if(alen > sizeof(r->addr)){
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ memmove(&r->addr, a, alen);
+
+ /* the rest is IP sepecific */
+ if (r->domain != PF_INET)
+ return 0;
+
+ cfd = open(r->ctl, O_RDWR);
+ if(cfd < 0){
+ errno = EBADF;
+ return -1;
+ }
+ lip = (struct sockaddr_in*)&r->addr;
+ if(lip->sin_port > 0)
+ snprintf(msg, sizeof msg, "bind %d", ntohs(lip->sin_port));
+ else
+ strcpy(msg, "bind *");
+ n = write(cfd, msg, strlen(msg));
+ if(n < 0){
+ errno = EOPNOTSUPP; /* Improve error reporting!!! */
+ close(cfd);
+ return -1;
+ }
+ close(cfd);
+
+ if(lip->sin_port <= 0)
+ _sock_ingetaddr(r, lip, &len, "local");
+
+ return 0;
+}