"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * Changes: |
| 8 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 9 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 10 | */ |
Rob Landley | ecae66a | 2006-06-02 20:53:38 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 12 | #include "utils.h" |
"Vladimir N. Oleynik" | 007a011 | 2005-09-22 11:11:11 +0000 | [diff] [blame] | 13 | #include "inet_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 14 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 15 | unsigned FAST_FUNC get_hz(void) |
Curt Brune | 6993470 | 2015-10-14 12:53:47 +0200 | [diff] [blame] | 16 | { |
| 17 | static unsigned hz_internal; |
| 18 | FILE *fp; |
| 19 | |
| 20 | if (hz_internal) |
| 21 | return hz_internal; |
| 22 | |
| 23 | fp = fopen_for_read("/proc/net/psched"); |
| 24 | if (fp) { |
| 25 | unsigned nom, denom; |
| 26 | |
| 27 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
| 28 | if (nom == 1000000) |
| 29 | hz_internal = denom; |
| 30 | fclose(fp); |
| 31 | } |
| 32 | if (!hz_internal) |
| 33 | hz_internal = bb_clk_tck(); |
| 34 | return hz_internal; |
| 35 | } |
| 36 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 37 | unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | { |
| 39 | unsigned long res; |
| 40 | char *ptr; |
| 41 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 42 | if (*arg) { |
| 43 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 44 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 45 | if (!*ptr && res <= UINT_MAX) { |
| 46 | return res; |
| 47 | } |
| 48 | } |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 49 | invarg_1_to_2(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 52 | uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | { |
| 54 | unsigned long res; |
| 55 | char *ptr; |
| 56 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 57 | if (*arg) { |
| 58 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 59 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 60 | if (!*ptr && res <= 0xFFFFFFFFUL) { |
| 61 | return res; |
| 62 | } |
| 63 | } |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 64 | invarg_1_to_2(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 67 | uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned long res; |
| 70 | char *ptr; |
| 71 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 72 | if (*arg) { |
| 73 | res = strtoul(arg, &ptr, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 74 | //FIXME: "" will be accepted too, is it correct?! |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 75 | if (!*ptr && res <= 0xFFFF) { |
| 76 | return res; |
| 77 | } |
| 78 | } |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 79 | invarg_1_to_2(arg, errmsg); /* does not return */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 82 | int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 83 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 84 | memset(addr, 0, sizeof(*addr)); |
| 85 | |
Denys Vlasenko | 0004e99 | 2010-09-01 12:01:17 +0200 | [diff] [blame] | 86 | if (strcmp(name, "default") == 0 |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 87 | || strcmp(name, "all") == 0 |
| 88 | || strcmp(name, "any") == 0 |
| 89 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | addr->family = family; |
| 91 | addr->bytelen = (family == AF_INET6 ? 16 : 4); |
| 92 | addr->bitlen = -1; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | if (strchr(name, ':')) { |
| 97 | addr->family = AF_INET6; |
| 98 | if (family != AF_UNSPEC && family != AF_INET6) |
| 99 | return -1; |
| 100 | if (inet_pton(AF_INET6, name, addr->data) <= 0) |
| 101 | return -1; |
| 102 | addr->bytelen = 16; |
| 103 | addr->bitlen = -1; |
| 104 | return 0; |
| 105 | } |
| 106 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | if (family != AF_UNSPEC && family != AF_INET) |
| 108 | return -1; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 109 | |
| 110 | /* Try to parse it as IPv4 */ |
| 111 | addr->family = AF_INET; |
| 112 | #if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */ |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 113 | if (inet_pton(AF_INET, name, addr->data) <= 0) |
| 114 | return -1; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 115 | #else |
| 116 | { |
| 117 | unsigned i = 0; |
| 118 | unsigned n = 0; |
| 119 | const char *cp = name - 1; |
| 120 | while (*++cp) { |
| 121 | if ((unsigned char)(*cp - '0') <= 9) { |
| 122 | n = 10 * n + (unsigned char)(*cp - '0'); |
| 123 | if (n >= 256) |
| 124 | return -1; |
| 125 | ((uint8_t*)addr->data)[i] = n; |
| 126 | continue; |
| 127 | } |
| 128 | if (*cp == '.' && ++i <= 3) { |
| 129 | n = 0; |
| 130 | continue; |
| 131 | } |
| 132 | return -1; |
| 133 | } |
| 134 | } |
| 135 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 136 | addr->bytelen = 4; |
| 137 | addr->bitlen = -1; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 138 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 142 | static void get_prefix_1(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 143 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 144 | char *slash; |
| 145 | |
| 146 | memset(dst, 0, sizeof(*dst)); |
| 147 | |
Denys Vlasenko | 0004e99 | 2010-09-01 12:01:17 +0200 | [diff] [blame] | 148 | if (strcmp(arg, "default") == 0 |
Denis Vlasenko | 787a492 | 2009-03-03 14:55:29 +0000 | [diff] [blame] | 149 | || strcmp(arg, "all") == 0 |
| 150 | || strcmp(arg, "any") == 0 |
| 151 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 152 | dst->family = family; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 153 | /*dst->bytelen = 0; - done by memset */ |
| 154 | /*dst->bitlen = 0;*/ |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 155 | return; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | slash = strchr(arg, '/'); |
| 159 | if (slash) |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 160 | *slash = '\0'; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 161 | |
| 162 | if (get_addr_1(dst, arg, family) == 0) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 163 | dst->bitlen = (dst->family == AF_INET6) ? 128 : 32; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 164 | if (slash) { |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 165 | unsigned plen; |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 166 | inet_prefix netmask_pfx; |
| 167 | |
| 168 | netmask_pfx.family = AF_UNSPEC; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 169 | plen = bb_strtou(slash + 1, NULL, 0); |
| 170 | if ((errno || plen > dst->bitlen) |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 171 | && get_addr_1(&netmask_pfx, slash + 1, family) != 0 |
| 172 | ) { |
| 173 | goto bad; |
| 174 | } |
| 175 | if (netmask_pfx.family == AF_INET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 176 | /* fill in prefix length of dotted quad */ |
| 177 | uint32_t mask = ntohl(netmask_pfx.data[0]); |
| 178 | uint32_t host = ~mask; |
| 179 | |
| 180 | /* a valid netmask must be 2^n - 1 */ |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 181 | if (host & (host + 1)) |
| 182 | goto bad; |
| 183 | |
| 184 | for (plen = 0; mask; mask <<= 1) |
| 185 | ++plen; |
| 186 | if (plen > dst->bitlen) |
| 187 | goto bad; |
| 188 | /* dst->flags |= PREFIXLEN_SPECIFIED; */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 189 | } |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 190 | dst->bitlen = plen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 193 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | if (slash) |
| 195 | *slash = '/'; |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 196 | return; |
| 197 | bad: |
| 198 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 201 | int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | { |
| 203 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 204 | bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 205 | } |
| 206 | if (get_addr_1(dst, arg, family)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 207 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 212 | void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 213 | { |
| 214 | if (family == AF_PACKET) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 215 | bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 216 | } |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 217 | get_prefix_1(dst, arg, family); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 220 | uint32_t FAST_FUNC get_addr32(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | { |
| 222 | inet_prefix addr; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 223 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 224 | if (get_addr_1(&addr, name, AF_INET)) { |
Bernhard Reutner-Fischer | 8fbd8ac | 2008-10-21 12:42:45 +0000 | [diff] [blame] | 225 | bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 226 | } |
| 227 | return addr.data[0]; |
| 228 | } |
| 229 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 230 | char** FAST_FUNC next_arg(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 231 | { |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 232 | if (!*++argv) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame^] | 233 | bb_simple_error_msg_and_die("command line is not complete, try \"help\""); |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 234 | return argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 237 | void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | { |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 239 | bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 242 | void FAST_FUNC duparg(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 244 | bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 247 | void FAST_FUNC duparg2(const char *key, const char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 248 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 249 | bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 252 | int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 253 | { |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 254 | const uint32_t *a1 = a->data; |
| 255 | const uint32_t *a2 = b->data; |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 256 | int words = bits >> 5; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | |
| 258 | bits &= 0x1f; |
| 259 | |
| 260 | if (words) |
| 261 | if (memcmp(a1, a2, words << 2)) |
| 262 | return -1; |
| 263 | |
| 264 | if (bits) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 265 | uint32_t w1, w2; |
| 266 | uint32_t mask; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 267 | |
| 268 | w1 = a1[words]; |
| 269 | w2 = a2[words]; |
| 270 | |
| 271 | mask = htonl((0xffffffff) << (0x20 - bits)); |
| 272 | |
| 273 | if ((w1 ^ w2) & mask) |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 280 | const char* FAST_FUNC rt_addr_n2a(int af, void *addr) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 281 | { |
| 282 | switch (af) { |
| 283 | case AF_INET: |
| 284 | case AF_INET6: |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 285 | return inet_ntop(af, addr, |
| 286 | auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN |
| 287 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 288 | default: |
| 289 | return "???"; |
| 290 | } |
| 291 | } |
| 292 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 293 | #ifdef RESOLVE_HOSTNAMES |
Denys Vlasenko | 826bdcd | 2017-04-07 17:15:40 +0200 | [diff] [blame] | 294 | const char* FAST_FUNC format_host(int af, int len, void *addr) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 295 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 296 | if (resolve_hosts) { |
| 297 | struct hostent *h_ent; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 298 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 299 | if (len <= 0) { |
| 300 | switch (af) { |
| 301 | case AF_INET: |
| 302 | len = 4; |
| 303 | break; |
| 304 | case AF_INET6: |
| 305 | len = 16; |
| 306 | break; |
Glenn L McGrath | 50c00f4 | 2002-11-18 07:26:42 +0000 | [diff] [blame] | 307 | default:; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
Denis Vlasenko | 8c1aaf3 | 2007-06-04 21:03:51 +0000 | [diff] [blame] | 310 | if (len > 0) { |
| 311 | h_ent = gethostbyaddr(addr, len, af); |
| 312 | if (h_ent != NULL) { |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 313 | return auto_string(xstrdup(h_ent->h_name)); |
Denis Vlasenko | 8c1aaf3 | 2007-06-04 21:03:51 +0000 | [diff] [blame] | 314 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
Denys Vlasenko | 926d801 | 2015-10-14 13:56:42 +0200 | [diff] [blame] | 317 | return rt_addr_n2a(af, addr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 318 | } |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 319 | #endif |