1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
|
/*
* ip.c -- scli ip mode implementation
*
* Copyright (C) 2001-2002 Juergen Schoenwaelder
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Id: ip.c 1996 2006-08-22 22:13:26Z schoenw $
*/
#include "scli.h"
#include "ip-mib.h"
#include "ip-forward-mib.h"
#include "ianaiftype-mib.h"
#include "tunnel-mib.h"
#include "if-mib.h"
#include "rfc1213-mib.h"
#include "ianaiftype-mib.h"
#include "ianaiftype-mib-proc.h"
#define RFC1213_MIB_IPROUTE_MASK \
( RFC1213_MIB_IPROUTEDEST | RFC1213_MIB_IPROUTEIFINDEX \
| RFC1213_MIB_IPROUTENEXTHOP | RFC1213_MIB_IPROUTETYPE \
| RFC1213_MIB_IPROUTEPROTO | RFC1213_MIB_IPROUTEMASK )
GNetSnmpEnum const forwarding[] = {
{ IP_MIB_IPFORWARDING_FORWARDING, "enabled" },
{ IP_MIB_IPFORWARDING_NOTFORWARDING, "disabled" },
{ 0, NULL }
};
static int
get_if_name_width(if_mib_ifXEntry_t **ifXTable)
{
int i, name_width = 4;
if (ifXTable) {
for (i = 0; ifXTable[i]; i++) {
if (ifXTable[i]->_ifNameLength > name_width) {
name_width = ifXTable[i]->_ifNameLength;
}
}
}
return name_width;
}
static void
fmt_ip_forward(GString *s,
ip_forward_mib_ipCidrRouteEntry_t *ipCidrRouteEntry,
if_mib_ifXEntry_t **ifXTable,
if_mib_ifEntry_t **ifTable)
{
const char *label;
int i, pos;
if (ipCidrRouteEntry->ipCidrRouteIfIndex) {
g_string_sprintfa(s, "%s/%s%n",
fmt_ipv4_address(ipCidrRouteEntry->ipCidrRouteDest,
SCLI_FMT_ADDR),
fmt_ipv4_mask(ipCidrRouteEntry->ipCidrRouteMask), &pos);
g_string_sprintfa(s, "%*s", MAX(20-pos, 1), "");
g_string_sprintfa(s, " %2d ", ipCidrRouteEntry->ipCidrRouteTos);
g_string_sprintfa(s, "%-16s",
fmt_ipv4_address(ipCidrRouteEntry->ipCidrRouteNextHop,
SCLI_FMT_ADDR));
label = NULL;
if (ipCidrRouteEntry->ipCidrRouteType) {
label = gnet_snmp_enum_get_label(ip_forward_mib_enums_ipCidrRouteType,
*ipCidrRouteEntry->ipCidrRouteType);
}
g_string_sprintfa(s, "%-10s", label ? label : "");
label = NULL;
if (ipCidrRouteEntry->ipCidrRouteProto) {
label = gnet_snmp_enum_get_label(ip_forward_mib_enums_ipCidrRouteProto,
*ipCidrRouteEntry->ipCidrRouteProto);
}
g_string_sprintfa(s, "%-10s", label ? label : "");
g_string_sprintfa(s, "%2d", *ipCidrRouteEntry->ipCidrRouteIfIndex);
if (ifXTable) {
for (i = 0; ifXTable[i]; i++) {
if (ifXTable[i]->ifIndex == *ipCidrRouteEntry->ipCidrRouteIfIndex
&& ifXTable[i]->ifName) {
g_string_sprintfa(s, " (%.*s)",
(int) ifXTable[i]->_ifNameLength,
ifXTable[i]->ifName);
break;
}
}
} else if (ifTable) {
for (i = 0; ifTable[i]; i++) {
if (ifTable[i]->ifIndex == *ipCidrRouteEntry->ipCidrRouteIfIndex
&& ifTable[i]->ifDescr) {
g_string_sprintfa(s, " (%.*s)",
(int) ifTable[i]->_ifDescrLength,
ifTable[i]->ifDescr);
break;
}
}
}
g_string_append(s, "\n");
}
}
static void
fmt_ip_route(GString *s,
rfc1213_mib_ipRouteEntry_t *ipRouteEntry,
if_mib_ifXEntry_t **ifXTable,
if_mib_ifEntry_t **ifTable)
{
const char *label;
int i, pos;
if (ipRouteEntry->ipRouteDest
&& ipRouteEntry->ipRouteNextHop
&& ipRouteEntry->ipRouteMask
&& ipRouteEntry->ipRouteIfIndex) {
g_string_sprintfa(s, "%s/%s%n",
fmt_ipv4_address(ipRouteEntry->ipRouteDest,
SCLI_FMT_ADDR),
fmt_ipv4_mask(ipRouteEntry->ipRouteMask), &pos);
g_string_sprintfa(s, "%*s", MAX(20-pos, 1), "");
g_string_sprintfa(s, " - ");
g_string_sprintfa(s, "%-16s",
fmt_ipv4_address(ipRouteEntry->ipRouteNextHop,
SCLI_FMT_ADDR));
label = NULL;
if (ipRouteEntry->ipRouteType) {
label = gnet_snmp_enum_get_label(rfc1213_mib_enums_ipRouteType,
*ipRouteEntry->ipRouteType);
}
g_string_sprintfa(s, "%-10s", label ? label : "");
label = NULL;
if (ipRouteEntry->ipRouteProto) {
label = gnet_snmp_enum_get_label(rfc1213_mib_enums_ipRouteProto,
*ipRouteEntry->ipRouteProto);
}
g_string_sprintfa(s, "%-10s", label ? label : "");
g_string_sprintfa(s, "%2d", *ipRouteEntry->ipRouteIfIndex);
if (ifXTable) {
for (i = 0; ifXTable[i]; i++) {
if (ifXTable[i]->ifIndex == *ipRouteEntry->ipRouteIfIndex
&& ifXTable[i]->ifName) {
g_string_sprintfa(s, " (%.*s)",
(int) ifXTable[i]->_ifNameLength,
ifXTable[i]->ifName);
break;
}
}
} else if (ifTable) {
for (i = 0; ifTable[i]; i++) {
if (ifTable[i]->ifIndex == *ipRouteEntry->ipRouteIfIndex
&& ifTable[i]->ifDescr) {
g_string_sprintfa(s, " (%.*s)",
(int) ifTable[i]->_ifDescrLength,
ifTable[i]->ifDescr);
break;
}
}
}
g_string_append(s, "\n");
}
}
static int
show_ip_forwarding(scli_interp_t *interp, int argc, char **argv)
{
ip_forward_mib_ipCidrRouteEntry_t **ipCidrRouteTable = NULL;
rfc1213_mib_ipRouteEntry_t **ipRouteTable = NULL;
if_mib_ifEntry_t **ifTable = NULL;
if_mib_ifXEntry_t **ifXTable = NULL;
int i;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip_forward_mib_get_ipCidrRouteTable(interp->peer, &ipCidrRouteTable, 0);
if (interp->peer->error_status || !ipCidrRouteTable) {
rfc1213_mib_get_ipRouteTable(interp->peer, &ipRouteTable, RFC1213_MIB_IPROUTE_MASK);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
}
if (ipCidrRouteTable || ipRouteTable) {
if_mib_get_ifTable(interp->peer, &ifTable, IF_MIB_IFDESCR);
if_mib_get_ifXTable(interp->peer, &ifXTable, IF_MIB_IFNAME);
g_string_sprintfa(interp->header, "%-20s TOS %-16s%-10s%-10s%s",
"DESTINATION", "NEXT-HOP", "TYPE", "PROTO", "INTERFACE");
if (ipCidrRouteTable) {
for (i = 0; ipCidrRouteTable[i]; i++) {
fmt_ip_forward(interp->result, ipCidrRouteTable[i],
ifXTable, ifTable);
}
} else if (ipRouteTable) {
for (i = 0; ipRouteTable[i]; i++) {
fmt_ip_route(interp->result, ipRouteTable[i],
ifXTable, ifTable);
}
}
}
if (ifTable)
if_mib_free_ifTable(ifTable);
if (ifXTable)
if_mib_free_ifXTable(ifXTable);
if (ipRouteTable)
rfc1213_mib_free_ipRouteTable(ipRouteTable);
if (ipCidrRouteTable)
ip_forward_mib_free_ipCidrRouteTable(ipCidrRouteTable);
return SCLI_OK;
}
static void
xml_ip_address(xmlNodePtr root, ip_mib_ipAddrEntry_t *ipAddrEntry)
{
xmlNodePtr tree;
char *name;
tree = xmlNewChild(root, NULL, "address", NULL);
xmlSetProp(tree, "address",
fmt_ipv4_address(ipAddrEntry->ipAdEntAddr, SCLI_FMT_ADDR));
xmlSetProp(tree, "type", "ipv4");
if (ipAddrEntry->ipAdEntNetMask) {
xmlSetProp(tree, "prefix", fmt_ipv4_mask(ipAddrEntry->ipAdEntNetMask));
}
if (ipAddrEntry->ipAdEntIfIndex) {
xml_set_prop(tree, "interface", "%d", *ipAddrEntry->ipAdEntIfIndex);
}
name = fmt_ipv4_address(ipAddrEntry->ipAdEntAddr, SCLI_FMT_NAME);
if (name) {
(void) xmlNewChild(tree, NULL, "name", name);
}
}
static void
fmt_ip_address(GString *s,
ip_mib_ipAddrEntry_t *ipAddrEntry,
int name_width,
if_mib_ifXEntry_t **ifXTable,
if_mib_ifEntry_t **ifTable,
int ifName_width)
{
char *name;
int i;
g_string_sprintfa(s, "%-17s ",
fmt_ipv4_address(ipAddrEntry->ipAdEntAddr, SCLI_FMT_ADDR));
if (ipAddrEntry->ipAdEntNetMask) {
g_string_sprintfa(s, "/%-4s",
fmt_ipv4_mask(ipAddrEntry->ipAdEntNetMask));;
} else {
g_string_sprintfa(s, "%-5s", "");
}
name = fmt_ipv4_address(ipAddrEntry->ipAdEntAddr, SCLI_FMT_NAME);
g_string_sprintfa(s, "%-*s ", name_width, name ? name : "");
if (ipAddrEntry->ipAdEntIfIndex) {
g_string_sprintfa(s, "%9u", *(ipAddrEntry->ipAdEntIfIndex));
for (i = 0; ifXTable && ifXTable[i]; i++) {
if (ifXTable[i]->ifIndex == *ipAddrEntry->ipAdEntIfIndex
&& ifXTable[i]->ifName) {
g_string_sprintfa(s, " %-*.*s", ifName_width,
(int) ifXTable[i]->_ifNameLength,
ifXTable[i]->ifName);
break;
}
}
if (!ifXTable || !ifXTable[i]) {
g_string_sprintfa(s, " %*s", ifName_width, "");
}
if (ifTable) {
for (i = 0; ifTable[i]; i++) {
if (ifTable[i]->ifIndex == *ipAddrEntry->ipAdEntIfIndex
&& ifTable[i]->ifDescr) {
g_string_sprintfa(s, " %.*s",
(int) ifTable[i]->_ifDescrLength,
ifTable[i]->ifDescr);
break;
}
}
}
}
g_string_append(s, "\n");
}
static int
show_ip_addresses(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ipAddrEntry_t **ipAddrTable = NULL;
if_mib_ifEntry_t **ifTable = NULL;
if_mib_ifXEntry_t **ifXTable = NULL;
int i, name_width = 4, ifName_width;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip_mib_get_ipAddrTable(interp->peer, &ipAddrTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (ipAddrTable) {
if_mib_get_ifTable(interp->peer, &ifTable, IF_MIB_IFDESCR);
if_mib_get_ifXTable(interp->peer, &ifXTable, IF_MIB_IFNAME);
ifName_width = get_if_name_width(ifXTable);
if (! scli_interp_xml(interp)) {
for (i = 0; ipAddrTable[i]; i++) {
char *name;
name = fmt_ipv4_address(ipAddrTable[i]->ipAdEntAddr, SCLI_FMT_NAME);
if (name && strlen(name) > name_width) {
name_width = strlen(name);
}
}
g_string_sprintfa(interp->header,
"ADDRESS PREFIX %-*s INTERFACE %-*s DESCRIPTION",
name_width, "NAME", ifName_width, "NAME");
}
for (i = 0; ipAddrTable[i]; i++) {
if (scli_interp_xml(interp)) {
xml_ip_address(interp->xml_node, ipAddrTable[i]);
} else {
fmt_ip_address(interp->result, ipAddrTable[i],
name_width, ifXTable, ifTable, ifName_width);
}
}
}
if (ifTable)
if_mib_free_ifTable(ifTable);
if (ifXTable)
if_mib_free_ifXTable(ifXTable);
if (ipAddrTable)
ip_mib_free_ipAddrTable(ipAddrTable);
return SCLI_OK;
}
static void
fmt_ip_tunnel(GString *s,
tunnel_mib_tunnelIfEntry_t *tunnelIfEntry,
if_mib_ifXEntry_t **ifXTable,
if_mib_ifEntry_t **ifTable)
{
int i;
const char *e;
g_return_if_fail(tunnelIfEntry);
if (tunnelIfEntry->tunnelIfLocalAddress) {
g_string_sprintfa(s, "%-15s ",
fmt_ipv4_address(tunnelIfEntry->tunnelIfLocalAddress,
SCLI_FMT_ADDR));
} else {
g_string_sprintfa(s, "%-15s ", "-");
}
if (tunnelIfEntry->tunnelIfRemoteAddress) {
g_string_sprintfa(s, "%-15s ",
fmt_ipv4_address(tunnelIfEntry->tunnelIfRemoteAddress,
SCLI_FMT_ADDR));
} else {
g_string_sprintfa(s, "%-15s ", "-");
}
e = fmt_enum(ianaiftype_mib_enums_IANAtunnelType,
tunnelIfEntry->tunnelIfEncapsMethod);
g_string_sprintfa(s, "%-8s", e ? e : "-");
e = fmt_enum(tunnel_mib_enums_tunnelIfSecurity,
tunnelIfEntry->tunnelIfSecurity);
g_string_sprintfa(s, "%-6s", e ? e : "-");
if (tunnelIfEntry->tunnelIfHopLimit) {
g_string_sprintfa(s, "%3d ", *tunnelIfEntry->tunnelIfHopLimit);
} else {
g_string_sprintfa(s, "%3s ", "---");
}
if (tunnelIfEntry->tunnelIfTOS) {
switch (*tunnelIfEntry->tunnelIfTOS) {
case -1:
/* A value of -1 indicates that the bits are copied from the
* payload's header. */
g_string_append(s, "CP");
break;
case -2:
/* A value of -2 indicates that a traffic conditioner is
* invoked and more information may be available in a traffic
* conditioner MIB. */
g_string_append(s, "TC");
break;
default:
g_string_sprintfa(s, "%2d", *tunnelIfEntry->tunnelIfTOS);
break;
}
} else {
g_string_append(s, "--");
}
if (tunnelIfEntry->ifIndex) {
g_string_sprintfa(s, " %2u", tunnelIfEntry->ifIndex);
if (ifXTable) {
for (i = 0; ifXTable[i]; i++) {
if(ifXTable[i]->ifIndex == tunnelIfEntry->ifIndex) {
g_string_sprintfa(s, " (%.*s)",
(int) ifXTable[i]->_ifNameLength,
ifXTable[i]->ifName);
break;
}
}
} else if (ifTable) {
for (i = 0; ifTable[i]; i++) {
if(ifTable[i]->ifIndex == tunnelIfEntry->ifIndex) {
g_string_sprintfa(s, " (%.*s)",
(int) ifTable[i]->_ifDescrLength,
ifTable[i]->ifDescr);
break;
}
}
}
}
g_string_append(s, "\n");
}
static int
show_ip_tunnel(scli_interp_t *interp, int argc, char **argv)
{
tunnel_mib_tunnelIfEntry_t **tunnelIfTable = NULL;
if_mib_ifEntry_t **ifTable = NULL;
if_mib_ifXEntry_t **ifXTable = NULL;
int i;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
tunnel_mib_get_tunnelIfTable(interp->peer, &tunnelIfTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (tunnelIfTable) {
if_mib_get_ifTable(interp->peer, &ifTable, IF_MIB_IFDESCR);
if_mib_get_ifXTable(interp->peer, &ifXTable, IF_MIB_IFNAME);
g_string_append(interp->header,
"LOCAL ADDRESS REMOTE ADDRESS TYPE SEC. TTL TOS INTERFACE");
for (i = 0; tunnelIfTable[i]; i++) {
fmt_ip_tunnel(interp->result, tunnelIfTable[i],
ifXTable, ifTable);
}
}
if (ifXTable) if_mib_free_ifXTable(ifXTable);
if (ifTable) if_mib_free_ifTable(ifTable);
if (tunnelIfTable) tunnel_mib_free_tunnelIfTable(tunnelIfTable);
return SCLI_OK;
}
static void
fmt_ip_mapping(GString *s,
ip_mib_ipNetToMediaEntry_t *ipNetToMediaEntry,
if_mib_ifEntry_t *ifEntry)
{
char *name;
const char *e;
int i;
g_string_sprintfa(s, "%9u ",
ipNetToMediaEntry->ipNetToMediaIfIndex);
e = fmt_enum(ip_mib_enums_ipNetToMediaType,
ipNetToMediaEntry->ipNetToMediaType);
g_string_sprintfa(s, "%-8s", e ? e : "");
g_string_sprintfa(s, " %-16s ",
fmt_ipv4_address(ipNetToMediaEntry->ipNetToMediaNetAddress,
SCLI_FMT_ADDR));
if (ipNetToMediaEntry->ipNetToMediaPhysAddress) {
for (i = 0;
i < ipNetToMediaEntry->_ipNetToMediaPhysAddressLength; i++) {
g_string_sprintfa(s, "%s%02x", i ? ":" : "",
ipNetToMediaEntry->ipNetToMediaPhysAddress[i]);
}
/* See RFC 2665 section 3.2.6. why the test below is so ugly... */
if (ifEntry && ifEntry->ifType
&& ipNetToMediaEntry->_ipNetToMediaPhysAddressLength == 6
&& ianaiftype_mib_proc_isether(*ifEntry->ifType)) {
name = fmt_ether_address(ipNetToMediaEntry->ipNetToMediaPhysAddress, SCLI_FMT_NAME);
if (name) {
g_string_sprintfa(s, " (%s)", name);
}
}
}
g_string_append(s, "\n");
}
static int
show_ip_mapping(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ipNetToMediaEntry_t **ipNetToMediaTable = NULL;
if_mib_ifEntry_t **ifTable = NULL, *ifEntry = NULL;
int i, j;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip_mib_get_ipNetToMediaTable(interp->peer, &ipNetToMediaTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (ipNetToMediaTable) {
if_mib_get_ifTable(interp->peer, &ifTable, IF_MIB_IFTYPE);
g_string_append(interp->header,
"INTERFACE STATUS ADDRESS LOWER LAYER ADDRESS");
for (i = 0; ipNetToMediaTable[i]; i++) {
if (ifTable) {
for (j = 0; ifTable[j]; j++) {
if (ipNetToMediaTable[i]->ipNetToMediaIfIndex
== ifTable[j]->ifIndex) break;
}
if (ifTable[j]) {
ifEntry = ifTable[j];
}
}
fmt_ip_mapping(interp->result, ipNetToMediaTable[i], ifEntry);
}
}
if (ipNetToMediaTable) ip_mib_free_ipNetToMediaTable(ipNetToMediaTable);
if (ifTable) if_mib_free_ifTable(ifTable);
return SCLI_OK;
}
static void
fmt_ip_info(GString *s, ip_mib_ip_t *ip)
{
int const indent = 18;
const char *e;
e = fmt_enum(forwarding, ip->ipForwarding);
if (e) {
g_string_sprintfa(s, "%-*s%s\n", indent, "Forwarding:", e);
}
if (ip->ipDefaultTTL) {
g_string_sprintfa(s, "%-*s%d hops\n", indent, "Default-TTL:",
*ip->ipDefaultTTL);
}
if (ip->ipReasmTimeout) {
g_string_sprintfa(s, "%-*s%d seconds\n", indent, "Reasm-Timeout:",
*ip->ipReasmTimeout);
}
}
static void
xml_ip_info(xmlNodePtr tree, ip_mib_ip_t *ip)
{
xmlNodePtr node;
const char *e;
e = fmt_enum(forwarding, ip->ipForwarding);
if (e) {
(void) xml_new_child(tree, NULL, "forwarding", "%s", e);
}
if (ip->ipDefaultTTL) {
node = xml_new_child(tree, NULL, "default-ttl", "%d",
*ip->ipDefaultTTL);
xml_set_prop(node, "unit", "hops");
}
if (ip->ipReasmTimeout) {
node = xml_new_child(tree, NULL, "reassemble-timeout", "%d",
*ip->ipDefaultTTL);
xml_set_prop(node, "unit", "seconds");
}
}
static void
xxx_ip_info(GString *s, xmlNodePtr tree)
{
int const indent = 18;
xmlNodePtr node;
for (node = tree->children; node; node = node->next) {
g_printerr("%s\n", xmlGetNodePath(node));
if (xmlStrcmp(node->name, (const xmlChar *) "forwarding") == 0) {
g_string_sprintfa(s, "%-*s%s\n", indent, "Forwarding:",
node->content);
}
}
}
static int
show_ip_info(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ip_t *ip;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip_mib_get_ip(interp->peer, &ip, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (ip) {
if (scli_interp_xml(interp)) {
xml_ip_info(interp->xml_node, ip);
} else {
fmt_ip_info(interp->result, ip);
}
}
if (scli_interp_xml(interp)) {
xxx_ip_info(interp->result, interp->xml_node);
g_printerr(interp->result->str);
}
if (ip) ip_mib_free_ip(ip);
return SCLI_OK;
}
static int
set_ip_forwarding(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ip_t *ip;
gint32 value;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc != 2) {
return SCLI_SYNTAX_NUMARGS;
}
if (! gnet_snmp_enum_get_number(forwarding, argv[1], &value)) {
g_string_assign(interp->result, argv[1]);
return SCLI_SYNTAX_VALUE;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip = ip_mib_new_ip();
ip->ipForwarding = &value;
ip_mib_set_ip(interp->peer, ip, IP_MIB_IPFORWARDING);
ip_mib_free_ip(ip);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
return SCLI_OK;
}
static int
set_ip_ttl(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ip_t *ip;
gint32 value;
char *end;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc != 2) {
return SCLI_SYNTAX_NUMARGS;
}
value = strtol(argv[1], &end, 0);
if (*end || value < 1 || value > 255) {
g_string_assign(interp->result, argv[1]);
return SCLI_SYNTAX_NUMBER;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip = ip_mib_new_ip();
ip->ipDefaultTTL = &value;
ip_mib_set_ip(interp->peer, ip, IP_MIB_IPDEFAULTTTL);
ip_mib_free_ip(ip);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
return SCLI_OK;
}
static int
dump_ip(scli_interp_t *interp, int argc, char **argv)
{
ip_mib_ip_t *ip;
const char *e;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
ip_mib_get_ip(interp->peer, &ip, IP_MIB_IPFORWARDING | IP_MIB_IPDEFAULTTTL);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (ip) {
e = fmt_enum(forwarding, ip->ipForwarding);
if (e) {
g_string_sprintfa(interp->result,
"set ip forwarding \"%s\"\n", e);
}
if (ip->ipDefaultTTL) {
g_string_sprintfa(interp->result,
"set ip ttl \"%d\"\n",
*ip->ipDefaultTTL);
}
}
if (ip) ip_mib_free_ip(ip);
return SCLI_OK;
}
void
scli_init_ip_mode(scli_interp_t *interp)
{
static scli_cmd_t cmds[] = {
{ "set ip forwarding", "<value>",
"The `set ip forwarding' command controls whether the IP protocol\n"
"engine forwards IP datagrams or not. The <value> parameter must\n"
"be one of the strings \"enabled\" or \"disabled\".",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY | SCLI_CMD_FLAG_NORECURSE,
NULL, NULL,
set_ip_forwarding },
{ "set ip ttl", "<number>",
"The `set ip ttl' command can be used to change the default\n"
"time to live (TTL) value used by the IP protocol engine. The\n"
"<number> parameter must be a number between 1 and 255 inclusive.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY | SCLI_CMD_FLAG_NORECURSE,
NULL, NULL,
set_ip_ttl },
{ "show ip info", NULL,
"The `show ip info' command displays parameters of the IP\n"
"protocol engine, such as the default TTL or whether the\n"
"node is forwarding IP packets.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_DRY,
"ip info", NULL,
show_ip_info },
{ "show ip forwarding", NULL,
"The `show ip forwarding' command displays the IP forwarding data\n"
"base. The command generates a table with the following columns:\n"
"\n"
" DESTINATION destination address and prefix\n"
" NEXT-HOP next hop towards the destination\n"
" TOS type of service selector\n"
" TYPE type (direct/indirect) of the entry\n"
" PROTO protocol which created the entry\n"
" INTERFACE interface used for forwarding",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
show_ip_forwarding },
{ "show ip addresses", NULL,
"The `show ip addresses' command displays the IP addresses\n"
"assigned to network interfaces. The command generates a table\n"
"with the following columns:\n"
"\n"
" ADDRESS IP address\n"
" PREFIX IP address prefix length\n"
" NAME name of the IP address\n"
" INTERFACE network interface number\n"
" DESCRIPTION description of the network interface",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_DRY,
"ip", NULL,
show_ip_addresses },
{ "show ip tunnel", NULL,
"The `show ip tunnel' command displays information about existing\n"
"IP tunnels.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
show_ip_tunnel },
{ "show ip mapping", NULL,
"The `show ip mapping' command displays the mapping of IP address\n"
"to lower layer address (e.g., IEEE 802 addresses). The command\n"
"generates a table with the following columns:\n"
"\n"
" INTERFACE network interface number\n"
" STATUS status of the mapping entry\n"
" ADDRESS IP address\n"
" ADDRESS lower layer address",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
show_ip_mapping },
{ "dump ip", NULL,
"The `dump ip' command generates a sequence of scli commands\n"
"which can be used to restore the IP configuration.\n",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
dump_ip },
{ NULL, NULL, NULL, 0, NULL, NULL, NULL }
};
static scli_mode_t ip_mode = {
"ip",
"The ip scli mode is based on the IP-MIB as published in\n"
"RFC 2011, the IP-FORWARD-MIB as published in RFC 2096, the\n"
"IP-TUNNEL-MIB as published in RFC 2667 and the RFC1213-MIB\n"
"as published in RFC 1213. It provides commands to browse,\n"
"monitor and configure IP protocol engines.",
cmds
};
scli_register_mode(interp, &ip_mode);
}
|