2 * lib/route/rtnl.c Routing Netlink
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
13 * @defgroup rtnl Routing Netlink
17 #include <netlink-local.h>
18 #include <netlink/netlink.h>
19 #include <netlink/utils.h>
20 #include <netlink/route/rtnl.h>
28 * Send routing netlink request message
29 * @arg handle Netlink handle.
30 * @arg type Netlink message type.
31 * @arg family Address family.
32 * @arg flags Additional netlink message flags.
34 * Fills out a routing netlink request message and sends it out
35 * using nl_send_simple().
37 * @return 0 on success or a negative error code.
39 int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
41 struct rtgenmsg gmsg = {
42 .rtgen_family = family,
45 return nl_send_simple(handle, type, flags, &gmsg, sizeof(gmsg));
51 * @name Routing Type Translations
55 static struct trans_tbl rtntypes[] = {
56 __ADD(RTN_UNSPEC,unspec)
57 __ADD(RTN_UNICAST,unicast)
58 __ADD(RTN_LOCAL,local)
59 __ADD(RTN_BROADCAST,broadcast)
60 __ADD(RTN_ANYCAST,anycast)
61 __ADD(RTN_MULTICAST,multicast)
62 __ADD(RTN_BLACKHOLE,blackhole)
63 __ADD(RTN_UNREACHABLE,unreachable)
64 __ADD(RTN_PROHIBIT,prohibit)
65 __ADD(RTN_THROW,throw)
67 __ADD(RTN_XRESOLVE,xresolve)
71 * Convert routing type to character string.
72 * @arg type Routing type.
73 * @arg buf Destination buffer.
74 * @arg size Size of destination buffer.
76 * Converts a routing type to a character string and stores it in
77 * the specified destination buffer.
79 * @return The destination buffer or the type encoded in hexidecimal
80 * form if the routing type is unknown.
82 char *nl_rtntype2str(int type, char *buf, size_t size)
84 return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
88 * Convert character string to routing type.
89 * @arg name Name of routing type.
91 * Converts the provided character string specifying a routing
92 * type to the corresponding numeric value.
94 * @return Routing type or a negative value if no match was found.
96 int nl_str2rtntype(const char *name)
98 return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
104 * @name Scope Translations
108 static struct trans_tbl scopes[] = {
117 * Convert scope identifier to character string.
118 * @arg scope Scope identifier.
119 * @arg buf Destination buffer
120 * @arg size Size of destination buffer.
122 * Converts a scope identifier to a character string and stores it in
123 * the specified destination buffer.
125 * @return The destination buffer or the type encoded in hexidecimal
126 * form if the scope identifier is unknown.
128 char *rtnl_scope2str(int scope, char *buf, size_t size)
130 return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
134 * Convert character string to scope identifier.
135 * @arg name Name of scope.
137 * Converts the provided character string specifying a scope identifier
138 * to the corresponding numeric value.
140 * @return Scope identifier or a negative value if no match was found.
142 int rtnl_str2scope(const char *name)
144 return __str2type(name, scopes, ARRAY_SIZE(scopes));
150 * @name Realms Translations
154 char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
156 int from = RTNL_REALM_FROM(realms);
157 int to = RTNL_REALM_TO(realms);
159 snprintf(buf, len, "%d/%d", from, to);