[SNMPAPI]
[reactos.git] / reactos / dll / win32 / snmpapi / main.c
1 /*
2 * Implementation of SNMPAPI.DLL
3 *
4 * Copyright 2002 Patrik Stridvall
5 * Copyright 2007 Hans Leidekker
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define WIN32_NO_STATUS
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include <windef.h>
30 #include <winbase.h>
31 #include <snmp.h>
32
33 #include <wine/debug.h>
34
35 WINE_DEFAULT_DEBUG_CHANNEL(snmpapi);
36
37 static INT asn_any_copy(AsnAny *dst, const AsnAny *src)
38 {
39 memset(dst, 0, sizeof(AsnAny));
40 switch (src->asnType)
41 {
42 case ASN_INTEGER32: dst->asnValue.number = src->asnValue.number; break;
43 case ASN_UNSIGNED32: dst->asnValue.unsigned32 = src->asnValue.unsigned32; break;
44 case ASN_COUNTER64: dst->asnValue.counter64 = src->asnValue.counter64; break;
45 case ASN_COUNTER32: dst->asnValue.counter = src->asnValue.counter; break;
46 case ASN_GAUGE32: dst->asnValue.gauge = src->asnValue.gauge; break;
47 case ASN_TIMETICKS: dst->asnValue.ticks = src->asnValue.ticks; break;
48
49 case ASN_OCTETSTRING:
50 case ASN_BITS:
51 case ASN_SEQUENCE:
52 case ASN_IPADDRESS:
53 case ASN_OPAQUE:
54 {
55 BYTE *stream;
56 UINT length = src->asnValue.string.length;
57
58 if (!(stream = HeapAlloc(GetProcessHeap(), 0, length))) return SNMPAPI_ERROR;
59 memcpy(stream, src->asnValue.string.stream, length);
60
61 dst->asnValue.string.stream = stream;
62 dst->asnValue.string.length = length;
63 dst->asnValue.string.dynamic = TRUE;
64 break;
65 }
66 case ASN_OBJECTIDENTIFIER:
67 {
68 UINT *ids, i, size = src->asnValue.object.idLength * sizeof(UINT);
69
70 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
71
72 dst->asnValue.object.ids = ids;
73 dst->asnValue.object.idLength = src->asnValue.object.idLength;
74
75 for (i = 0; i < dst->asnValue.object.idLength; i++)
76 dst->asnValue.object.ids[i] = src->asnValue.object.ids[i];
77 break;
78 }
79 default:
80 {
81 WARN("unknown ASN type: %d\n", src->asnType);
82 return SNMPAPI_ERROR;
83 }
84 }
85 dst->asnType = src->asnType;
86 return SNMPAPI_NOERROR;
87 }
88
89 static void asn_any_free(AsnAny *any)
90 {
91 switch (any->asnType)
92 {
93 case ASN_OCTETSTRING:
94 case ASN_BITS:
95 case ASN_SEQUENCE:
96 case ASN_IPADDRESS:
97 case ASN_OPAQUE:
98 {
99 if (any->asnValue.string.dynamic)
100 {
101 HeapFree(GetProcessHeap(), 0, any->asnValue.string.stream);
102 any->asnValue.string.stream = NULL;
103 }
104 break;
105 }
106 case ASN_OBJECTIDENTIFIER:
107 {
108 HeapFree(GetProcessHeap(), 0, any->asnValue.object.ids);
109 any->asnValue.object.ids = NULL;
110 break;
111 }
112 default: break;
113 }
114 any->asnType = ASN_NULL;
115 }
116
117 static ULONGLONG startTime;
118
119 /***********************************************************************
120 * DllMain for SNMPAPI
121 */
122 BOOL WINAPI DllMain(
123 HINSTANCE hInstDLL,
124 DWORD fdwReason,
125 LPVOID lpvReserved)
126 {
127 TRACE("(%p,%d,%p)\n", hInstDLL, fdwReason, lpvReserved);
128
129 switch(fdwReason) {
130 case DLL_WINE_PREATTACH:
131 return FALSE; /* prefer native version */
132 case DLL_PROCESS_ATTACH:
133 DisableThreadLibraryCalls(hInstDLL);
134 startTime = GetTickCount64();
135 break;
136 }
137
138 return TRUE;
139 }
140
141 /***********************************************************************
142 * SnmpSvcGetUptime (SNMPAPI.@)
143 *
144 * BUGS
145 * This returns the number of centiseconds since the DLL was loaded,
146 * rather than the number of centiseconds since the SNMP service was
147 * started, since there isn't yet any SNMP service in Wine.
148 */
149 DWORD WINAPI SnmpSvcGetUptime(void)
150 {
151 ULONGLONG now = GetTickCount64();
152
153 return (now - startTime) / 10;
154 }
155
156 /***********************************************************************
157 * SnmpUtilDbgPrint (SNMPAPI.@)
158 *
159 * NOTES
160 * The Microsoft headers claim this function uses the stdcall calling
161 * convention. But stdcall functions cannot take a variable number of
162 * arguments so this does not make sense. The stdcall specification is
163 * probably ignored by Microsoft's compiler in this case. So declare it
164 * correctly in Wine so it works with all compilers.
165 */
166 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
167 {
168 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
169 }
170
171 /***********************************************************************
172 * SnmpUtilMemAlloc (SNMPAPI.@)
173 */
174 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
175 {
176 TRACE("(%d)\n", nbytes);
177 return HeapAlloc(GetProcessHeap(), 0, nbytes);
178 }
179
180 /***********************************************************************
181 * SnmpUtilMemReAlloc (SNMPAPI.@)
182 */
183 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
184 {
185 TRACE("(%p, %d)\n", mem, nbytes);
186 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
187 }
188
189 /***********************************************************************
190 * SnmpUtilMemFree (SNMPAPI.@)
191 */
192 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
193 {
194 TRACE("(%p)\n", mem);
195 HeapFree(GetProcessHeap(), 0, mem);
196 }
197
198 /***********************************************************************
199 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
200 */
201 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
202 {
203 TRACE("(%p, %p)\n", dst, src);
204 return asn_any_copy(dst, src);
205 }
206
207 /***********************************************************************
208 * SnmpUtilAsnAnyFree (SNMPAPI.@)
209 */
210 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
211 {
212 TRACE("(%p)\n", any);
213 asn_any_free(any);
214 }
215
216 /***********************************************************************
217 * SnmpUtilOctetsCpy (SNMPAPI.@)
218 */
219 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
220 {
221 TRACE("(%p, %p)\n", dst, src);
222
223 if (!dst) return SNMPAPI_ERROR;
224 if (!src)
225 {
226 dst->dynamic = FALSE;
227 dst->length = 0;
228 dst->stream = NULL;
229 return SNMPAPI_NOERROR;
230 }
231 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
232 {
233 unsigned int i;
234
235 dst->dynamic = TRUE;
236 dst->length = src->length;
237 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
238 return SNMPAPI_NOERROR;
239 }
240 return SNMPAPI_ERROR;
241 }
242
243 /***********************************************************************
244 * SnmpUtilOctetsFree (SNMPAPI.@)
245 */
246 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
247 {
248 TRACE("(%p)\n", octets);
249
250 if (octets)
251 {
252 octets->length = 0;
253 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
254 octets->stream = NULL;
255 octets->dynamic = FALSE;
256 }
257 }
258
259 /***********************************************************************
260 * SnmpUtilOctetsNCmp (SNMPAPI.@)
261 */
262 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
263 {
264 INT ret;
265 unsigned int i;
266
267 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
268
269 if (!octets1 || !octets2) return 0;
270
271 for (i = 0; i < count; i++)
272 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
273
274 return 0;
275 }
276
277 /***********************************************************************
278 * SnmpUtilOctetsCmp (SNMPAPI.@)
279 */
280 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
281 {
282 TRACE("(%p, %p)\n", octets1, octets2);
283
284 if (octets1->length < octets2->length) return -1;
285 if (octets1->length > octets2->length) return 1;
286
287 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
288 }
289
290 /***********************************************************************
291 * SnmpUtilOidAppend (SNMPAPI.@)
292 */
293 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
294 {
295 UINT *ids, i, size;
296
297 TRACE("(%p, %p)\n", dst, src);
298
299 if (!dst) return SNMPAPI_ERROR;
300 if (!src) return SNMPAPI_NOERROR;
301
302 size = (src->idLength + dst->idLength) * sizeof(UINT);
303 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
304 {
305 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
306 {
307 SetLastError(SNMP_MEM_ALLOC_ERROR);
308 return SNMPAPI_ERROR;
309 }
310 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
311 }
312
313 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
314 dst->idLength = dst->idLength + src->idLength;
315 dst->ids = ids;
316
317 return SNMPAPI_NOERROR;
318 }
319
320 /***********************************************************************
321 * SnmpUtilOidCpy (SNMPAPI.@)
322 */
323 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
324 {
325 TRACE("(%p, %p)\n", dst, src);
326
327 if (!dst) return SNMPAPI_ERROR;
328 if (!src)
329 {
330 dst->idLength = 0;
331 dst->ids = NULL;
332 return SNMPAPI_NOERROR;
333 }
334 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
335 {
336 unsigned int i;
337
338 dst->idLength = src->idLength;
339 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
340 return SNMPAPI_NOERROR;
341 }
342 return SNMPAPI_ERROR;
343 }
344
345 /***********************************************************************
346 * SnmpUtilOidFree (SNMPAPI.@)
347 */
348 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
349 {
350 TRACE("(%p)\n", oid);
351
352 if (!oid) return;
353
354 oid->idLength = 0;
355 HeapFree(GetProcessHeap(), 0, oid->ids);
356 oid->ids = NULL;
357 }
358
359 /***********************************************************************
360 * SnmpUtilOidNCmp (SNMPAPI.@)
361 */
362 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
363 {
364 unsigned int i, len;
365
366 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
367
368 if (!oid1 || !oid2) return 0;
369
370 len = min(count, oid1->idLength);
371 len = min(len, oid2->idLength);
372 for (i = 0; i < len; i++)
373 {
374 if (oid1->ids[i] > oid2->ids[i]) return 1;
375 if (oid1->ids[i] < oid2->ids[i]) return -1;
376 }
377 if (i == count) return 0;
378 if (oid1->idLength < oid2->idLength) return -1;
379 if (oid1->idLength > oid2->idLength) return 1;
380 return 0;
381 }
382
383 /***********************************************************************
384 * SnmpUtilOidCmp (SNMPAPI.@)
385 */
386 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
387 {
388 TRACE("(%p, %p)\n", oid1, oid2);
389
390 if (oid1->idLength < oid2->idLength) return -1;
391 if (oid1->idLength > oid2->idLength) return 1;
392
393 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
394 }
395
396 /***********************************************************************
397 * SnmpUtilVarBindCpy (SNMPAPI.@)
398 */
399 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
400 {
401 unsigned int i, size;
402
403 TRACE("(%p, %p)\n", dst, src);
404
405 if (!dst) return SNMPAPI_ERROR;
406 if (!src)
407 {
408 dst->value.asnType = ASN_NULL;
409 return SNMPAPI_NOERROR;
410 }
411
412 size = src->name.idLength * sizeof(UINT);
413 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
414
415 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
416 dst->name.idLength = src->name.idLength;
417
418 if (!asn_any_copy(&dst->value, &src->value))
419 {
420 HeapFree(GetProcessHeap(), 0, dst->name.ids);
421 return SNMPAPI_ERROR;
422 }
423 return SNMPAPI_NOERROR;
424 }
425
426 /***********************************************************************
427 * SnmpUtilVarBindFree (SNMPAPI.@)
428 */
429 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
430 {
431 TRACE("(%p)\n", vb);
432
433 if (!vb) return;
434
435 asn_any_free(&vb->value);
436 HeapFree(GetProcessHeap(), 0, vb->name.ids);
437 vb->name.idLength = 0;
438 vb->name.ids = NULL;
439 }
440
441 /***********************************************************************
442 * SnmpUtilVarBindListCpy (SNMPAPI.@)
443 */
444 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
445 {
446 unsigned int i, size;
447 SnmpVarBind *src_entry, *dst_entry;
448
449 TRACE("(%p, %p)\n", dst, src);
450
451 if (!src)
452 {
453 dst->list = NULL;
454 dst->len = 0;
455 return SNMPAPI_NOERROR;
456 }
457 size = src->len * sizeof(SnmpVarBind);
458 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
459 return SNMPAPI_ERROR;
460
461 src_entry = src->list;
462 dst_entry = dst->list;
463 for (i = 0; i < src->len; i++)
464 {
465 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
466 {
467 src_entry++;
468 dst_entry++;
469 }
470 else
471 {
472 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
473 HeapFree(GetProcessHeap(), 0, dst->list);
474 return SNMPAPI_ERROR;
475 }
476 }
477 dst->len = src->len;
478 return SNMPAPI_NOERROR;
479 }
480
481 /***********************************************************************
482 * SnmpUtilVarBindListFree (SNMPAPI.@)
483 */
484 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
485 {
486 unsigned int i;
487 SnmpVarBind *entry;
488
489 TRACE("(%p)\n", vb);
490
491 entry = vb->list;
492 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
493 HeapFree(GetProcessHeap(), 0, vb->list);
494 vb->list = NULL;
495 vb->len = 0;
496 }
497
498 /***********************************************************************
499 * SnmpUtilIdsToA (SNMPAPI.@)
500 */
501 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
502 {
503 static char one[10], oid[514], null_oid[] = "<null oid>";
504 unsigned int i, len, left = sizeof(oid) - 1;
505
506 TRACE("(%p, %d)\n", ids, length);
507
508 if (!ids || !length) return null_oid;
509
510 *oid = 0;
511 for (i = 0; i < length; i++)
512 {
513 sprintf(one, "%d", ids[i]);
514 len = strlen(one);
515 if (left >= len)
516 {
517 strcat(oid, one);
518 left -= len;
519 }
520 else return oid;
521
522 if (i < length - 1)
523 {
524 if (left > 0)
525 {
526 strcat(oid, ".");
527 left--;
528 }
529 else return oid;
530 }
531 }
532 return oid;
533 }
534
535 /***********************************************************************
536 * SnmpUtilOidToA (SNMPAPI.@)
537 */
538 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
539 {
540 static char null_oid[] = "<null oid>";
541
542 TRACE("(%p)\n", oid);
543
544 if (oid)
545 return SnmpUtilIdsToA(oid->ids, oid->idLength);
546 else
547 return null_oid;
548 }
549
550 /***********************************************************************
551 * SnmpUtilPrintOid (SNMPAPI.@)
552 */
553 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
554 {
555 unsigned int i;
556
557 TRACE("(%p)\n", oid);
558
559 if (!oid) return;
560
561 for (i = 0; i < oid->idLength; i++)
562 {
563 TRACE("%u", oid->ids[i]);
564 if (i < oid->idLength - 1) TRACE(".");
565 }
566 TRACE("\n");
567 }
568
569 /***********************************************************************
570 * SnmpUtilPrintAsnAny (SNMPAPI.@)
571 */
572 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
573 {
574 unsigned int i;
575
576 TRACE("(%p)\n", any);
577
578 switch (any->asnType)
579 {
580 case ASN_NULL: TRACE("Null value\n"); return;
581 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
582 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
583 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
584 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
585 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
586 case ASN_COUNTER64:
587 {
588 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
589 return;
590 }
591 case ASN_OCTETSTRING:
592 {
593 TRACE("String ");
594 for (i = 0; i < any->asnValue.string.length; i++)
595 TRACE("%c", any->asnValue.string.stream[i]);
596 TRACE("\n");
597 return;
598 }
599 case ASN_IPADDRESS:
600 {
601 TRACE("IpAddress ");
602 if (any->asnValue.string.length < 4)
603 {
604 TRACE("Invalid\n");
605 return;
606 }
607 for (i = 0; i < 4; i++)
608 {
609 TRACE("%u", any->asnValue.string.stream[i]);
610 if (i < 3) TRACE(".");
611 }
612 TRACE("\n");
613 return;
614 }
615 case ASN_BITS:
616 {
617 TRACE("Bits ");
618 for (i = 0; i < any->asnValue.string.length; i++)
619 {
620 TRACE("0x%02x", any->asnValue.string.stream[i]);
621 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
622 }
623 TRACE("\n");
624 return;
625 }
626 case ASN_OPAQUE:
627 {
628 TRACE("Opaque ");
629 for (i = 0; i < any->asnValue.string.length; i++)
630 {
631 TRACE("0x%02x", any->asnValue.string.stream[i]);
632 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
633 }
634 TRACE("\n");
635 return;
636 }
637 case ASN_OBJECTIDENTIFIER:
638 {
639 TRACE("ObjectID ");
640 for (i = 0; i < any->asnValue.object.idLength; i++)
641 {
642 TRACE("%u", any->asnValue.object.ids[i]);
643 if (i < any->asnValue.object.idLength - 1) TRACE(".");
644 }
645 TRACE("\n");
646 return;
647 }
648 default:
649 {
650 TRACE("Invalid type %d\n", any->asnType);
651 return;
652 }
653 }
654 }