]> git.proxmox.com Git - mirror_iproute2.git/blob - include/utils.h
ip-rule: fix json key "to_tbl" for unspecific rule action
[mirror_iproute2.git] / include / utils.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __UTILS_H__
3 #define __UTILS_H__ 1
4
5 #include <sys/types.h>
6 #include <asm/types.h>
7 #include <resolv.h>
8 #include <stdlib.h>
9 #include <stdbool.h>
10 #include <time.h>
11
12 #ifdef HAVE_LIBBSD
13 #include <bsd/string.h>
14 #endif
15
16 #include "libnetlink.h"
17 #include "ll_map.h"
18 #include "rtm_map.h"
19 #include "json_print.h"
20
21 extern int preferred_family;
22 extern int human_readable;
23 extern int use_iec;
24 extern int show_stats;
25 extern int show_details;
26 extern int show_raw;
27 extern int resolve_hosts;
28 extern int oneline;
29 extern int brief;
30 extern int json;
31 extern int pretty;
32 extern int timestamp;
33 extern int timestamp_short;
34 extern const char * _SL_;
35 extern int max_flush_loops;
36 extern int batch_mode;
37 extern bool do_all;
38
39 #ifndef CONFDIR
40 #define CONFDIR "/etc/iproute2"
41 #endif
42
43 #define SPRINT_BSIZE 64
44 #define SPRINT_BUF(x) char x[SPRINT_BSIZE]
45
46 void incomplete_command(void) __attribute__((noreturn));
47
48 #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
49 #define NEXT_ARG_OK() (argc - 1 > 0)
50 #define NEXT_ARG_FWD() do { argv++; argc--; } while(0)
51 #define PREV_ARG() do { argv--; argc++; } while(0)
52
53 #define TIME_UNITS_PER_SEC 1000000
54 #define NSEC_PER_USEC 1000
55 #define NSEC_PER_MSEC 1000000
56 #define NSEC_PER_SEC 1000000000LL
57
58 typedef struct
59 {
60 __u16 flags;
61 __u16 bytelen;
62 __s16 bitlen;
63 /* These next two fields match rtvia */
64 __u16 family;
65 __u32 data[64];
66 } inet_prefix;
67
68 enum {
69 PREFIXLEN_SPECIFIED = (1 << 0),
70 ADDRTYPE_INET = (1 << 1),
71 ADDRTYPE_UNSPEC = (1 << 2),
72 ADDRTYPE_MULTI = (1 << 3),
73
74 ADDRTYPE_INET_UNSPEC = ADDRTYPE_INET | ADDRTYPE_UNSPEC,
75 ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI
76 };
77
78 static inline void inet_prefix_reset(inet_prefix *p)
79 {
80 p->flags = 0;
81 }
82
83 static inline bool is_addrtype_inet(const inet_prefix *p)
84 {
85 return p->flags & ADDRTYPE_INET;
86 }
87
88 static inline bool is_addrtype_inet_unspec(const inet_prefix *p)
89 {
90 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET_UNSPEC;
91 }
92
93 static inline bool is_addrtype_inet_multi(const inet_prefix *p)
94 {
95 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET_MULTI;
96 }
97
98 static inline bool is_addrtype_inet_not_unspec(const inet_prefix *p)
99 {
100 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET;
101 }
102
103 static inline bool is_addrtype_inet_not_multi(const inet_prefix *p)
104 {
105 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET;
106 }
107
108 #define DN_MAXADDL 20
109 #ifndef AF_DECnet
110 #define AF_DECnet 12
111 #endif
112
113 struct dn_naddr
114 {
115 unsigned short a_len;
116 unsigned char a_addr[DN_MAXADDL];
117 };
118
119 #ifndef AF_MPLS
120 # define AF_MPLS 28
121 #endif
122 #ifndef IPPROTO_MPLS
123 #define IPPROTO_MPLS 137
124 #endif
125
126 #ifndef CLOCK_TAI
127 # define CLOCK_TAI 11
128 #endif
129
130 __u32 get_addr32(const char *name);
131 int get_addr_1(inet_prefix *dst, const char *arg, int family);
132 int get_prefix_1(inet_prefix *dst, char *arg, int family);
133 int get_addr(inet_prefix *dst, const char *arg, int family);
134 int get_prefix(inet_prefix *dst, char *arg, int family);
135 int mask2bits(__u32 netmask);
136 int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family);
137 int get_addr_ila(__u64 *val, const char *arg);
138
139 int read_prop(const char *dev, char *prop, long *value);
140 int parse_percent(double *val, const char *str);
141 int get_hex(char c);
142 int get_integer(int *val, const char *arg, int base);
143 int get_unsigned(unsigned *val, const char *arg, int base);
144 int get_time_rtt(unsigned *val, const char *arg, int *raw);
145 #define get_byte get_u8
146 #define get_ushort get_u16
147 #define get_short get_s16
148 int get_s64(__s64 *val, const char *arg, int base);
149 int get_u64(__u64 *val, const char *arg, int base);
150 int get_u32(__u32 *val, const char *arg, int base);
151 int get_s32(__s32 *val, const char *arg, int base);
152 int get_u16(__u16 *val, const char *arg, int base);
153 int get_u8(__u8 *val, const char *arg, int base);
154 int get_be64(__be64 *val, const char *arg, int base);
155 int get_be32(__be32 *val, const char *arg, int base);
156 int get_be16(__be16 *val, const char *arg, int base);
157 int get_addr64(__u64 *ap, const char *cp);
158
159 int hex2mem(const char *buf, uint8_t *mem, int count);
160 char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
161 __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len);
162 #define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
163 int addr64_n2a(__u64 addr, char *buff, size_t len);
164
165 int af_bit_len(int af);
166
167 const char *format_host_r(int af, int len, const void *addr,
168 char *buf, int buflen);
169 #define format_host_rta_r(af, rta, buf, buflen) \
170 format_host_r(af, RTA_PAYLOAD(rta), RTA_DATA(rta), \
171 buf, buflen)
172
173 const char *format_host(int af, int lne, const void *addr);
174 #define format_host_rta(af, rta) \
175 format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
176 const char *rt_addr_n2a_r(int af, int len, const void *addr,
177 char *buf, int buflen);
178 const char *rt_addr_n2a(int af, int len, const void *addr);
179 #define rt_addr_n2a_rta(af, rta) \
180 rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
181
182 int read_family(const char *name);
183 const char *family_name(int family);
184
185 void missarg(const char *) __attribute__((noreturn));
186 void invarg(const char *, const char *) __attribute__((noreturn));
187 void duparg(const char *, const char *) __attribute__((noreturn));
188 void duparg2(const char *, const char *) __attribute__((noreturn));
189 int nodev(const char *dev);
190 int check_ifname(const char *);
191 int get_ifname(char *, const char *);
192 const char *get_ifname_rta(int ifindex, const struct rtattr *rta);
193 int matches(const char *arg, const char *pattern);
194 int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
195 int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta);
196
197 const char *mpls_ntop(int af, const void *addr, char *str, size_t len);
198 int mpls_pton(int af, const char *src, void *addr, size_t alen);
199
200 extern int __iproute2_hz_internal;
201 int __get_hz(void);
202
203 static __inline__ int get_hz(void)
204 {
205 if (__iproute2_hz_internal == 0)
206 __iproute2_hz_internal = __get_hz();
207 return __iproute2_hz_internal;
208 }
209
210 extern int __iproute2_user_hz_internal;
211 int __get_user_hz(void);
212
213 static __inline__ int get_user_hz(void)
214 {
215 if (__iproute2_user_hz_internal == 0)
216 __iproute2_user_hz_internal = __get_user_hz();
217 return __iproute2_user_hz_internal;
218 }
219
220 static inline __u32 nl_mgrp(__u32 group)
221 {
222 if (group > 31 ) {
223 fprintf(stderr, "Use setsockopt for this group %d\n", group);
224 exit(-1);
225 }
226 return group ? (1 << (group - 1)) : 0;
227 }
228
229 /* courtesy of bridge-utils */
230 static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
231 {
232 unsigned long long jif;
233
234 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
235
236 return jif/10000;
237 }
238
239 static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
240 {
241 unsigned long long tvusec;
242
243 tvusec = 10000ULL*jiffies;
244 tv->tv_sec = tvusec/1000000;
245 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
246 }
247
248 void print_escape_buf(const __u8 *buf, size_t len, const char *escape);
249
250 int print_timestamp(FILE *fp);
251 void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
252
253 unsigned int print_name_and_link(const char *fmt,
254 const char *name, struct rtattr *tb[]);
255
256 #define BIT(nr) (1UL << (nr))
257
258 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
259
260 #define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
261
262 #ifndef offsetof
263 # define offsetof(type, member) ((size_t) &((type *)0)->member)
264 #endif
265
266 #ifndef min
267 # define min(x, y) ({ \
268 typeof(x) _min1 = (x); \
269 typeof(y) _min2 = (y); \
270 (void) (&_min1 == &_min2); \
271 _min1 < _min2 ? _min1 : _min2; })
272 #endif
273
274 #ifndef __check_format_string
275 # define __check_format_string(pos_str, pos_args) \
276 __attribute__ ((format (printf, (pos_str), (pos_args))))
277 #endif
278
279 #define _textify(x) #x
280 #define textify(x) _textify(x)
281
282 #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
283 #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
284
285 extern int cmdlineno;
286 ssize_t getcmdline(char **line, size_t *len, FILE *in);
287 int makeargs(char *line, char *argv[], int maxargs);
288
289 int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
290 bool show_label);
291
292 char *int_to_str(int val, char *buf);
293 int get_guid(__u64 *guid, const char *arg);
294 int get_real_family(int rtm_type, int rtm_family);
295
296 int cmd_exec(const char *cmd, char **argv, bool do_fork);
297 int make_path(const char *path, mode_t mode);
298 char *find_cgroup2_mount(void);
299 int get_command_name(const char *pid, char *comm, size_t len);
300
301 int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64,
302 struct rtattr *tb[]);
303
304 #ifdef NEED_STRLCPY
305 size_t strlcpy(char *dst, const char *src, size_t size);
306 size_t strlcat(char *dst, const char *src, size_t size);
307 #endif
308
309 void drop_cap(void);
310
311 int get_time(unsigned int *time, const char *str);
312 int get_time64(__s64 *time, const char *str);
313 char *sprint_time(__u32 time, char *buf);
314 char *sprint_time64(__s64 time, char *buf);
315
316 #endif /* __UTILS_H__ */