]> git.proxmox.com Git - mirror_iproute2.git/blob - include/utils.h
man: tc-csum.8: Fix inconsistency in example description
[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 #include "libnetlink.h"
13 #include "ll_map.h"
14 #include "rtm_map.h"
15
16 extern int preferred_family;
17 extern int human_readable;
18 extern int use_iec;
19 extern int show_stats;
20 extern int show_details;
21 extern int show_raw;
22 extern int resolve_hosts;
23 extern int oneline;
24 extern int brief;
25 extern int json;
26 extern int timestamp;
27 extern int timestamp_short;
28 extern const char * _SL_;
29 extern int max_flush_loops;
30 extern int batch_mode;
31 extern bool do_all;
32
33 #ifndef CONFDIR
34 #define CONFDIR "/etc/iproute2"
35 #endif
36
37 #define SPRINT_BSIZE 64
38 #define SPRINT_BUF(x) char x[SPRINT_BSIZE]
39
40 void incomplete_command(void) __attribute__((noreturn));
41
42 #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
43 #define NEXT_ARG_OK() (argc - 1 > 0)
44 #define NEXT_ARG_FWD() do { argv++; argc--; } while(0)
45 #define PREV_ARG() do { argv--; argc++; } while(0)
46
47 typedef struct
48 {
49 __u16 flags;
50 __u16 bytelen;
51 __s16 bitlen;
52 /* These next two fields match rtvia */
53 __u16 family;
54 __u32 data[64];
55 } inet_prefix;
56
57 #define PREFIXLEN_SPECIFIED 1
58
59 #define DN_MAXADDL 20
60 #ifndef AF_DECnet
61 #define AF_DECnet 12
62 #endif
63
64 struct dn_naddr
65 {
66 unsigned short a_len;
67 unsigned char a_addr[DN_MAXADDL];
68 };
69
70 #define IPX_NODE_LEN 6
71
72 struct ipx_addr {
73 u_int32_t ipx_net;
74 u_int8_t ipx_node[IPX_NODE_LEN];
75 };
76
77 #ifndef AF_MPLS
78 # define AF_MPLS 28
79 #endif
80 #ifndef IPPROTO_MPLS
81 #define IPPROTO_MPLS 137
82 #endif
83
84 __u32 get_addr32(const char *name);
85 int get_addr_1(inet_prefix *dst, const char *arg, int family);
86 int get_prefix_1(inet_prefix *dst, char *arg, int family);
87 int get_addr(inet_prefix *dst, const char *arg, int family);
88 int get_prefix(inet_prefix *dst, char *arg, int family);
89 int mask2bits(__u32 netmask);
90 int get_addr_ila(__u64 *val, const char *arg);
91
92 int read_prop(const char *dev, char *prop, long *value);
93 int parse_percent(double *val, const char *str);
94 int get_hex(char c);
95 int get_integer(int *val, const char *arg, int base);
96 int get_unsigned(unsigned *val, const char *arg, int base);
97 int get_time_rtt(unsigned *val, const char *arg, int *raw);
98 #define get_byte get_u8
99 #define get_ushort get_u16
100 #define get_short get_s16
101 int get_u64(__u64 *val, const char *arg, int base);
102 int get_u32(__u32 *val, const char *arg, int base);
103 int get_s32(__s32 *val, const char *arg, int base);
104 int get_u16(__u16 *val, const char *arg, int base);
105 int get_s16(__s16 *val, const char *arg, int base);
106 int get_u8(__u8 *val, const char *arg, int base);
107 int get_s8(__s8 *val, const char *arg, int base);
108 int get_be64(__be64 *val, const char *arg, int base);
109 int get_be32(__be32 *val, const char *arg, int base);
110 int get_be16(__be16 *val, const char *arg, int base);
111 int get_addr64(__u64 *ap, const char *cp);
112
113 int hex2mem(const char *buf, uint8_t *mem, int count);
114 char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
115 __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len);
116 #define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
117 int addr64_n2a(__u64 addr, char *buff, size_t len);
118
119 int af_bit_len(int af);
120 int af_byte_len(int af);
121
122 const char *format_host_r(int af, int len, const void *addr,
123 char *buf, int buflen);
124 const char *format_host(int af, int lne, const void *addr);
125 #define format_host_rta(af, rta) \
126 format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
127 const char *rt_addr_n2a_r(int af, int len, const void *addr,
128 char *buf, int buflen);
129 const char *rt_addr_n2a(int af, int len, const void *addr);
130 #define rt_addr_n2a_rta(af, rta) \
131 rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
132
133 int read_family(const char *name);
134 const char *family_name(int family);
135
136 void missarg(const char *) __attribute__((noreturn));
137 void invarg(const char *, const char *) __attribute__((noreturn));
138 void duparg(const char *, const char *) __attribute__((noreturn));
139 void duparg2(const char *, const char *) __attribute__((noreturn));
140 int check_ifname(const char *);
141 int get_ifname(char *, const char *);
142 int matches(const char *arg, const char *pattern);
143 int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
144
145 const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
146 int dnet_pton(int af, const char *src, void *addr);
147
148 const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
149 int ipx_pton(int af, const char *src, void *addr);
150
151 const char *mpls_ntop(int af, const void *addr, char *str, size_t len);
152 int mpls_pton(int af, const char *src, void *addr, size_t alen);
153
154 extern int __iproute2_hz_internal;
155 int __get_hz(void);
156
157 static __inline__ int get_hz(void)
158 {
159 if (__iproute2_hz_internal == 0)
160 __iproute2_hz_internal = __get_hz();
161 return __iproute2_hz_internal;
162 }
163
164 extern int __iproute2_user_hz_internal;
165 int __get_user_hz(void);
166
167 static __inline__ int get_user_hz(void)
168 {
169 if (__iproute2_user_hz_internal == 0)
170 __iproute2_user_hz_internal = __get_user_hz();
171 return __iproute2_user_hz_internal;
172 }
173
174 static inline __u32 nl_mgrp(__u32 group)
175 {
176 if (group > 31 ) {
177 fprintf(stderr, "Use setsockopt for this group %d\n", group);
178 exit(-1);
179 }
180 return group ? (1 << (group - 1)) : 0;
181 }
182
183 /* courtesy of bridge-utils */
184 static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
185 {
186 unsigned long long jif;
187
188 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
189
190 return jif/10000;
191 }
192
193 static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
194 {
195 unsigned long long tvusec;
196
197 tvusec = 10000ULL*jiffies;
198 tv->tv_sec = tvusec/1000000;
199 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
200 }
201
202 void print_escape_buf(const __u8 *buf, size_t len, const char *escape);
203
204 int print_timestamp(FILE *fp);
205 void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
206
207 #define BIT(nr) (1UL << (nr))
208
209 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
210
211 #define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
212
213 #ifndef offsetof
214 # define offsetof(type, member) ((size_t) &((type *)0)->member)
215 #endif
216
217 #ifndef min
218 # define min(x, y) ({ \
219 typeof(x) _min1 = (x); \
220 typeof(y) _min2 = (y); \
221 (void) (&_min1 == &_min2); \
222 _min1 < _min2 ? _min1 : _min2; })
223 #endif
224
225 #ifndef __check_format_string
226 # define __check_format_string(pos_str, pos_args) \
227 __attribute__ ((format (printf, (pos_str), (pos_args))))
228 #endif
229
230 #define _textify(x) #x
231 #define textify(x) _textify(x)
232
233 #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
234 #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
235
236 extern int cmdlineno;
237 ssize_t getcmdline(char **line, size_t *len, FILE *in);
238 int makeargs(char *line, char *argv[], int maxargs);
239 int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6);
240
241 struct iplink_req {
242 struct nlmsghdr n;
243 struct ifinfomsg i;
244 char buf[1024];
245 };
246
247 int iplink_parse(int argc, char **argv, struct iplink_req *req,
248 char **name, char **type, char **link, char **dev,
249 int *group, int *index);
250
251 int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
252 bool show_label);
253
254 char *int_to_str(int val, char *buf);
255 int get_guid(__u64 *guid, const char *arg);
256 int get_real_family(int rtm_type, int rtm_family);
257
258 int cmd_exec(const char *cmd, char **argv, bool do_fork);
259 int make_path(const char *path, mode_t mode);
260 char *find_cgroup2_mount(void);
261 int get_command_name(const char *pid, char *comm, size_t len);
262
263 #ifdef NEED_STRLCPY
264 size_t strlcpy(char *dst, const char *src, size_t size);
265 size_t strlcat(char *dst, const char *src, size_t size);
266 #endif
267
268 #endif /* __UTILS_H__ */