oops
[libnl.git] / lib / route / rtnl.c
1 /*
2  * lib/route/rtnl.c             Routing Netlink
3  *
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
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 /**
13  * @defgroup rtnl Routing Netlink
14  * @{
15  */
16
17 #include <netlink-local.h>
18 #include <netlink/netlink.h>
19 #include <netlink/utils.h>
20 #include <netlink/route/rtnl.h>
21
22 /**
23  * @name Sending
24  * @{
25  */
26
27 /**
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.
33  *
34  * Fills out a routing netlink request message and sends it out
35  * using nl_send_simple().
36  *
37  * @return 0 on success or a negative error code.
38  */
39 int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
40 {
41         struct rtgenmsg gmsg = {
42                 .rtgen_family = family,
43         };
44
45         return nl_send_simple(handle, type, flags, &gmsg, sizeof(gmsg));
46 }
47
48 /** @} */
49
50 /**
51  * @name Routing Type Translations
52  * @{
53  */
54
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)
66         __ADD(RTN_NAT,nat)
67         __ADD(RTN_XRESOLVE,xresolve)
68 };
69
70 /**
71  * Convert routing type to character string.
72  * @arg type            Routing type.
73  * @arg buf             Destination buffer.
74  * @arg size            Size of destination buffer.
75  *
76  * Converts a routing type to a character string and stores it in
77  * the specified destination buffer.
78  *
79  * @return The destination buffer or the type encoded in hexidecimal
80  *         form if the routing type is unknown.
81  */
82 char *nl_rtntype2str(int type, char *buf, size_t size)
83 {
84         return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
85 }
86
87 /**
88  * Convert character string to routing type.
89  * @arg name            Name of routing type.
90  *
91  * Converts the provided character string specifying a routing
92  * type to the corresponding numeric value.
93  *
94  * @return Routing type or a negative value if no match was found.
95  */
96 int nl_str2rtntype(const char *name)
97 {
98         return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
99 }
100
101 /** @} */
102
103 /**
104  * @name Scope Translations
105  * @{
106  */
107
108 static struct trans_tbl scopes[] = {
109         __ADD(255,nowhere)
110         __ADD(254,host)
111         __ADD(253,link)
112         __ADD(200,site)
113         __ADD(0,global)
114 };
115
116 /**
117  * Convert scope identifier to character string.
118  * @arg scope           Scope identifier.
119  * @arg buf             Destination buffer
120  * @arg size            Size of destination buffer.
121  *
122  * Converts a scope identifier to a character string and stores it in
123  * the specified destination buffer.
124  *
125  * @return The destination buffer or the type encoded in hexidecimal
126  *         form if the scope identifier is unknown.
127  */
128 char *rtnl_scope2str(int scope, char *buf, size_t size)
129 {
130         return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
131 }
132
133 /**
134  * Convert character string to scope identifier.
135  * @arg name            Name of scope.
136  *
137  * Converts the provided character string specifying a scope identifier
138  * to the corresponding numeric value.
139  *
140  * @return Scope identifier or a negative value if no match was found.
141  */
142 int rtnl_str2scope(const char *name)
143 {
144         return __str2type(name, scopes, ARRAY_SIZE(scopes));
145 }
146
147 /** @} */
148
149 /**
150  * @name Realms Translations
151  * @{
152  */
153
154 char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
155 {
156         int from = RTNL_REALM_FROM(realms);
157         int to = RTNL_REALM_TO(realms);
158
159         snprintf(buf, len, "%d/%d", from, to);
160
161         return buf;
162 }
163
164 /** @} */
165
166
167
168 /** @} */