core: misc style fixes to libnl compat code

This commit is contained in:
Dan Williams
2011-07-30 15:46:33 -05:00
parent daeb181fa2
commit b562839df8
8 changed files with 99 additions and 58 deletions

View File

@@ -94,7 +94,8 @@ void nm_generic_enable_loopback (void)
if ((err = rtnl_addr_add (nlh, addr, 0)) < 0) { if ((err = rtnl_addr_add (nlh, addr, 0)) < 0) {
if (err != -EEXIST) { if (err != -EEXIST) {
nm_log_warn (LOGD_CORE, "error %d returned from rtnl_addr_add():\n%s", err, nl_geterror(err)); nm_log_warn (LOGD_CORE, "error setting loopback address: (%d) %s",
err, nl_geterror (err));
} }
} }
out: out:

View File

@@ -1346,8 +1346,10 @@ nm_ip6_manager_init (NMIP6Manager *manager)
G_CALLBACK (netlink_notification), manager); G_CALLBACK (netlink_notification), manager);
priv->nlh = nm_netlink_get_default_handle (); priv->nlh = nm_netlink_get_default_handle ();
rtnl_addr_alloc_cache(priv->nlh, &priv->addr_cache); rtnl_addr_alloc_cache (priv->nlh, &priv->addr_cache);
g_warn_if_fail (priv->addr_cache != NULL);
rtnl_route_alloc_cache (priv->nlh, NETLINK_ROUTE, NL_AUTO_PROVIDE, &priv->route_cache); rtnl_route_alloc_cache (priv->nlh, NETLINK_ROUTE, NL_AUTO_PROVIDE, &priv->route_cache);
g_warn_if_fail (priv->route_cache != NULL);
} }
static void static void

View File

