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