From 255a9dcfc2a017432157e89634f8c2391db2a264 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 1 Oct 2013 19:13:06 -0600 Subject: [PATCH] Simple routing table hooked into network build system --- net/Kconfig | 15 ++++++++++++++- net/Makefile | 6 ++++++ net/net_addroute.c | 3 ++- net/net_delroute.c | 23 ++++++++++++----------- net/net_findroute.c | 25 +++++++++++++------------ net/net_foreachroute.c | 5 +++-- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/net/Kconfig b/net/Kconfig index c581e32a3d..fbc34ec78a 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -295,7 +295,7 @@ config NET_ARPTAB_SIZE int "ARP table size" default 16 ---help--- - The size of the ARP table + The size of the ARP table (in entries). config NET_ARP_IPIN bool "ARP address harvesting" @@ -304,6 +304,19 @@ config NET_ARP_IPIN Harvest IP/MAC address mappings from the ARP table from incoming IP packets. +config NET_ROUTE + bool "Routing table suport" + default n + ---help--- + Build in support for a routing table. See include/nuttx/net/route.h + +config NET_MAXROUTES + int "Routing table size" + default 4 + depends on NET_ROUTE + ---help--- + The size of the routing table (in entries). + config NET_MULTICAST bool "Multi-cast Tx support" default n diff --git a/net/Makefile b/net/Makefile index 2344344965..0aced63432 100644 --- a/net/Makefile +++ b/net/Makefile @@ -62,6 +62,12 @@ endif endif endif +# Routing table support + +ifeq ($(CONFIG_NET_ROUTE),y) +SOCK_CSRCS += net_addroute.c net_delroute.c net_findroute.c net_foreachroute.c +endif + # Support for network access using streams ifneq ($(CONFIG_NFILE_STREAMS),0) diff --git a/net/net_addroute.c b/net/net_addroute.c index fd4e7de9aa..6b7ac9f6e3 100644 --- a/net/net_addroute.c +++ b/net/net_addroute.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -117,4 +118,4 @@ int net_addroute(uip_ipaddr_t target, uip_ipaddr_t netmask, return net_foreachroute(net_available, &route) ? OK : -EAGAIN; } -#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */ +#endif /* CONFIG_NET && CONFIG_NET_ROUTE */ diff --git a/net/net_delroute.c b/net/net_delroute.c index 08d9ad154c..b203f0349a 100644 --- a/net/net_delroute.c +++ b/net/net_delroute.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -54,8 +55,8 @@ struct route_match_s { - uip_ipaddr_t target; /* The target IP address to match */ - FAR struct net_route_s *route; /* The location to return the route */ + uip_ipaddr_t target; /* The target IP address to match */ + uip_ipaddr_t netmask; /* The network mask to match */ }; /**************************************************************************** @@ -81,17 +82,17 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) { FAR struct route_match_s *match = ( FAR struct route_match_s *)arg; - /* To match, the entry has to be in use, the masked target addresses must - * be the same. In the event of multiple matches, only the first is - * returned. + /* To match, the entry has to be in use, the masked target address must + * be the same, and the masks must be the same. */ if (route->inuse && - uip_ipaddr_maskcmp(route->target, match->target, route>netmask)) + uip_ipaddr_maskcmp(route->target, match->target, match->netmask) && + uip_ipaddr_cmp(route->target, match->netmask)) { /* They match.. clear the route table entry */ - memcpy(match->route, route, sizeof(struct net_route_s)); + memset(route, 0, sizeof(struct net_route_s)); return 1; } @@ -103,10 +104,10 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) ****************************************************************************/ /**************************************************************************** - * Function: net_findroute + * Function: net_delroute * * Description: - * Given an IP address, return a copy of the routing table contents + * Remove an existing route from the routing table * * Parameters: * @@ -115,7 +116,7 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) * ****************************************************************************/ -int net_findroute(uip_ipaddr_t target, FAR struct net_route_s *route); +int net_delroute(uip_ipaddr_t target, uip_ipaddr_t netmask) { struct route_match_s match; @@ -129,4 +130,4 @@ int net_findroute(uip_ipaddr_t target, FAR struct net_route_s *route); return net_foreachroute(net_match, &match) ? OK : -ENOENT; } -#endif /* CONFIG_NET && CONFIG_NET_ROUTE */ +#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */ diff --git a/net/net_findroute.c b/net/net_findroute.c index 252f9a0986..5dea9d9acb 100644 --- a/net/net_findroute.c +++ b/net/net_findroute.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -54,8 +55,8 @@ struct route_match_s { - uip_ipaddr_t target; /* The target IP address to match */ - uip_ipaddr_t netmask; /* The network mask to match */ + uip_ipaddr_t target; /* The target IP address to match */ + FAR struct net_route_s *route; /* The location to return the route */ }; /**************************************************************************** @@ -81,17 +82,17 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) { FAR struct route_match_s *match = ( FAR struct route_match_s *)arg; - /* To match, the entry has to be in use, the masked target address must - * be the same, and the masks must be the same. + /* To match, the entry has to be in use, the masked target addresses must + * be the same. In the event of multiple matches, only the first is + * returned. */ if (route->inuse && - uip_ipaddr_maskcmp(route->target, match->target, match->netmask) && - uip_ipaddr_cmp(route->target, match->netmask)) + uip_ipaddr_maskcmp(route->target, match->target, route->netmask)) { /* They match.. clear the route table entry */ - memset(route, 0, sizeof(struct net_route_s)); + memcpy(match->route, route, sizeof(struct net_route_s)); return 1; } @@ -103,10 +104,10 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) ****************************************************************************/ /**************************************************************************** - * Function: net_delroute + * Function: net_findroute * * Description: - * Remove an existing route from the routing table + * Given an IP address, return a copy of the routing table contents * * Parameters: * @@ -115,18 +116,18 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) * ****************************************************************************/ -int net_delroute(uip_ipaddr_t target, uip_ipaddr_t netmask); +int net_findroute(uip_ipaddr_t target, FAR struct net_route_s *route) { struct route_match_s match; /* Set up the comparison structure */ uip_ipaddr_copy(match.target, target); - uip_ipaddr_copy(match.netmask, netmask); + match.route = route; /* Then remove the entry from the routing table */ return net_foreachroute(net_match, &match) ? OK : -ENOENT; } -#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */ +#endif /* CONFIG_NET && CONFIG_NET_ROUTE */ diff --git a/net/net_foreachroute.c b/net/net_foreachroute.c index 4c82a688b7..8eb8443343 100644 --- a/net/net_foreachroute.c +++ b/net/net_foreachroute.c @@ -42,6 +42,7 @@ #include #include +#include #include #include "net_internal.h" @@ -85,7 +86,7 @@ int net_foreachroute(route_handler_t handler, FAR void *arg) for (i = 0; i < CONFIG_NET_MAXROUTES && ret == 0; i++) { - ret = handler(&g_route[i], arg); + ret = handler(&g_routes[i], arg); } /* Unlock uIP */ @@ -94,4 +95,4 @@ int net_foreachroute(route_handler_t handler, FAR void *arg) return ret; } -#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */ +#endif /* CONFIG_NET && CONFIG_NET_ROUTE */