osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | #ifndef __UTILS_H__ |
| 2 | #define __UTILS_H__ 1 |
| 3 | |
Strake | 5bd9dd4 | 2012-12-23 08:46:04 -0500 | [diff] [blame^] | 4 | #include <sys/types.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 5 | #include <asm/types.h> |
| 6 | #include <resolv.h> |
jamal | 56b9406 | 2007-02-25 12:02:23 -0500 | [diff] [blame] | 7 | #include <stdlib.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 8 | |
| 9 | #include "libnetlink.h" |
| 10 | #include "ll_map.h" |
| 11 | #include "rtm_map.h" |
| 12 | |
| 13 | extern int preferred_family; |
| 14 | extern int show_stats; |
| 15 | extern int show_details; |
| 16 | extern int show_raw; |
| 17 | extern int resolve_hosts; |
| 18 | extern int oneline; |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 19 | extern int timestamp; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | extern char * _SL_; |
Ben Greear | 64c7956 | 2010-12-01 11:13:51 -0800 | [diff] [blame] | 21 | extern int max_flush_loops; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 22 | |
| 23 | #ifndef IPPROTO_ESP |
| 24 | #define IPPROTO_ESP 50 |
| 25 | #endif |
| 26 | #ifndef IPPROTO_AH |
| 27 | #define IPPROTO_AH 51 |
| 28 | #endif |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 29 | #ifndef IPPROTO_COMP |
| 30 | #define IPPROTO_COMP 108 |
| 31 | #endif |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 32 | #ifndef IPSEC_PROTO_ANY |
| 33 | #define IPSEC_PROTO_ANY 255 |
| 34 | #endif |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 35 | |
| 36 | #define SPRINT_BSIZE 64 |
| 37 | #define SPRINT_BUF(x) char x[SPRINT_BSIZE] |
| 38 | |
| 39 | extern void incomplete_command(void) __attribute__((noreturn)); |
| 40 | |
| 41 | #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 42 | #define NEXT_ARG_OK() (argc - 1 > 0) |
| 43 | #define PREV_ARG() do { argv--; argc++; } while(0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | |
| 45 | typedef struct |
| 46 | { |
| 47 | __u8 family; |
| 48 | __u8 bytelen; |
| 49 | __s16 bitlen; |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 50 | __u32 flags; |
Olivier Fourdan | 7f71c0c | 2008-11-25 12:36:22 +0000 | [diff] [blame] | 51 | __u32 data[8]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | } inet_prefix; |
| 53 | |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 54 | #define PREFIXLEN_SPECIFIED 1 |
| 55 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 56 | #define DN_MAXADDL 20 |
| 57 | #ifndef AF_DECnet |
| 58 | #define AF_DECnet 12 |
| 59 | #endif |
| 60 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 61 | struct dn_naddr |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | { |
| 63 | unsigned short a_len; |
| 64 | unsigned char a_addr[DN_MAXADDL]; |
| 65 | }; |
| 66 | |
| 67 | #define IPX_NODE_LEN 6 |
| 68 | |
| 69 | struct ipx_addr { |
| 70 | u_int32_t ipx_net; |
| 71 | u_int8_t ipx_node[IPX_NODE_LEN]; |
| 72 | }; |
| 73 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 74 | extern __u32 get_addr32(const char *name); |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 75 | extern int get_addr_1(inet_prefix *dst, const char *arg, int family); |
osdl.net!shemminger | fb9b1d0 | 2005-02-07 18:15:04 +0000 | [diff] [blame] | 76 | extern int get_prefix_1(inet_prefix *dst, char *arg, int family); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 77 | extern int get_addr(inet_prefix *dst, const char *arg, int family); |
osdl.net!shemminger | fb9b1d0 | 2005-02-07 18:15:04 +0000 | [diff] [blame] | 78 | extern int get_prefix(inet_prefix *dst, char *arg, int family); |
Stephen Hemminger | 6b1ac65 | 2007-12-31 10:29:52 -0800 | [diff] [blame] | 79 | extern int mask2bits(__u32 netmask); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 81 | extern int get_integer(int *val, const char *arg, int base); |
| 82 | extern int get_unsigned(unsigned *val, const char *arg, int base); |
Gerrit Renker | 81d03dc | 2011-02-25 12:54:37 -0800 | [diff] [blame] | 83 | extern int get_time_rtt(unsigned *val, const char *arg, int *raw); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 84 | #define get_byte get_u8 |
| 85 | #define get_ushort get_u16 |
| 86 | #define get_short get_s16 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 87 | extern int get_u64(__u64 *val, const char *arg, int base); |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 88 | extern int get_u32(__u32 *val, const char *arg, int base); |
Hagen Paul Pfeifer | 30d10db | 2011-12-20 11:28:46 +0000 | [diff] [blame] | 89 | extern int get_s32(__s32 *val, const char *arg, int base); |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 90 | extern int get_u16(__u16 *val, const char *arg, int base); |
| 91 | extern int get_s16(__s16 *val, const char *arg, int base); |
| 92 | extern int get_u8(__u8 *val, const char *arg, int base); |
| 93 | extern int get_s8(__s8 *val, const char *arg, int base); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 94 | |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 95 | extern char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen); |
| 96 | extern __u8* hexstring_a2n(const char *str, __u8 *buf, int blen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 97 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 98 | extern const char *format_host(int af, int len, const void *addr, |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 99 | char *buf, int buflen); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 100 | extern const char *rt_addr_n2a(int af, int len, const void *addr, |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 101 | char *buf, int buflen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 102 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 103 | void missarg(const char *) __attribute__((noreturn)); |
osdl.org!shemminger | 04457b3 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 104 | void invarg(const char *, const char *) __attribute__((noreturn)); |
| 105 | void duparg(const char *, const char *) __attribute__((noreturn)); |
| 106 | void duparg2(const char *, const char *) __attribute__((noreturn)); |
| 107 | int matches(const char *arg, const char *pattern); |
| 108 | extern int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 109 | |
| 110 | const char *dnet_ntop(int af, const void *addr, char *str, size_t len); |
| 111 | int dnet_pton(int af, const char *src, void *addr); |
| 112 | |
| 113 | const char *ipx_ntop(int af, const void *addr, char *str, size_t len); |
| 114 | int ipx_pton(int af, const char *src, void *addr); |
| 115 | |
| 116 | extern int __iproute2_hz_internal; |
| 117 | extern int __get_hz(void); |
| 118 | |
| 119 | static __inline__ int get_hz(void) |
| 120 | { |
| 121 | if (__iproute2_hz_internal == 0) |
| 122 | __iproute2_hz_internal = __get_hz(); |
| 123 | return __iproute2_hz_internal; |
| 124 | } |
| 125 | |
net[shemminger]!shemminger | 5e8bc63 | 2005-03-14 18:44:54 +0000 | [diff] [blame] | 126 | extern int __iproute2_user_hz_internal; |
| 127 | extern int __get_user_hz(void); |
| 128 | |
| 129 | static __inline__ int get_user_hz(void) |
| 130 | { |
| 131 | if (__iproute2_user_hz_internal == 0) |
| 132 | __iproute2_user_hz_internal = __get_user_hz(); |
| 133 | return __iproute2_user_hz_internal; |
| 134 | } |
| 135 | |
jamal | 8cd09e6 | 2006-12-07 20:47:03 -0500 | [diff] [blame] | 136 | static inline __u32 nl_mgrp(__u32 group) |
| 137 | { |
jamal | 56b9406 | 2007-02-25 12:02:23 -0500 | [diff] [blame] | 138 | if (group > 31 ) { |
| 139 | fprintf(stderr, "Use setsockopt for this group %d\n", group); |
| 140 | exit(-1); |
| 141 | } |
| 142 | return group ? (1 << (group - 1)) : 0; |
jamal | 8cd09e6 | 2006-12-07 20:47:03 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 146 | int print_timestamp(FILE *fp); |
| 147 | |
shemminger | 02d2ae5 | 2005-06-23 17:31:27 +0000 | [diff] [blame] | 148 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 149 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 150 | extern int cmdlineno; |
Andreas Henriksson | d21e883 | 2007-10-12 10:56:42 +0200 | [diff] [blame] | 151 | extern ssize_t getcmdline(char **line, size_t *len, FILE *in); |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 152 | extern int makeargs(char *line, char *argv[], int maxargs); |
| 153 | |
Pavel Emelyanov | 909dfe2 | 2007-07-19 13:32:31 +0400 | [diff] [blame] | 154 | struct iplink_req; |
| 155 | int iplink_parse(int argc, char **argv, struct iplink_req *req, |
Vlad Dogaru | db02608 | 2011-02-02 20:23:41 +0200 | [diff] [blame] | 156 | char **name, char **type, char **link, char **dev, |
| 157 | int *group); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 158 | #endif /* __UTILS_H__ */ |