]> git.proxmox.com Git - mirror_iproute2.git/blob - include/utils.h
ip: add iec formatted option and cleanup code
[mirror_iproute2.git] / include / utils.h
1 #ifndef __UTILS_H__
2 #define __UTILS_H__ 1
3
4 #include <sys/types.h>
5 #include <asm/types.h>
6 #include <resolv.h>
7 #include <stdlib.h>
8
9 #include "libnetlink.h"
10 #include "ll_map.h"
11 #include "rtm_map.h"
12
13 extern int preferred_family;
14 extern int human_readable;
15 extern int use_iec;
16 extern int show_stats;
17 extern int show_details;
18 extern int show_raw;
19 extern int resolve_hosts;
20 extern int oneline;
21 extern int timestamp;
22 extern char * _SL_;
23 extern int max_flush_loops;
24 extern int batch_mode;
25
26 #ifndef IPPROTO_ESP
27 #define IPPROTO_ESP 50
28 #endif
29 #ifndef IPPROTO_AH
30 #define IPPROTO_AH 51
31 #endif
32 #ifndef IPPROTO_COMP
33 #define IPPROTO_COMP 108
34 #endif
35 #ifndef IPSEC_PROTO_ANY
36 #define IPSEC_PROTO_ANY 255
37 #endif
38
39 #define SPRINT_BSIZE 64
40 #define SPRINT_BUF(x) char x[SPRINT_BSIZE]
41
42 extern void incomplete_command(void) __attribute__((noreturn));
43
44 #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
45 #define NEXT_ARG_OK() (argc - 1 > 0)
46 #define PREV_ARG() do { argv--; argc++; } while(0)
47
48 typedef struct
49 {
50 __u8 family;
51 __u8 bytelen;
52 __s16 bitlen;
53 __u32 flags;
54 __u32 data[8];
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 extern __u32 get_addr32(const char *name);
78 extern int get_addr_1(inet_prefix *dst, const char *arg, int family);
79 extern int get_prefix_1(inet_prefix *dst, char *arg, int family);
80 extern int get_addr(inet_prefix *dst, const char *arg, int family);
81 extern int get_prefix(inet_prefix *dst, char *arg, int family);
82 extern int mask2bits(__u32 netmask);
83
84 extern int get_integer(int *val, const char *arg, int base);
85 extern int get_unsigned(unsigned *val, const char *arg, int base);
86 extern int get_time_rtt(unsigned *val, const char *arg, int *raw);
87 #define get_byte get_u8
88 #define get_ushort get_u16
89 #define get_short get_s16
90 extern int get_u64(__u64 *val, const char *arg, int base);
91 extern int get_u32(__u32 *val, const char *arg, int base);
92 extern int get_s32(__s32 *val, const char *arg, int base);
93 extern int get_u16(__u16 *val, const char *arg, int base);
94 extern int get_s16(__s16 *val, const char *arg, int base);
95 extern int get_u8(__u8 *val, const char *arg, int base);
96 extern int get_s8(__s8 *val, const char *arg, int base);
97
98 extern char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
99 extern __u8* hexstring_a2n(const char *str, __u8 *buf, int blen);
100
101 extern const char *format_host(int af, int len, const void *addr,
102 char *buf, int buflen);
103 extern const char *rt_addr_n2a(int af, const void *addr,
104 char *buf, int buflen);
105
106 void missarg(const char *) __attribute__((noreturn));
107 void invarg(const char *, const char *) __attribute__((noreturn));
108 void duparg(const char *, const char *) __attribute__((noreturn));
109 void duparg2(const char *, const char *) __attribute__((noreturn));
110 int matches(const char *arg, const char *pattern);
111 extern int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
112
113 const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
114 int dnet_pton(int af, const char *src, void *addr);
115
116 const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
117 int ipx_pton(int af, const char *src, void *addr);
118
119 extern int __iproute2_hz_internal;
120 extern int __get_hz(void);
121
122 static __inline__ int get_hz(void)
123 {
124 if (__iproute2_hz_internal == 0)
125 __iproute2_hz_internal = __get_hz();
126 return __iproute2_hz_internal;
127 }
128
129 extern int __iproute2_user_hz_internal;
130 extern int __get_user_hz(void);
131
132 static __inline__ int get_user_hz(void)
133 {
134 if (__iproute2_user_hz_internal == 0)
135 __iproute2_user_hz_internal = __get_user_hz();
136 return __iproute2_user_hz_internal;
137 }
138
139 static inline __u32 nl_mgrp(__u32 group)
140 {
141 if (group > 31 ) {
142 fprintf(stderr, "Use setsockopt for this group %d\n", group);
143 exit(-1);
144 }
145 return group ? (1 << (group - 1)) : 0;
146 }
147
148
149 int print_timestamp(FILE *fp);
150
151 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
152
153 extern int cmdlineno;
154 extern ssize_t getcmdline(char **line, size_t *len, FILE *in);
155 extern int makeargs(char *line, char *argv[], int maxargs);
156 extern int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6);
157
158 struct iplink_req;
159 int iplink_parse(int argc, char **argv, struct iplink_req *req,
160 char **name, char **type, char **link, char **dev,
161 int *group, int *index);
162 #endif /* __UTILS_H__ */