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