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