-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoid.c
More file actions
196 lines (159 loc) · 4.79 KB
/
Copy pathoid.c
File metadata and controls
196 lines (159 loc) · 4.79 KB
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
/*******************************************************************************
* *
* Brno University of Technology *
* Faculty of Information Technology *
* *
* Exportér SNMP Gauge metrik do OpenTelemetry (OTEL) *
* *
* Author: Hugo Bohácsek [xbohach00 AT stud.fit.vutbr.cz] *
* Brno 2025 *
* *
* Implementation of the OID parsing and handling functions *
* *
*******************************************************************************/
#include "snmp2otel.h"
int parse_oid_string(const char *oid_str, oid_t *oid) {
char buffer[MAX_LINE_LEN];
char *token;
int i = 0;
if (!oid_str || !oid) {
return -1;
}
/* Initialize OID structure */
memset(oid, 0, sizeof(oid_t));
strncpy(buffer, oid_str, MAX_LINE_LEN - 1);
buffer[MAX_LINE_LEN - 1] = '\0';
/* Skip to the interesting parts */
char *start = buffer;
while (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r') {
start++;
}
char *end = start + strlen(start) - 1;
while (end > start && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
*end = '\0';
end--;
}
if (*start == '.') {
start++;
}
/* Parse OID numbers */
token = strtok(start, ".");
while (token != NULL && i < MAX_OID_LEN) {
char *endptr;
long num = strtol(token, &endptr, 10);
if (*endptr != '\0' || num < 0 || num > UINT_MAX) {
log_error("Invalid OID component: %s\n", token);
return -1;
}
oid->numbers[i] = (unsigned int)num;
i++;
token = strtok(NULL, ".");
}
if (i == 0) {
log_error("Empty OID\n");
return -1;
}
if (token != NULL) {
log_error("OID too long (max %d components)\n", MAX_OID_LEN);
return -1;
}
oid->length = i;
/* Store string representation */
snprintf(oid->str, sizeof(oid->str), "%s", oid_str);
/* Normalize string representation */
snprintf(oid->str, sizeof(oid->str), "%u", oid->numbers[0]);
for (int j = 1; j < oid->length; j++) {
char temp[32];
snprintf(temp, sizeof(temp), ".%u", oid->numbers[j]);
strncat(oid->str, temp, sizeof(oid->str) - strlen(oid->str) - 1);
}
return 0;
}
int load_oids_from_file(const char *filename, oid_list_t *oid_list) {
FILE *file;
char line[MAX_LINE_LEN];
int line_number = 0;
if (!filename || !oid_list) {
return -1;
}
/* Initialize OID list */
memset(oid_list, 0, sizeof(oid_list_t));
/* Open file and read from it */
file = fopen(filename, "r");
if (!file) {
log_error("Cannot open OID file '%s': %s\n", filename, strerror(errno));
return -1;
}
while (fgets(line, sizeof(line), file) != NULL && oid_list->count < MAX_OID_COUNT) {
line_number++;
char *newline = strchr(line, '\n');
if (newline) {
*newline = '\0';
}
/* Skip empty lines and comments */
char *start = line;
while (*start == ' ' || *start == '\t') {
start++;
}
if (*start == '\0' || *start == '#') {
continue;
}
/* Parse OID */
oid_t oid;
if (parse_oid_string(start, &oid) == 0) {
/* Check for duplicates */
int duplicate = 0;
for (int i = 0; i < oid_list->count; i++) {
if (compare_oids(&oid, &oid_list->oids[i]) == 0) {
log_message(verbose_mode, "Warning: Duplicate OID %s on line %d\n", oid.str, line_number);
duplicate = 1;
break;
}
}
if (!duplicate) {
oid_list->oids[oid_list->count] = oid;
oid_list->count++;
log_message(verbose_mode, "Loaded OID: %s\n", oid.str);
}
} else {
log_error("Invalid OID on line %d: %s\n", line_number, start);
fclose(file);
return -1;
}
}
fclose(file);
if (oid_list->count == 0) {
log_error("No valid OIDs found in file '%s'\n", filename);
return -1;
}
if (oid_list->count >= MAX_OID_COUNT) {
log_message(verbose_mode, "Warning: Maximum OID limit (%d) reached\n", MAX_OID_COUNT);
}
return 0;
}
void print_oid(const oid_t *oid) {
if (!oid) {
return;
}
printf("%s", oid->str);
}
int compare_oids(const oid_t *oid1, const oid_t *oid2) {
if (!oid1 || !oid2) {
return -1;
}
/* Compare lengths */
if (oid1->length != oid2->length) {
return oid1->length - oid2->length;
}
/* Compare components */
for (int i = 0; i < oid1->length; i++) {
if (oid1->numbers[i] != oid2->numbers[i]) {
if (oid1->numbers[i] < oid2->numbers[i]) {
return -1;
} else {
return 1;
}
}
}
return 0;
}