| 1 |
/* |
| 2 |
* Template MIB group implementation - at.c |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/* Portions of this file are subject to the following copyright(s). See |
| 7 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 8 |
* that may apply: |
| 9 |
*/ |
| 10 |
/* |
| 11 |
* Portions of this file are copyrighted by: |
| 12 |
* Copyright © 2003 Sun Microsystems, Inc. All rights reserved. |
| 13 |
* Use is subject to license terms specified in the COPYING file |
| 14 |
* distributed with the Net-SNMP package. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <net-snmp/net-snmp-config.h> |
| 18 |
#include <net-snmp/net-snmp-includes.h> |
| 19 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 20 |
#include <net-snmp/data_access/interface.h> |
| 21 |
#include "../at.h" |
| 22 |
#ifdef HAVE_STRING_H |
| 23 |
#include <string.h> |
| 24 |
#else |
| 25 |
#include <strings.h> |
| 26 |
#endif |
| 27 |
#ifdef HAVE_STDLIB_H |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
#if defined(NETSNMP_IFNET_NEEDS_KERNEL) && !defined(_KERNEL) |
| 31 |
#define _KERNEL 1 |
| 32 |
#define _I_DEFINED_KERNEL |
| 33 |
#endif |
| 34 |
#include <sys/types.h> |
| 35 |
#ifdef TIME_WITH_SYS_TIME |
| 36 |
# include <sys/time.h> |
| 37 |
# include <time.h> |
| 38 |
#else |
| 39 |
# ifdef HAVE_SYS_TIME_H |
| 40 |
# include <sys/time.h> |
| 41 |
# else |
| 42 |
# include <time.h> |
| 43 |
# endif |
| 44 |
#endif |
| 45 |
#ifdef HAVE_SYS_SOCKET_H |
| 46 |
#include <sys/socket.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef HAVE_NETINET_IN_H |
| 50 |
#include <netinet/in.h> |
| 51 |
#endif |
| 52 |
#ifdef HAVE_NET_IF_H |
| 53 |
#include <net/if.h> |
| 54 |
#endif |
| 55 |
#ifdef HAVE_NET_IF_VAR_H |
| 56 |
#include <net/if_var.h> |
| 57 |
#endif |
| 58 |
#ifdef _I_DEFINED_KERNEL |
| 59 |
#undef _KERNEL |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef HAVE_NETINET_IF_ETHER_H |
| 63 |
#include <netinet/if_ether.h> |
| 64 |
#endif |
| 65 |
#ifdef HAVE_INET_MIB2_H |
| 66 |
#include <inet/mib2.h> |
| 67 |
#endif |
| 68 |
#ifdef HAVE_SYS_PARAM_H |
| 69 |
#include <sys/param.h> |
| 70 |
#endif |
| 71 |
#ifdef HAVE_SYS_SYSCTL_H |
| 72 |
#include <sys/sysctl.h> |
| 73 |
#endif |
| 74 |
#ifdef HAVE_NET_IF_DL_H |
| 75 |
#ifndef dynix |
| 76 |
#include <net/if_dl.h> |
| 77 |
#else |
| 78 |
#include <sys/net/if_dl.h> |
| 79 |
#endif |
| 80 |
#endif |
| 81 |
#ifdef HAVE_SYS_STREAM_H |
| 82 |
#include <sys/stream.h> |
| 83 |
#endif |
| 84 |
#ifdef HAVE_NET_ROUTE_H |
| 85 |
#include <net/route.h> |
| 86 |
#endif |
| 87 |
#ifdef solaris2 |
| 88 |
#include "kernel_sunos5.h" |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifdef hpux11 |
| 92 |
#include <sys/mib.h> |
| 93 |
#include <netinet/mib_kern.h> |
| 94 |
#endif /* hpux11 */ |
| 95 |
|
| 96 |
static int arptab_size, arptab_current; |
| 97 |
static mib_ipNetToMediaEnt *at; |
| 98 |
|
| 99 |
void |
| 100 |
ARP_Scan_Init(void) |
| 101 |
{ |
| 102 |
int fd; |
| 103 |
struct nmparms p; |
| 104 |
int val; |
| 105 |
unsigned int ulen; |
| 106 |
int ret; |
| 107 |
|
| 108 |
if (at) |
| 109 |
free(at); |
| 110 |
at = (mib_ipNetToMediaEnt *) 0; |
| 111 |
arptab_size = 0; |
| 112 |
|
| 113 |
if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { |
| 114 |
p.objid = ID_ipNetToMediaTableNum; |
| 115 |
p.buffer = (void *) &val; |
| 116 |
ulen = sizeof(int); |
| 117 |
p.len = &ulen; |
| 118 |
if ((ret = get_mib_info(fd, &p)) == 0) |
| 119 |
arptab_size = val; |
| 120 |
|
| 121 |
if (arptab_size > 0) { |
| 122 |
ulen = (unsigned) arptab_size *sizeof(mib_ipNetToMediaEnt); |
| 123 |
at = (mib_ipNetToMediaEnt *) malloc(ulen); |
| 124 |
memset(at, 0, ulen); |
| 125 |
p.objid = ID_ipNetToMediaTable; |
| 126 |
p.buffer = (void *) at; |
| 127 |
p.len = &ulen; |
| 128 |
if ((ret = get_mib_info(fd, &p)) < 0) |
| 129 |
arptab_size = 0; |
| 130 |
else |
| 131 |
arptab_size = *p.len / sizeof(mib_ipNetToMediaEnt); |
| 132 |
} |
| 133 |
|
| 134 |
close_mib(fd); |
| 135 |
} |
| 136 |
|
| 137 |
arptab_current = 0; |
| 138 |
} |
| 139 |
|
| 140 |
int |
| 141 |
ARP_Scan_Next(in_addr_t * IPAddr, char *PhysAddr, int *PhysAddrLen, |
| 142 |
u_long * ifType, u_short * ifIndex) |
| 143 |
{ |
| 144 |
*ifIndex = 0; |
| 145 |
|
| 146 |
if (arptab_current < arptab_size) { |
| 147 |
/* |
| 148 |
* copy values |
| 149 |
*/ |
| 150 |
*IPAddr = at[arptab_current].NetAddr; |
| 151 |
memcpy(PhysAddr, at[arptab_current].PhysAddr.o_bytes, |
| 152 |
at[arptab_current].PhysAddr.o_length); |
| 153 |
*ifType = at[arptab_current].Type; |
| 154 |
*ifIndex = at[arptab_current].IfIndex; |
| 155 |
*PhysAddrLen = at[arptab_current].PhysAddr.o_length; |
| 156 |
/* |
| 157 |
* increment to point next entry |
| 158 |
*/ |
| 159 |
arptab_current++; |
| 160 |
/* |
| 161 |
* return success |
| 162 |
*/ |
| 163 |
return (1); |
| 164 |
} |
| 165 |
} |