Skip to content

Commit 96d7d72

Browse files
committed
Get rid of hardcoded interfaces limit
1 parent 0127807 commit 96d7d72

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

snmp_bulkget.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
*/
8585

8686

87-
void create_pdu(int, char **, netsnmp_pdu **, struct OIDStruct **, int, int);
87+
void create_pdu(int, char **, netsnmp_pdu **, struct OIDStruct **, int, long);
8888

8989

9090
/* hardware mode */
@@ -95,6 +95,8 @@ unsigned int uptime = 0, sleep_usecs = 0;
9595
unsigned int lastcheck = 0;
9696
unsigned long global_timeout = DFLT_TIMEOUT;
9797

98+
int ifNumber = 0;
99+
98100
int
99101
main(int argc, char *argv[])
100102
{
@@ -105,7 +107,6 @@ main(int argc, char *argv[])
105107
netsnmp_variable_list *vars;
106108
int status, status2;
107109
int count = 0; /* used for: the number of interfaces we receive, the number of regex matches */
108-
int ifNumber = 0;
109110
int i, j, k;
110111
int errorflag = 0;
111112
int warnflag = 0;
@@ -121,8 +122,8 @@ main(int argc, char *argv[])
121122
int bw = 0;
122123
size_t size,size2;
123124

124-
struct ifStruct interfaces[MAX_INTERFACES]; /* current interface data */
125-
struct ifStruct oldperfdata[MAX_INTERFACES]; /* previous check interface data */
125+
struct ifStruct *interfaces = NULL; /* current interface data */
126+
struct ifStruct *oldperfdata = NULL; /* previous check interface data */
126127
struct OIDStruct *OIDp;
127128

128129

@@ -174,11 +175,6 @@ main(int argc, char *argv[])
174175
oid_vals = oid_vals_default;
175176
if_vars = if_vars_default;
176177

177-
/* zero the interfaces array */
178-
memset(interfaces, '\0', sizeof (interfaces));
179-
memset(oldperfdata, '\0', sizeof (oldperfdata));
180-
181-
182178
char *progname = strrchr(argv[0], '/');
183179
if (*progname && *(progname+1))
184180
progname++;
@@ -418,7 +414,7 @@ main(int argc, char *argv[])
418414

419415
/* build our request depending on the mode */
420416
if (count==0)
421-
create_pdu(mode, oid_ifp, &pdu, &OIDp, 2, MAX_INTERFACES);
417+
create_pdu(mode, oid_ifp, &pdu, &OIDp, 2, MAX_REPETITIONS_LIMIT);
422418
else {
423419
/* we have not received all interfaces in the preceding packet, so fetch the next lot */
424420

@@ -427,7 +423,7 @@ main(int argc, char *argv[])
427423
else {
428424
pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
429425
pdu->non_repeaters = 0;
430-
pdu->max_repetitions = MAX_INTERFACES;
426+
pdu->max_repetitions = MAX_REPETITIONS_LIMIT;
431427
}
432428
snmp_add_null_var(pdu, lastOid.name, lastOid.name_len);
433429
}
@@ -467,11 +463,8 @@ main(int argc, char *argv[])
467463
vars = vars->next_variable;
468464
}
469465

470-
if (ifNumber > MAX_INTERFACES) {
471-
/* if MAX_INTERFACES is not enough then we need to recompile */
472-
printf("Error, this device has more than %d interfaces - you will need to alter the code and recompile, sorry.\n", MAX_INTERFACES);
473-
exit (3);
474-
}
466+
interfaces = (struct ifStruct*)calloc((size_t)ifNumber, sizeof(struct ifStruct));
467+
oldperfdata = (struct ifStruct*)calloc((size_t)ifNumber, sizeof(struct ifStruct));
475468

476469
#ifdef DEBUG
477470
fprintf(stderr, "got %d interfaces\n", ifNumber);
@@ -1391,7 +1384,7 @@ void set_value(struct ifStruct *oldperfdata, char *interface, char *var, u64 val
13911384
else
13921385
if_vars = if_vars_default;
13931386

1394-
for (i=0; i < MAX_INTERFACES; i++) {
1387+
for (i=0; i < ifNumber; i++) {
13951388
if (strcmp(interface, oldperfdata[i].descr) == 0) {
13961389
if (strcmp(var, if_vars[0]) == 0)
13971390
oldperfdata[i].inOctets = value;
@@ -1503,7 +1496,7 @@ int parseoids(int i, char *oid_list, struct OIDStruct *query)
15031496
return(0);
15041497
}
15051498

1506-
void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **oids, int nonrepeaters, int max)
1499+
void create_pdu(int mode, char **oidlist, netsnmp_pdu **pdu, struct OIDStruct **oids, int nonrepeaters, long max)
15071500
{
15081501
int i;
15091502
static char **oid_ifp;

snmp_bulkget.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22

3+
#include <limits.h>
4+
35
#ifdef HAVE_GETADDRINFO
46
#include <sys/types.h>
57
#include <sys/socket.h>
@@ -11,11 +13,10 @@
1113

1214
/*
1315
* defines
14-
* MAX_INTERFACES = allocate memory for this number of interfaces
16+
* MAX_REPETITIONS_LIMIT = limit for netsnmp_pdu.max_repetitions
1517
* MAX_STRING = allocate memory for this length of output string
1618
*/
17-
/*#define MAX_INTERFACES 64 */
18-
#define MAX_INTERFACES 4096
19+
#define MAX_REPETITIONS_LIMIT (LONG_MAX - 1L)
1920
#define MAX_STRING 65536
2021
#define MAX_DESCR_LEN 60
2122
#define UPTIME_TOLERANCE_IN_SECS 30

0 commit comments

Comments
 (0)