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