-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnmpSession.m
117 lines (107 loc) · 2.87 KB
/
SnmpSession.m
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// $Id$
// Copyright (C) 2006 Paul Kunysch. GNU GPL v2.
#import "SnmpSession.h"
char *errorString;
@implementation SnmpSession
-(id) initWithHost:(NSString*) host
community:(NSString*) community
{
if (nil == host || nil == community) return nil;
self = [super init];
if (nil == self) return nil;
init_snmp("SNMP-Test-Cocoa");
snmp_sess_init(&s);
s.version = SNMP_VERSION_1;
s.community = (u_char*)strdup([community UTF8String]);
s.community_len = strlen((char*)s.community);
s.peername = strdup([host UTF8String]);
s.timeout = 40000;
session = snmp_open(&s);
if (NULL == session) {
return nil;
}
return self;
}
-(void) dealloc
{
snmp_close(session);
}
-(id) stringForOid:(NSString *)oidStr
{
if (NULL == session)
{
NSLog(@"NULL session");
return nil;
}
id result = nil;
size_t length = MAX_OID_LEN;
oid objectid[MAX_OID_LEN];
read_objid([oidStr UTF8String], objectid, &length);
struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, objectid, length);
struct snmp_pdu *response;
if (STAT_SUCCESS != snmp_synch_response(session, pdu, &response))
return nil;
struct variable_list *var = response->variables;
switch (var->type) {
case 2: // INTEGER
result = [NSString stringWithFormat:@"%i",*(var->val.integer)];
break;
case 0x81: // HEX data
case 4: // STRING
result = [NSString stringWithCString:(char*)var->val.string length:var->val_len];
break;
default:
NSLog(@"Unsupported datatype 0x%02x for \"%@\"", var->type, oidStr);
result = @"Error";
break;
}
snmp_free_pdu(response);
return result;
// snmp_close(session);
}
-(void) setOidIntValue:(NSString *)objectIdString value:(int)intVal
{
char *errorString[100];
size_t errorString_Length;
netsnmp_variable_list *vars;
if (NULL == session)
{
NSLog(@"NULL session");
exit;
}
size_t length = MAX_OID_LEN;
oid objectid[MAX_OID_LEN];
read_objid([objectIdString UTF8String], objectid, &length);
struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_SET);
NSString * val = [NSString stringWithFormat:@"%i", intVal];
int addVarResult = snmp_add_var(pdu, objectid, length, 'i', [val UTF8String]);
if (addVarResult)
{
NSLog(@"snmp_add_var ERROR %s",snmp_api_errstring(addVarResult));
}
struct snmp_pdu *response = NULL;
int status = snmp_synch_response(session, pdu, &response);
if (status == STAT_SUCCESS)
{
if (response->errstat != SNMP_ERR_NOERROR)
{
NSLog(@"Error in packet.\nReason: %s\n",snmp_errstring(response->errstat));
if (response->errindex != 0)
{
vars = response->variables;
if (vars)
{
//fprint_objid(stderr, vars->name, vars->name_length);
snprint_objid(errorString, errorString_Length, vars->name, vars->name_length);
NSLog(@"Failed object: %@", [NSString stringWithFormat:@"%s",errorString]);
}
}
}
}
if (response)
{
snmp_free_pdu(response);
}
}
@end