📄 inet.c
字号:
/**************************************************************** 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>#if HAVE_STDLIB_H#include <stdlib.h>#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 HAVE_WINSOCK_H#include <winsock2.h>#include <ws2tcpip.h>#include "winstub.h"#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#include "main.h"#include <net-snmp/net-snmp-includes.h>#include "netstat.h"static char *inetname(struct in_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];};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"},};struct tcpconn_entry { oid instance[10]; struct in_addr localAddress; int locAddrSet; u_short localPort; int locPortSet; struct in_addr remoteAddress; int remAddrSet; u_short remotePort; int remPortSet; int state; int stateSet; struct tcpconn_entry *next;};struct udp_entry { oid instance[5]; struct in_addr localAddress; int locAddrSet; u_short localPort; int locPortSet; struct udp_entry *next;};#define TCPCONN_STATE 1#define TCPCONN_LOCADDR 2#define TCPCONN_LOCPORT 3#define TCPCONN_REMADDR 4#define TCPCONN_REMPORT 5static oid oid_tcpconntable[] = { 1, 3, 6, 1, 2, 1, 6, 13, 1 };#define TCP_ENTRY 9#define UDP_LOCADDR 1#define UDP_LOCPORT 2static oid oid_udptable[] = { 1, 3, 6, 1, 2, 1, 7, 5, 1 };#define UDP_ENTRY 9const char *tcpstates[] = { "", "CLOSED", "LISTEN", "SYNSENT", "SYNRECEIVED", "ESTABLISHED", "FINWAIT1", "FINWAIT2", "CLOSEWAIT", "LASTACK", "CLOSING", "TIMEWAIT"};#define TCP_NSTATES 11int 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. */voidprotopr(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 (strcmp(name, "tcp") == 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_sess_perror("SNMP request failed", Session); break; } vp = response->variables; if (!vp) break; if (vp->name_length != 20 || 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(u_long)); 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(u_long)); 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)"); first = 0; } printf("%-5.5s ", "tcp"); inetprint(&tp->localAddress, tp->localPort, "tcp"); inetprint(&tp->remoteAddress, tp->remotePort, "tcp"); if (tp->state < 1 || tp->state > TCP_NSTATES) printf(" %d", tp->state); else printf(" %s", tcpstates[tp->state]); putchar('\n'); } if (newtp) free(newtp); response = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -