[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 06/18] slirp: Factorizing and cleaning solookup()
From: |
Samuel Thibault |
Subject: |
[Qemu-devel] [PATCH 06/18] slirp: Factorizing and cleaning solookup() |
Date: |
Mon, 31 Mar 2014 00:22:57 +0200 |
This patch makes solookup() compatible with varying address families. Also,
this function was only compatible with TCP. Having the socket list in
argument, it is now compatible with UDP too. Finally, some optimization
code is factorized inside the function (the function look at the last
returned result before browsing the complete socket list).
This also adds a sockaddr_equal() function to compare two
sockaddr_storage.
Signed-off-by: Guillaume Subiron <address@hidden>
---
slirp/socket.c | 30 ++++++++++++++++--------------
slirp/socket.h | 23 ++++++++++++++++++++++-
slirp/tcp_input.c | 27 +++++++++++----------------
slirp/udp.c | 25 ++++++-------------------
4 files changed, 55 insertions(+), 50 deletions(-)
diff --git a/slirp/socket.c b/slirp/socket.c
index c0c8bc1..0e37a52 100644
--- a/slirp/socket.c
+++ b/slirp/socket.c
@@ -15,24 +15,26 @@
static void sofcantrcvmore(struct socket *so);
static void sofcantsendmore(struct socket *so);
-struct socket *
-solookup(struct socket *head, struct in_addr laddr, u_int lport,
- struct in_addr faddr, u_int fport)
+struct socket *solookup(struct socket **last, struct socket *head,
+ struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
{
- struct socket *so;
+ struct socket *so = *last;
- for (so = head->so_next; so != head; so = so->so_next) {
- if (so->so_lport == lport &&
- so->so_laddr.s_addr == laddr.s_addr &&
- so->so_faddr.s_addr == faddr.s_addr &&
- so->so_fport == fport)
- break;
- }
+ /* Optimisation */
+ if (sockaddr_equal(&(so->lhost.ss), lhost)
+ && (!fhost || sockaddr_equal(&(so->fhost.ss), fhost))) {
+ return so;
+ }
- if (so == head)
- return (struct socket *)NULL;
- return so;
+ for (so = head->so_next; so != head; so = so->so_next) {
+ if (sockaddr_equal(&(so->lhost.ss), lhost)
+ && (!fhost || sockaddr_equal(&(so->fhost.ss), fhost))) {
+ *last = so;
+ return so;
+ }
+ }
+ return (struct socket *)NULL;
}
/*
diff --git a/slirp/socket.h b/slirp/socket.h
index 50059be..5a673ae 100644
--- a/slirp/socket.h
+++ b/slirp/socket.h
@@ -93,7 +93,28 @@ struct socket {
#define SS_HOSTFWD 0x1000 /* Socket describes host->guest
forwarding */
#define SS_INCOMING 0x2000 /* Connection was initiated by a host
on the internet */
-struct socket * solookup(struct socket *, struct in_addr, u_int, struct
in_addr, u_int);
+static inline int sockaddr_equal(struct sockaddr_storage *a,
+ struct sockaddr_storage *b)
+{
+ if (a->ss_family != b->ss_family) {
+ return 0;
+ } else {
+ 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:
+ assert(0);
+ }
+ }
+}
+
+struct socket *solookup(struct socket **, struct socket *,
+ struct sockaddr_storage *, struct sockaddr_storage *);
struct socket * socreate(Slirp *);
void sofree(struct socket *);
int soread(struct socket *);
diff --git a/slirp/tcp_input.c b/slirp/tcp_input.c
index 4b1a383..1b9a0b1 100644
--- a/slirp/tcp_input.c
+++ b/slirp/tcp_input.c
@@ -227,6 +227,7 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
int iss = 0;
u_long tiwin;
int ret;
+ struct sockaddr_storage lhost, fhost;
struct ex_list *ex_ptr;
Slirp *slirp;
@@ -320,16 +321,14 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
* Locate pcb for segment.
*/
findso:
- so = slirp->tcp_last_so;
- if (so->so_fport != ti->ti_dport ||
- so->so_lport != ti->ti_sport ||
- so->so_laddr.s_addr != ti->ti_src.s_addr ||
- so->so_faddr.s_addr != ti->ti_dst.s_addr) {
- so = solookup(&slirp->tcb, ti->ti_src, ti->ti_sport,
- ti->ti_dst, ti->ti_dport);
- if (so)
- slirp->tcp_last_so = so;
- }
+ lhost.ss_family = AF_INET;
+ ((struct sockaddr_in *)&lhost)->sin_addr = ti->ti_src;
+ ((struct sockaddr_in *)&lhost)->sin_port = ti->ti_sport;
+ fhost.ss_family = AF_INET;
+ ((struct sockaddr_in *)&fhost)->sin_addr = ti->ti_dst;
+ ((struct sockaddr_in *)&fhost)->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
@@ -374,12 +373,8 @@ findso:
sbreserve(&so->so_snd, TCP_SNDSPACE);
sbreserve(&so->so_rcv, TCP_RCVSPACE);
- so->so_lfamily = AF_INET;
- so->so_laddr = ti->ti_src;
- so->so_lport = ti->ti_sport;
- so->so_ffamily = AF_INET;
- so->so_faddr = ti->ti_dst;
- so->so_fport = ti->ti_dport;
+ so->lhost.ss = lhost;
+ so->fhost.ss = fhost;
if ((so->so_iptos = tcp_tos(so)) == 0)
so->so_iptos = ((struct ip *)ti)->ip_tos;
diff --git a/slirp/udp.c b/slirp/udp.c
index 309f0a4..94f104d 100644
--- a/slirp/udp.c
+++ b/slirp/udp.c
@@ -70,6 +70,7 @@ udp_input(register struct mbuf *m, int iphlen)
int len;
struct ip save_ip;
struct socket *so;
+ struct sockaddr_storage lhost;
DEBUG_CALL("udp_input");
DEBUG_ARG("m = %lx", (long)m);
@@ -151,25 +152,11 @@ udp_input(register struct mbuf *m, int iphlen)
/*
* Locate pcb for datagram.
*/
- so = slirp->udp_last_so;
- if (so->so_lport != uh->uh_sport ||
- so->so_laddr.s_addr != ip->ip_src.s_addr) {
- struct socket *tmp;
-
- for (tmp = slirp->udb.so_next; tmp != &slirp->udb;
- tmp = tmp->so_next) {
- if (tmp->so_lport == uh->uh_sport &&
- tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
- so = tmp;
- break;
- }
- }
- if (tmp == &slirp->udb) {
- so = NULL;
- } else {
- slirp->udp_last_so = so;
- }
- }
+ lhost.ss_family = AF_INET;
+ ((struct sockaddr_in *)&lhost)->sin_addr = ip->ip_src;
+ ((struct sockaddr_in *)&lhost)->sin_port = uh->uh_sport;
+
+ so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
if (so == NULL) {
/*
--
1.9.0
- [Qemu-devel] [PATCHv4 00/18] slirp: Adding IPv6 support to Qemu -net user mode, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 01/18] slirp: goto bad in udp_input if sosendto fails, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 02/18] slirp: Generalizing and neutralizing code before adding IPv6 stuff, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 18/18] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 03/18] slirp: Reindent after refactoring, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 09/18] qemu/timer.h : Adding function to second scale, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 05/18] slirp: Factorizing address translation, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 04/18] slirp: Make Socket structure IPv6 compatible, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 16/18] slirp: Handle IPv6 in TCP functions, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 06/18] slirp: Factorizing and cleaning solookup(),
Samuel Thibault <=
- [Qemu-devel] [PATCH 07/18] slirp: Make udp_attach IPv6 compatible, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 17/18] slirp: Adding IPv6 address for DNS relay, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 12/18] slirp: Adding IPv6 UDP support, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 15/18] slirp: Reindent after refactoring, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 08/18] slirp: Adding family argument to tcp_fconnect(), Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 10/18] slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 14/18] slirp: Generalizing and neutralizing various TCP functions before adding IPv6 stuff, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 11/18] slirp: Adding ICMPv6 error sending, Samuel Thibault, 2014/03/30
- [Qemu-devel] [PATCH 13/18] slirp: Factorizing tcpiphdr structure with an union, Samuel Thibault, 2014/03/30