-
Notifications
You must be signed in to change notification settings - Fork 35
/
oflops_snmp.c
65 lines (55 loc) · 1.91 KB
/
oflops_snmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "oflops_snmp.h"
#include <utils.h>
int snmp_channel_init(struct snmp_channel* channel,
char* host, char* community_string)
{
bzero(channel, sizeof(snmp_channel));
channel->hostname = strdup(host);
channel->community_string = strdup(community_string);
return 0;
}
void setup_snmp_channel(struct oflops_context* ctx)
{
if ((ctx->snmp_channel_info->hostname == NULL) ||
(ctx->snmp_channel_info->community_string == NULL))
return;
fprintf(stderr, "Setting up SNMP\n");
init_snmp("oflops");
init_mib();
add_mibdir("/var/lib/mibs/ietf/");
snmp_sess_init(&ctx->snmp_channel_info->session);
ctx->snmp_channel_info->session.version = SNMP_VERSION_2c;
ctx->snmp_channel_info->session.peername = \
ctx->snmp_channel_info->hostname;
ctx->snmp_channel_info->session.community = \
(unsigned char*) ctx->snmp_channel_info->community_string;
ctx->snmp_channel_info->session.community_len = \
strlen(ctx->snmp_channel_info->community_string);
ctx->snmp_channel_info->session.callback = snmp_response;
ctx->snmp_channel_info->session.callback_magic = ctx;
ctx->snmp_channel_info->req = NULL;
}
int snmp_response(int operation, struct snmp_session *sp, int reqid,
struct snmp_pdu *pdu, void * magic)
{
struct oflops_context* ctx = (struct oflops_context*) magic;
if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE)
{
struct snmp_event* se = malloc_and_check(sizeof(snmp_event));
se->pdu = pdu;
se->reply = pdu->variables;
ctx->curr_test->handle_snmp_event(ctx, se);
free(se);
}
return 0;
}
void teardown_snmp_channel(struct oflops_context* ctx)
{
if (ctx->snmp_channel_info->req != NULL)
snmp_free_pdu(ctx->snmp_channel_info->req);
if ((ctx->snmp_channel_info->hostname == NULL) ||
(ctx->snmp_channel_info->community_string == NULL))
return;
fprintf(stderr, "Tearing down SNMP\n");
snmp_close(&ctx->snmp_channel_info->session);
}