libnl 1.1.4
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 * @ingroup nlfam
14 * @defgroup rtnl Routing Netlink
15 * @{
16 */
17
18#include <netlink-local.h>
19#include <netlink/netlink.h>
20#include <netlink/utils.h>
21#include <netlink/route/rtnl.h>
22
23/**
24 * @name Sending
25 * @{
26 */
27
28/**
29 * Send routing netlink request message
30 * @arg handle Netlink handle.
31 * @arg type Netlink message type.
32 * @arg family Address family.
33 * @arg flags Additional netlink message flags.
34 *
35 * Fills out a routing netlink request message and sends it out
36 * using nl_send_simple().
37 *
38 * @return 0 on success or a negative error code.
39 */
40int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
41{
42 struct rtgenmsg gmsg = {
43 .rtgen_family = family,
44 };
45
46 return nl_send_simple(handle, type, flags, &gmsg, sizeof(gmsg));
47}
48
49/** @} */
50
51/**
52 * @name Routing Type Translations
53 * @{
54 */
55
56static struct trans_tbl rtntypes[] = {
57 __ADD(RTN_UNSPEC,unspec)
58 __ADD(RTN_UNICAST,unicast)
59 __ADD(RTN_LOCAL,local)
60 __ADD(RTN_BROADCAST,broadcast)
61 __ADD(RTN_ANYCAST,anycast)
62 __ADD(RTN_MULTICAST,multicast)
63 __ADD(RTN_BLACKHOLE,blackhole)
64 __ADD(RTN_UNREACHABLE,unreachable)
65 __ADD(RTN_PROHIBIT,prohibit)
66 __ADD(RTN_THROW,throw)
67 __ADD(RTN_NAT,nat)
68 __ADD(RTN_XRESOLVE,xresolve)
69};
70
71char *nl_rtntype2str(int type, char *buf, size_t size)
72{
73 return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
74}
75
76int nl_str2rtntype(const char *name)
77{
78 return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
79}
80
81/** @} */
82
83/**
84 * @name Scope Translations
85 * @{
86 */
87
88static struct trans_tbl scopes[] = {
89 __ADD(255,nowhere)
90 __ADD(254,host)
91 __ADD(253,link)
92 __ADD(200,site)
93 __ADD(0,universe)
94};
95
96char *rtnl_scope2str(int scope, char *buf, size_t size)
97{
98 return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
99}
100
101int rtnl_str2scope(const char *name)
102{
103 return __str2type(name, scopes, ARRAY_SIZE(scopes));
104}
105
106/** @} */
107
108/**
109 * @name Realms Translations
110 * @{
111 */
112
113char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
114{
115 int from = RTNL_REALM_FROM(realms);
116 int to = RTNL_REALM_TO(realms);
117
118 snprintf(buf, len, "%d/%d", from, to);
119
120 return buf;
121}
122
123/** @} */
124
125/** @} */
int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf, size_t size)
Send simple netlink message using nl_send_auto_complete()
Definition: nl.c:410
#define RTNL_REALM_TO(realm)
Extract TO realm from a realms field.
Definition: rtnl.h:42
int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
Send routing netlink request message.
Definition: rtnl.c:40
#define RTNL_REALM_FROM(realm)
Extract FROM realm from a realms field.
Definition: rtnl.h:37