⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inet6.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************	Copyright 1989, 1991, 1992 by Carnegie Mellon University                      All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of CMU not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission.  CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* * Copyright (c) 1983,1988 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that this notice is preserved and that due credit is given * to the University of California at Berkeley. The name of the University * may not be used to endorse or promote products derived from this * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. */#include <net-snmp/net-snmp-config.h>#ifdef INET6#if HAVE_STDLIB_H#include <stdlib.h>#endif/* * hack-o-matic for Cygwin to use winsock2*/#if defined(cygwin)#undef HAVE_UNISTD_H#undef HAVE_NETINET_IN_H#undef HAVE_ARPA_INET_H#undef HAVE_SYS_PARAM_H#undef HAVE_SYS_SELECT_H#undef HAVE_SYS_SOCKET_H#undef HAVE_IN_ADDR_T#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <stdio.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if defined(HAVE_WINSOCK_H) || defined(cygwin)#include <winsock2.h>#include <ws2tcpip.h>#include "winstub.h"extern const char *inet_ntop(int, const void*, char*, size_t);#else#include <sys/socket.h>#include <netdb.h>#endif#include "main.h"#include <net-snmp/net-snmp-includes.h>#include "netstat.h"void            inet6print(struct in6_addr *, u_short, const char *);static char    *inet6name(struct in6_addr *);struct stat_table {    int             entry;      /* entry number in table */    /*     * format string to printf(description, value, plural(value));      */    /*     * warning: the %d must be before the %s      */    char            description[80];};#if DEBUGGING_INFORMATION        /*         * The following tables provide useful debugging information.         * This isn't normally needed or easily accessible,         *   but could potentially be very useful.         * Rather than delete it totally, it's commented out         */static oid      oid_ipstats[] = { 1, 3, 6, 1, 2, 1, 4, 0, 0 };struct stat_table ip_stattab[] = {    {3, "%d total datagram%s received"},    {4, "%d datagram%s with header errors"},    {5, "%d datagram%s with an invalid destination address"},    {6, "%d datagram%s forwarded"},    {7, "%d datagram%s with unknown protocol"},    {8, "%d datagram%s discarded"},    {9, "%d datagram%s delivered"},    {10, "%d output datagram request%s"},    {11, "%d output datagram%s discarded"},    {12, "%d datagram%s with no route"},    {14, "%d fragment%s received"},    {15, "%d datagram%s reassembled"},    {16, "%d reassembly failure%s"},    {17, "%d datagram%s fragmented"},    {18, "%d fragmentation failure%s"},    {19, "%d fragment%s created"}};static oid      oid_udpstats[] = { 1, 3, 6, 1, 2, 1, 7, 0, 0 };struct stat_table udp_stattab[] = {    {1, "%d total datagram%s received"},    {2, "%d datagram%s to invalid port"},    {3, "%d datagram%s dropped due to errors"},    {4, "%d output datagram request%s"}};static oid      oid_tcpstats[] = { 1, 3, 6, 1, 2, 1, 6, 0, 0 };struct stat_table tcp_stattab[] = {    {5, "%d active open%s"},    {6, "%d passive open%s"},    {7, "%d failed attempt%s"},    {8, "%d reset%s of established connections"},    {9, "%d current established connection%s"},    {10, "%d segment%s received"},    {11, "%d segment%s sent"},    {12, "%d segment%s retransmitted"}};static oid      oid_icmpstats[] = { 1, 3, 6, 1, 2, 1, 5, 0, 0 };struct stat_table icmp_stattab[] = {    {1, "%d total message%s received"},    {2, "%d message%s dropped due to errors"},    {14, "%d ouput message request%s"},    {15, "%d output message%s discarded"}};struct stat_table icmp_inhistogram[] = {    {3, "Destination unreachable: %d"},    {4, "Time Exceeded: %d"},    {5, "Parameter Problem: %d"},    {6, "Source Quench: %d"},    {7, "Redirect: %d"},    {8, "Echo Request: %d"},    {9, "Echo Reply: %d"},    {10, "Timestamp Request: %d"},    {11, "Timestamp Reply: %d"},    {12, "Address Mask Request: %d"},    {13, "Addrss Mask Reply:%d"},};struct stat_table icmp_outhistogram[] = {    {16, "Destination unreachable: %d"},    {17, "Time Exceeded: %d"},    {18, "Parameter Problem: %d"},    {19, "Source Quench: %d"},    {20, "Redirect: %d"},    {21, "Echo Request: %d"},    {22, "Echo Reply: %d"},    {23, "Timestamp Request: %d"},    {24, "Timestamp Reply: %d"},    {25, "Address Mask Request: %d"},    {26, "Addrss Mask Reply:%d"},};#endifstruct tcpconn_entry {    oid             instance[16 + 1 + 16 + 1 + 1];    struct in6_addr localAddress;    int             locAddrSet;    u_short         localPort;    int             locPortSet;    struct in6_addr remoteAddress;    int             remAddrSet;    u_short         remotePort;    int             remPortSet;    int             state;    int             stateSet;    struct tcpconn_entry *next;};struct udp_entry {    oid             instance[16 + 1 + 1];    struct in6_addr localAddress;    int             locAddrSet;    u_short         localPort;    int             locPortSet;    struct udp_entry *next;};#define TCPCONN_LOCADDR	1#define TCPCONN_LOCPORT	2#define TCPCONN_REMADDR	3#define TCPCONN_REMPORT	4#define TCPCONN_IFINDEX	5#define TCPCONN_STATE	6static oid      oid_tcpconntable[] = { 1, 3, 6, 1, 3, 86, 1, 16, 1 };#define TCP_ENTRY 9#define UDP_LOCADDR 	1#define UDP_LOCPORT	2#define UDP_IFINDEX	3static oid      oid_udptable[] = { 1, 3, 6, 1, 3, 87, 1, 1, 1 };#define UDP_ENTRY 9static const char *tcpstates[] = {    "",    "CLOSED",    "LISTEN",    "SYNSENT",    "SYNRECEIVED",    "ESTABLISHED",    "FINWAIT1",    "FINWAIT2",    "CLOSEWAIT",    "LASTACK",    "CLOSING",    "TIMEWAIT"};#define TCP_NSTATES 11static int      validUShortAssign(unsigned short *, int, const char *);/* * Print a summary of connections related to an Internet * protocol (currently only TCP).  For TCP, also give state of connection. */voidprotopr6(const char *name){    struct tcpconn_entry *tcpconn = NULL, *tcplast = NULL, *tp, *newtp;    struct udp_entry *udpconn = NULL, *udplast = NULL, *up, *newup;    netsnmp_pdu    *request = NULL, *response = NULL;    netsnmp_variable_list *vp;    oid            *instance;    int             first, status;    response = NULL;    if (strncmp(name, "tcp", 3) == 0) {        request = snmp_pdu_create(SNMP_MSG_GETNEXT);        snmp_add_null_var(request, oid_tcpconntable,                          sizeof(oid_tcpconntable) / sizeof(oid));        status = STAT_SUCCESS;    } else        status = STAT_TIMEOUT;    while (status == STAT_SUCCESS) {        if (response)            snmp_free_pdu(response);        response = NULL;        status = snmp_synch_response(Session, request, &response);        if (status != STAT_SUCCESS            || response->errstat != SNMP_ERR_NOERROR) {            snmp_perror("SNMP request failed");            break;        }        vp = response->variables;        if (!vp)            break;        if (vp->name_length != 46 ||            memcmp(vp->name, oid_tcpconntable, sizeof(oid_tcpconntable))) {            break;        }        request = snmp_pdu_create(SNMP_MSG_GETNEXT);        snmp_add_null_var(request, vp->name, vp->name_length);        instance = vp->name + 10;        for (tp = tcpconn; tp != NULL; tp = tp->next) {            if (!memcmp(instance, tp->instance, sizeof(tp->instance)))                break;        }        if (tp == NULL) {            tp = (struct tcpconn_entry *) calloc(1,                                                 sizeof(struct                                                        tcpconn_entry));            if (tp == NULL)                break;            if (tcplast != NULL)                tcplast->next = tp;            tcplast = tp;            if (tcpconn == NULL)                tcpconn = tp;            memmove(tp->instance, instance, sizeof(tp->instance));        }        if (vp->name[TCP_ENTRY] == TCPCONN_STATE) {            tp->state = *vp->val.integer;            tp->stateSet = 1;        }        if (vp->name[TCP_ENTRY] == TCPCONN_LOCADDR) {            memmove(&tp->localAddress, vp->val.string,                    sizeof(struct in6_addr));            tp->locAddrSet = 1;        }        if (vp->name[TCP_ENTRY] == TCPCONN_LOCPORT) {            if (validUShortAssign(&tp->localPort, *vp->val.integer,                                  "TCPCONN_LOCPORT"))                tp->locPortSet = 1;        }        if (vp->name[TCP_ENTRY] == TCPCONN_REMADDR) {            memmove(&tp->remoteAddress, vp->val.string,                    sizeof(struct in6_addr));            tp->remAddrSet = 1;        }        if (vp->name[TCP_ENTRY] == TCPCONN_REMPORT) {            if (validUShortAssign(&tp->remotePort, *vp->val.integer,                                  "TCPCONN_REMPORT"))                tp->remPortSet = 1;        }    }    if (response)        snmp_free_pdu(response);    response = NULL;    for (first = 1, tp = tcpconn, newtp = NULL; tp != NULL; tp = tp->next) {        if (newtp)            free(newtp);        newtp = tp;        if (!(tp->stateSet && tp->locAddrSet              && tp->locPortSet && tp->remAddrSet && tp->remPortSet)) {            printf("incomplete entry\n");            continue;        }        if (!aflag && tp->state == MIB_TCPCONNSTATE_LISTEN)            continue;        if (first) {            printf("Active Internet (%s) Connections", name);            if (aflag)                printf(" (including servers)");            putchar('\n');            printf("%-5.5s %-28.28s %-28.28s %s\n",                   "Proto", "Local Address", "Foreign Address", "(state)");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -