=== modified file 'modules/ntrack-libnl.c' --- a/modules/ntrack-libnl.c 2011-04-25 17:25:24 +0000 +++ b/modules/ntrack-libnl.c 2011-05-19 21:43:46 +0000 @@ -59,16 +59,20 @@ typedef struct nl_handle NTRACK_nl_handl name, \ 0) #define NL_PRINT_ERROR(text,err) \ printf ("%s/%s\n", text, nl_geterror()) #define RTNL_ROUTE_GET_PRIO rtnl_route_get_prio #elif defined(HAVE_LIBNL2) || defined (HAVE_LIBNL3) + +/* define LIBNL1 name NL_DUMP_FULL as NL_DUMP_DETAILS */ +#define NL_DUMP_FULL NL_DUMP_DETAILS + typedef struct nl_sock NTRACK_nl_handle; #define NL_CACHE_MNGR_ALLOC(handle,mngr_o) \ nl_cache_mngr_alloc (handle, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr_o) #define NL_CACHE_MNGR_ADD(mngr,name,cache_o) \ nl_cache_mngr_add (mngr, \ name, \ @@ -97,17 +101,17 @@ struct _nl_info struct nl_cache *link_cache; struct nl_cache *neigh_cache; struct rtnl_route *topmost_route; int update_run; }; static void -setup_main_handle (struct _nl_info *nl_info); +resetup_main_handle (struct _nl_info *nl_info); static void setup_smart_handle (struct _nl_info *nl_info); static int smart_cache_update (ntrack_monitor_t *self, ntrackpointer user_data); static int @@ -138,32 +142,17 @@ _ntrack_arch_new (ntrack_monitor_callbac /* setup ntrack_monitor_t part */ ((ntrack_monitor_t *)arch)->state = NTRACK_STATE_UNKNOWN; /* setup struct ntrack_monitor_arch part */ arch->data = nl_info; arch->cb = callback; arch->cb_user_data = user_data; -#ifdef HAVE_LIBNL1 - nl_info->handle = nl_handle_alloc(); - nl_info->smart_update_handle = nl_handle_alloc(); -#elif defined(HAVE_LIBNL2) || defined(HAVE_LIBNL3) - nl_info->handle = nl_socket_alloc(); - nl_info->smart_update_handle = nl_socket_alloc(); -#else -#error "needs libnl-1 or -2.0" -#endif - if (!nl_info->handle) - error (1, 255, "OOM (nl_handle_alloc)"); - - if (!nl_info->smart_update_handle) - error (1, 257, "OOM (nl_handle_alloc)"); - - setup_main_handle (nl_info); + resetup_main_handle (nl_info); setup_smart_handle (nl_info); /* set smart update handle to nonblocking */ if (nl_socket_set_nonblocking (nl_info->smart_update_handle)) error (1,260,"cannot set smart handle to nonblocking"); update_caches (nl_info); update_connectivity ((ntrack_monitor_t *) arch, nl_info); @@ -197,19 +186,18 @@ _ntrack_arch_process_data (ntrackpointer int rval = nl_cache_mngr_data_ready (nl_info->smart_update_cache_mngr); /* * if all looks good and there was relevant data for the managed * caches then we attempt a smart cache update. This will flush * our caches and reinterpret the new state and emit events * as appropriate. */ - if(rval) - if (!smart_cache_update (self, nl_info)) - return -1; + if (!smart_cache_update (self, nl_info)) + return -1; return rval; } void _ntrack_arch_free (ntrackpointer self) { struct _ntrack_monitor_arch *arch = self; struct _nl_info *nl_info = arch->data; @@ -225,49 +213,77 @@ _ntrack_arch_free (ntrackpointer self) if (nl_info->handle) nl_close (nl_info->handle); free (nl_info); free (self); } static void -setup_main_handle (struct _nl_info *nl_info) +resetup_main_handle (struct _nl_info *nl_info) { - NL_CACHE_MNGR_ALLOC (nl_info->handle, nl_info->update_cache_mngr); - - if (!nl_info->addr_cache) { - NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/addr", nl_info->addr_cache); - if (!nl_info->addr_cache) - error (1, 366, "cache mngr add fail."); - } +#ifdef HAVE_LIBNL1 + if (nl_info->handle) + nl_handle_destroy (nl_info->handle); + nl_info->handle = nl_handle_alloc(); +#elif defined(HAVE_LIBNL2) || defined(HAVE_LIBNL3) + if (nl_info->handle) + nl_socket_free (nl_info->handle); + nl_info->handle = nl_socket_alloc(); +#else +#error "needs libnl-1 or -2.0" +#endif + if (!nl_info->handle) + error (1, 255, "OOM (nl_handle_alloc)"); - if (!nl_info->link_cache) { - NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/link", nl_info->link_cache); - if (!nl_info->link_cache) - error (1, 367, "cache mngr add fail."); - } + if (nl_info->update_cache_mngr) + nl_cache_mngr_free (nl_info->update_cache_mngr); - if (!nl_info->route_cache) { - NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/route", nl_info->route_cache); - if (!nl_info->route_cache) - error (1, 368, "cache mngr add fail."); - } + NL_CACHE_MNGR_ALLOC (nl_info->handle, nl_info->update_cache_mngr); - if (!nl_info->neigh_cache) { - NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/neigh", nl_info->neigh_cache); - if (!nl_info->neigh_cache) - error (1, 369, "cache mngr add fail."); - } + NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/addr", nl_info->addr_cache); + if (!nl_info->addr_cache) + error (1, 366, "cache mngr add fail."); + + NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/link", nl_info->link_cache); + if (!nl_info->link_cache) + error (1, 367, "cache mngr add fail."); + + NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/route", nl_info->route_cache); + if (!nl_info->route_cache) + error (1, 368, "cache mngr add fail."); + + NL_CACHE_MNGR_ADD (nl_info->update_cache_mngr, "route/neigh", nl_info->neigh_cache); + if (!nl_info->neigh_cache) + error (1, 369, "cache mngr add fail."); } static void setup_smart_handle (struct _nl_info *nl_info) { struct nl_cache *tmp_cache = 0; + +#ifdef HAVE_LIBNL1 + if (nl_info->smart_update_handle) { + nl_close (nl_info->smart_update_handle); + nl_handle_destroy (nl_info->smart_update_handle); + } + nl_info->smart_update_handle = nl_handle_alloc(); +#elif defined(HAVE_LIBNL2) || defined(HAVE_LIBNL3) + if (nl_info->smart_update_handle) { + nl_close (nl_info->smart_update_handle); + nl_socket_free (nl_info->smart_update_handle); + } + nl_info->smart_update_handle = nl_socket_alloc(); +#else +#error "needs libnl-1 or -2.0" +#endif + if (!nl_info->smart_update_handle) + error (1, 257, "OOM (nl_handle_alloc)"); + NL_CACHE_MNGR_ALLOC (nl_info->smart_update_handle, nl_info->smart_update_cache_mngr); if (!nl_info->smart_update_cache_mngr) error (1, 265, "cache mngr alloc failed"); /* add caches for events we are interested in. We * don't need to remember them as we are solely * interested on whether something of relevance for @@ -404,16 +420,17 @@ get_nl_link_by_index (ntrack_monitor_t * static void update_connectivity (ntrack_monitor_t *self, struct _nl_info *nl_info) { struct rtnl_route *topmost_route = NULL; struct rtnl_route *filter_route = rtnl_route_alloc (); struct nl_addr *dst_filter = nl_addr_alloc (0); struct nl_object *iter; ntrack_list_t *link_list = NULL; + struct rtnl_link *iter_link = NULL; /* fill link info */ iter = nl_cache_get_first (nl_info->link_cache); while (iter) { link_list = ntrack_list_insert_sorted (link_list, iter, nl_link_index_cmp); nl_object_get (iter); iter = nl_cache_get_next (iter); } @@ -421,17 +438,16 @@ update_connectivity (ntrack_monitor_t *s /* get topmost route with link that has is up and has carrier */ nl_addr_set_family (dst_filter, AF_INET); nl_addr_set_binary_addr (dst_filter, "\0", 0); nl_addr_set_prefixlen (dst_filter, 0); rtnl_route_set_dst (filter_route, dst_filter); iter = nl_cache_get_first (nl_info->route_cache); while (iter) { - struct rtnl_link *iter_link; #if defined(HAVE_LIBNL2) || defined(HAVE_LIBNL3) struct rtnl_nexthop *nexth=0; int nhops=0; #endif if (!nl_object_match_filter (iter, OBJ_CAST(filter_route))) goto next; #if defined(HAVE_LIBNL2) || defined(HAVE_LIBNL3) @@ -447,17 +463,17 @@ update_connectivity (ntrack_monitor_t *s #endif /* if we dont have a link for the current oif, * this is not a valid topmost_route */ if (!iter_link) goto next; /* if new route is lower than the current topmost, continue */ - if (topmost_route && cmp_rtnl_route_metric (topmost_route, iter) <= 0) + if (topmost_route && (cmp_rtnl_route_metric (topmost_route, iter) <= 0 && iter_link != NULL)) goto next; /* if the link of the potential new topmost route is up, we replace the current topmost_route */ if (rtnl_link_get_flags (iter_link) & IFF_LOWER_UP) { /* release ref to current last topmost_route */ if (topmost_route) nl_object_put (OBJ_CAST(topmost_route));