-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.c
207 lines (157 loc) · 4.93 KB
/
db.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include "snmp.h"
#include "db.h"
unsigned int num_digits(unsigned int num) {
if (num < 10)
return 1;
return 1 + num_digits(num / 10);
}
struct oid_db *create_oid_db(void)
{
struct oid_db *db = (struct oid_db *) malloc(sizeof(struct oid_db));
db->length = 0;
db->oids = NULL;
return db;
}
int add_oid(struct oid_db *db, unsigned char *oid, unsigned char data_type, void *value)
{
struct varbind *varbind = NULL;
varbind = create_varbind(oid, data_type, value);
/* Increase database _pointer_ range */
db->oids = (struct varbind **) realloc(db->oids,
sizeof(struct varbind *) * (db->length + 1));
db->oids[db->length] = varbind;
db->length++;
/* Return index of added oid */
return (int) db->length - 1;
}
int del_oid(struct oid_db *db, unsigned char *oid)
{
int i;
int del_idx = search_oid(db, oid);
if(del_idx < 0) {
printf("From del_oid(): Couldn't found oid %s\n", oid);
return -1;
}
printf("Deleting %s (%d)\n", oid, del_idx);
clr_varbind(db->oids[del_idx]);
for(i = del_idx; i < db->length - 1; i++)
db->oids[i] = db->oids[i + 1];
db->length--;
return db->length;
}
/* Return index of required oid */
int search_oid(struct oid_db *db, unsigned char *oid)
{
unsigned int i;
for(i = 0; i < db->length; i++) {
if(!strcmp((char *) db->oids[i]->oid, (char *) oid))
return i;
}
/* Not found */
return -1;
}
int search_next_oid(struct oid_db *db, unsigned char *oid)
{
regex_t regex;
size_t nmatch = 2;
regmatch_t match[2];
int ret;
int idx;
/* Get minor oid part (witch will be incremented) */
ret = regcomp(®ex, "^[[:digit:]\\.]+\\.([[:digit:]]+)$", REG_EXTENDED);
if(ret) {
printf("Could not compile regex\n");
return -1;
}
ret = regexec(®ex, (const char *) oid, nmatch, match, 0);
regfree(®ex);
if(ret == REG_NOMATCH) {
printf("No match\n");
return -1;
} else if(ret != 0) {
/* Use regerror() to get regex error */
printf("regexec execution error\n");
return -1;
}
if(match[1].rm_so == -1) {
printf("Substring match fail\n");
return -1;
}
char *minor_str = malloc(match[1].rm_eo - match[1].rm_so + 1);
memcpy(minor_str, (char *) oid + match[1].rm_so,
match[1].rm_eo - match[1].rm_so);
*(minor_str + match[1].rm_eo - match[1].rm_so) = '\0';
int minor_dig = atoi(minor_str);
free(minor_str);
minor_dig++;
unsigned int minor_dig_len = num_digits(minor_dig);
minor_str = (char *) malloc(minor_dig_len + 1);
snprintf(minor_str, minor_dig_len + 1, "%d", minor_dig);
/* Get major oid part */
ret = regcomp(®ex, "^([[:digit:]\\.]+\\.)[[:digit:]]+$", REG_EXTENDED);
if(ret) {
printf("Couldn't compile regex\n");
return -1;
}
ret = regexec(®ex, (const char *) oid, (size_t) 2, match, 0);
regfree(®ex);
if(ret == REG_NOMATCH) {
printf("No match\n");
return -1;
} else if(ret != 0) {
/* Use regerror() to get regex error */
printf("regexec execution error\n");
return -1;
}
if(match[1].rm_so == -1) {
printf("Substring match fail\n");
return -1;
}
char *major_str = malloc(match[1].rm_eo - match[1].rm_so + 1);
memcpy(major_str, (char *) oid + match[1].rm_so,
match[1].rm_eo - match[1].rm_so);
*(major_str + match[1].rm_eo - match[1].rm_so) = '\0';
major_str = realloc(major_str, strlen(major_str) + strlen(minor_str) + 1);
strcat(major_str, minor_str);
free(minor_str);
idx = search_oid(db, (unsigned char *) major_str);
if(idx >= 0) {
/* Replace oid found */
oid = (unsigned char *) realloc(oid, strlen(major_str) + 1);
memcpy(oid, major_str, strlen(major_str) + 1);
free(major_str);
return idx;
}
/* Else build next level oid (just add ".1" to original oid) */
char *nl_oid = (char *) malloc(strlen((char *) oid) + 3);
memcpy(nl_oid, oid, strlen((char *)oid) + 1);
strcat(nl_oid, ".1");
idx = search_oid(db, (unsigned char *) nl_oid);
if(idx >= 0) {
/* Replace oid found */
oid = (unsigned char *) realloc(oid, strlen(nl_oid) + 1);
memcpy(oid, nl_oid, strlen(nl_oid) + 1);
free(nl_oid);
return idx;
}
/* Not found */
return -1;
}
void print_oid_db(struct oid_db *db)
{
unsigned int i;
printf("==== OID DB ====\n");
for(i = 0; i < db->length; i++) {
switch(db->oids[i]->data_type) {
case PRIMV_OCTSTR:
printf("=%s=\t=%s=\n", (char *) db->oids[i]->oid, (char *) db->oids[i]->value);
break;
default:
printf("=%s=\t=%d=\n", (char *) db->oids[i]->oid, *(int *) db->oids[i]->value);
}
}
}