• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __UTILS_H__
2 #define __UTILS_H__ 1
3 
4 #include <asm/types.h>
5 #include <resolv.h>
6 #include <stdlib.h>
7 
8 #include "libnetlink.h"
9 #include "ll_map.h"
10 #include "rtm_map.h"
11 
12 extern int preferred_family;
13 extern int show_stats;
14 extern int show_details;
15 extern int show_raw;
16 extern int resolve_hosts;
17 extern int oneline;
18 extern int timestamp;
19 extern char * _SL_;
20 
21 #ifndef IPPROTO_ESP
22 #define IPPROTO_ESP	50
23 #endif
24 #ifndef IPPROTO_AH
25 #define IPPROTO_AH	51
26 #endif
27 #ifndef IPPROTO_COMP
28 #define IPPROTO_COMP	108
29 #endif
30 #ifndef IPSEC_PROTO_ANY
31 #define IPSEC_PROTO_ANY	255
32 #endif
33 
34 #define SPRINT_BSIZE 64
35 #define SPRINT_BUF(x)	char x[SPRINT_BSIZE]
36 
37 extern void incomplete_command(void) __attribute__((noreturn));
38 
39 #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
40 #define NEXT_ARG_OK() (argc - 1 > 0)
41 #define PREV_ARG() do { argv--; argc++; } while(0)
42 
43 typedef struct
44 {
45 	__u8 family;
46 	__u8 bytelen;
47 	__s16 bitlen;
48 	__u32 flags;
49 	__u32 data[8];
50 } inet_prefix;
51 
52 #define PREFIXLEN_SPECIFIED 1
53 
54 #define DN_MAXADDL 20
55 #ifndef AF_DECnet
56 #define AF_DECnet 12
57 #endif
58 
59 struct dn_naddr
60 {
61         unsigned short          a_len;
62         unsigned char a_addr[DN_MAXADDL];
63 };
64 
65 #define IPX_NODE_LEN 6
66 
67 struct ipx_addr {
68 	u_int32_t ipx_net;
69 	u_int8_t  ipx_node[IPX_NODE_LEN];
70 };
71 
72 extern __u32 get_addr32(const char *name);
73 extern int get_addr_1(inet_prefix *dst, const char *arg, int family);
74 extern int get_prefix_1(inet_prefix *dst, char *arg, int family);
75 extern int get_addr(inet_prefix *dst, const char *arg, int family);
76 extern int get_prefix(inet_prefix *dst, char *arg, int family);
77 extern int mask2bits(__u32 netmask);
78 
79 extern int get_integer(int *val, const char *arg, int base);
80 extern int get_unsigned(unsigned *val, const char *arg, int base);
81 extern int get_jiffies(unsigned *val, const char *arg, int base, int *raw);
82 #define get_byte get_u8
83 #define get_ushort get_u16
84 #define get_short get_s16
85 extern int get_u64(__u64 *val, const char *arg, int base);
86 extern int get_u32(__u32 *val, const char *arg, int base);
87 extern int get_u16(__u16 *val, const char *arg, int base);
88 extern int get_s16(__s16 *val, const char *arg, int base);
89 extern int get_u8(__u8 *val, const char *arg, int base);
90 extern int get_s8(__s8 *val, const char *arg, int base);
91 
92 extern char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
93 extern __u8* hexstring_a2n(const char *str, __u8 *buf, int blen);
94 
95 extern const char *format_host(int af, int len, const void *addr,
96 			       char *buf, int buflen);
97 extern const char *rt_addr_n2a(int af, int len, const void *addr,
98 			       char *buf, int buflen);
99 
100 void missarg(const char *) __attribute__((noreturn));
101 void invarg(const char *, const char *) __attribute__((noreturn));
102 void duparg(const char *, const char *) __attribute__((noreturn));
103 void duparg2(const char *, const char *) __attribute__((noreturn));
104 int matches(const char *arg, const char *pattern);
105 extern int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
106 
107 const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
108 int dnet_pton(int af, const char *src, void *addr);
109 
110 const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
111 int ipx_pton(int af, const char *src, void *addr);
112 
113 extern int __iproute2_hz_internal;
114 extern int __get_hz(void);
115 
get_hz(void)116 static __inline__ int get_hz(void)
117 {
118 	if (__iproute2_hz_internal == 0)
119 		__iproute2_hz_internal = __get_hz();
120 	return __iproute2_hz_internal;
121 }
122 
123 extern int __iproute2_user_hz_internal;
124 extern int __get_user_hz(void);
125 
get_user_hz(void)126 static __inline__ int get_user_hz(void)
127 {
128 	if (__iproute2_user_hz_internal == 0)
129 		__iproute2_user_hz_internal = __get_user_hz();
130 	return __iproute2_user_hz_internal;
131 }
132 
nl_mgrp(__u32 group)133 static inline __u32 nl_mgrp(__u32 group)
134 {
135 	if (group > 31 ) {
136 		fprintf(stderr, "Use setsockopt for this group %d\n", group);
137 		exit(-1);
138 	}
139 	return group ? (1 << (group - 1)) : 0;
140 }
141 
142 
143 int print_timestamp(FILE *fp);
144 
145 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
146 
147 extern int cmdlineno;
148 extern ssize_t getcmdline(char **line, size_t *len, FILE *in);
149 extern int makeargs(char *line, char *argv[], int maxargs);
150 
151 struct iplink_req;
152 int iplink_parse(int argc, char **argv, struct iplink_req *req,
153 		char **name, char **type, char **link, char **dev);
154 #endif /* __UTILS_H__ */
155