slirp: Add sockaddr_equal, make solookup family-agnostic

This patch makes solookup() compatible with varying address
families, by using a new sockaddr_equal() function that compares
two sockaddr_storage.

This prepares for IPv6 support.

Signed-off-by: Guillaume Subiron <maethor@subiron.org>
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Guillaume Subiron 2015-12-19 22:25:01 +01:00 committed by Jason Wang
parent a5fd24aa6d
commit 8a87f121ca
4 changed files with 51 additions and 27 deletions

View File

@ -15,29 +15,20 @@
static void sofcantrcvmore(struct socket *so); static void sofcantrcvmore(struct socket *so);
static void sofcantsendmore(struct socket *so); static void sofcantsendmore(struct socket *so);
struct socket * struct socket *solookup(struct socket **last, struct socket *head,
solookup(struct socket **last, struct socket *head, struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
struct in_addr laddr, u_int lport,
struct in_addr faddr, u_int fport)
{ {
struct socket *so = *last; struct socket *so = *last;
/* Optimisation */ /* Optimisation */
if (so != head && if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
so->so_lport == lport && && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
so->so_laddr.s_addr == laddr.s_addr &&
(!faddr.s_addr ||
(so->so_faddr.s_addr == faddr.s_addr &&
so->so_fport == fport))) {
return so; return so;
} }
for (so = head->so_next; so != head; so = so->so_next) { for (so = head->so_next; so != head; so = so->so_next) {
if (so->so_lport == lport && if (sockaddr_equal(&(so->lhost.ss), lhost)
so->so_laddr.s_addr == laddr.s_addr && && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
(!faddr.s_addr ||
(so->so_faddr.s_addr == faddr.s_addr &&
so->so_fport == fport))) {
*last = so; *last = so;
return so; return so;
} }

View File

@ -87,8 +87,30 @@ struct socket {
#define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */ #define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */
#define SS_INCOMING 0x2000 /* Connection was initiated by a host on the internet */ #define SS_INCOMING 0x2000 /* Connection was initiated by a host on the internet */
static inline int sockaddr_equal(struct sockaddr_storage *a,
struct sockaddr_storage *b)
{
if (a->ss_family != b->ss_family) {
return 0;
}
switch (a->ss_family) {
case AF_INET:
{
struct sockaddr_in *a4 = (struct sockaddr_in *) a;
struct sockaddr_in *b4 = (struct sockaddr_in *) b;
return a4->sin_addr.s_addr == b4->sin_addr.s_addr
&& a4->sin_port == b4->sin_port;
}
default:
g_assert_not_reached();
}
return 0;
}
struct socket *solookup(struct socket **, struct socket *, struct socket *solookup(struct socket **, struct socket *,
struct in_addr, u_int, struct in_addr, u_int); struct sockaddr_storage *, struct sockaddr_storage *);
struct socket *socreate(Slirp *); struct socket *socreate(Slirp *);
void sofree(struct socket *); void sofree(struct socket *);
int soread(struct socket *); int soread(struct socket *);

View File

@ -227,6 +227,8 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
int iss = 0; int iss = 0;
u_long tiwin; u_long tiwin;
int ret; int ret;
struct sockaddr_storage lhost, fhost;
struct sockaddr_in *lhost4, *fhost4;
struct ex_list *ex_ptr; struct ex_list *ex_ptr;
Slirp *slirp; Slirp *slirp;
@ -320,9 +322,16 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
* Locate pcb for segment. * Locate pcb for segment.
*/ */
findso: findso:
so = solookup(&slirp->tcp_last_so, &slirp->tcb, lhost.ss_family = AF_INET;
ti->ti_src, ti->ti_sport, lhost4 = (struct sockaddr_in *) &lhost;
ti->ti_dst, ti->ti_dport); lhost4->sin_addr = ti->ti_src;
lhost4->sin_port = ti->ti_sport;
fhost.ss_family = AF_INET;
fhost4 = (struct sockaddr_in *) &fhost;
fhost4->sin_addr = ti->ti_dst;
fhost4->sin_port = ti->ti_dport;
so = solookup(&slirp->tcp_last_so, &slirp->tcb, &lhost, &fhost);
/* /*
* If the state is CLOSED (i.e., TCB does not exist) then * If the state is CLOSED (i.e., TCB does not exist) then
@ -367,12 +376,8 @@ findso:
sbreserve(&so->so_snd, TCP_SNDSPACE); sbreserve(&so->so_snd, TCP_SNDSPACE);
sbreserve(&so->so_rcv, TCP_RCVSPACE); sbreserve(&so->so_rcv, TCP_RCVSPACE);
so->so_lfamily = AF_INET; so->lhost.ss = lhost;
so->so_laddr = ti->ti_src; so->fhost.ss = fhost;
so->so_lport = ti->ti_sport;
so->so_ffamily = AF_INET;
so->so_faddr = ti->ti_dst;
so->so_fport = ti->ti_dport;
if ((so->so_iptos = tcp_tos(so)) == 0) if ((so->so_iptos = tcp_tos(so)) == 0)
so->so_iptos = ((struct ip *)ti)->ip_tos; so->so_iptos = ((struct ip *)ti)->ip_tos;

View File

@ -70,6 +70,8 @@ udp_input(register struct mbuf *m, int iphlen)
int len; int len;
struct ip save_ip; struct ip save_ip;
struct socket *so; struct socket *so;
struct sockaddr_storage lhost;
struct sockaddr_in *lhost4;
DEBUG_CALL("udp_input"); DEBUG_CALL("udp_input");
DEBUG_ARG("m = %p", m); DEBUG_ARG("m = %p", m);
@ -151,8 +153,12 @@ udp_input(register struct mbuf *m, int iphlen)
/* /*
* Locate pcb for datagram. * Locate pcb for datagram.
*/ */
so = solookup(&slirp->udp_last_so, &slirp->udb, lhost.ss_family = AF_INET;
ip->ip_src, uh->uh_sport, (struct in_addr) {0}, 0); lhost4 = (struct sockaddr_in *) &lhost;
lhost4->sin_addr = ip->ip_src;
lhost4->sin_port = uh->uh_sport;
so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
if (so == NULL) { if (so == NULL) {
/* /*