2011-03-30 12:46:40 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
2013-03-31 16:52:45 +00:00
|
|
|
int n, len, cfd, port;
|
2013-04-01 23:40:29 +00:00
|
|
|
struct sockaddr *sa;
|
2011-03-30 12:46:40 +00:00
|
|
|
Rock *r;
|
|
|
|
char msg[128];
|
|
|
|
|
|
|
|
/* assign the address */
|
|
|
|
r = _sock_findrock(fd, 0);
|
|
|
|
if(r == 0){
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-04-01 23:40:29 +00:00
|
|
|
sa = (struct sockaddr*)a;
|
|
|
|
if(sa->sa_family != r->domain){
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-30 12:46:40 +00:00
|
|
|
if(alen > sizeof(r->addr)){
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memmove(&r->addr, a, alen);
|
|
|
|
|
|
|
|
/* the rest is IP sepecific */
|
2013-03-31 16:52:45 +00:00
|
|
|
if (r->domain != PF_INET && r->domain != PF_INET6)
|
2011-03-30 12:46:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
cfd = open(r->ctl, O_RDWR);
|
|
|
|
if(cfd < 0){
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-31 16:52:45 +00:00
|
|
|
port = _sock_inport(&r->addr);
|
|
|
|
if(port > 0)
|
|
|
|
snprintf(msg, sizeof msg, "bind %d", port);
|
2011-03-30 12:46:40 +00:00
|
|
|
else
|
|
|
|
strcpy(msg, "bind *");
|
|
|
|
n = write(cfd, msg, strlen(msg));
|
|
|
|
if(n < 0){
|
|
|
|
errno = EOPNOTSUPP; /* Improve error reporting!!! */
|
|
|
|
close(cfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(cfd);
|
2013-03-31 16:52:45 +00:00
|
|
|
if(port <= 0)
|
|
|
|
_sock_ingetaddr(r, &r->addr, 0, "local");
|
2011-03-30 12:46:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|