easier tube fallback

git-svn-id: file:///svn/unbound/trunk@2183 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2010-07-06 09:20:12 +00:00
parent bfc0ddd0e0
commit d332a19aa0

View file

@ -49,68 +49,8 @@
/* on unix */
#ifndef HAVE_SOCKETPAIR
/** define socketpair to another name in case it sneakily exists somehow */
#define socketpair socketpair_compat
static int socketpair(int ATTR_UNUSED(f), int ATTR_UNUSED(tp),
int ATTR_UNUSED(p), int sv[2])
{
/* no socketpair() available, like on Minix 3.1.7,
* create two TCP sockets connected to each other on 127.0.0.1 */
struct sockaddr_storage addr;
socklen_t len;
int s, t;
/* hope this port is not in use ... */
static int the_port = 0;
if(the_port == 0) the_port = 1025 + (getpid() % 16384);
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1) {
log_err("socket: %s", strerror(errno));
return -1;
}
if(extstrtoaddr("127.0.0.1@0", &addr, &len) == 0) {
log_err("internal error, cannot parse in socketpair_compat");
close(s);
errno=EINVAL;
return -1;
}
((struct sockaddr_in*)&addr)->sin_port = htons(the_port++);
if(bind(s, (struct sockaddr*)&addr, len) == -1) {
log_err("bind: %s", strerror(errno));
close(s);
return -1;
}
if(listen(s, 5) == -1) {
log_err("listen: %s", strerror(errno));
close(s);
return -1;
}
/* create send sock */
t = socket(AF_INET, SOCK_STREAM, 0);
if(t == -1) {
log_err("socket: %s", strerror(errno));
close(s);
return -1;
}
if(connect(t, (struct sockaddr*)&addr, len) == -1) {
log_err("connect: %s", strerror(errno));
close(s);
close(t);
return -1;
}
len = (socklen_t)sizeof(addr);
sv[0] = accept(s, (struct sockaddr*)&addr, &len);
if(sv[0] == -1) {
log_err("accept: %s", strerror(errno));
close(s);
close(t);
return -1;
}
sv[1] = t;
close(s);
return 0;
}
/** no socketpair() available, like on Minix 3.1.7, use pipe */
#define socketpair(f, t, p, sv) pipe(sv)
#endif /* HAVE_SOCKETPAIR */
struct tube* tube_create(void)