e66f3c9f5b9bf8cb66a120f0721f4b8d590df741
[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 case DLL_PROCESS_DETACH:
137 break;
138 }
139
140 return TRUE;
141 }
142
143 /***********************************************************************
144 * SnmpSvcGetUptime (SNMPAPI.@)
145 *
146 * BUGS
147 * This returns the number of centiseconds since the DLL was loaded,
148 * rather than the number of centiseconds since the SNMP service was
149 * started, since there isn't yet any SNMP service in Wine.
150 */
151 DWORD WINAPI SnmpSvcGetUptime(void)
152 {
153 ULONGLONG now = GetTickCount64();
154
155 return (now - startTime) / 10;
156 }
157
158 /***********************************************************************
159 * SnmpUtilDbgPrint (SNMPAPI.@)
160 *
161 * NOTES
162 * The Microsoft headers claim this function uses the stdcall calling
163 * convention. But stdcall functions cannot take a variable number of
164 * arguments so this does not make sense. The stdcall specification is
165 * probably ignored by Microsoft's compiler in this case. So declare it
166 * correctly in Wine so it works with all compilers.
167 */
168 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
169 {
170 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
171 }
172
173 /***********************************************************************
174 * SnmpUtilMemAlloc (SNMPAPI.@)
175 */
176 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
177 {
178 TRACE("(%d)\n", nbytes);
179 return HeapAlloc(GetProcessHeap(), 0, nbytes);
180 }
181
182 /***********************************************************************
183 * SnmpUtilMemReAlloc (SNMPAPI.@)
184 */
185 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
186 {
187 TRACE("(%p, %d)\n", mem, nbytes);
188 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
189 }
190
191 /***********************************************************************
192 * SnmpUtilMemFree (SNMPAPI.@)
193 */
194 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
195 {
196 TRACE("(%p)\n", mem);
197 HeapFree(GetProcessHeap(), 0, mem);
198 }
199
200 /***********************************************************************
201 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
202 */
203 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
204 {
205 TRACE("(%p, %p)\n", dst, src);
206 return asn_any_copy(dst, src);
207 }
208
209 /***********************************************************************
210 * SnmpUtilAsnAnyFree (SNMPAPI.@)
211 */
212 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
213 {
214 TRACE("(%p)\n", any);
215 asn_any_free(any);
216 }
217
218 /***********************************************************************
219 * SnmpUtilOctetsCpy (SNMPAPI.@)
220 */
221 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
222 {
223 TRACE("(%p, %p)\n", dst, src);
224
225 if (!dst) return SNMPAPI_ERROR;
226 if (!src)
227 {
228 dst->dynamic = FALSE;
229 dst->length = 0;
230 dst->stream = NULL;
231 return SNMPAPI_NOERROR;
232 }
233 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
234 {
235 unsigned int i;
236
237 dst->dynamic = TRUE;
238 dst->length = src->length;
239 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
240 return SNMPAPI_NOERROR;
241 }
242 return SNMPAPI_ERROR;
243 }
244
245 /***********************************************************************
246 * SnmpUtilOctetsFree (SNMPAPI.@)
247 */
248 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
249 {
250 TRACE("(%p)\n", octets);
251
252 if (octets)
253 {
254 octets->length = 0;
255 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
256 octets->stream = NULL;
257 octets->dynamic = FALSE;
258 }
259 }
260
261 /***********************************************************************
262 * SnmpUtilOctetsNCmp (SNMPAPI.@)
263 */
264 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
265 {
266 INT ret;
267 unsigned int i;
268
269 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
270
271 if (!octets1 || !octets2) return 0;
272
273 for (i = 0; i < count; i++)
274 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
275
276 return 0;
277 }
278
279 /***********************************************************************
280 * SnmpUtilOctetsCmp (SNMPAPI.@)
281 */
282 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
283 {
284 TRACE("(%p, %p)\n", octets1, octets2);
285
286 if (octets1->length < octets2->length) return -1;
287 if (octets1->length > octets2->length) return 1;
288
289 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
290 }
291
292 /***********************************************************************
293 * SnmpUtilOidAppend (SNMPAPI.@)
294 */
295 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
296 {
297 UINT *ids, i, size;
298
299 TRACE("(%p, %p)\n", dst, src);
300
301 if (!dst) return SNMPAPI_ERROR;
302 if (!src) return SNMPAPI_NOERROR;
303
304 size = (src->idLength + dst->idLength) * sizeof(UINT);
305 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
306 {
307 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
308 {
309 SetLastError(SNMP_MEM_ALLOC_ERROR);
310 return SNMPAPI_ERROR;
311 }
312 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
313 }
314
315 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
316 dst->idLength = dst->idLength + src->idLength;
317 dst->ids = ids;
318
319 return SNMPAPI_NOERROR;
320 }
321
322 /***********************************************************************
323 * SnmpUtilOidCpy (SNMPAPI.@)
324 */
325 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
326 {
327 TRACE("(%p, %p)\n", dst, src);
328
329 if (!dst) return SNMPAPI_ERROR;
330 if (!src)
331 {
332 dst->idLength = 0;
333 dst->ids = NULL;
334 return SNMPAPI_NOERROR;
335 }
336 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
337 {
338 unsigned int i;
339
340 dst->idLength = src->idLength;
341 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
342 return SNMPAPI_NOERROR;
343 }
344 return SNMPAPI_ERROR;
345 }
346
347 /***********************************************************************
348 * SnmpUtilOidFree (SNMPAPI.@)
349 */
350 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
351 {
352 TRACE("(%p)\n", oid);
353
354 if (!oid) return;
355
356 oid->idLength = 0;
357 HeapFree(GetProcessHeap(), 0, oid->ids);
358 oid->ids = NULL;
359 }
360
361 /***********************************************************************
362 * SnmpUtilOidNCmp (SNMPAPI.@)
363 */
364 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
365 {
366 unsigned int i, len;
367
368 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
369
370 if (!oid1 || !oid2) return 0;
371
372 len = min(count, oid1->idLength);
373 len = min(len, oid2->idLength);
374 for (i = 0; i < len; i++)
375 {
376 if (oid1->ids[i] > oid2->ids[i]) return 1;
377 if (oid1->ids[i] < oid2->ids[i]) return -1;
378 }
379 if (i == count) return 0;
380 if (oid1->idLength < oid2->idLength) return -1;
381 if (oid1->idLength > oid2->idLength) return 1;
382 return 0;
383 }
384
385 /***********************************************************************
386 * SnmpUtilOidCmp (SNMPAPI.@)
387 */
388 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
389 {
390 TRACE("(%p, %p)\n", oid1, oid2);
391
392 if (oid1->idLength < oid2->idLength) return -1;
393 if (oid1->idLength > oid2->idLength) return 1;
394
395 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
396 }
397
398 /***********************************************************************
399 * SnmpUtilVarBindCpy (SNMPAPI.@)
400 */
401 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
402 {
403 unsigned int i, size;
404
405 TRACE("(%p, %p)\n", dst, src);
406
407 if (!dst) return SNMPAPI_ERROR;
408 if (!src)
409 {
410 dst->value.asnType = ASN_NULL;
411 return SNMPAPI_NOERROR;
412 }
413
414 size = src->name.idLength * sizeof(UINT);
415 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
416
417 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
418 dst->name.idLength = src->name.idLength;
419
420 if (!asn_any_copy(&dst->value, &src->value))
421 {
422 HeapFree(GetProcessHeap(), 0, dst->name.ids);
423 return SNMPAPI_ERROR;
424 }
425 return SNMPAPI_NOERROR;
426 }
427
428 /***********************************************************************
429 * SnmpUtilVarBindFree (SNMPAPI.@)
430 */
431 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
432 {
433 TRACE("(%p)\n", vb);
434
435 if (!vb) return;
436
437 asn_any_free(&vb->value);
438 HeapFree(GetProcessHeap(), 0, vb->name.ids);
439 vb->name.idLength = 0;
440 vb->name.ids = NULL;
441 }
442
443 /***********************************************************************
444 * SnmpUtilVarBindListCpy (SNMPAPI.@)
445 */
446 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
447 {
448 unsigned int i, size;
449 SnmpVarBind *src_entry, *dst_entry;
450
451 TRACE("(%p, %p)\n", dst, src);
452
453 if (!src)
454 {
455 dst->list = NULL;
456 dst->len = 0;
457 return SNMPAPI_NOERROR;
458 }
459 size = src->len * sizeof(SnmpVarBind);
460 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
461 return SNMPAPI_ERROR;
462
463 src_entry = src->list;
464 dst_entry = dst->list;
465 for (i = 0; i < src->len; i++)
466 {
467 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
468 {
469 src_entry++;
470 dst_entry++;
471 }
472 else
473 {
474 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
475 HeapFree(GetProcessHeap(), 0, dst->list);
476 return SNMPAPI_ERROR;
477 }
478 }
479 dst->len = src->len;
480 return SNMPAPI_NOERROR;
481 }
482
483 /***********************************************************************
484 * SnmpUtilVarBindListFree (SNMPAPI.@)
485 */
486 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
487 {
488 unsigned int i;
489 SnmpVarBind *entry;
490
491 TRACE("(%p)\n", vb);
492
493 entry = vb->list;
494 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
495 HeapFree(GetProcessHeap(), 0, vb->list);
496 vb->list = NULL;
497 vb->len = 0;
498 }
499
500 /***********************************************************************
501 * SnmpUtilIdsToA (SNMPAPI.@)
502 */
503 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
504 {
505 static char one[10], oid[514], null_oid[] = "<null oid>";
506 unsigned int i, len, left = sizeof(oid) - 1;
507
508 TRACE("(%p, %d)\n", ids, length);
509
510 if (!ids || !length) return null_oid;
511
512 *oid = 0;
513 for (i = 0; i < length; i++)
514 {
515 sprintf(one, "%d", ids[i]);
516 len = strlen(one);
517 if (left >= len)
518 {
519 strcat(oid, one);
520 left -= len;
521 }
522 else return oid;
523
524 if (i < length - 1)
525 {
526 if (left > 0)
527 {
528 strcat(oid, ".");
529 left--;
530 }
531 else return oid;
532 }
533 }
534 return oid;
535 }
536
537 /***********************************************************************
538 * SnmpUtilOidToA (SNMPAPI.@)
539 */
540 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
541 {
542 static char null_oid[] = "<null oid>";
543
544 TRACE("(%p)\n", oid);
545
546 if (oid)
547 return SnmpUtilIdsToA(oid->ids, oid->idLength);
548 else
549 return null_oid;
550 }
551
552 /***********************************************************************
553 * SnmpUtilPrintOid (SNMPAPI.@)
554 */
555 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
556 {
557 unsigned int i;
558
559 TRACE("(%p)\n", oid);
560
561 if (!oid) return;
562
563 for (i = 0; i < oid->idLength; i++)
564 {
565 TRACE("%u", oid->ids[i]);
566 if (i < oid->idLength - 1) TRACE(".");
567 }
568 TRACE("\n");
569 }
570
571 /***********************************************************************
572 * SnmpUtilPrintAsnAny (SNMPAPI.@)
573 */
574 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
575 {
576 unsigned int i;
577
578 TRACE("(%p)\n", any);
579
580 switch (any->asnType)
581 {
582 case ASN_NULL: TRACE("Null value\n"); return;
583 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
584 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
585 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
586 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
587 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
588 case ASN_COUNTER64:
589 {
590 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
591 return;
592 }
593 case ASN_OCTETSTRING:
594 {
595 TRACE("String ");
596 for (i = 0; i < any->asnValue.string.length; i++)
597 TRACE("%c", any->asnValue.string.stream[i]);
598 TRACE("\n");
599 return;
600 }
601 case ASN_IPADDRESS:
602 {
603 TRACE("IpAddress ");
604 if (any->asnValue.string.length < 4)
605 {
606 TRACE("Invalid\n");
607 return;
608 }
609 for (i = 0; i < 4; i++)
610 {
611 TRACE("%u", any->asnValue.string.stream[i]);
612 if (i < 3) TRACE(".");
613 }
614 TRACE("\n");
615 return;
616 }
617 case ASN_BITS:
618 {
619 TRACE("Bits ");
620 for (i = 0; i < any->asnValue.string.length; i++)
621 {
622 TRACE("0x%02x", any->asnValue.string.stream[i]);
623 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
624 }
625 TRACE("\n");
626 return;
627 }
628 case ASN_OPAQUE:
629 {
630 TRACE("Opaque ");
631 for (i = 0; i < any->asnValue.string.length; i++)
632 {
633 TRACE("0x%02x", any->asnValue.string.stream[i]);
634 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
635 }
636 TRACE("\n");
637 return;
638 }
639 case ASN_OBJECTIDENTIFIER:
640 {
641 TRACE("ObjectID ");
642 for (i = 0; i < any->asnValue.object.idLength; i++)
643 {
644 TRACE("%u", any->asnValue.object.ids[i]);
645 if (i < any->asnValue.object.idLength - 1) TRACE(".");
646 }
647 TRACE("\n");
648 return;
649 }
650 default:
651 {
652 TRACE("Invalid type %d\n", any->asnType);
653 return;
654 }
655 }
656 }