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#include <stdbool.h> 9 10#include "libnetlink.h" 11#include "ll_map.h" 12#include "rtm_map.h" 13 14extern int preferred_family; 15extern int human_readable; 16extern int use_iec; 17extern int show_stats; 18extern int show_details; 19extern int show_raw; 20extern int resolve_hosts; 21extern int oneline; 22extern int brief; 23extern int timestamp; 24extern int timestamp_short; 25extern const char * _SL_; 26extern int max_flush_loops; 27extern int batch_mode; 28extern bool do_all; 29 30#ifndef IPPROTO_ESP 31#define IPPROTO_ESP 50 32#endif 33#ifndef IPPROTO_AH 34#define IPPROTO_AH 51 35#endif 36#ifndef IPPROTO_COMP 37#define IPPROTO_COMP 108 38#endif 39#ifndef IPSEC_PROTO_ANY 40#define IPSEC_PROTO_ANY 255 41#endif 42 43#ifndef CONFDIR 44#define CONFDIR "/etc/iproute2" 45#endif 46 47#define SPRINT_BSIZE 64 48#define SPRINT_BUF(x) char x[SPRINT_BSIZE] 49 50void incomplete_command(void) __attribute__((noreturn)); 51 52#define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0) 53#define NEXT_ARG_OK() (argc - 1 > 0) 54#define NEXT_ARG_FWD() do { argv++; argc--; } while(0) 55#define PREV_ARG() do { argv--; argc++; } while(0) 56 57typedef struct 58{ 59 __u16 flags; 60 __u16 bytelen; 61 __s16 bitlen; 62 /* These next two fields match rtvia */ 63 __u16 family; 64 __u32 data[8]; 65} inet_prefix; 66 67#define PREFIXLEN_SPECIFIED 1 68 69#define DN_MAXADDL 20 70#ifndef AF_DECnet 71#define AF_DECnet 12 72#endif 73 74struct dn_naddr 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 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 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); 100int get_addr_ila(__u64 *val, const char *arg); 101 102int get_integer(int *val, const char *arg, int base); 103int get_unsigned(unsigned *val, const char *arg, int base); 104int get_time_rtt(unsigned *val, const char *arg, int *raw); 105#define get_byte get_u8 106#define get_ushort get_u16 107#define get_short get_s16 108int get_u64(__u64 *val, const char *arg, int base); 109int get_u32(__u32 *val, const char *arg, int base); 110int get_s32(__s32 *val, const char *arg, int base); 111int get_u16(__u16 *val, const char *arg, int base); 112int get_s16(__s16 *val, const char *arg, int base); 113int get_u8(__u8 *val, const char *arg, int base); 114int get_s8(__s8 *val, const char *arg, int base); 115int get_addr64(__u64 *ap, const char *cp); 116 117char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen); 118__u8* hexstring_a2n(const char *str, __u8 *buf, int blen); 119#define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx") 120int addr64_n2a(__u64 addr, char *buff, size_t len); 121 122int af_bit_len(int af); 123int af_byte_len(int af); 124 125const char *format_host(int af, int len, const void *addr, 126 char *buf, int buflen); 127const char *rt_addr_n2a(int af, int len, const void *addr, 128 char *buf, int buflen); 129 130int read_family(const char *name); 131const char *family_name(int family); 132 133void missarg(const char *) __attribute__((noreturn)); 134void invarg(const char *, const char *) __attribute__((noreturn)); 135void duparg(const char *, const char *) __attribute__((noreturn)); 136void duparg2(const char *, const char *) __attribute__((noreturn)); 137int matches(const char *arg, const char *pattern); 138int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits); 139 140const char *dnet_ntop(int af, const void *addr, char *str, size_t len); 141int dnet_pton(int af, const char *src, void *addr); 142 143const char *ipx_ntop(int af, const void *addr, char *str, size_t len); 144int ipx_pton(int af, const char *src, void *addr); 145 146const char *mpls_ntop(int af, const void *addr, char *str, size_t len); 147int mpls_pton(int af, const char *src, void *addr); 148 149extern int __iproute2_hz_internal; 150int __get_hz(void); 151 152static __inline__ int get_hz(void) 153{ 154 if (__iproute2_hz_internal == 0) 155 __iproute2_hz_internal = __get_hz(); 156 return __iproute2_hz_internal; 157} 158 159extern int __iproute2_user_hz_internal; 160int __get_user_hz(void); 161 162static __inline__ int get_user_hz(void) 163{ 164 if (__iproute2_user_hz_internal == 0) 165 __iproute2_user_hz_internal = __get_user_hz(); 166 return __iproute2_user_hz_internal; 167} 168 169static inline __u32 nl_mgrp(__u32 group) 170{ 171 if (group > 31 ) { 172 fprintf(stderr, "Use setsockopt for this group %d\n", group); 173 exit(-1); 174 } 175 return group ? (1 << (group - 1)) : 0; 176} 177 178 179int print_timestamp(FILE *fp); 180void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n); 181 182#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 183 184#define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) 185 186#ifndef offsetof 187# define offsetof(type, member) ((size_t) &((type *)0)->member) 188#endif 189 190#ifndef min 191# define min(x, y) ({ \ 192 typeof(x) _min1 = (x); \ 193 typeof(y) _min2 = (y); \ 194 (void) (&_min1 == &_min2); \ 195 _min1 < _min2 ? _min1 : _min2; }) 196#endif 197 198#ifndef __check_format_string 199# define __check_format_string(pos_str, pos_args) \ 200 __attribute__ ((format (printf, (pos_str), (pos_args)))) 201#endif 202 203#define _textify(x) #x 204#define textify(x) _textify(x) 205 206#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) 207#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) 208 209extern int cmdlineno; 210ssize_t getcmdline(char **line, size_t *len, FILE *in); 211int makeargs(char *line, char *argv[], int maxargs); 212int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6); 213 214struct iplink_req; 215int iplink_parse(int argc, char **argv, struct iplink_req *req, 216 char **name, char **type, char **link, char **dev, 217 int *group, int *index); 218 219int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, 220 bool show_label); 221 222char *int_to_str(int val, char *buf); 223 224#endif /* __UTILS_H__ */ 225