route.c (302408) | route.c (311670) |
---|---|
1/*- 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 20 unchanged lines hidden (view full) --- 29 30#if 0 31#ifndef lint 32static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; 33#endif /* not lint */ 34#endif 35 36#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 20 unchanged lines hidden (view full) --- 29 30#if 0 31#ifndef lint 32static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; 33#endif /* not lint */ 34#endif 35 36#include <sys/cdefs.h> |
37__FBSDID("$FreeBSD: stable/11/usr.bin/netstat/route.c 297321 2016-03-27 20:02:21Z pfg $"); | 37__FBSDID("$FreeBSD: stable/11/usr.bin/netstat/route.c 311670 2017-01-08 07:25:22Z delphij $"); |
38 39#include <sys/param.h> 40#include <sys/protosw.h> 41#include <sys/socket.h> 42#include <sys/socketvar.h> 43#include <sys/sysctl.h> 44#include <sys/time.h> 45 --- 5 unchanged lines hidden (view full) --- 51 52#include <netinet/in.h> 53#include <netgraph/ng_socket.h> 54 55#include <arpa/inet.h> 56#include <ifaddrs.h> 57#include <libutil.h> 58#include <netdb.h> | 38 39#include <sys/param.h> 40#include <sys/protosw.h> 41#include <sys/socket.h> 42#include <sys/socketvar.h> 43#include <sys/sysctl.h> 44#include <sys/time.h> 45 --- 5 unchanged lines hidden (view full) --- 51 52#include <netinet/in.h> 53#include <netgraph/ng_socket.h> 54 55#include <arpa/inet.h> 56#include <ifaddrs.h> 57#include <libutil.h> 58#include <netdb.h> |
59#include <stdbool.h> |
|
59#include <stdint.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <stdbool.h> 63#include <string.h> 64#include <sysexits.h> 65#include <unistd.h> 66#include <err.h> --- 41 unchanged lines hidden (view full) --- 108static void p_rtable_sysctl(int, int); 109static void p_rtentry_sysctl(const char *name, struct rt_msghdr *); 110static int p_sockaddr(const char *name, struct sockaddr *, struct sockaddr *, 111 int, int); 112static const char *fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, 113 int flags); 114static void p_flags(int, const char *); 115static const char *fmt_flags(int f); | 60#include <stdint.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <stdbool.h> 64#include <string.h> 65#include <sysexits.h> 66#include <unistd.h> 67#include <err.h> --- 41 unchanged lines hidden (view full) --- 109static void p_rtable_sysctl(int, int); 110static void p_rtentry_sysctl(const char *name, struct rt_msghdr *); 111static int p_sockaddr(const char *name, struct sockaddr *, struct sockaddr *, 112 int, int); 113static const char *fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, 114 int flags); 115static void p_flags(int, const char *); 116static const char *fmt_flags(int f); |
116static void domask(char *, in_addr_t, u_long); | 117static void domask(char *, size_t, u_long); |
117 118 119/* 120 * Print routing tables. 121 */ 122void 123routepr(int fibnum, int af) 124{ --- 362 unchanged lines hidden (view full) --- 487 } 488 default: 489 { 490 u_char *s = (u_char *)sa->sa_data, *slim; 491 char *cq, *cqlim; 492 493 cq = buf; 494 slim = sa->sa_len + (u_char *) sa; | 118 119 120/* 121 * Print routing tables. 122 */ 123void 124routepr(int fibnum, int af) 125{ --- 362 unchanged lines hidden (view full) --- 488 } 489 default: 490 { 491 u_char *s = (u_char *)sa->sa_data, *slim; 492 char *cq, *cqlim; 493 494 cq = buf; 495 slim = sa->sa_len + (u_char *) sa; |
495 cqlim = cq + sizeof(buf) - 6; 496 cq += sprintf(cq, "(%d)", sa->sa_family); | 496 cqlim = cq + sizeof(buf) - sizeof(" ffff"); 497 snprintf(cq, sizeof(cq), "(%d)", sa->sa_family); 498 cq += strlen(cq); |
497 while (s < slim && cq < cqlim) { | 499 while (s < slim && cq < cqlim) { |
498 cq += sprintf(cq, " %02x", *s++); 499 if (s < slim) 500 cq += sprintf(cq, "%02x", *s++); | 500 snprintf(cq, sizeof(" ff"), " %02x", *s++); 501 cq += strlen(cq); 502 if (s < slim) { 503 snprintf(cq, sizeof("ff"), "%02x", *s++); 504 cq += strlen(cq); 505 } |
501 } 502 cp = buf; 503 } 504 } 505 506 return (cp); 507} 508 --- 60 unchanged lines hidden (view full) --- 569 570#define NSHIFT(m) ( \ 571 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ 572 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ 573 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 574 0) 575 576static void | 506 } 507 cp = buf; 508 } 509 } 510 511 return (cp); 512} 513 --- 60 unchanged lines hidden (view full) --- 574 575#define NSHIFT(m) ( \ 576 (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \ 577 (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \ 578 (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \ 579 0) 580 581static void |
577domask(char *dst, in_addr_t addr __unused, u_long mask) | 582domask(char *dst, size_t buflen, u_long mask) |
578{ 579 int b, i; 580 581 if (mask == 0) { 582 *dst = '\0'; 583 return; 584 } 585 i = 0; --- 5 unchanged lines hidden (view full) --- 591 for (bb = b+1; bb < 32; bb++) 592 if (!(mask & (1 << bb))) { 593 i = -1; /* noncontig */ 594 break; 595 } 596 break; 597 } 598 if (i == -1) | 583{ 584 int b, i; 585 586 if (mask == 0) { 587 *dst = '\0'; 588 return; 589 } 590 i = 0; --- 5 unchanged lines hidden (view full) --- 596 for (bb = b+1; bb < 32; bb++) 597 if (!(mask & (1 << bb))) { 598 i = -1; /* noncontig */ 599 break; 600 } 601 break; 602 } 603 if (i == -1) |
599 sprintf(dst, "&0x%lx", mask); | 604 snprintf(dst, buflen, "&0x%lx", mask); |
600 else | 605 else |
601 sprintf(dst, "/%d", 32-i); | 606 snprintf(dst, buflen, "/%d", 32-i); |
602} 603 604/* 605 * Return the name of the network whose address is given. 606 */ 607const char * 608netname(struct sockaddr *sa, struct sockaddr *mask) 609{ --- 14 unchanged lines hidden (view full) --- 624 return (NULL); 625 } 626} 627 628static const char * 629netname4(in_addr_t in, in_addr_t mask) 630{ 631 char *cp = 0; | 607} 608 609/* 610 * Return the name of the network whose address is given. 611 */ 612const char * 613netname(struct sockaddr *sa, struct sockaddr *mask) 614{ --- 14 unchanged lines hidden (view full) --- 629 return (NULL); 630 } 631} 632 633static const char * 634netname4(in_addr_t in, in_addr_t mask) 635{ 636 char *cp = 0; |
632 static char line[MAXHOSTNAMELEN + sizeof("/xx")]; | 637 static char line[MAXHOSTNAMELEN + sizeof("&0xffffffff")]; |
633 char nline[INET_ADDRSTRLEN]; 634 struct netent *np = 0; 635 in_addr_t i; 636 637 if (in == INADDR_ANY && mask == 0) { 638 strlcpy(line, "default", sizeof(line)); 639 return (line); 640 } --- 9 unchanged lines hidden (view full) --- 650 trimdomain(cp, strlen(cp)); 651 } 652 } 653 if (cp != NULL) 654 strlcpy(line, cp, sizeof(line)); 655 else { 656 inet_ntop(AF_INET, &in, nline, sizeof(nline)); 657 strlcpy(line, nline, sizeof(line)); | 638 char nline[INET_ADDRSTRLEN]; 639 struct netent *np = 0; 640 in_addr_t i; 641 642 if (in == INADDR_ANY && mask == 0) { 643 strlcpy(line, "default", sizeof(line)); 644 return (line); 645 } --- 9 unchanged lines hidden (view full) --- 655 trimdomain(cp, strlen(cp)); 656 } 657 } 658 if (cp != NULL) 659 strlcpy(line, cp, sizeof(line)); 660 else { 661 inet_ntop(AF_INET, &in, nline, sizeof(nline)); 662 strlcpy(line, nline, sizeof(line)); |
658 domask(line + strlen(line), i, ntohl(mask)); | 663 domask(line + strlen(line), sizeof(line) - strlen(line), ntohl(mask)); |
659 } 660 661 return (line); 662} 663 664#undef NSHIFT 665 666#ifdef INET6 --- 12 unchanged lines hidden (view full) --- 679 sa6->sin6_scope_id = 680 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]); 681 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0; 682 } 683#endif 684} 685 686/* Mask to length table. To check an invalid value, (length + 1) is used. */ | 664 } 665 666 return (line); 667} 668 669#undef NSHIFT 670 671#ifdef INET6 --- 12 unchanged lines hidden (view full) --- 684 sa6->sin6_scope_id = 685 ntohs(*(u_int16_t *)&sa6->sin6_addr.s6_addr[2]); 686 sa6->sin6_addr.s6_addr[2] = sa6->sin6_addr.s6_addr[3] = 0; 687 } 688#endif 689} 690 691/* Mask to length table. To check an invalid value, (length + 1) is used. */ |
687static int masktolen[256] = { | 692static const u_char masktolen[256] = { |
688 [0xff] = 8 + 1, 689 [0xfe] = 7 + 1, 690 [0xfc] = 6 + 1, 691 [0xf8] = 5 + 1, 692 [0xf0] = 4 + 1, 693 [0xe0] = 3 + 1, 694 [0xc0] = 2 + 1, 695 [0x80] = 1 + 1, 696 [0x00] = 0 + 1, 697}; 698 699static const char * 700netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask) 701{ 702 static char line[NI_MAXHOST + sizeof("/xxx") - 1]; 703 struct sockaddr_in6 addr; 704 char nline[NI_MAXHOST]; | 693 [0xff] = 8 + 1, 694 [0xfe] = 7 + 1, 695 [0xfc] = 6 + 1, 696 [0xf8] = 5 + 1, 697 [0xf0] = 4 + 1, 698 [0xe0] = 3 + 1, 699 [0xc0] = 2 + 1, 700 [0x80] = 1 + 1, 701 [0x00] = 0 + 1, 702}; 703 704static const char * 705netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask) 706{ 707 static char line[NI_MAXHOST + sizeof("/xxx") - 1]; 708 struct sockaddr_in6 addr; 709 char nline[NI_MAXHOST]; |
710 char maskbuf[sizeof("/xxx")]; |
|
705 u_char *p, *lim; | 711 u_char *p, *lim; |
706 int masklen, illegal = 0, i; | 712 u_char masklen; 713 int i; 714 bool illegal = false; |
707 708 if (mask) { 709 p = (u_char *)&mask->sin6_addr; 710 for (masklen = 0, lim = p + 16; p < lim; p++) { | 715 716 if (mask) { 717 p = (u_char *)&mask->sin6_addr; 718 for (masklen = 0, lim = p + 16; p < lim; p++) { |
711 if (masktolen[*p] > 0) | 719 if (masktolen[*p] > 0) { |
712 /* -1 is required. */ | 720 /* -1 is required. */ |
713 masklen += masktolen[*p] - 1; 714 else 715 illegal++; | 721 masklen += (masktolen[*p] - 1); 722 } else 723 illegal = true; |
716 } 717 if (illegal) 718 xo_error("illegal prefixlen\n"); 719 720 memcpy(&addr, sa6, sizeof(addr)); 721 for (i = 0; i < 16; ++i) 722 addr.sin6_addr.s6_addr[i] &= 723 mask->sin6_addr.s6_addr[i]; --- 7 unchanged lines hidden (view full) --- 731 732 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline), 733 NULL, 0, NI_NUMERICHOST); 734 if (numeric_addr) 735 strlcpy(line, nline, sizeof(line)); 736 else 737 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, 738 sizeof(line), NULL, 0, 0); | 724 } 725 if (illegal) 726 xo_error("illegal prefixlen\n"); 727 728 memcpy(&addr, sa6, sizeof(addr)); 729 for (i = 0; i < 16; ++i) 730 addr.sin6_addr.s6_addr[i] &= 731 mask->sin6_addr.s6_addr[i]; --- 7 unchanged lines hidden (view full) --- 739 740 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline), 741 NULL, 0, NI_NUMERICHOST); 742 if (numeric_addr) 743 strlcpy(line, nline, sizeof(line)); 744 else 745 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, 746 sizeof(line), NULL, 0, 0); |
739 if (numeric_addr || strcmp(line, nline) == 0) 740 sprintf(&line[strlen(line)], "/%d", masklen); | 747 if (numeric_addr || strcmp(line, nline) == 0) { 748 snprintf(maskbuf, sizeof(maskbuf), "/%d", masklen); 749 strlcat(line, maskbuf, sizeof(line)); 750 } |
741 742 return (line); 743} 744#endif /*INET6*/ 745 746/* 747 * Print routing statistics 748 */ --- 39 unchanged lines hidden --- | 751 752 return (line); 753} 754#endif /*INET6*/ 755 756/* 757 * Print routing statistics 758 */ --- 39 unchanged lines hidden --- |