📄 inet.c
字号:
/* $OpenBSD: inet.c,v 1.92 2005/02/10 14:25:08 itojun Exp $ *//* $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $ *//* * Copyright (c) 1983, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifdef INHERITED_CODE#ifndef lint#if 0static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94";#elsestatic const char *rcsid = "$OpenBSD: inet.c,v 1.92 2005/02/10 14:25:08 itojun Exp $";#endif#endif /* not lint */#endif#include <net-snmp/net-snmp-config.h>#if HAVE_UNISTD_H#include <unistd.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#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#include <net-snmp/net-snmp-includes.h>#include "main.h"#include "netstat.h"struct stat_table { int entry; /* entry number in table */ /* * format string to printf(description, value) * warning: the %d must be before the %s */ char description[80];};char *inetname(struct in_addr *);void inetprint(struct in_addr *, int, const char *, int); /* * Print a summary of connections related to * an Internet protocol (kread-based) - Omitted *//* * Print a summary of TCP connections * Listening processes are suppressed unless the * -a (all) flag is specified. */const char *tcpstates[] = { "", "CLOSED", "LISTEN", "SYNSENT", "SYNRECEIVED", "ESTABLISHED", "FINWAIT1", "FINWAIT2", "CLOSEWAIT", "LASTACK", "CLOSING", "TIMEWAIT"};#define TCP_NSTATES 11voidtcpprotopr(const char *name){ netsnmp_variable_list *var, *vp; oid tcpConnState_oid[] = { 1,3,6,1,2,1,6,13,1,1 }; size_t tcpConnState_len = OID_LENGTH( tcpConnState_oid ); int state, width; union { struct in_addr addr; char data[4]; } tmpAddr; oid localPort, remotePort; struct in_addr localAddr, remoteAddr; char *cp; int first = 1; /* * Walking the tcpConnState column will provide all * the necessary information. */ var = NULL; snmp_varlist_add_variable( &var, tcpConnState_oid, tcpConnState_len, ASN_NULL, NULL, 0); if (!var) return; if (netsnmp_query_walk( var, ss ) != SNMP_ERR_NOERROR) return; for (vp = var; vp ; vp=vp->next_variable) { state = *vp->val.integer; if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) continue; if (first) { printf("Active Internet (%s) Connections", name); if (aflag) printf(" (including servers)"); putchar('\n'); width = Aflag ? 18 : 22; printf("%-5.5s %*.*s %*.*s %s\n", "Proto", -width, width, "Local Address", -width, width, "Remote Address", "(state)"); first=0; } /* Extract the local/remote information from the index values */ cp = tmpAddr.data; cp[0] = vp->name[ 10 ] & 0xff; cp[1] = vp->name[ 11 ] & 0xff; cp[2] = vp->name[ 12 ] & 0xff; cp[3] = vp->name[ 13 ] & 0xff; localAddr.s_addr = tmpAddr.addr.s_addr; localPort = ntohs(vp->name[ 14 ]); cp = tmpAddr.data; cp[0] = vp->name[ 15 ] & 0xff; cp[1] = vp->name[ 16 ] & 0xff; cp[2] = vp->name[ 17 ] & 0xff; cp[3] = vp->name[ 18 ] & 0xff; remoteAddr.s_addr = tmpAddr.addr.s_addr; remotePort = ntohs(vp->name[ 19 ]); printf("%-5.5s", name); inetprint(&localAddr, localPort, name, 1); inetprint(&remoteAddr, remotePort, name, 0); if ( state < 1 || state > TCP_NSTATES ) printf("%d\n", state ); else printf("%s\n", tcpstates[ state ]); } snmp_free_varbind( var );}/* * Print a summary of UDP "connections" * XXX - what about "listening" services ?? */voidudpprotopr(const char *name){ netsnmp_variable_list *var, *vp; oid udpLocalAddress_oid[] = { 1,3,6,1,2,1,7,5,1,1 }; size_t udpLocalAddress_len = OID_LENGTH( udpLocalAddress_oid ); union { struct in_addr addr; char data[4]; } tmpAddr; struct in_addr localAddr; oid localPort; char *cp; /* * Walking a single column of the udpTable will provide * all the necessary information from the index values. */ var = NULL; snmp_varlist_add_variable( &var, udpLocalAddress_oid, udpLocalAddress_len, ASN_NULL, NULL, 0); if (!var) return; if (netsnmp_query_walk( var, ss ) != SNMP_ERR_NOERROR) return; printf("Active Internet (%s) Connections\n", name); printf("%-5.5s %-28.28s\n", "Proto", "Local Address"); for (vp = var; vp ; vp=vp->next_variable) { printf("%-5.5s", name); /* * Extract the local port from the index values, but take * the IP address from the varbind value, (which is why * we walked udpLocalAddress rather than udpLocalPort) */ cp = tmpAddr.data; cp[0] = vp->name[ 10 ] & 0xff; cp[1] = vp->name[ 11 ] & 0xff; cp[2] = vp->name[ 12 ] & 0xff; cp[3] = vp->name[ 13 ] & 0xff; localAddr.s_addr = tmpAddr.addr.s_addr; localPort = ntohs( vp->name[ 14 ]); inetprint(&localAddr, localPort, name, 1); putchar('\n'); } snmp_free_varbind( var );} /********************* * * Internet-protocol statistics * *********************/void_dump_stats( const char *name, oid *oid_buf, size_t buf_len, struct stat_table *stable ){ netsnmp_variable_list *var, *vp; struct stat_table *sp; oid stat; var = NULL; for (sp=stable; sp->entry; sp++) { oid_buf[buf_len-2] = sp->entry; snmp_varlist_add_variable( &var, oid_buf, buf_len, ASN_NULL, NULL, 0); } if (netsnmp_query_get( var, ss ) != SNMP_ERR_NOERROR) { /* Need to fix and re-try SNMPv1 errors */ snmp_free_var( var ); return; } printf("%s:\n", name); sp=stable; for (vp=var; vp; vp=vp->next_variable, sp++) { /* * Match the returned results against * the original stats table. */ stat = vp->name[buf_len-2]; while (sp->entry < stat) { sp++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -