| 1 |
/* |
| 2 |
* $OpenBSD: route.c,v 1.66 2004/11/17 01:47:20 itojun Exp $ |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $ |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* Copyright (c) 1983, 1988, 1993 |
| 10 |
* The Regents of the University of California. All rights reserved. |
| 11 |
* |
| 12 |
* Redistribution and use in source and binary forms, with or without |
| 13 |
* modification, are permitted provided that the following conditions |
| 14 |
* are met: |
| 15 |
* 1. Redistributions of source code must retain the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer. |
| 17 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer in the |
| 19 |
* documentation and/or other materials provided with the distribution. |
| 20 |
* 3. Neither the name of the University nor the names of its contributors |
| 21 |
* may be used to endorse or promote products derived from this software |
| 22 |
* without specific prior written permission. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 |
* SUCH DAMAGE. |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <net-snmp/net-snmp-config.h> |
| 38 |
|
| 39 |
#ifdef HAVE_SYS_TYPES_H |
| 40 |
#include <sys/types.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef HAVE_NETINET_IN_H |
| 44 |
#include <netinet/in.h> |
| 45 |
#endif |
| 46 |
#ifdef HAVE_ARPA_INET_H |
| 47 |
#include <arpa/inet.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include <net-snmp/net-snmp-includes.h> |
| 51 |
|
| 52 |
#ifdef HAVE_UNISTD_H |
| 53 |
#include <unistd.h> |
| 54 |
#endif |
| 55 |
#ifdef HAVE_NETDB_H |
| 56 |
#include <netdb.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef INET |
| 60 |
#define INET |
| 61 |
#endif |
| 62 |
|
| 63 |
#include "main.h" |
| 64 |
#include "netstat.h" |
| 65 |
#include "ffs.h" |
| 66 |
#ifdef HAVE_WINSOCK_H |
| 67 |
#include "winstub.h" |
| 68 |
#endif |
| 69 |
|
| 70 |
#define MAX_IFNAME 64 |
| 71 |
|
| 72 |
#define SET_MASK 0x01 |
| 73 |
#define SET_GWAY 0x02 |
| 74 |
#define SET_IFNO 0x04 |
| 75 |
#define SET_TYPE 0x08 |
| 76 |
#define SET_PRTO 0x10 |
| 77 |
#define SET_ALL 0x1f |
| 78 |
|
| 79 |
struct route_entry { |
| 80 |
in_addr_t destination; |
| 81 |
in_addr_t mask; |
| 82 |
in_addr_t gateway; |
| 83 |
int ifNumber; |
| 84 |
int type; |
| 85 |
int proto; |
| 86 |
int af; |
| 87 |
int set_bits; |
| 88 |
char ifname[MAX_IFNAME]; |
| 89 |
}; |
| 90 |
|
| 91 |
void p_rtnode(struct route_entry *rp); |
| 92 |
|
| 93 |
/* |
| 94 |
* Print routing tables. |
| 95 |
*/ |
| 96 |
void |
| 97 |
routepr(void) |
| 98 |
{ |
| 99 |
struct route_entry route, *rp = &route; |
| 100 |
oid rtcol_oid[] = { 1, 3, 6, 1, 2, 1, 4, 21, 1, 0 }; |
| 101 |
size_t rtcol_len = OID_LENGTH(rtcol_oid); |
| 102 |
union { |
| 103 |
in_addr_t addr; |
| 104 |
char data[4]; |
| 105 |
} tmpAddr; |
| 106 |
netsnmp_variable_list *var = NULL, *vp; |
| 107 |
char *cp; |
| 108 |
|
| 109 |
printf("Routing tables (ipRouteTable)\n"); |
| 110 |
pr_rthdr(AF_INET); |
| 111 |
|
| 112 |
#define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \ |
| 113 |
snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL, 0) |
| 114 |
ADD_RTVAR(2); /* ipRouteIfIndex */ |
| 115 |
ADD_RTVAR(7); /* ipRouteNextHop */ |
| 116 |
ADD_RTVAR(8); /* ipRouteType */ |
| 117 |
ADD_RTVAR(9); /* ipRouteProto */ |
| 118 |
ADD_RTVAR(11); /* ipRouteMask */ |
| 119 |
#undef ADD_RTVAR |
| 120 |
|
| 121 |
/* |
| 122 |
* Now walk the ipRouteTable, reporting the various route entries |
| 123 |
*/ |
| 124 |
while (1) { |
| 125 |
if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR) |
| 126 |
break; |
| 127 |
rtcol_oid[rtcol_len - 1] = 2; /* ifRouteIfIndex */ |
| 128 |
if (snmp_oid_compare(rtcol_oid, rtcol_len, |
| 129 |
var->name, rtcol_len) != 0) |
| 130 |
break; /* End of Table */ |
| 131 |
if (var->type == SNMP_NOSUCHOBJECT || |
| 132 |
var->type == SNMP_NOSUCHINSTANCE || |
| 133 |
var->type == SNMP_ENDOFMIBVIEW) |
| 134 |
break; |
| 135 |
memset(&route, 0, sizeof(struct route_entry)); |
| 136 |
/* |
| 137 |
* Extract ipRouteDest index value |
| 138 |
*/ |
| 139 |
cp = tmpAddr.data; |
| 140 |
cp[0] = var->name[10] & 0xff; |
| 141 |
cp[1] = var->name[11] & 0xff; |
| 142 |
cp[2] = var->name[12] & 0xff; |
| 143 |
cp[3] = var->name[13] & 0xff; |
| 144 |
rp->destination = tmpAddr.addr; |
| 145 |
|
| 146 |
for (vp = var; vp; vp = vp->next_variable) { |
| 147 |
switch (vp->name[9]) { |
| 148 |
case 2: /* ifRouteIfIndex */ |
| 149 |
rp->ifNumber = *vp->val.integer; |
| 150 |
rp->set_bits |= SET_IFNO; |
| 151 |
break; |
| 152 |
case 7: /* ipRouteNextHop */ |
| 153 |
memmove(&rp->gateway, vp->val.string, 4); |
| 154 |
rp->set_bits |= SET_GWAY; |
| 155 |
break; |
| 156 |
case 8: /* ipRouteType */ |
| 157 |
rp->type = *vp->val.integer; |
| 158 |
rp->set_bits |= SET_TYPE; |
| 159 |
break; |
| 160 |
case 9: /* ipRouteProto */ |
| 161 |
rp->proto = *vp->val.integer; |
| 162 |
rp->set_bits |= SET_PRTO; |
| 163 |
break; |
| 164 |
case 11: /* ipRouteMask */ |
| 165 |
memmove(&rp->mask, vp->val.string, 4); |
| 166 |
rp->set_bits |= SET_MASK; |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
if (rp->set_bits != SET_ALL) { |
| 171 |
continue; /* Incomplete query */ |
| 172 |
} |
| 173 |
|
| 174 |
p_rtnode(rp); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
int |
| 180 |
route4pr(int af) |
| 181 |
{ |
| 182 |
struct route_entry route, *rp = &route; |
| 183 |
oid rtcol_oid[] = { 1, 3, 6, 1, 2, 1, 4, 24, 4, 1, 0 }; /* ipCidrRouteEntry */ |
| 184 |
size_t rtcol_len = OID_LENGTH(rtcol_oid); |
| 185 |
netsnmp_variable_list *var = NULL, *vp; |
| 186 |
union { |
| 187 |
in_addr_t addr; |
| 188 |
unsigned char data[4]; |
| 189 |
} tmpAddr; |
| 190 |
int printed = 0; |
| 191 |
int hdr_af = AF_UNSPEC; |
| 192 |
|
| 193 |
if (af != AF_UNSPEC && af != AF_INET) |
| 194 |
return 0; |
| 195 |
|
| 196 |
#define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \ |
| 197 |
snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL, 0) |
| 198 |
ADD_RTVAR(5); /* ipCidrRouteIfIndex */ |
| 199 |
ADD_RTVAR(6); /* ipCidrRouteType */ |
| 200 |
ADD_RTVAR(7); /* ipCidrRouteProto */ |
| 201 |
#undef ADD_RTVAR |
| 202 |
|
| 203 |
/* |
| 204 |
* Now walk the ipCidrRouteTable, reporting the various route entries |
| 205 |
*/ |
| 206 |
while (1) { |
| 207 |
oid *op; |
| 208 |
unsigned char *cp; |
| 209 |
|
| 210 |
if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR) |
| 211 |
break; |
| 212 |
rtcol_oid[rtcol_len - 1] = 5; /* ipRouteIfIndex */ |
| 213 |
if (snmp_oid_compare(rtcol_oid, rtcol_len, |
| 214 |
var->name, rtcol_len) != 0) |
| 215 |
break; /* End of Table */ |
| 216 |
if (var->type == SNMP_NOSUCHOBJECT || |
| 217 |
var->type == SNMP_NOSUCHINSTANCE || |
| 218 |
var->type == SNMP_ENDOFMIBVIEW) |
| 219 |
break; |
| 220 |
memset(&route, 0, sizeof(struct route_entry)); |
| 221 |
rp->af = AF_INET; |
| 222 |
op = var->name + rtcol_len; |
| 223 |
cp = tmpAddr.data; |
| 224 |
cp[0] = *op++ & 0xff; |
| 225 |
cp[1] = *op++ & 0xff; |
| 226 |
cp[2] = *op++ & 0xff; |
| 227 |
cp[3] = *op++ & 0xff; |
| 228 |
rp->destination = tmpAddr.addr; |
| 229 |
cp = tmpAddr.data; |
| 230 |
cp[0] = *op++ & 0xff; |
| 231 |
cp[1] = *op++ & 0xff; |
| 232 |
cp[2] = *op++ & 0xff; |
| 233 |
cp[3] = *op++ & 0xff; |
| 234 |
rp->mask = tmpAddr.addr; |
| 235 |
op++; /* ipCidrRouteTos */ |
| 236 |
cp = tmpAddr.data; |
| 237 |
cp[0] = *op++ & 0xff; |
| 238 |
cp[1] = *op++ & 0xff; |
| 239 |
cp[2] = *op++ & 0xff; |
| 240 |
cp[3] = *op++ & 0xff; |
| 241 |
rp->gateway = tmpAddr.addr; |
| 242 |
rp->set_bits = SET_MASK | SET_GWAY; |
| 243 |
|
| 244 |
for (vp = var; vp; vp = vp->next_variable) { |
| 245 |
switch (vp->name[rtcol_len - 1]) { |
| 246 |
case 5: /* ipCidrRouteIfIndex */ |
| 247 |
rp->ifNumber = *vp->val.integer; |
| 248 |
rp->set_bits |= SET_IFNO; |
| 249 |
break; |
| 250 |
case 6: /* ipCidrRouteType */ |
| 251 |
rp->type = *vp->val.integer; |
| 252 |
rp->set_bits |= SET_TYPE; |
| 253 |
break; |
| 254 |
case 7: /* ipCidrRouteProto */ |
| 255 |
rp->proto = *vp->val.integer; |
| 256 |
rp->set_bits |= SET_PRTO; |
| 257 |
break; |
| 258 |
} |
| 259 |
} |
| 260 |
if (rp->set_bits != SET_ALL) { |
| 261 |
continue; /* Incomplete query */ |
| 262 |
} |
| 263 |
|
| 264 |
if (hdr_af != rp->af) { |
| 265 |
if (hdr_af != AF_UNSPEC) |
| 266 |
printf("\n"); |
| 267 |
hdr_af = rp->af; |
| 268 |
printf("Routing tables (ipCidrRouteTable)\n"); |
| 269 |
pr_rthdr(hdr_af); |
| 270 |
} |
| 271 |
p_rtnode(rp); |
| 272 |
printed++; |
| 273 |
} |
| 274 |
snmp_free_varbind(var); |
| 275 |
return printed; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
struct iflist { |
| 280 |
int index; |
| 281 |
char name[MAX_IFNAME]; |
| 282 |
struct iflist *next; |
| 283 |
} *Iflist = NULL; |
| 284 |
|
| 285 |
void |
| 286 |
get_ifname(char *name, int ifIndex) |
| 287 |
{ |
| 288 |
oid ifdescr_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 2, 0 }; |
| 289 |
size_t ifdescr_len = OID_LENGTH(ifdescr_oid); |
| 290 |
oid ifxcol_oid[] = { 1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 1, 0 }; |
| 291 |
size_t ifxcol_len = OID_LENGTH(ifxcol_oid); |
| 292 |
netsnmp_variable_list *var = NULL; |
| 293 |
struct iflist *ip; |
| 294 |
|
| 295 |
for (ip = Iflist; ip; ip = ip->next) { |
| 296 |
if (ip->index == ifIndex) |
| 297 |
break; |
| 298 |
} |
| 299 |
if (ip) { |
| 300 |
strlcpy(name, ip->name, MAX_IFNAME); |
| 301 |
return; |
| 302 |
} |
| 303 |
ip = (struct iflist *) malloc(sizeof(struct iflist)); |
| 304 |
if (ip == NULL) |
| 305 |
return; |
| 306 |
ip->next = Iflist; |
| 307 |
Iflist = ip; |
| 308 |
ip->index = ifIndex; |
| 309 |
|
| 310 |
ifxcol_oid[ifxcol_len - 1] = ifIndex; |
| 311 |
snmp_varlist_add_variable(&var, ifxcol_oid, ifxcol_len, |
| 312 |
ASN_NULL, NULL, 0); |
| 313 |
if (netsnmp_query_get(var, ss) == SNMP_ERR_NOERROR) { |
| 314 |
if (var->val_len >= sizeof(ip->name)) |
| 315 |
var->val_len = sizeof(ip->name) - 1; |
| 316 |
memmove(ip->name, var->val.string, var->val_len); |
| 317 |
ip->name[var->val_len] = '\0'; |
| 318 |
strlcpy(name, ip->name, MAX_IFNAME); |
| 319 |
snmp_free_varbind(var); |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
ifdescr_oid[ifdescr_len - 1] = ifIndex; |
| 324 |
snmp_varlist_add_variable(&var, ifdescr_oid, ifdescr_len, |
| 325 |
ASN_NULL, NULL, 0); |
| 326 |
if (netsnmp_query_get(var, ss) == SNMP_ERR_NOERROR) { |
| 327 |
if (var->val_len >= sizeof(ip->name)) |
| 328 |
var->val_len = sizeof(ip->name) - 1; |
| 329 |
memmove(ip->name, var->val.string, var->val_len); |
| 330 |
ip->name[var->val_len] = '\0'; |
| 331 |
snmp_free_varbind(var); |
| 332 |
} else { |
| 333 |
snprintf(ip->name, MAX_IFNAME, "if%d", ifIndex); |
| 334 |
} |
| 335 |
strlcpy(name, ip->name, MAX_IFNAME); |
| 336 |
} |
| 337 |
|
| 338 |
/* |
| 339 |
* column widths; each followed by one space |
| 340 |
*/ |
| 341 |
#ifndef NETSNMP_ENABLE_IPV6 |
| 342 |
#define WID_DST(af) 26 /* width of destination column */ |
| 343 |
#define WID_GW(af) 18 /* width of gateway column */ |
| 344 |
#else |
| 345 |
/* |
| 346 |
* width of destination/gateway column |
| 347 |
*/ |
| 348 |
#if 1 |
| 349 |
/* |
| 350 |
* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 |
| 351 |
*/ |
| 352 |
#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 26) : 26) |
| 353 |
#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18) |
| 354 |
#else |
| 355 |
/* |
| 356 |
* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 |
| 357 |
*/ |
| 358 |
#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18) |
| 359 |
#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18) |
| 360 |
#endif |
| 361 |
#endif /* NETSNMP_ENABLE_IPV6 */ |
| 362 |
|
| 363 |
/* |
| 364 |
* Print header for routing table columns. |
| 365 |
*/ |
| 366 |
void |
| 367 |
pr_rthdr(int af) |
| 368 |
{ |
| 369 |
/* |
| 370 |
* if (Aflag) |
| 371 |
* printf("%-*.*s ", PLEN, PLEN, "Address"); |
| 372 |
* if (Sflag) |
| 373 |
* printf("%-*.*s ", |
| 374 |
* WID_DST(af), WID_DST(af), "Source"); |
| 375 |
*/ |
| 376 |
printf("%-*.*s ", WID_DST(af), WID_DST(af), "Destination"); |
| 377 |
printf("%-*.*s %-6.6s %s\n", |
| 378 |
WID_GW(af), WID_GW(af), "Gateway", "Flags", "Interface"); |
| 379 |
} |
| 380 |
|
| 381 |
#if 0 |
| 382 |
/* |
| 383 |
* Print header for PF_KEY entries. |
| 384 |
*/ |
| 385 |
void |
| 386 |
pr_encaphdr(void) |
| 387 |
{ |
| 388 |
if (Aflag) |
| 389 |
printf("%-*s ", PLEN, "Address"); |
| 390 |
printf("%-18s %-5s %-18s %-5s %-5s %-22s\n", |
| 391 |
"Source", "Port", "Destination", |
| 392 |
"Port", "Proto", "SA(Address/Proto/Type/Direction)"); |
| 393 |
} |
| 394 |
#endif |
| 395 |
|
| 396 |
char * |
| 397 |
routename(in_addr_t in) |
| 398 |
{ |
| 399 |
char *cp; |
| 400 |
static char line[MAXHOSTNAMELEN]; |
| 401 |
char host[MAXHOSTNAMELEN]; |
| 402 |
static char domain[MAXHOSTNAMELEN]; |
| 403 |
static int first = 1; |
| 404 |
struct sockaddr_in sin; |
| 405 |
|
| 406 |
memset(&sin, 0, sizeof(sin)); |
| 407 |
if (first) { |
| 408 |
first = 0; |
| 409 |
if (gethostname(line, sizeof line) == 0 && |
| 410 |
(cp = strchr(line, '.'))) |
| 411 |
strlcpy(domain, cp + 1, sizeof domain); |
| 412 |
else |
| 413 |
domain[0] = '\0'; |
| 414 |
} |
| 415 |
sin.sin_family = AF_INET; |
| 416 |
sin.sin_addr.s_addr = in; |
| 417 |
if (getnameinfo((struct sockaddr *) &sin, sizeof(sin), host, |
| 418 |
sizeof(host), NULL, 0, nflag ? NI_NUMERICHOST : 0) < 0) |
| 419 |
strlcpy(host, "?", sizeof(host)); |
| 420 |
if ((cp = strchr(host, '.')) && strcmp(cp + 1, domain) == 0) |
| 421 |
*cp = '\0'; |
| 422 |
strlcpy(line, host, sizeof(line)); |
| 423 |
return line; |
| 424 |
} |
| 425 |
|
| 426 |
#define C(x) (unsigned int)((x) & 0xffU) |
| 427 |
/* |
| 428 |
* Return the name of the network whose address is given. |
| 429 |
* The address is assumed to be that of a net or subnet, not a host. |
| 430 |
*/ |
| 431 |
char * |
| 432 |
netname(in_addr_t in, in_addr_t mask) |
| 433 |
{ |
| 434 |
char *cp = NULL; |
| 435 |
static char line[MAXHOSTNAMELEN]; |
| 436 |
struct netent *np = NULL; |
| 437 |
int mbits; |
| 438 |
|
| 439 |
in = ntohl(in); |
| 440 |
mask = ntohl(mask); |
| 441 |
if (!nflag && in != INADDR_ANY) { |
| 442 |
if ((np = getnetbyaddr(in, AF_INET)) != NULL) |
| 443 |
cp = np->n_name; |
| 444 |
} |
| 445 |
mbits = mask ? 33 - _ffs(mask) : 0; |
| 446 |
if (cp) { |
| 447 |
strlcpy(line, cp, sizeof(line)); |
| 448 |
} else if (mbits < 9) |
| 449 |
snprintf(line, sizeof line, "%u/%d", C(in >> 24), mbits); |
| 450 |
else if (mbits < 17) |
| 451 |
snprintf(line, sizeof line, "%u.%u/%d", |
| 452 |
C(in >> 24), C(in >> 16), mbits); |
| 453 |
else if (mbits < 25) |
| 454 |
snprintf(line, sizeof line, "%u.%u.%u/%d", |
| 455 |
C(in >> 24), C(in >> 16), C(in >> 8), mbits); |
| 456 |
else |
| 457 |
snprintf(line, sizeof line, "%u.%u.%u.%u/%d", C(in >> 24), |
| 458 |
C(in >> 16), C(in >> 8), C(in), mbits); |
| 459 |
return (line); |
| 460 |
} |
| 461 |
|
| 462 |
#undef NETSNMP_ENABLE_IPV6 |
| 463 |
#ifdef NETSNMP_ENABLE_IPV6 |
| 464 |
char * |
| 465 |
netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) |
| 466 |
{ |
| 467 |
static char line[MAXHOSTNAMELEN + 1]; |
| 468 |
struct sockaddr_in6 sin6; |
| 469 |
u_char *p; |
| 470 |
u_char *lim; |
| 471 |
int masklen, final = 0, illegal = 0; |
| 472 |
int i; |
| 473 |
char hbuf[NI_MAXHOST]; |
| 474 |
int flag = 0; |
| 475 |
int error; |
| 476 |
|
| 477 |
sin6 = *sa6; |
| 478 |
|
| 479 |
masklen = 0; |
| 480 |
lim = (u_char *) (mask + 1); |
| 481 |
i = 0; |
| 482 |
if (mask) { |
| 483 |
for (p = (u_char *) mask; p < lim; p++) { |
| 484 |
if (final && *p) { |
| 485 |
illegal++; |
| 486 |
sin6.sin6_addr.s6_addr[i++] = 0x00; |
| 487 |
continue; |
| 488 |
} |
| 489 |
|
| 490 |
switch (*p & 0xff) { |
| 491 |
case 0xff: |
| 492 |
masklen += 8; |
| 493 |
break; |
| 494 |
case 0xfe: |
| 495 |
masklen += 7; |
| 496 |
final++; |
| 497 |
break; |
| 498 |
case 0xfc: |
| 499 |
masklen += 6; |
| 500 |
final++; |
| 501 |
break; |
| 502 |
case 0xf8: |
| 503 |
masklen += 5; |
| 504 |
final++; |
| 505 |
break; |
| 506 |
case 0xf0: |
| 507 |
masklen += 4; |
| 508 |
final++; |
| 509 |
break; |
| 510 |
case 0xe0: |
| 511 |
masklen += 3; |
| 512 |
final++; |
| 513 |
break; |
| 514 |
case 0xc0: |
| 515 |
masklen += 2; |
| 516 |
final++; |
| 517 |
break; |
| 518 |
case 0x80: |
| 519 |
masklen += 1; |
| 520 |
final++; |
| 521 |
break; |
| 522 |
case 0x00: |
| 523 |
final++; |
| 524 |
break; |
| 525 |
default: |
| 526 |
final++; |
| 527 |
illegal++; |
| 528 |
break; |
| 529 |
} |
| 530 |
|
| 531 |
if (!illegal) |
| 532 |
sin6.sin6_addr.s6_addr[i++] &= *p; |
| 533 |
else |
| 534 |
sin6.sin6_addr.s6_addr[i++] = 0x00; |
| 535 |
} |
| 536 |
} else |
| 537 |
masklen = 128; |
| 538 |
|
| 539 |
if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) |
| 540 |
return ("default"); |
| 541 |
|
| 542 |
if (illegal) |
| 543 |
fprintf(stderr, "illegal prefixlen\n"); |
| 544 |
|
| 545 |
if (nflag) |
| 546 |
flag |= NI_NUMERICHOST; |
| 547 |
error = getnameinfo((struct sockaddr *) &sin6, sin6.sin6_len, |
| 548 |
hbuf, sizeof(hbuf), NULL, 0, flag); |
| 549 |
if (error) |
| 550 |
snprintf(hbuf, sizeof(hbuf), "invalid"); |
| 551 |
|
| 552 |
snprintf(line, sizeof(line), "%s/%d", hbuf, masklen); |
| 553 |
return line; |
| 554 |
} |
| 555 |
|
| 556 |
char * |
| 557 |
routename6(struct sockaddr_in6 *sa6) |
| 558 |
{ |
| 559 |
static char line[NI_MAXHOST]; |
| 560 |
const int niflag = NI_NUMERICHOST; |
| 561 |
|
| 562 |
if (getnameinfo((struct sockaddr *) sa6, sa6->sin6_len, |
| 563 |
line, sizeof(line), NULL, 0, niflag) != 0) |
| 564 |
strlcpy(line, "", sizeof line); |
| 565 |
return line; |
| 566 |
} |
| 567 |
#endif /*NETSNMP_ENABLE_IPV6 */ |
| 568 |
|
| 569 |
char * |
| 570 |
s_rtflags(struct route_entry *rp) |
| 571 |
{ |
| 572 |
static char flag_buf[10]; |
| 573 |
char *cp = flag_buf; |
| 574 |
|
| 575 |
*cp++ = '<'; |
| 576 |
*cp++ = 'U'; /* route is in use */ |
| 577 |
if (rp->mask == 0xffffffff) |
| 578 |
*cp++ = 'H'; /* host */ |
| 579 |
if (rp->proto == 4) |
| 580 |
*cp++ = 'D'; /* ICMP redirect */ |
| 581 |
if (rp->type == 4) |
| 582 |
*cp++ = 'G'; /* remote destination/net */ |
| 583 |
*cp++ = '>'; |
| 584 |
*cp = 0; |
| 585 |
return flag_buf; |
| 586 |
} |
| 587 |
|
| 588 |
void |
| 589 |
p_rtnode(struct route_entry *rp) |
| 590 |
{ |
| 591 |
get_ifname(rp->ifname, rp->ifNumber); |
| 592 |
printf("%-*.*s ", |
| 593 |
WID_DST(AF_INET), WID_DST(AF_INET), |
| 594 |
(rp->destination == INADDR_ANY) ? "default" : |
| 595 |
(rp->set_bits & SET_MASK) ? |
| 596 |
(rp->mask == 0xffffffff ? |
| 597 |
routename(rp->destination) : |
| 598 |
netname(rp->destination, rp->mask)) : |
| 599 |
netname(rp->destination, 0L)); |
| 600 |
printf("%-*.*s %-6.6s %s\n", |
| 601 |
WID_GW(af), WID_GW(af), |
| 602 |
rp->gateway ? routename(rp->gateway) : "*", |
| 603 |
s_rtflags(rp), rp->ifname); |
| 604 |
} |