Debian Codegraph @

agent/mibgroup/mibII/data_access/at_iphlpapi.c

raw
1 /*
2 * Template MIB group implementation - at.c
3 *
4 */
5
6 /* Portions of this file are subject to the following copyright(s). See
7 * the Net-SNMP's COPYING file for more details and other copyrights
8 * that may apply:
9 */
10 /*
11 * Portions of this file are copyrighted by:
12 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
13 * Use is subject to license terms specified in the COPYING file
14 * distributed with the Net-SNMP package.
15 */
16
17 #include <net-snmp/net-snmp-config.h>
18 #include <net-snmp/net-snmp-includes.h>
19 #include <net-snmp/agent/net-snmp-agent-includes.h>
20 #include <net-snmp/data_access/interface.h>
21 #include "../at.h"
22 #if defined(cygwin)
23 #include <windows.h>
24 #endif
25 #if defined(cygwin) || defined(mingw32)
26 #include <winerror.h>
27 #endif
28 #include <iphlpapi.h>
29
30 static WriteMethod write_arp;
31 MIB_IPNETROW *arp_row = NULL;
32 static int create_flag;
33
34 u_char *
35 var_atEntry(struct variable *vp,
36 oid * name,
37 size_t * length,
38 int exact, size_t * var_len, WriteMethod ** write_method)
39 {
40 /*
41 * Address Translation table object identifier is of form:
42 * 1.3.6.1.2.1.3.1.?.interface.1.A.B.C.D, where A.B.C.D is IP address.
43 * Interface is at offset 10,
44 * IPADDR starts at offset 12.
45 *
46 * IP Net to Media table object identifier is of form:
47 * 1.3.6.1.2.1.4.22.1.?.interface.A.B.C.D, where A.B.C.D is IP address.
48 * Interface is at offset 10,
49 * IPADDR starts at offset 11.
50 */
51 u_char *cp;
52 oid *op;
53 oid lowest[16];
54 oid current[16];
55 int oid_length;
56 int lowState = -1; /* Don't have one yet */
57 PMIB_IPNETTABLE pIpNetTable = NULL;
58 DWORD status = NO_ERROR;
59 DWORD dwActualSize = 0;
60 UINT i;
61 int j;
62 u_char dest_addr[4];
63 void *result = NULL;
64 static in_addr_t addr_ret;
65
66 /*
67 * fill in object part of name for current (less sizeof instance part)
68 */
69 memcpy((char *) current, (char *) vp->name,
70 (int) vp->namelen * sizeof(oid));
71
72 if (current[6] == 3) { /* AT group oid */
73 oid_length = 16;
74 } else { /* IP NetToMedia group oid */
75 oid_length = 15;
76 }
77
78 status = GetIpNetTable(pIpNetTable, &dwActualSize, TRUE);
79 if (status == ERROR_INSUFFICIENT_BUFFER) {
80 pIpNetTable = malloc(dwActualSize);
81 if (pIpNetTable)
82 status = GetIpNetTable(pIpNetTable, &dwActualSize, TRUE);
83 }
84
85 i = -1;
86
87 if (status == NO_ERROR) {
88 for (i = 0; i < pIpNetTable->dwNumEntries; ++i) {
89 current[10] = pIpNetTable->table[i].dwIndex;
90
91
92 if (current[6] == 3) { /* AT group oid */
93 current[11] = 1;
94 op = current + 12;
95 } else { /* IP NetToMedia group oid */
96 op = current + 11;
97 }
98 cp = (u_char *) & pIpNetTable->table[i].dwAddr;
99 *op++ = *cp++;
100 *op++ = *cp++;
101 *op++ = *cp++;
102 *op++ = *cp++;
103
104 if (exact) {
105 if (snmp_oid_compare(current, oid_length, name, *length) ==
106 0) {
107 memcpy((char *) lowest, (char *) current,
108 oid_length * sizeof(oid));
109 lowState = 0;
110 break; /* no need to search further */
111 }
112 } else {
113 if (snmp_oid_compare(current, oid_length, name, *length) >
114 0) {
115 memcpy((char *) lowest, (char *) current,
116 oid_length * sizeof(oid));
117 lowState = 0;
118 break; /* As the table is sorted, no need to search further */
119 }
120 }
121 }
122 }
123 if (arp_row == NULL) {
124 /*
125 * Free allocated memory in case of SET request's FREE phase
126 */
127 arp_row = (PMIB_IPNETROW) malloc(sizeof(MIB_IPNETROW));
128 }
129
130 if (lowState < 0 || status != NO_ERROR) {
131 /*
132 * for creation of new row, only ipNetToMediaTable case is considered
133 */
134 if (*length == 15 || *length == 16) {
135 create_flag = 1;
136 *write_method = write_arp;
137 arp_row->dwIndex = name[10];
138
139 if (*length == 15) { /* ipNetToMediaTable */
140 j = 11;
141 } else { /* at Table */
142
143 j = 12;
144 }
145
146 dest_addr[0] = (u_char) name[j];
147 dest_addr[1] = (u_char) name[j + 1];
148 dest_addr[2] = (u_char) name[j + 2];
149 dest_addr[3] = (u_char) name[j + 3];
150 arp_row->dwAddr = *((DWORD *) dest_addr);
151
152 arp_row->dwType = 4; /* Static */
153 arp_row->dwPhysAddrLen = 0;
154 }
155 goto out;
156 }
157
158 create_flag = 0;
159 memcpy((char *) name, (char *) lowest, oid_length * sizeof(oid));
160 *length = oid_length;
161 *write_method = write_arp;
162 netsnmp_assert(i < pIpNetTable->dwNumEntries);
163 *arp_row = pIpNetTable->table[i];
164
165 switch (vp->magic) {
166 case IPMEDIAIFINDEX: /* also ATIFINDEX */
167 *var_len = sizeof long_return;
168 long_return = pIpNetTable->table[i].dwIndex;
169 result = &long_return;
170 break;
171 case IPMEDIAPHYSADDRESS: /* also ATPHYSADDRESS */
172 *var_len = pIpNetTable->table[i].dwPhysAddrLen;
173 memcpy(return_buf, pIpNetTable->table[i].bPhysAddr, *var_len);
174 result = return_buf;
175 break;
176 case IPMEDIANETADDRESS: /* also ATNETADDRESS */
177 *var_len = sizeof(addr_ret);
178 addr_ret = pIpNetTable->table[i].dwAddr;
179 result = &addr_ret;
180 break;
181 case IPMEDIATYPE:
182 *var_len = sizeof long_return;
183 long_return = pIpNetTable->table[i].dwType;
184 result = &long_return;
185 break;
186 default:
187 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_atEntry\n",
188 vp->magic));
189 break;
190 }
191 out:
192 free(pIpNetTable);
193 return result;
194 }
195
196 int
197 write_arp(int action,
198 u_char * var_val,
199 u_char var_val_type,
200 size_t var_val_len, u_char * statP, oid * name, size_t length)
201 {
202 int var, retval = SNMP_ERR_NOERROR;
203 static PMIB_IPNETROW oldarp_row = NULL;
204 MIB_IPNETROW temp_row;
205 DWORD status = NO_ERROR;
206
207 /*
208 * IP Net to Media table object identifier is of form:
209 * 1.3.6.1.2.1.4.22.1.?.interface.A.B.C.D, where A.B.C.D is IP address.
210 * Interface is at offset 10,
211 * IPADDR starts at offset 11.
212 */
213
214 if (name[6] == 3) { /* AT group oid */
215 if (length != 16) {
216 snmp_log(LOG_ERR, "length error\n");
217 return SNMP_ERR_NOCREATION;
218 }
219 } else { /* IP NetToMedia group oid */
220 if (length != 15) {
221 snmp_log(LOG_ERR, "length error\n");
222 return SNMP_ERR_NOCREATION;
223 }
224 }
225
226
227 /*
228 * #define for ipNetToMediaTable entries are 1 less than corresponding sub-id in MIB
229 * * i.e. IPMEDIAIFINDEX defined as 0, but ipNetToMediaIfIndex registered as 1
230 */
231 var = name[9] - 1;
232 switch (action) {
233 case RESERVE1:
234 switch (var) {
235 case IPMEDIAIFINDEX:
236 if (var_val_type != ASN_INTEGER) {
237 snmp_log(LOG_ERR, "not integer\n");
238 return SNMP_ERR_WRONGTYPE;
239 }
240 if ((*((int *) var_val)) < 0) {
241 snmp_log(LOG_ERR, "invalid media ifIndex");
242 return SNMP_ERR_WRONGVALUE;
243 }
244 if (var_val_len > sizeof(int)) {
245 snmp_log(LOG_ERR, "bad length\n");
246 return SNMP_ERR_WRONGLENGTH;
247 }
248 break;
249 case IPMEDIANETADDRESS:
250 if (var_val_type != ASN_IPADDRESS) {
251 snmp_log(LOG_ERR, "not IP Address\n");
252 return SNMP_ERR_WRONGTYPE;
253 }
254 if ((*((int *) var_val)) < 0) {
255 snmp_log(LOG_ERR, "invalid media net address");
256 return SNMP_ERR_WRONGVALUE;
257 }
258 if (var_val_len > sizeof(DWORD)) {
259 snmp_log(LOG_ERR, "bad length\n");
260 return SNMP_ERR_WRONGLENGTH;
261 }
262 break;
263 case IPMEDIATYPE:
264 if (var_val_type != ASN_INTEGER) {
265 snmp_log(LOG_ERR, "not integer\n");
266 return SNMP_ERR_WRONGTYPE;
267 }
268 if ((*((int *) var_val)) < 1 || (*((int *) var_val)) > 4) {
269 snmp_log(LOG_ERR, "invalid media type");
270 return SNMP_ERR_WRONGVALUE;
271 }
272 if (var_val_len > sizeof(int)) {
273 snmp_log(LOG_ERR, "bad length\n");
274 return SNMP_ERR_WRONGLENGTH;
275 }
276 break;
277 case IPMEDIAPHYSADDRESS:
278 if (var_val_type != ASN_OCTET_STR) {
279 snmp_log(LOG_ERR, "not octet str");
280 return SNMP_ERR_WRONGTYPE;
281 }
282 if (var_val_len != 6) {
283 snmp_log(LOG_ERR,
284 "incorrect ipAddress length %" NETSNMP_PRIz "d",
285 var_val_len);
286 return SNMP_ERR_WRONGLENGTH;
287 }
288 break;
289 default:
290 DEBUGMSGTL(("snmpd", "unknown sub-id %d in write_rte\n",
291 var + 1));
292 return SNMP_ERR_NOTWRITABLE;
293 }
294 break;
295 case RESERVE2:
296 /*
297 * Save the old value, in case of UNDO
298 */
299 if (oldarp_row == NULL) {
300 oldarp_row = (PMIB_IPNETROW) malloc(sizeof(MIB_IPNETROW));
301 *oldarp_row = *arp_row;
302 }
303 break;
304 case ACTION: /* Perform the SET action (if reversible) */
305 switch (var) {
306
307 case IPMEDIAIFINDEX:
308 temp_row = *arp_row;
309 arp_row->dwIndex = *((int *) var_val);
310 /*
311 * In case of new entry, physical address is mandatory.
312 * * SetIpNetEntry will be done in COMMIT case
313 */
314 if (!create_flag) {
315 if (SetIpNetEntry(arp_row) != NO_ERROR) {
316 arp_row->dwIndex = temp_row.dwIndex;
317 retval = SNMP_ERR_COMMITFAILED;
318 }
319 /*
320 * Don't know yet, whether change in ifIndex creates new row or not
321 */
322 /*
323 * else{
324 */
325 /*
326 * temp_row.dwType = 2;
327 */
328 /*
329 * if(SetIpNetEntry(&temp_row) != NO_ERROR)
330 */
331 /*
332 * retval = SNMP_ERR_COMMITFAILED;
333 */
334 /*
335 * }
336 */
337 }
338 break;
339 case IPMEDIANETADDRESS:
340 temp_row = *arp_row;
341 arp_row->dwAddr = *((int *) var_val);
342 if (!create_flag) {
343 if (SetIpNetEntry(arp_row) != NO_ERROR) {
344 arp_row->dwAddr = oldarp_row->dwAddr;
345 retval = SNMP_ERR_COMMITFAILED;
346 } else {
347 temp_row.dwType = 2;
348 if (SetIpNetEntry(&temp_row) != NO_ERROR) {
349 snmp_log(LOG_ERR,
350 "Failed in ACTION, while deleting old row \n");
351 retval = SNMP_ERR_COMMITFAILED;
352 }
353 }
354 }
355 break;
356 case IPMEDIATYPE:
357 arp_row->dwType = *((int *) var_val);
358 if (!create_flag) {
359 if (SetIpNetEntry(arp_row) != NO_ERROR)
360 retval = SNMP_ERR_COMMITFAILED;
361 }
362 break;
363 case IPMEDIAPHYSADDRESS:
364 memcpy(arp_row->bPhysAddr, var_val, var_val_len);
365 arp_row->dwPhysAddrLen = var_val_len;
366 if (!create_flag) {
367 if (SetIpNetEntry(arp_row) != NO_ERROR)
368 retval = SNMP_ERR_COMMITFAILED;
369 }
370 break;
371 default:
372 DEBUGMSGTL(("snmpd", "unknown sub-id %d in write_arp\n",
373 var + 1));
374 retval = SNMP_ERR_NOTWRITABLE;
375 }
376 return retval;
377 case UNDO:
378 /*
379 * Reverse the SET action and free resources
380 */
381 if (oldarp_row != NULL) {
382 /*
383 * UNDO the changes done for existing entry.
384 */
385 if (!create_flag) {
386 if ((status = SetIpNetEntry(oldarp_row)) != NO_ERROR) {
387 snmp_log(LOG_ERR, "Error in case UNDO, status : %u\n",
388 (unsigned int)status);
389 retval = SNMP_ERR_UNDOFAILED;
390 }
391 }
392
393 if (oldarp_row->dwAddr != arp_row->dwAddr) {
394 arp_row->dwType = 2; /*If row was added/created delete that row */
395
396 if ((status = SetIpNetEntry(arp_row)) != NO_ERROR) {
397 snmp_log(LOG_ERR,
398 "Error while deleting added row, status : %u\n",
399 (unsigned int)status);
400 retval = SNMP_ERR_UNDOFAILED;
401 }
402 }
403 free(oldarp_row);
404 oldarp_row = NULL;
405 free(arp_row);
406 arp_row = NULL;
407 return retval;
408 }
409 break;
410 case COMMIT:
411 /*
412 * if new entry and physical address specified, create new entry
413 */
414 if (create_flag) {
415 if (arp_row->dwPhysAddrLen != 0) {
416 if ((status = CreateIpNetEntry(arp_row)) != NO_ERROR) {
417 snmp_log(LOG_ERR,
418 "Inside COMMIT: CreateIpNetEntry failed, status %u\n",
419 (unsigned int)status);
420 retval = SNMP_ERR_COMMITFAILED;
421 }
422 } else {
423 /*
424 * For new entry, physical address must be set.
425 */
426 snmp_log(LOG_ERR,
427 "Can't create new entry without physical address\n");
428 retval = SNMP_ERR_WRONGVALUE;
429 }
430 /*
431 * unset the create_flag, so that CreateIpNetEntry called only once
432 */
433 create_flag = 0;
434 }
435 /* FALL THROUGH */
436
437 case FREE:
438 /*
439 * Free any resources allocated
440 */
441 free(oldarp_row);
442 oldarp_row = NULL;
443 free(arp_row);
444 arp_row = NULL;
445 break;
446 }
447 return retval;
448 }