void tools_delete_router_address (const gchar *address) { struct nl_sock *sock = NULL; struct nl_addr *dst = NULL; struct nl_addr *gw = NULL; struct rtnl_nexthop *nhop = NULL; struct rtnl_route *route = NULL; sock = nl_socket_alloc (); nl_connect (sock, NETLINK_ROUTE); nhop = rtnl_route_nh_alloc (); nl_addr_parse (address, AF_INET, &gw); rtnl_route_nh_set_gateway (nhop, gw); rtnl_route_nh_set_flags (nhop, 0); route = rtnl_route_alloc (); rtnl_route_set_family (route, AF_INET); nl_addr_parse ("default", AF_INET, &dst); rtnl_route_add_nexthop (route, nhop); if (rtnl_route_delete (sock, route, NLM_F_CREATE | NLM_F_REPLACE) != 0) g_warning (_("Failed to delete default route.")); nl_socket_free (sock); rtnl_route_put (route); nl_addr_put (dst); nl_addr_put (gw); }
int rtnl_route_set_gateway (struct rtnl_route * route, struct nl_addr * gw_addr) { struct rtnl_nexthop * nh; nh = nm_netlink_get_nh(route); g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND); rtnl_route_nh_set_gateway(nh, gw_addr); return 0; }
TError TNlLink::SetDefaultGw(const TNlAddr &addr) { struct rtnl_route *route; struct rtnl_nexthop *nh; TError error; TNlAddr all; int ret; error = all.Parse(addr.Family(), "default"); if (error) return error; route = rtnl_route_alloc(); if (!route) return TError(EError::Unknown, "Unable to allocate route"); ret = rtnl_route_set_dst(route, all.Addr); if (ret < 0) { rtnl_route_put(route); return Error(ret, "Cannot set route destination"); } nh = rtnl_route_nh_alloc(); if (!route) { rtnl_route_put(route); return TError(EError::Unknown, "Unable to allocate next hop"); } rtnl_route_nh_set_gateway(nh, addr.Addr); rtnl_route_nh_set_ifindex(nh, GetIndex()); rtnl_route_add_nexthop(route, nh); Dump("add", route); ret = rtnl_route_add(GetSock(), route, NLM_F_MATCH); rtnl_route_put(route); if (ret < 0) return Error(ret, "Cannot set default gateway"); return TError::Success(); }