mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [RFC PATCH 1/5] net: Add a function to retrieve UDP connection
@ 2022-08-09 13:20 Jules Maselbas
  2022-08-09 13:20 ` [RFC PATCH 2/5] net: Implement source port randomization Jules Maselbas
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Jules Maselbas @ 2022-08-09 13:20 UTC (permalink / raw)
  To: barebox; +Cc: Jules Maselbas

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-08-12 16:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-09 13:20 [RFC PATCH 1/5] net: Add a function to retrieve UDP connection Jules Maselbas
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox