1
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#include <stdint.h>
12
13#ifdef HAVE_LIBBSD
14#include <bsd/string.h>
15#endif
16
17#include "libnetlink.h"
18#include "ll_map.h"
19#include "rtm_map.h"
20#include "json_print.h"
21
22extern int preferred_family;
23extern int human_readable;
24extern int show_stats;
25extern int show_details;
26extern int show_raw;
27extern int resolve_hosts;
28extern int oneline;
29extern int brief;
30extern int json;
31extern int pretty;
32extern int timestamp;
33extern int timestamp_short;
34extern const char * _SL_;
35extern int max_flush_loops;
36extern int batch_mode;
37extern int numeric;
38extern bool do_all;
39extern int echo_request;
40extern int use_iec;
41
42#ifndef CONF_USR_DIR
43#define CONF_USR_DIR "/usr/lib/iproute2"
44#endif
45#ifndef CONF_ETC_DIR
46#define CONF_ETC_DIR "/etc/iproute2"
47#endif
48
49#define SPRINT_BSIZE 64
50#define SPRINT_BUF(x) char x[SPRINT_BSIZE]
51
52void incomplete_command(void) __attribute__((noreturn));
53
54#define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
55#define NEXT_ARG_OK() (argc - 1 > 0)
56#define NEXT_ARG_FWD() do { argv++; argc--; } while(0)
57#define PREV_ARG() do { argv--; argc++; } while(0)
58
59
60#define MAX_ARGS 512
61
62#define TIME_UNITS_PER_SEC 1000000
63#define NSEC_PER_USEC 1000
64#define NSEC_PER_MSEC 1000000
65#define NSEC_PER_SEC 1000000000LL
66
67typedef struct
68{
69 __u16 flags;
70 __u16 bytelen;
71 __s16 bitlen;
72
73 __u16 family;
74 __u32 data[64];
75} inet_prefix;
76
77enum {
78 PREFIXLEN_SPECIFIED = (1 << 0),
79 ADDRTYPE_INET = (1 << 1),
80 ADDRTYPE_UNSPEC = (1 << 2),
81 ADDRTYPE_MULTI = (1 << 3),
82
83 ADDRTYPE_INET_UNSPEC = ADDRTYPE_INET | ADDRTYPE_UNSPEC,
84 ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI
85};
86
87static inline void inet_prefix_reset(inet_prefix *p)
88{
89 p->flags = 0;
90}
91
92static inline bool is_addrtype_inet(const inet_prefix *p)
93{
94 return p->flags & ADDRTYPE_INET;
95}
96
97static inline bool is_addrtype_inet_unspec(const inet_prefix *p)
98{
99 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET_UNSPEC;
100}
101
102static inline bool is_addrtype_inet_multi(const inet_prefix *p)
103{
104 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET_MULTI;
105}
106
107static inline bool is_addrtype_inet_not_unspec(const inet_prefix *p)
108{
109 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET;
110}
111
112static inline bool is_addrtype_inet_not_multi(const inet_prefix *p)
113{
114 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET;
115}
116
117#ifndef AF_MPLS
118# define AF_MPLS 28
119#endif
120#ifndef IPPROTO_MPLS
121#define IPPROTO_MPLS 137
122#endif
123
124#ifndef CLOCK_TAI
125# define CLOCK_TAI 11
126#endif
127
128#ifndef AF_XDP
129# define AF_XDP 44
130# if AF_MAX < 45
131# undef AF_MAX
132# define AF_MAX 45
133# endif
134#endif
135
136__u32 get_addr32(const char *name);
137int get_addr_1(inet_prefix *dst, const char *arg, int family);
138int get_prefix_1(inet_prefix *dst, char *arg, int family);
139int get_addr(inet_prefix *dst, const char *arg, int family);
140int get_prefix(inet_prefix *dst, char *arg, int family);
141int mask2bits(__u32 netmask);
142int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family);
143int get_addr_ila(__u64 *val, const char *arg);
144
145int read_prop(const char *dev, char *prop, long *value);
146int get_long(long *val, const char *arg, int base);
147int get_integer(int *val, const char *arg, int base);
148int get_unsigned(unsigned *val, const char *arg, int base);
149int get_time_rtt(unsigned *val, const char *arg, int *raw);
150#define get_byte get_u8
151#define get_ushort get_u16
152#define get_short get_s16
153int get_s64(__s64 *val, const char *arg, int base);
154int get_u64(__u64 *val, const char *arg, int base);
155int get_u32(__u32 *val, const char *arg, int base);
156int get_s32(__s32 *val, const char *arg, int base);
157int get_u16(__u16 *val, const char *arg, int base);
158int get_u8(__u8 *val, const char *arg, int base);
159int get_be64(__be64 *val, const char *arg, int base);
160int get_be32(__be32 *val, const char *arg, int base);
161int get_be16(__be16 *val, const char *arg, int base);
162int get_addr64(__u64 *ap, const char *cp);
163int get_rate(unsigned int *rate, const char *str);
164int get_rate64(__u64 *rate, const char *str);
165int get_size(unsigned int *size, const char *str);
166
167int hex2mem(const char *buf, uint8_t *mem, int count);
168char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
169__u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len);
170#define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
171int addr64_n2a(__u64 addr, char *buff, size_t len);
172
173int af_bit_len(int af);
174
175const char *format_host_r(int af, int len, const void *addr,
176 char *buf, int buflen);
177#define format_host_rta_r(af, rta, buf, buflen) \
178 format_host_r(af, RTA_PAYLOAD(rta), RTA_DATA(rta), \
179 buf, buflen)
180
181const char *format_host(int af, int lne, const void *addr);
182#define format_host_rta(af, rta) \
183 format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
184const char *rt_addr_n2a_r(int af, int len, const void *addr,
185 char *buf, int buflen);
186const char *rt_addr_n2a(int af, int len, const void *addr);
187#define rt_addr_n2a_rta(af, rta) \
188 rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
189
190int read_family(const char *name);
191const char *family_name(int family);
192
193void missarg(const char *) __attribute__((noreturn));
194void invarg(const char *, const char *) __attribute__((noreturn));
195void duparg(const char *, const char *) __attribute__((noreturn));
196void duparg2(const char *, const char *) __attribute__((noreturn));
197int nodev(const char *dev);
198int check_ifname(const char *);
199int check_altifname(const char *name);
200int get_ifname(char *, const char *);
201const char *get_ifname_rta(int ifindex, const struct rtattr *rta);
202int matches(const char *prefix, const char *string);
203int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
204int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta);
205
206const char *ax25_ntop(int af, const void *addr, char *str, socklen_t len);
207
208const char *rose_ntop(int af, const void *addr, char *buf, socklen_t buflen);
209
210const char *mpls_ntop(int af, const void *addr, char *str, size_t len);
211int mpls_pton(int af, const char *src, void *addr, size_t alen);
212
213const char *netrom_ntop(int af, const void *addr, char *str, socklen_t len);
214
215extern int __iproute2_hz_internal;
216int __get_hz(void);
217
218static __inline__ int get_hz(void)
219{
220 if (__iproute2_hz_internal == 0)
221 __iproute2_hz_internal = __get_hz();
222 return __iproute2_hz_internal;
223}
224
225extern int __iproute2_user_hz_internal;
226int __get_user_hz(void);
227
228static __inline__ int get_user_hz(void)
229{
230 if (__iproute2_user_hz_internal == 0)
231 __iproute2_user_hz_internal = __get_user_hz();
232 return __iproute2_user_hz_internal;
233}
234
235static inline __u32 nl_mgrp(__u32 group)
236{
237 if (group > 31 ) {
238 fprintf(stderr, "Use setsockopt for this group %d\n", group);
239 exit(-1);
240 }
241 return group ? (1 << (group - 1)) : 0;
242}
243
244
245static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
246{
247 unsigned long long jif;
248
249 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
250
251 return jif/10000;
252}
253
254static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
255{
256 unsigned long long tvusec;
257
258 tvusec = 10000ULL*jiffies;
259 tv->tv_sec = tvusec/1000000;
260 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
261}
262
263void print_escape_buf(const __u8 *buf, size_t len, const char *escape);
264
265int print_timestamp(FILE *fp);
266void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
267
268unsigned int print_name_and_link(const char *fmt,
269 const char *name, struct rtattr *tb[])
270 __attribute__((format(printf, 1, 0)));
271
272
273#define BIT(nr) (UINT64_C(1) << (nr))
274
275#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
276
277#define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
278
279#ifndef offsetof
280# define offsetof(type, member) ((size_t) &((type *)0)->member)
281#endif
282
283#ifndef min
284# define min(x, y) ({ \
285 typeof(x) _min1 = (x); \
286 typeof(y) _min2 = (y); \
287 (void) (&_min1 == &_min2); \
288 _min1 < _min2 ? _min1 : _min2; })
289#endif
290
291#ifndef max
292# define max(x, y) ({ \
293 typeof(x) _max1 = (x); \
294 typeof(y) _max2 = (y); \
295 (void) (&_max1 == &_max2); \
296 _max1 < _max2 ? _max2 : _max1; })
297#endif
298
299#ifndef __check_format_string
300# define __check_format_string(pos_str, pos_args) \
301 __attribute__ ((format (printf, (pos_str), (pos_args))))
302#endif
303
304#define _textify(x) #x
305#define textify(x) _textify(x)
306
307#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
308#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
309
310extern int cmdlineno;
311
312char *int_to_str(int val, char *buf);
313char *uint_to_str(unsigned int val, char *buf);
314int get_guid(__u64 *guid, const char *arg);
315int get_real_family(int rtm_type, int rtm_family);
316
317int cmd_exec(const char *cmd, char **argv, bool do_fork,
318 int (*setup)(void *), void *arg);
319int make_path(const char *path, mode_t mode);
320char *find_cgroup2_mount(bool do_mount);
321__u64 get_cgroup2_id(const char *path);
322char *get_cgroup2_path(__u64 id, bool full);
323int get_command_name(const char *pid, char *comm, size_t len);
324int get_task_name(pid_t pid, char *name, size_t len);
325
326int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64,
327 struct rtattr *tb[]);
328
329#ifdef NEED_STRLCPY
330size_t strlcpy(char *dst, const char *src, size_t size);
331size_t strlcat(char *dst, const char *src, size_t size);
332#endif
333
334void drop_cap(void);
335
336int get_time(unsigned int *time, const char *str);
337int get_time64(__s64 *time, const char *str);
338char *sprint_time(__u32 time, char *buf);
339char *sprint_time64(__s64 time, char *buf);
340void print_num(FILE *fp, unsigned int width, uint64_t count);
341
342int do_batch(const char *name, bool force,
343 int (*cmd)(int argc, char *argv[], void *user), void *user);
344
345int parse_one_of(const char *msg, const char *realval, const char * const *list,
346 size_t len, int *p_err);
347int parse_one_of_deprecated(const char *msg, const char *realval,
348 const char * const *list,
349 size_t len, int *p_err);
350bool parse_on_off(const char *msg, const char *realval, int *p_err);
351
352int parse_mapping_num_all(__u32 *keyp, const char *key);
353int parse_mapping_gen(int *argcp, char ***argvp,
354 int (*key_cb)(__u32 *keyp, const char *key),
355 int (*mapping_cb)(__u32 key, char *value, void *data),
356 void *mapping_cb_data);
357int parse_mapping(int *argcp, char ***argvp, bool allow_all,
358 int (*mapping_cb)(__u32 key, char *value, void *data),
359 void *mapping_cb_data);
360
361struct str_num_map {
362 const char *str;
363 unsigned int num;
364};
365
366int str_map_lookup_str(const struct str_num_map *map, const char *needle);
367const char *str_map_lookup_uint(const struct str_num_map *map,
368 unsigned int val);
369const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
370const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
371
372unsigned int get_str_char_count(const char *str, int match);
373int str_split_by_char(char *str, char **before, char **after, int match);
374
375#define INDENT_STR_MAXLEN 32
376
377struct indent_mem {
378 int indent_level;
379 char indent_str[INDENT_STR_MAXLEN + 1];
380};
381
382struct indent_mem *alloc_indent_mem(void);
383void free_indent_mem(struct indent_mem *mem);
384void inc_indent(struct indent_mem *mem);
385void dec_indent(struct indent_mem *mem);
386void print_indent(struct indent_mem *mem);
387
388struct proto {
389 int id;
390 const char *name;
391};
392
393int proto_a2n(unsigned short *id, const char *buf,
394 const struct proto *proto_tb, size_t tb_len);
395const char *proto_n2a(unsigned short id, char *buf, int len,
396 const struct proto *proto_tb, size_t tb_len);
397
398FILE *generic_proc_open(const char *env, const char *name);
399
400int open_fds_add(int fd);
401void open_fds_close(void);
402
403#endif
404