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