diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2021-03-05 22:19:59 +0100 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2021-03-05 22:21:53 +0100 |
commit | 84579d030ab42f6d8d462bf2ac1041fff18962d1 (patch) | |
tree | e93fcc26134f58d1393e21e6099c6e841bea0054 /src | |
parent | 8dc8d72ae73ecde10c2ed933c2322056c907c4b4 (diff) | |
download | slirp-84579d030ab42f6d8d462bf2ac1041fff18962d1.zip slirp-84579d030ab42f6d8d462bf2ac1041fff18962d1.tar.gz slirp-84579d030ab42f6d8d462bf2ac1041fff18962d1.tar.bz2 |
udpx/tcpx_listen: Use struct sockaddr * types
This actually makes most of the code simpler.
Diffstat (limited to 'src')
-rw-r--r-- | src/socket.c | 21 | ||||
-rw-r--r-- | src/socket.h | 13 | ||||
-rw-r--r-- | src/udp.c | 16 | ||||
-rw-r--r-- | src/udp.h | 4 |
4 files changed, 32 insertions, 22 deletions
diff --git a/src/socket.c b/src/socket.c index c50f866..d90c0a4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -737,8 +737,8 @@ int sosendto(struct socket *so, struct mbuf *m) * Listen for incoming TCP connections */ struct socket *tcpx_listen(Slirp *slirp, - const union slirp_sockaddr *haddr, socklen_t haddrlen, - const union slirp_sockaddr *laddr, socklen_t laddrlen, + const struct sockaddr *haddr, socklen_t haddrlen, + const struct sockaddr *laddr, socklen_t laddrlen, int flags) { struct socket *so; @@ -750,11 +750,11 @@ struct socket *tcpx_listen(Slirp *slirp, char addrstr[INET6_ADDRSTRLEN]; char portstr[6]; int ret; - ret = getnameinfo((const struct sockaddr *) haddr, haddrlen, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST|NI_NUMERICSERV); + ret = getnameinfo(haddr, haddrlen, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST|NI_NUMERICSERV); g_assert(ret == 0); DEBUG_ARG("haddr = %s", addrstr); DEBUG_ARG("hport = %s", portstr); - ret = getnameinfo((const struct sockaddr *) laddr, laddrlen, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST|NI_NUMERICSERV); + ret = getnameinfo(laddr, laddrlen, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST|NI_NUMERICSERV); g_assert(ret == 0); DEBUG_ARG("laddr = %s", addrstr); DEBUG_ARG("lport = %s", portstr); @@ -777,12 +777,13 @@ struct socket *tcpx_listen(Slirp *slirp, so->so_state &= SS_PERSISTENT_MASK; so->so_state |= (SS_FACCEPTCONN | flags); - so->lhost = *laddr; - s = slirp_socket(haddr->ss.ss_family, SOCK_STREAM, 0); + sockaddr_copy(&so->lhost.sa, sizeof(so->lhost), laddr, laddrlen); + + s = slirp_socket(haddr->sa_family, SOCK_STREAM, 0); if ((s < 0) || (slirp_socket_set_fast_reuse(s) < 0) || - (bind(s, (const struct sockaddr *)haddr, haddrlen) < 0) || + (bind(s, haddr, haddrlen) < 0) || (listen(s, 1) < 0)) { int tmperrno = errno; /* Don't clobber the real reason we failed */ if (s >= 0) { @@ -801,7 +802,7 @@ struct socket *tcpx_listen(Slirp *slirp, slirp_socket_set_nodelay(s); addrlen = sizeof(so->fhost); - getsockname(s, (struct sockaddr *)&so->fhost, &addrlen); + getsockname(s, &so->fhost.sa, &addrlen); sotranslate_accept(so); so->s = s; @@ -823,7 +824,7 @@ struct socket *tcp_listen(Slirp *slirp, uint32_t haddr, unsigned hport, lsa.sin_addr.s_addr = laddr; lsa.sin_port = lport; - return tcpx_listen(slirp, (const union slirp_sockaddr*) &hsa, sizeof(hsa), (union slirp_sockaddr*) &lsa, sizeof(lsa), flags); + return tcpx_listen(slirp, (const struct sockaddr *) &hsa, sizeof(hsa), (struct sockaddr *) &lsa, sizeof(lsa), flags); } struct socket * @@ -842,7 +843,7 @@ tcp6_listen(Slirp *slirp, struct in6_addr haddr, u_int hport, lsa.sin6_addr = laddr; lsa.sin6_port = lport; - return tcpx_listen(slirp, (const union slirp_sockaddr*) &hsa, sizeof(hsa), (union slirp_sockaddr*) &lsa, sizeof(lsa), flags); + return tcpx_listen(slirp, (const struct sockaddr *) &hsa, sizeof(hsa), (struct sockaddr*) &lsa, sizeof(lsa), flags); } /* diff --git a/src/socket.h b/src/socket.h index 9025897..524312f 100644 --- a/src/socket.h +++ b/src/socket.h @@ -24,6 +24,7 @@ typedef union in4or6_addr in4or6_addr; */ union slirp_sockaddr { + struct sockaddr sa; struct sockaddr_storage ss; struct sockaddr_in sin; struct sockaddr_in6 sin6; @@ -145,6 +146,14 @@ static inline socklen_t sockaddr_size(const struct sockaddr_storage *a) } } +static inline void sockaddr_copy(struct sockaddr *dst, socklen_t dstlen, const struct sockaddr *src, socklen_t srclen) +{ + socklen_t len = sockaddr_size((const struct sockaddr_storage *) src); + g_assert(len <= srclen); + g_assert(len <= dstlen); + memcpy(dst, src, len); +} + struct socket *solookup(struct socket **, struct socket *, struct sockaddr_storage *, struct sockaddr_storage *); struct socket *socreate(Slirp *); @@ -159,8 +168,8 @@ struct socket *tcp_listen(Slirp *, uint32_t, unsigned, uint32_t, unsigned, int); struct socket *tcp6_listen(Slirp *, struct in6_addr, u_int, struct in6_addr, u_int, int); struct socket *tcpx_listen(Slirp *slirp, - const union slirp_sockaddr *haddr, socklen_t haddrlen, - const union slirp_sockaddr *laddr, socklen_t laddrlen, + const struct sockaddr *haddr, socklen_t haddrlen, + const struct sockaddr *laddr, socklen_t laddrlen, int flags); void soisfconnecting(register struct socket *); void soisfconnected(register struct socket *); @@ -354,15 +354,15 @@ static uint8_t udp_tos(struct socket *so) } struct socket *udpx_listen(Slirp *slirp, - const union slirp_sockaddr *haddr, socklen_t haddrlen, - const union slirp_sockaddr *laddr, socklen_t laddrlen, + const struct sockaddr *haddr, socklen_t haddrlen, + const struct sockaddr *laddr, socklen_t laddrlen, int flags) { struct socket *so; socklen_t addrlen; so = socreate(slirp); - so->s = slirp_socket(haddr->ss.ss_family, SOCK_DGRAM, 0); + so->s = slirp_socket(haddr->sa_family, SOCK_DGRAM, 0); if (so->s < 0) { sofree(so); return NULL; @@ -370,17 +370,17 @@ struct socket *udpx_listen(Slirp *slirp, so->so_expire = curtime + SO_EXPIRE; insque(so, &slirp->udb); - if (bind(so->s, (const struct sockaddr *)haddr, haddrlen) < 0) { + if (bind(so->s, haddr, haddrlen) < 0) { udp_detach(so); return NULL; } slirp_socket_set_fast_reuse(so->s); addrlen = sizeof(so->fhost); - getsockname(so->s, (struct sockaddr *)&so->fhost, &addrlen); + getsockname(so->s, &so->fhost.sa, &addrlen); sotranslate_accept(so); - so->lhost = *laddr; + sockaddr_copy(&so->lhost.sa, sizeof(so->lhost), laddr, laddrlen); if (flags != SS_FACCEPTONCE) so->so_expire = 0; @@ -405,7 +405,7 @@ struct socket *udp_listen(Slirp *slirp, uint32_t haddr, unsigned hport, lsa.sin_addr.s_addr = laddr; lsa.sin_port = lport; - return udpx_listen(slirp, (const union slirp_sockaddr*) &hsa, sizeof(hsa), (union slirp_sockaddr*) &lsa, sizeof(lsa), flags); + return udpx_listen(slirp, (const struct sockaddr *) &hsa, sizeof(hsa), (struct sockaddr *) &lsa, sizeof(lsa), flags); } struct socket * @@ -424,5 +424,5 @@ udp6_listen(Slirp *slirp, struct in6_addr haddr, u_int hport, lsa.sin6_addr = laddr; lsa.sin6_port = lport; - return udpx_listen(slirp, (const union slirp_sockaddr*) &hsa, sizeof(hsa), (union slirp_sockaddr*) &lsa, sizeof(lsa), flags); + return udpx_listen(slirp, (const struct sockaddr *) &hsa, sizeof(hsa), (struct sockaddr *) &lsa, sizeof(lsa), flags); } @@ -85,8 +85,8 @@ struct socket *udp_listen(Slirp *, uint32_t, unsigned, uint32_t, unsigned, int); struct socket *udp6_listen(Slirp *slirp, struct in6_addr, u_int, struct in6_addr, u_int, int); struct socket *udpx_listen(Slirp *, - const union slirp_sockaddr *haddr, socklen_t haddrlen, - const union slirp_sockaddr *laddr, socklen_t laddrlen, + const struct sockaddr *haddr, socklen_t haddrlen, + const struct sockaddr *laddr, socklen_t laddrlen, int flags); int udp_output(struct socket *so, struct mbuf *m, struct sockaddr_in *saddr, struct sockaddr_in *daddr, int iptos); |