From 9be5a92778d20a28277219de0503e8681fe31933 Mon Sep 17 00:00:00 2001 From: Martin Pieuchot Date: Wed, 20 Mar 2013 10:34:13 +0000 Subject: Introduce if_get() to retrieve an interface descriptor pointer given an interface index and replace all the redondant checks and accesses to a global array by a call to this function. With imputs from and ok bluhm@, mikeb@ --- sys/net/if.c | 26 +++++++++++++++++++------- sys/net/if.h | 5 ++--- 2 files changed, 21 insertions(+), 10 deletions(-) (limited to 'sys/net') diff --git a/sys/net/if.c b/sys/net/if.c index 7b240389444..6d6f5cc70fb 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.251 2013/03/15 20:45:34 tedu Exp $ */ +/* $OpenBSD: if.c,v 1.252 2013/03/20 10:34:12 mpi Exp $ */ /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */ /* @@ -190,8 +190,8 @@ ifinit() if_slowtimo(&if_slowtim); } -static int if_index = 0; -int if_indexlim = 0; +static unsigned int if_index = 0; +static unsigned int if_indexlim = 0; struct ifaddr **ifnet_addrs = NULL; struct ifnet **ifindex2ifnet = NULL; struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet); @@ -932,8 +932,7 @@ ifa_ifwithnet(struct sockaddr *addr, u_int rdomain) rdomain = rtable_l2(rdomain); if (af == AF_LINK) { struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; - if (sdl->sdl_index && sdl->sdl_index < if_indexlim && - ifindex2ifnet[sdl->sdl_index]) + if (sdl->sdl_index && if_get(sdl->sdl_index)) return (ifnet_addrs[sdl->sdl_index]); } TAILQ_FOREACH(ifp, &ifnet, if_list) { @@ -1164,8 +1163,7 @@ if_slowtimo(void *arg) } /* - * Map interface name to - * interface structure pointer. + * Map interface name to interface structure pointer. */ struct ifnet * ifunit(const char *name) @@ -1179,6 +1177,20 @@ ifunit(const char *name) return (NULL); } +/* + * Map interface index to interface structure pointer. + */ +struct ifnet * +if_get(unsigned int index) +{ + struct ifnet *ifp = NULL; + + if (index < if_indexlim) + ifp = ifindex2ifnet[index]; + + return (ifp); +} + /* * Interface ioctls. */ diff --git a/sys/net/if.h b/sys/net/if.h index 5c3d4d1251a..f496059d9d9 100644 --- a/sys/net/if.h +++ b/sys/net/if.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if.h,v 1.139 2013/03/07 09:40:19 mpi Exp $ */ +/* $OpenBSD: if.h,v 1.140 2013/03/20 10:34:12 mpi Exp $ */ /* $NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $ */ /* @@ -798,9 +798,7 @@ do { \ extern int ifqmaxlen; extern struct ifnet_head ifnet; -extern struct ifnet **ifindex2ifnet; extern struct ifnet *lo0ifp; -extern int if_indexlim; #define ether_input_mbuf(ifp, m) ether_input((ifp), NULL, (m)) @@ -833,6 +831,7 @@ int if_addgroup(struct ifnet *, const char *); int if_delgroup(struct ifnet *, const char *); void if_group_routechange(struct sockaddr *, struct sockaddr *); struct ifnet *ifunit(const char *); +struct ifnet *if_get(unsigned int); void if_start(struct ifnet *); void ifnewlladdr(struct ifnet *); -- cgit v1.2.3