@@ -24,33 +24,40 @@
#include "nm-netlink-compat.h" #include "nm-netlink-compat.h"
#ifndef HAVE_LIBNL1 #ifndef HAVE_LIBNL1
struct rtnl_nexthop * nm_netlink_get_nh(struct rtnl_route * route) { struct rtnl_nexthop *
nm_netlink_get_nh (struct rtnl_route * route)
{
int hops; int hops;
hops = rtnl_route_get_nnexthops (route); hops = rtnl_route_get_nnexthops (route);
g_return_val_if_fail(hops > 0, NULL); g_return_val_if_fail(hops > 0, NULL);
return rtnl_route_nexthop_n (route, 0); return rtnl_route_nexthop_n (route, 0);
} }
int rtnl_route_get_oif(struct rtnl_route * route) { int
rtnl_route_get_oif (struct rtnl_route * route)
{
struct rtnl_nexthop * nh; struct rtnl_nexthop * nh;
nh = nm_netlink_get_nh(route); nh = nm_netlink_get_nh(route);
g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND); g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND);
return rtnl_route_nh_get_ifindex (nh); return rtnl_route_nh_get_ifindex (nh);
} }
int rtnl_route_set_oif(struct rtnl_route * route, int ifindex) { int
rtnl_route_set_oif (struct rtnl_route * route, int ifindex)
{
struct rtnl_nexthop * nh; struct rtnl_nexthop * nh;
nh = rtnl_route_nh_alloc(); nh = rtnl_route_nh_alloc();
rtnl_route_nh_set_ifindex(nh, ifindex); rtnl_route_nh_set_ifindex(nh, ifindex);
rtnl_route_add_nexthop(route, nh); rtnl_route_add_nexthop(route, nh);
return 0; return 0;
} }
struct nl_addr * rtnl_route_get_gateway(struct rtnl_route * route) { struct nl_addr *
rtnl_route_get_gateway (struct rtnl_route * route)
{
struct rtnl_nexthop * nh; struct rtnl_nexthop * nh;
nh = nm_netlink_get_nh(route); nh = nm_netlink_get_nh(route);
@@ -58,7 +65,9 @@ struct nl_addr * rtnl_route_get_gateway(struct rtnl_route * route) {
return rtnl_route_nh_get_gateway(nh); return rtnl_route_nh_get_gateway(nh);
} }
int rtnl_route_set_gateway(struct rtnl_route * route, struct nl_addr * gw_addr) { int
rtnl_route_set_gateway (struct rtnl_route * route, struct nl_addr * gw_addr)
{
struct rtnl_nexthop * nh; struct rtnl_nexthop * nh;
nh = nm_netlink_get_nh(route); nh = nm_netlink_get_nh(route);
@@ -68,7 +77,9 @@ int rtnl_route_set_gateway(struct rtnl_route * route, struct nl_addr * gw_addr)
return 0; return 0;
} }
int rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) { int
rtnl_route_get_dst_len(struct rtnl_route * rtnlroute)
{
struct nl_addr * dst; struct nl_addr * dst;
dst = rtnl_route_get_dst(rtnlroute); dst = rtnl_route_get_dst(rtnlroute);
@@ -77,14 +88,16 @@ int rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) {
#endif #endif
#ifdef HAVE_LIBNL1 #ifdef HAVE_LIBNL1
int nl_compat_error(int err) { int
err = abs(err); nl_compat_error (int err)
{
err = abs (err);
if(err==EEXIST) if (err == EEXIST)
err = NLE_EXIST; err = NLE_EXIST;
else if(err==ENOENT) else if (err == ENOENT)
err = NLE_OBJ_NOTFOUND; err = NLE_OBJ_NOTFOUND;
else if(err==ERANGE) else if (err == ERANGE)
err = NLE_RANGE; err = NLE_RANGE;
return -err; return -err;

View File

@@ -17,6 +17,7 @@
* *
* Copyright (C) 2011 Caixa Magica Software. * Copyright (C) 2011 Caixa Magica Software.
*/ */
#ifndef NM_NETLINK_COMPAT_H #ifndef NM_NETLINK_COMPAT_H
#define NM_NETLINK_COMPAT_H #define NM_NETLINK_COMPAT_H
@@ -53,7 +54,9 @@ struct nl_addr * rtnl_route_get_gateway(struct rtnl_route *);
/* libnl-2 API compatibility for libnl-3 */ /* libnl-2 API compatibility for libnl-3 */
#ifdef HAVE_LIBNL3 #ifdef HAVE_LIBNL3
static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { static inline int
__rtnl_link_alloc_cache (struct nl_sock *h, struct nl_cache **cache)
{
return rtnl_link_alloc_cache (h, AF_UNSPEC, cache); return rtnl_link_alloc_cache (h, AF_UNSPEC, cache);
} }
#define rtnl_link_alloc_cache __rtnl_link_alloc_cache #define rtnl_link_alloc_cache __rtnl_link_alloc_cache
@@ -73,64 +76,82 @@ static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **c
#define rtnl_route_set_priority rtnl_route_set_prio #define rtnl_route_set_priority rtnl_route_set_prio
/* auxiliary functions */ /* auxiliary functions */
int nl_compat_error(int); int nl_compat_error (int);
/* libnl-1.0 functions with modified prototypes in libnl-2/3*/ /* libnl-1.0 functions with modified prototypes in libnl-2/3*/
static inline const char * __nl_geterror(int err) static inline const char *
__nl_geterror (int err)
{ {
/* err is set, can be parsed */ /* err is set, can be parsed */
return nl_geterror(); return nl_geterror ();
} }
#define nl_geterror __nl_geterror #define nl_geterror __nl_geterror
static inline int __rtnl_addr_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { static inline int
*cache = rtnl_addr_alloc_cache(h); __rtnl_addr_alloc_cache (struct nl_sock *h, struct nl_cache **cache)
if(!*cache) {
return -ENOMEM; g_return_val_if_fail (cache != NULL, -EINVAL);
return 0;
*cache = rtnl_addr_alloc_cache (h);
return *cache ? 0 : -ENOMEM;
} }
#define rtnl_addr_alloc_cache __rtnl_addr_alloc_cache #define rtnl_addr_alloc_cache __rtnl_addr_alloc_cache
static inline int __rtnl_route_alloc_cache(struct nl_sock *h, int family, int flags, struct nl_cache **cache) { static inline int
*cache = rtnl_route_alloc_cache(h); __rtnl_route_alloc_cache (struct nl_sock *h, int family, int flags, struct nl_cache **cache)
if(!*cache) {
return -ENOMEM; g_return_val_if_fail (cache != NULL, -EINVAL);
return 0;
*cache = rtnl_route_alloc_cache (h);
return *cache ? 0 : -ENOMEM;
} }
#define rtnl_route_alloc_cache __rtnl_route_alloc_cache #define rtnl_route_alloc_cache __rtnl_route_alloc_cache
static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { static inline int
*cache = rtnl_link_alloc_cache (h); __rtnl_link_alloc_cache (struct nl_sock *h, struct nl_cache **cache)
if(!*cache) {
return -ENOMEM; g_return_val_if_fail (cache != NULL, -EINVAL);
return 0;
*cache = rtnl_link_alloc_cache (h);
return *cache ? 0 : -ENOMEM;
} }
#define rtnl_link_alloc_cache __rtnl_link_alloc_cache #define rtnl_link_alloc_cache __rtnl_link_alloc_cache
static inline int __rtnl_route_get_metric(struct rtnl_route * route, int metric, unsigned int *value) { static inline int
*value = rtnl_route_get_metric(route, metric); __rtnl_route_get_metric (struct rtnl_route *route, int metric, unsigned int *value)
{
g_return_val_if_fail (value != NULL, -EINVAL);
*value = rtnl_route_get_metric (route, metric);
return 0; return 0;
} }
#define rtnl_route_get_metric __rtnl_route_get_metric #define rtnl_route_get_metric __rtnl_route_get_metric
static inline int __rtnl_addr_add(struct nl_sock * h, struct rtnl_addr * addr, int flags) { static inline int
return nl_compat_error(rtnl_addr_add(h,addr,flags)); __rtnl_addr_add (struct nl_sock *h, struct rtnl_addr *addr, int flags)
{
return nl_compat_error (rtnl_addr_add (h, addr, flags));
} }
#define rtnl_addr_add __rtnl_addr_add #define rtnl_addr_add __rtnl_addr_add
static inline int rtnl_route_delete(struct nl_sock * h, struct rtnl_route * route, int flags) { static inline int
return nl_compat_error(rtnl_route_del(h, route, flags)); rtnl_route_delete (struct nl_sock *h, struct rtnl_route *route, int flags)
{
return nl_compat_error (rtnl_route_del (h, route, flags));
} }
#define rtnl_route_del rtnl_route_delete #define rtnl_route_del rtnl_route_delete
static inline int __rtnl_link_change(struct nl_sock * h, struct rtnl_link *old, struct rtnl_link * tmpl, int flags) { static inline int
return nl_compat_error(rtnl_link_change(h, old, tmpl,flags)); __rtnl_link_change (struct nl_sock *h, struct rtnl_link *old, struct rtnl_link *tmpl, int flags)
{
return nl_compat_error (rtnl_link_change (h, old, tmpl,flags));
} }
#define rtnl_link_change __rtnl_link_change #define rtnl_link_change __rtnl_link_change
static inline int __nl_cache_include(struct nl_cache * cache, struct nl_object * obj, change_func_t cb, void * data) static inline int
__nl_cache_include (struct nl_cache *cache, struct nl_object *obj, change_func_t cb, void *data)
{ {
return nl_cache_include(cache, obj, cb); return nl_cache_include (cache, obj, cb);
} }
#define nl_cache_include __nl_cache_include #define nl_cache_include __nl_cache_include

View File

@@ -227,7 +227,8 @@ event_handler (GIOChannel *channel,
g_return_val_if_fail (!(io_condition & ~EVENT_CONDITIONS), FALSE); g_return_val_if_fail (!(io_condition & ~EVENT_CONDITIONS), FALSE);
/* Process the netlink messages */ /* Process the netlink messages */
if ((err = nl_recvmsgs_default (priv->nlh_event)) < 0) { err = nl_recvmsgs_default (priv->nlh_event);
if (err < 0) {
error = g_error_new (NM_NETLINK_MONITOR_ERROR, error = g_error_new (NM_NETLINK_MONITOR_ERROR,
NM_NETLINK_MONITOR_ERROR_PROCESSING_MESSAGE, NM_NETLINK_MONITOR_ERROR_PROCESSING_MESSAGE,
_("error processing netlink message: %s"), _("error processing netlink message: %s"),
@@ -252,7 +253,8 @@ nlh_setup (struct nl_sock *nlh,
if (valid_func) if (valid_func)
nl_socket_modify_cb (nlh, NL_CB_VALID, NL_CB_CUSTOM, valid_func, cb_data); nl_socket_modify_cb (nlh, NL_CB_VALID, NL_CB_CUSTOM, valid_func, cb_data);
if ((err = nl_connect (nlh, NETLINK_ROUTE)) < 0) { err = nl_connect (nlh, NETLINK_ROUTE);
if (err < 0) {
g_set_error (error, NM_NETLINK_MONITOR_ERROR, g_set_error (error, NM_NETLINK_MONITOR_ERROR,
NM_NETLINK_MONITOR_ERROR_NETLINK_CONNECT, NM_NETLINK_MONITOR_ERROR_NETLINK_CONNECT,
_("unable to connect to netlink for monitoring link status: %s"), _("unable to connect to netlink for monitoring link status: %s"),
@@ -371,13 +373,13 @@ sync_connection_setup (NMNetlinkMonitor *self, GError **error)
* themselves, busting caching. * themselves, busting caching.
*/ */
rtnl_addr_alloc_cache (priv->nlh_sync, &addr_cache); rtnl_addr_alloc_cache (priv->nlh_sync, &addr_cache);
g_warn_if_fail (addr_cache != NULL);
nl_cache_get_ops (addr_cache)->co_obj_ops->oo_id_attrs &= ~0x80; nl_cache_get_ops (addr_cache)->co_obj_ops->oo_id_attrs &= ~0x80;
nl_cache_free (addr_cache); nl_cache_free (addr_cache);
#endif #endif
err = rtnl_link_alloc_cache (priv->nlh_sync, &priv->link_cache); err = rtnl_link_alloc_cache (priv->nlh_sync, &priv->link_cache);
if (err < 0) {
if (err) {
g_set_error (error, NM_NETLINK_MONITOR_ERROR, g_set_error (error, NM_NETLINK_MONITOR_ERROR,
NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_LINK_CACHE, NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_LINK_CACHE,
_("unable to allocate netlink link cache for monitoring link status: %s"), _("unable to allocate netlink link cache for monitoring link status: %s"),
@@ -503,7 +505,8 @@ nm_netlink_monitor_subscribe (NMNetlinkMonitor *self, int group, GError **error)
subs = get_subs (self, group) + 1; subs = get_subs (self, group) + 1;
if (subs == 1) { if (subs == 1) {
if ((err = nl_socket_add_membership (priv->nlh_event, group)) < 0) { err = nl_socket_add_membership (priv->nlh_event, group);
if (err < 0) {
g_set_error (error, NM_NETLINK_MONITOR_ERROR, g_set_error (error, NM_NETLINK_MONITOR_ERROR,
NM_NETLINK_MONITOR_ERROR_NETLINK_JOIN_GROUP, NM_NETLINK_MONITOR_ERROR_NETLINK_JOIN_GROUP,
_("unable to join netlink group: %s"), _("unable to join netlink group: %s"),
@@ -572,9 +575,10 @@ deferred_emit_carrier_state (gpointer user_data)
/* Update the link cache with latest state, and if there are no errors /* Update the link cache with latest state, and if there are no errors
* emit the link states for all the interfaces in the cache. * emit the link states for all the interfaces in the cache.
*/ */
if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { err = nl_cache_refill (priv->nlh_sync, priv->link_cache);
if (err < 0)
nm_log_err (LOGD_HW, "error updating link cache: %s", nl_geterror (err)); nm_log_err (LOGD_HW, "error updating link cache: %s", nl_geterror (err));
} else else
nl_cache_foreach_filter (priv->link_cache, NULL, link_msg_handler, self); nl_cache_foreach_filter (priv->link_cache, NULL, link_msg_handler, self);
return FALSE; return FALSE;
@@ -631,7 +635,8 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self,
priv = NM_NETLINK_MONITOR_GET_PRIVATE (self); priv = NM_NETLINK_MONITOR_GET_PRIVATE (self);
/* Update the link cache with the latest information */ /* Update the link cache with the latest information */
if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { err = nl_cache_refill (priv->nlh_sync, priv->link_cache);
if (err < 0) {
g_set_error (error, g_set_error (error,
NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR,
NM_NETLINK_MONITOR_ERROR_LINK_CACHE_UPDATE, NM_NETLINK_MONITOR_ERROR_LINK_CACHE_UPDATE,

View File

@@ -96,6 +96,6 @@ gboolean nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *monitor
int nm_netlink_iface_to_index (const char *iface); int nm_netlink_iface_to_index (const char *iface);
char * nm_netlink_index_to_iface (int idx); char * nm_netlink_index_to_iface (int idx);
struct rtnl_link *nm_netlink_index_to_rtnl_link (int idx); struct rtnl_link *nm_netlink_index_to_rtnl_link (int idx);
struct nl_sock *nm_netlink_get_default_handle (void); struct nl_sock * nm_netlink_get_default_handle (void);
#endif /* NM_NETLINK_MONITOR_H */ #endif /* NM_NETLINK_MONITOR_H */

View File

@@ -174,14 +174,14 @@ gboolean
nm_netlink_route_delete (struct rtnl_route *route) nm_netlink_route_delete (struct rtnl_route *route)
{ {
struct nl_sock *nlh; struct nl_sock *nlh;
int err=0; int err = 0;
g_return_val_if_fail (route != NULL, FALSE); g_return_val_if_fail (route != NULL, FALSE);
nlh = nm_netlink_get_default_handle (); nlh = nm_netlink_get_default_handle ();
err = rtnl_route_delete (nlh, route, 0); err = rtnl_route_delete (nlh, route, 0);
return ((err < 0) && (err != -NLE_RANGE)) ? FALSE: TRUE; return (err && (err != -NLE_RANGE)) ? FALSE : TRUE;
} }

View File

@@ -194,9 +194,8 @@ sync_addresses (int ifindex,
if (!nlh) if (!nlh)
return FALSE; return FALSE;
rtnl_addr_alloc_cache(nlh, &addr_cache); err = rtnl_addr_alloc_cache (nlh, &addr_cache);
if (err < 0)
if (!addr_cache)
return FALSE; return FALSE;
filter_addr = rtnl_addr_alloc (); filter_addr = rtnl_addr_alloc ();