summaryrefslogtreecommitdiff
path: root/sysdep/unix/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysdep/unix/io.c')
-rw-r--r--sysdep/unix/io.c43
1 files changed, 24 insertions, 19 deletions
diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c
index 486319ff..69c17d60 100644
--- a/sysdep/unix/io.c
+++ b/sysdep/unix/io.c
@@ -32,14 +32,14 @@
#include "nest/bird.h"
#include "lib/lists.h"
#include "lib/resource.h"
-#include "lib/timer.h"
+#include "sysdep/unix/timer.h"
#include "lib/socket.h"
#include "lib/event.h"
#include "lib/string.h"
#include "nest/iface.h"
-#include "lib/unix.h"
-#include "lib/sysio.h"
+#include "sysdep/unix/unix.h"
+#include CONFIG_INCLUDE_SYSIO_H
/* Maximum number of calls of tx handler for one socket in one
* poll iteration. Should be small enough to not monopolize CPU by
@@ -587,6 +587,7 @@ sockaddr_read(sockaddr *sa, int af, ip_addr *a, struct iface **ifa, uint *port)
return -1;
}
+const int fam_to_af[] = { [SK_FAM_IPV4] = AF_INET, [SK_FAM_IPV6] = AF_INET6 };
/*
* IPv6 multicast syscalls
@@ -1193,7 +1194,7 @@ sk_setup(sock *s)
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
ERR("O_NONBLOCK");
- if (!s->af)
+ if (!s->fam)
return 0;
if (ipa_nonzero(s->saddr) && !(s->flags & SKF_BIND))
@@ -1294,7 +1295,7 @@ sk_tcp_connected(sock *s)
int sa_len = sizeof(sa);
if ((getsockname(s->fd, &sa.sa, &sa_len) < 0) ||
- (sockaddr_read(&sa, s->af, &s->saddr, &s->iface, &s->sport) < 0))
+ (sockaddr_read(&sa, fam_to_af[s->fam], &s->saddr, &s->iface, &s->sport) < 0))
log(L_WARN "SOCK: Cannot get local IP address for TCP>");
s->type = SK_TCP;
@@ -1319,8 +1320,8 @@ sk_passive_connected(sock *s, int type)
sock *t = sk_new(s->pool);
t->type = type;
+ t->fam = s->fam;
t->fd = fd;
- t->af = s->af;
t->ttl = s->ttl;
t->tos = s->tos;
t->rbsize = s->rbsize;
@@ -1329,10 +1330,10 @@ sk_passive_connected(sock *s, int type)
if (type == SK_TCP)
{
if ((getsockname(fd, &loc_sa.sa, &loc_sa_len) < 0) ||
- (sockaddr_read(&loc_sa, s->af, &t->saddr, &t->iface, &t->sport) < 0))
+ (sockaddr_read(&loc_sa, fam_to_af[s->fam], &t->saddr, &t->iface, &t->sport) < 0))
log(L_WARN "SOCK: Cannot get local IP address for TCP<");
- if (sockaddr_read(&rem_sa, s->af, &t->daddr, &t->iface, &t->dport) < 0)
+ if (sockaddr_read(&rem_sa, fam_to_af[s->fam], &t->daddr, &t->iface, &t->dport) < 0)
log(L_WARN "SOCK: Cannot get remote IP address for TCP<");
}
@@ -1367,7 +1368,6 @@ sk_passive_connected(sock *s, int type)
int
sk_open(sock *s)
{
- int af = BIRD_AF;
int fd = -1;
int do_bind = 0;
int bind_port = 0;
@@ -1380,28 +1380,28 @@ sk_open(sock *s)
s->ttx = ""; /* Force s->ttx != s->tpos */
/* Fall thru */
case SK_TCP_PASSIVE:
- fd = socket(af, SOCK_STREAM, IPPROTO_TCP);
+ fd = socket(fam_to_af[s->fam], SOCK_STREAM, IPPROTO_TCP);
bind_port = s->sport;
bind_addr = s->saddr;
do_bind = bind_port || ipa_nonzero(bind_addr);
break;
case SK_UDP:
- fd = socket(af, SOCK_DGRAM, IPPROTO_UDP);
+ fd = socket(fam_to_af[s->fam], SOCK_DGRAM, IPPROTO_UDP);
bind_port = s->sport;
bind_addr = (s->flags & SKF_BIND) ? s->saddr : IPA_NONE;
do_bind = 1;
break;
case SK_IP:
- fd = socket(af, SOCK_RAW, s->dport);
+ fd = socket(fam_to_af[s->fam], SOCK_RAW, s->dport);
bind_port = 0;
bind_addr = (s->flags & SKF_BIND) ? s->saddr : IPA_NONE;
do_bind = ipa_nonzero(bind_addr);
break;
case SK_MAGIC:
- af = 0;
+ s->fam = SK_FAM_NONE;
fd = s->fd;
break;
@@ -1412,7 +1412,6 @@ sk_open(sock *s)
if (fd < 0)
ERR("socket");
- s->af = af;
s->fd = fd;
if (sk_setup(s) < 0)
@@ -1441,7 +1440,7 @@ sk_open(sock *s)
if (sk_set_high_port(s) < 0)
log(L_WARN "Socket error: %s%#m", s->err);
- sockaddr_fill(&sa, af, bind_addr, s->iface, bind_port);
+ sockaddr_fill(&sa, fam_to_af[s->fam], bind_addr, s->iface, bind_port);
if (bind(fd, &sa.sa, SA_LEN(sa)) < 0)
ERR2("bind");
}
@@ -1453,7 +1452,7 @@ sk_open(sock *s)
switch (s->type)
{
case SK_TCP_ACTIVE:
- sockaddr_fill(&sa, af, s->daddr, s->iface, s->dport);
+ sockaddr_fill(&sa, fam_to_af[s->fam], s->daddr, s->iface, s->dport);
if (connect(fd, &sa.sa, SA_LEN(sa)) >= 0)
sk_tcp_connected(s);
else if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS &&
@@ -1560,7 +1559,7 @@ sk_sendmsg(sock *s)
byte cmsg_buf[CMSG_TX_SPACE];
sockaddr dst;
- sockaddr_fill(&dst, s->af, s->daddr, s->iface, s->dport);
+ sockaddr_fill(&dst, fam_to_af[s->fam], s->daddr, s->iface, s->dport);
struct msghdr msg = {
.msg_name = &dst.sa,
@@ -1613,7 +1612,7 @@ sk_recvmsg(sock *s)
// rv = ipv4_skip_header(pbuf, rv);
//endif
- sockaddr_read(&src, s->af, &s->faddr, NULL, &s->fport);
+ sockaddr_read(&src, fam_to_af[s->fam], &s->faddr, NULL, &s->fport);
sk_process_cmsgs(s, &msg);
if (msg.msg_flags & MSG_TRUNC)
@@ -1833,7 +1832,7 @@ sk_write(sock *s)
case SK_TCP_ACTIVE:
{
sockaddr sa;
- sockaddr_fill(&sa, s->af, s->daddr, s->iface, s->dport);
+ sockaddr_fill(&sa, fam_to_af[s->fam], s->daddr, s->iface, s->dport);
if (connect(s->fd, &sa.sa, SA_LEN(sa)) >= 0 || errno == EISCONN)
sk_tcp_connected(s);
@@ -1853,6 +1852,12 @@ sk_write(sock *s)
}
}
+int sk_is_ipv4(sock *s)
+{ return s->fam == SK_FAM_IPV4; }
+
+int sk_is_ipv6(sock *s)
+{ return s->fam == SK_FAM_IPV6; }
+
void
sk_dump_all(void)
{