]>
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; |
90f93024 | 23 | extern int timestamp; |
79aa79d0 | 24 | extern int timestamp_short; |
ec7aff5c | 25 | extern const char * _SL_; |
64c79560 | 26 | extern int max_flush_loops; |
a3aa47a5 | 27 | extern int batch_mode; |
b13ba03f | 28 | extern bool do_all; |
aba5acdf SH |
29 | |
30 | #ifndef IPPROTO_ESP | |
31 | #define IPPROTO_ESP 50 | |
32 | #endif | |
33 | #ifndef IPPROTO_AH | |
34 | #define IPPROTO_AH 51 | |
35 | #endif | |
c7699875 | 36 | #ifndef IPPROTO_COMP |
37 | #define IPPROTO_COMP 108 | |
38 | #endif | |
bd641cd6 | 39 | #ifndef IPSEC_PROTO_ANY |
40 | #define IPSEC_PROTO_ANY 255 | |
41 | #endif | |
aba5acdf SH |
42 | |
43 | #define SPRINT_BSIZE 64 | |
44 | #define SPRINT_BUF(x) char x[SPRINT_BSIZE] | |
45 | ||
892e2124 | 46 | void incomplete_command(void) __attribute__((noreturn)); |
aba5acdf SH |
47 | |
48 | #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0) | |
c7699875 | 49 | #define NEXT_ARG_OK() (argc - 1 > 0) |
faa8a463 | 50 | #define NEXT_ARG_FWD() do { argv++; argc--; } while(0) |
c7699875 | 51 | #define PREV_ARG() do { argv--; argc++; } while(0) |
aba5acdf SH |
52 | |
53 | typedef struct | |
54 | { | |
93ae2835 EB |
55 | __u16 flags; |
56 | __u16 bytelen; | |
aba5acdf | 57 | __s16 bitlen; |
93ae2835 EB |
58 | /* These next two fields match rtvia */ |
59 | __u16 family; | |
7f71c0ca | 60 | __u32 data[8]; |
aba5acdf SH |
61 | } inet_prefix; |
62 | ||
f082b64f | 63 | #define PREFIXLEN_SPECIFIED 1 |
64 | ||
aba5acdf SH |
65 | #define DN_MAXADDL 20 |
66 | #ifndef AF_DECnet | |
67 | #define AF_DECnet 12 | |
68 | #endif | |
69 | ||
ae665a52 | 70 | struct dn_naddr |
aba5acdf SH |
71 | { |
72 | unsigned short a_len; | |
73 | unsigned char a_addr[DN_MAXADDL]; | |
74 | }; | |
75 | ||
76 | #define IPX_NODE_LEN 6 | |
77 | ||
78 | struct ipx_addr { | |
79 | u_int32_t ipx_net; | |
80 | u_int8_t ipx_node[IPX_NODE_LEN]; | |
81 | }; | |
82 | ||
dacc5d41 EB |
83 | #ifndef AF_MPLS |
84 | # define AF_MPLS 28 | |
85 | #endif | |
86 | ||
87 | /* Maximum number of labels the mpls helpers support */ | |
88 | #define MPLS_MAX_LABELS 8 | |
89 | ||
892e2124 SH |
90 | __u32 get_addr32(const char *name); |
91 | int get_addr_1(inet_prefix *dst, const char *arg, int family); | |
92 | int get_prefix_1(inet_prefix *dst, char *arg, int family); | |
93 | int get_addr(inet_prefix *dst, const char *arg, int family); | |
94 | int get_prefix(inet_prefix *dst, char *arg, int family); | |
95 | int mask2bits(__u32 netmask); | |
aba5acdf | 96 | |
892e2124 SH |
97 | int get_integer(int *val, const char *arg, int base); |
98 | int get_unsigned(unsigned *val, const char *arg, int base); | |
99 | int get_time_rtt(unsigned *val, const char *arg, int *raw); | |
aba5acdf SH |
100 | #define get_byte get_u8 |
101 | #define get_ushort get_u16 | |
102 | #define get_short get_s16 | |
892e2124 SH |
103 | int get_u64(__u64 *val, const char *arg, int base); |
104 | int get_u32(__u32 *val, const char *arg, int base); | |
105 | int get_s32(__s32 *val, const char *arg, int base); | |
106 | int get_u16(__u16 *val, const char *arg, int base); | |
107 | int get_s16(__s16 *val, const char *arg, int base); | |
108 | int get_u8(__u8 *val, const char *arg, int base); | |
109 | int get_s8(__s8 *val, const char *arg, int base); | |
aba5acdf | 110 | |
892e2124 SH |
111 | char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen); |
112 | __u8* hexstring_a2n(const char *str, __u8 *buf, int blen); | |
aba5acdf | 113 | |
892e2124 SH |
114 | int af_bit_len(int af); |
115 | int af_byte_len(int af); | |
f3a2ddc1 | 116 | |
892e2124 | 117 | const char *format_host(int af, int len, const void *addr, |
04457b3d | 118 | char *buf, int buflen); |
892e2124 | 119 | const char *rt_addr_n2a(int af, int len, const void *addr, |
04457b3d | 120 | char *buf, int buflen); |
aba5acdf | 121 | |
892e2124 SH |
122 | int read_family(const char *name); |
123 | const char *family_name(int family); | |
45c90d19 | 124 | |
c7699875 | 125 | void missarg(const char *) __attribute__((noreturn)); |
04457b3d SH |
126 | void invarg(const char *, const char *) __attribute__((noreturn)); |
127 | void duparg(const char *, const char *) __attribute__((noreturn)); | |
128 | void duparg2(const char *, const char *) __attribute__((noreturn)); | |
129 | int matches(const char *arg, const char *pattern); | |
892e2124 | 130 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits); |
aba5acdf SH |
131 | |
132 | const char *dnet_ntop(int af, const void *addr, char *str, size_t len); | |
133 | int dnet_pton(int af, const char *src, void *addr); | |
134 | ||
135 | const char *ipx_ntop(int af, const void *addr, char *str, size_t len); | |
136 | int ipx_pton(int af, const char *src, void *addr); | |
137 | ||
dacc5d41 EB |
138 | const char *mpls_ntop(int af, const void *addr, char *str, size_t len); |
139 | int mpls_pton(int af, const char *src, void *addr); | |
140 | ||
aba5acdf | 141 | extern int __iproute2_hz_internal; |
e3c27c2d | 142 | int __get_hz(void); |
aba5acdf SH |
143 | |
144 | static __inline__ int get_hz(void) | |
145 | { | |
146 | if (__iproute2_hz_internal == 0) | |
147 | __iproute2_hz_internal = __get_hz(); | |
148 | return __iproute2_hz_internal; | |
149 | } | |
150 | ||
5e8bc631 | 151 | extern int __iproute2_user_hz_internal; |
892e2124 | 152 | int __get_user_hz(void); |
5e8bc631 | 153 | |
154 | static __inline__ int get_user_hz(void) | |
155 | { | |
156 | if (__iproute2_user_hz_internal == 0) | |
157 | __iproute2_user_hz_internal = __get_user_hz(); | |
158 | return __iproute2_user_hz_internal; | |
159 | } | |
160 | ||
8cd09e61 | 161 | static inline __u32 nl_mgrp(__u32 group) |
162 | { | |
56b94061 | 163 | if (group > 31 ) { |
164 | fprintf(stderr, "Use setsockopt for this group %d\n", group); | |
165 | exit(-1); | |
166 | } | |
167 | return group ? (1 << (group - 1)) : 0; | |
8cd09e61 | 168 | } |
169 | ||
170 | ||
90f93024 | 171 | int print_timestamp(FILE *fp); |
ddb1129b | 172 | void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n); |
90f93024 | 173 | |
02d2ae55 SH |
174 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
175 | ||
6256f8c9 DB |
176 | #define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) |
177 | ||
178 | #ifndef offsetof | |
179 | # define offsetof(type, member) ((size_t) &((type *)0)->member) | |
180 | #endif | |
181 | ||
182 | #ifndef min | |
183 | # define min(x, y) ({ \ | |
184 | typeof(x) _min1 = (x); \ | |
185 | typeof(y) _min2 = (y); \ | |
186 | (void) (&_min1 == &_min2); \ | |
187 | _min1 < _min2 ? _min1 : _min2; }) | |
188 | #endif | |
189 | ||
11c39b5e DB |
190 | #ifndef __check_format_string |
191 | # define __check_format_string(pos_str, pos_args) \ | |
192 | __attribute__ ((format (printf, (pos_str), (pos_args)))) | |
193 | #endif | |
194 | ||
32e93fb7 DB |
195 | #define _textify(x) #x |
196 | #define textify(x) _textify(x) | |
197 | ||
1e529305 RP |
198 | #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) |
199 | #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) | |
200 | ||
351efcde | 201 | extern int cmdlineno; |
892e2124 SH |
202 | ssize_t getcmdline(char **line, size_t *len, FILE *in); |
203 | int makeargs(char *line, char *argv[], int maxargs); | |
204 | int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6); | |
351efcde | 205 | |
909dfe2c PE |
206 | struct iplink_req; |
207 | int iplink_parse(int argc, char **argv, struct iplink_req *req, | |
db02608b | 208 | char **name, char **type, char **link, char **dev, |
5e25cf77 | 209 | int *group, int *index); |
ddb1129b | 210 | |
892e2124 | 211 | int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, |
e998e118 VK |
212 | bool show_label); |
213 | ||
b217df10 VK |
214 | char *int_to_str(int val, char *buf); |
215 | ||
aba5acdf | 216 | #endif /* __UTILS_H__ */ |