| 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_NET_IF_H |
| 23 |
#include <net/if.h> |
| 24 |
#endif |
| 25 |
#ifdef HAVE_NET_IF_DL_H |
| 26 |
#include <net/if_dl.h> |
| 27 |
#endif |
| 28 |
#ifdef HAVE_NETINET_IF_ETHER_H |
| 29 |
#include <netinet/if_ether.h> |
| 30 |
#endif |
| 31 |
#ifdef HAVE_NET_ROUTE_H |
| 32 |
#include <net/route.h> |
| 33 |
#endif |
| 34 |
#ifdef HAVE_NETINET_IN_H |
| 35 |
#include <netinet/in.h> |
| 36 |
#endif |
| 37 |
#ifdef HAVE_SYS_SYSCTL_H |
| 38 |
#include <sys/sysctl.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
static char *lim, *rtnext; |
| 42 |
static char *at; |
| 43 |
|
| 44 |
void |
| 45 |
ARP_Scan_Init(void) |
| 46 |
{ |
| 47 |
int mib[6]; |
| 48 |
size_t needed; |
| 49 |
|
| 50 |
mib[0] = CTL_NET; |
| 51 |
mib[1] = PF_ROUTE; |
| 52 |
mib[2] = 0; |
| 53 |
mib[3] = AF_INET; |
| 54 |
mib[4] = NET_RT_FLAGS; |
| 55 |
#if defined RTF_LLINFO |
| 56 |
mib[5] = RTF_LLINFO; |
| 57 |
#elif defined(RTF_LLDATA) |
| 58 |
mib[5] = RTF_LLDATA; |
| 59 |
#else |
| 60 |
mib[5] = 0; |
| 61 |
#endif |
| 62 |
|
| 63 |
if (at) |
| 64 |
free(at); |
| 65 |
rtnext = lim = at = 0; |
| 66 |
|
| 67 |
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) |
| 68 |
snmp_log_perror("route-sysctl-estimate"); |
| 69 |
else { |
| 70 |
if ((at = malloc(needed ? needed : 1)) == NULL) |
| 71 |
snmp_log_perror("malloc"); |
| 72 |
else { |
| 73 |
if (sysctl(mib, 6, at, &needed, NULL, 0) < 0) |
| 74 |
snmp_log_perror("actual retrieval of routing table"); |
| 75 |
else { |
| 76 |
lim = at + needed; |
| 77 |
rtnext = at; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
int |
| 84 |
ARP_Scan_Next(in_addr_t * IPAddr, char *PhysAddr, int *PhysAddrLen, |
| 85 |
u_long * ifType, u_short * ifIndex) |
| 86 |
{ |
| 87 |
struct rt_msghdr *rtm; |
| 88 |
struct sockaddr_inarp *sin; |
| 89 |
struct sockaddr_dl *sdl; |
| 90 |
|
| 91 |
while (rtnext < lim) { |
| 92 |
rtm = (struct rt_msghdr *) rtnext; |
| 93 |
sin = (struct sockaddr_inarp *) (rtm + 1); |
| 94 |
sdl = (struct sockaddr_dl *) (sin + 1); |
| 95 |
rtnext += rtm->rtm_msglen; |
| 96 |
if (sdl->sdl_alen) { |
| 97 |
#ifdef irix6 |
| 98 |
*IPAddr = sin->sarp_addr.s_addr; |
| 99 |
#else |
| 100 |
*IPAddr = sin->sin_addr.s_addr; |
| 101 |
#endif |
| 102 |
memcpy(PhysAddr, (char *) LLADDR(sdl), sdl->sdl_alen); |
| 103 |
*PhysAddrLen = sdl->sdl_alen; |
| 104 |
*ifIndex = sdl->sdl_index; |
| 105 |
*ifType = 1; /* XXX */ |
| 106 |
return 1; |
| 107 |
} |
| 108 |
} |
| 109 |
return 0; /* "EOF" */ |
| 110 |
} |