libnl 3.7.0
rtnl.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @defgroup rtnl Routing Library (libnl-route)
8 * @{
9 */
10
11#include <netlink-private/netlink.h>
12#include <netlink/netlink.h>
13#include <netlink/utils.h>
14#include <netlink/route/rtnl.h>
15
16/**
17 * @name Sending
18 * @{
19 */
20
21/**
22 * Send routing netlink request message
23 * @arg sk Netlink socket.
24 * @arg type Netlink message type.
25 * @arg family Address family.
26 * @arg flags Additional netlink message flags.
27 *
28 * Fills out a routing netlink request message and sends it out
29 * using nl_send_simple().
30 *
31 * @return 0 on success or a negative error code. Due to a bug in older
32 * version of the library, this function returned the number of bytes sent.
33 * Treat any non-negative number as success.
34 */
35int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
36{
37 int err;
38 struct rtgenmsg gmsg = {
39 .rtgen_family = family,
40 };
41
42 err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));
43
44 return err >= 0 ? 0 : err;
45}
46
47/** @} */
48
49/**
50 * @name Routing Type Translations
51 * @{
52 */
53
54static const struct trans_tbl rtntypes[] = {
55 __ADD(RTN_UNSPEC,unspec),
56 __ADD(RTN_UNICAST,unicast),
57 __ADD(RTN_LOCAL,local),
58 __ADD(RTN_BROADCAST,broadcast),
59 __ADD(RTN_ANYCAST,anycast),
60 __ADD(RTN_MULTICAST,multicast),
61 __ADD(RTN_BLACKHOLE,blackhole),
62 __ADD(RTN_UNREACHABLE,unreachable),
63 __ADD(RTN_PROHIBIT,prohibit),
64 __ADD(RTN_THROW,throw),
65 __ADD(RTN_NAT,nat),
66 __ADD(RTN_XRESOLVE,xresolve),
67};
68
69char *nl_rtntype2str(int type, char *buf, size_t size)
70{
71 return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
72}
73
74int nl_str2rtntype(const char *name)
75{
76 return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
77}
78
79/** @} */
80
81/**
82 * @name Scope Translations
83 * @{
84 */
85
86static const struct trans_tbl scopes[] = {
87 __ADD(255,nowhere),
88 __ADD(254,host),
89 __ADD(253,link),
90 __ADD(200,site),
91 __ADD(0,global),
92};
93
94char *rtnl_scope2str(int scope, char *buf, size_t size)
95{
96 return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
97}
98
99int rtnl_str2scope(const char *name)
100{
101 return __str2type(name, scopes, ARRAY_SIZE(scopes));
102}
103
104/** @} */
105
106/**
107 * @name Realms Translations
108 * @{
109 */
110
111char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
112{
113 int from = RTNL_REALM_FROM(realms);
114 int to = RTNL_REALM_TO(realms);
115
116 snprintf(buf, len, "%d/%d", from, to);
117
118 return buf;
119}
120
121/** @} */
122
123/** @} */
#define RTNL_REALM_TO(realm)
Extract TO realm from a realms field.
Definition: rtnl.h:34
int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
Send routing netlink request message.
Definition: rtnl.c:35
#define RTNL_REALM_FROM(realm)
Extract FROM realm from a realms field.
Definition: rtnl.h:29
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition: nl.c:574