| 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 |
#include <linux/if_arp.h> |
| 23 |
|
| 24 |
/* |
| 25 |
* at used to be allocated every time we needed to look at the arp cache. |
| 26 |
* This cause us to parse /proc/net/arp twice for each request and didn't |
| 27 |
* allow us to filter things like we'd like to. So now we use it |
| 28 |
* semi-statically. We initialize it to size 0 and if we need more room |
| 29 |
* we realloc room for ARP_CACHE_INCR more entries in the table. |
| 30 |
* We never release what we've taken . . . |
| 31 |
*/ |
| 32 |
#define ARP_CACHE_INCR 1024 |
| 33 |
|
| 34 |
static int arptab_size, arptab_current; |
| 35 |
static struct arptab *at; |
| 36 |
|
| 37 |
void |
| 38 |
ARP_Scan_Init(void) |
| 39 |
{ |
| 40 |
static time_t tm = 0; /* Time of last scan */ |
| 41 |
FILE *in; |
| 42 |
int i, j; |
| 43 |
char line[128]; |
| 44 |
int za, zb, zc, zd; |
| 45 |
char ifname[21]; |
| 46 |
char mac[3*MAX_MAC_ADDR_LEN+1]; |
| 47 |
char *tok; |
| 48 |
|
| 49 |
arptab_current = 0; /* Anytime this is called we need to reset 'current' */ |
| 50 |
|
| 51 |
if (time(NULL) < tm + 1) { /*Our cool one second cache implementation :-) */ |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
in = fopen("/proc/net/arp", "r"); |
| 56 |
if (!in) { |
| 57 |
snmp_log_perror("mibII/at: Cannot open /proc/net/arp"); |
| 58 |
arptab_size = 0; |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* Get rid of the header line |
| 64 |
*/ |
| 65 |
NETSNMP_IGNORE_RESULT(fgets(line, sizeof(line), in)); |
| 66 |
|
| 67 |
i = 0; |
| 68 |
while (fgets(line, sizeof(line), in)) { |
| 69 |
static int arptab_curr_max_size; |
| 70 |
u_long tmp_a; |
| 71 |
unsigned int tmp_flags; |
| 72 |
|
| 73 |
if (i >= arptab_curr_max_size) { |
| 74 |
struct arptab *newtab = (struct arptab *) |
| 75 |
realloc(at, (sizeof(struct arptab) * |
| 76 |
(arptab_curr_max_size + ARP_CACHE_INCR))); |
| 77 |
if (newtab == NULL) { |
| 78 |
snmp_log(LOG_ERR, |
| 79 |
"Error allocating more space for arpcache. " |
| 80 |
"Cache will continue to be limited to %d entries", |
| 81 |
arptab_curr_max_size); |
| 82 |
newtab = at; |
| 83 |
break; |
| 84 |
} else { |
| 85 |
arptab_curr_max_size += ARP_CACHE_INCR; |
| 86 |
at = newtab; |
| 87 |
} |
| 88 |
} |
| 89 |
if (7 != sscanf(line, "%d.%d.%d.%d 0x%*x 0x%x %s %*[^ ] %20s\n", |
| 90 |
&za, &zb, &zc, &zd, &tmp_flags, mac, ifname)) { |
| 91 |
snmp_log(LOG_ERR, "Bad line in /proc/net/arp: %s", line); |
| 92 |
continue; |
| 93 |
} |
| 94 |
/* |
| 95 |
* Invalidated entries have their flag set to 0. |
| 96 |
* * We want to ignore them |
| 97 |
*/ |
| 98 |
if (tmp_flags == 0) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
ifname[sizeof(ifname)-1] = 0; /* make sure name is null terminated */ |
| 102 |
at[i].at_flags = tmp_flags; |
| 103 |
tmp_a = ((u_long) za << 24) | |
| 104 |
((u_long) zb << 16) | ((u_long) zc << 8) | ((u_long) zd); |
| 105 |
at[i].at_iaddr.s_addr = htonl(tmp_a); |
| 106 |
at[i].if_index = netsnmp_access_interface_index_find(ifname); |
| 107 |
|
| 108 |
for (j=0,tok=strtok(mac, ":"); tok != NULL; tok=strtok(NULL, ":"),j++) { |
| 109 |
at[i].at_enaddr[j] = strtol(tok, NULL, 16); |
| 110 |
} |
| 111 |
at[i].at_enaddr_len = j; |
| 112 |
i++; |
| 113 |
} |
| 114 |
arptab_size = i; |
| 115 |
|
| 116 |
fclose(in); |
| 117 |
time(&tm); |
| 118 |
} |
| 119 |
|
| 120 |
int |
| 121 |
ARP_Scan_Next(in_addr_t * IPAddr, char *PhysAddr, int *PhysAddrLen, |
| 122 |
u_long * ifType, u_short * ifIndex) |
| 123 |
{ |
| 124 |
if (arptab_current >= arptab_size) |
| 125 |
return 0; |
| 126 |
|
| 127 |
/* |
| 128 |
* copy values |
| 129 |
*/ |
| 130 |
*IPAddr = at[arptab_current].at_iaddr.s_addr; |
| 131 |
*ifType = at[arptab_current].at_flags & ATF_PERM ? |
| 132 |
4 /*static */ : 3 /*dynamic */; |
| 133 |
*ifIndex = at[arptab_current].if_index; |
| 134 |
memcpy(PhysAddr, &at[arptab_current].at_enaddr, |
| 135 |
sizeof(at[arptab_current].at_enaddr)); |
| 136 |
*PhysAddrLen = at[arptab_current].at_enaddr_len; |
| 137 |
|
| 138 |
/* |
| 139 |
* increment to point next entry |
| 140 |
*/ |
| 141 |
arptab_current++; |
| 142 |
/* |
| 143 |
* return success |
| 144 |
*/ |
| 145 |
return 1; |
| 146 |
} |