mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jules Maselbas <jmaselbas@kalray.eu>
To: barebox@lists.infradead.org
Cc: Jules Maselbas <jmaselbas@kalray.eu>
Subject: [RFC PATCH 1/5] net: Add a function to retrieve UDP connection
Date: Tue,  9 Aug 2022 15:20:17 +0200	[thread overview]
Message-ID: <20220809132021.7110-1-jmaselbas@kalray.eu> (raw)

Add a function that search for a UDP connection based on it's source
port. This function can be easily extended to do the same for TCP.

Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
---
 net/net.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/net/net.c b/net/net.c
index eae45e1843..c01bf49b92 100644
--- a/net/net.c
+++ b/net/net.c
@@ -36,6 +36,22 @@ IPaddr_t net_gateway;
 static IPaddr_t net_nameserver;
 static char *net_domainname;
 
+static LIST_HEAD(connection_list);
+
+static struct net_connection *net_ip_get_con(int proto, uint16_t port)
+{
+	struct net_connection *con;
+
+	list_for_each_entry(con, &connection_list, list) {
+		if (con->proto != proto)
+			continue;
+		if (con->proto == IPPROTO_UDP && ntohs(con->udp->uh_sport) == port)
+			return con;
+	}
+
+	return NULL;
+}
+
 void net_set_nameserver(IPaddr_t nameserver)
 {
 	net_nameserver = nameserver;
@@ -358,8 +374,6 @@ IPaddr_t net_get_gateway(void)
 	return net_gateway;
 }
 
-static LIST_HEAD(connection_list);
-
 static struct net_connection *net_new(struct eth_device *edev, IPaddr_t dest,
 				      rx_handler_f *handler, void *ctx)
 {
@@ -587,15 +601,12 @@ static int net_handle_udp(unsigned char *pkt, int len)
 	struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE);
 	struct net_connection *con;
 	struct udphdr *udp;
-	int port;
 
 	udp = (struct udphdr *)(ip + 1);
-	port = ntohs(udp->uh_dport);
-	list_for_each_entry(con, &connection_list, list) {
-		if (con->proto == IPPROTO_UDP && port == ntohs(con->udp->uh_sport)) {
-			con->handler(con->priv, pkt, len);
-			return 0;
-		}
+	con = net_ip_get_con(IPPROTO_UDP, ntohs(udp->uh_dport));
+	if (con) {
+		con->handler(con->priv, pkt, len);
+		return 0;
 	}
 	return -EINVAL;
 }
-- 
2.17.1




             reply	other threads:[~2022-08-09 13:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-09 13:20 Jules Maselbas [this message]
2022-08-09 13:20 ` [RFC PATCH 2/5] net: Implement source port randomization Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 3/5] net: Add simple TCP support Jules Maselbas
2022-08-12  7:04   ` Sascha Hauer
2022-08-12 16:17     ` Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 4/5] Add irc command Jules Maselbas
2022-08-09 13:20 ` [RFC PATCH 5/5] Add tcpdump command Jules Maselbas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220809132021.7110-1-jmaselbas@kalray.eu \
    --to=jmaselbas@kalray.eu \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox