mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / snmpapi / main.c
blob1fd98c3da8d0b0aa766802f9029dfd8319085f4e
1 /*
2 * Implementation of SNMPAPI.DLL
4 * Copyright 2002 Patrik Stridvall
5 * Copyright 2007 Hans Leidekker
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.
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.
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
22 #include <stdio.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "snmp.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(snmpapi);
33 static INT asn_any_copy(AsnAny *dst, const AsnAny *src)
35 memset(dst, 0, sizeof(AsnAny));
36 switch (src->asnType)
38 case ASN_INTEGER32: dst->asnValue.number = src->asnValue.number; break;
39 case ASN_UNSIGNED32: dst->asnValue.unsigned32 = src->asnValue.unsigned32; break;
40 case ASN_COUNTER64: dst->asnValue.counter64 = src->asnValue.counter64; break;
41 case ASN_COUNTER32: dst->asnValue.counter = src->asnValue.counter; break;
42 case ASN_GAUGE32: dst->asnValue.gauge = src->asnValue.gauge; break;
43 case ASN_TIMETICKS: dst->asnValue.ticks = src->asnValue.ticks; break;
45 case ASN_OCTETSTRING:
46 case ASN_BITS:
47 case ASN_SEQUENCE:
48 case ASN_IPADDRESS:
49 case ASN_OPAQUE:
51 BYTE *stream;
52 UINT length = src->asnValue.string.length;
54 if (!(stream = HeapAlloc(GetProcessHeap(), 0, length))) return SNMPAPI_ERROR;
55 memcpy(stream, src->asnValue.string.stream, length);
57 dst->asnValue.string.stream = stream;
58 dst->asnValue.string.length = length;
59 dst->asnValue.string.dynamic = TRUE;
60 break;
62 case ASN_OBJECTIDENTIFIER:
64 UINT *ids, i, size = src->asnValue.object.idLength * sizeof(UINT);
66 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
68 dst->asnValue.object.ids = ids;
69 dst->asnValue.object.idLength = src->asnValue.object.idLength;
71 for (i = 0; i < dst->asnValue.object.idLength; i++)
72 dst->asnValue.object.ids[i] = src->asnValue.object.ids[i];
73 break;
75 default:
77 WARN("unknown ASN type: %d\n", src->asnType);
78 return SNMPAPI_ERROR;
81 dst->asnType = src->asnType;
82 return SNMPAPI_NOERROR;
85 static void asn_any_free(AsnAny *any)
87 switch (any->asnType)
89 case ASN_OCTETSTRING:
90 case ASN_BITS:
91 case ASN_SEQUENCE:
92 case ASN_IPADDRESS:
93 case ASN_OPAQUE:
95 if (any->asnValue.string.dynamic)
97 HeapFree(GetProcessHeap(), 0, any->asnValue.string.stream);
98 any->asnValue.string.stream = NULL;
100 break;
102 case ASN_OBJECTIDENTIFIER:
104 HeapFree(GetProcessHeap(), 0, any->asnValue.object.ids);
105 any->asnValue.object.ids = NULL;
106 break;
108 default: break;
110 any->asnType = ASN_NULL;
113 static ULONGLONG startTime;
115 /***********************************************************************
116 * DllMain for SNMPAPI
118 BOOL WINAPI DllMain(
119 HINSTANCE hInstDLL,
120 DWORD fdwReason,
121 LPVOID lpvReserved)
123 TRACE("(%p,%d,%p)\n", hInstDLL, fdwReason, lpvReserved);
125 switch(fdwReason) {
126 case DLL_PROCESS_ATTACH:
127 DisableThreadLibraryCalls(hInstDLL);
128 startTime = GetTickCount64();
129 break;
132 return TRUE;
135 /***********************************************************************
136 * SnmpSvcGetUptime (SNMPAPI.@)
138 * BUGS
139 * This returns the number of centiseconds since the DLL was loaded,
140 * rather than the number of centiseconds since the SNMP service was
141 * started, since there isn't yet any SNMP service in Wine.
143 DWORD WINAPI SnmpSvcGetUptime(void)
145 ULONGLONG now = GetTickCount64();
147 return (now - startTime) / 10;
150 /***********************************************************************
151 * SnmpUtilDbgPrint (SNMPAPI.@)
153 * NOTES
154 * The Microsoft headers claim this function uses the stdcall calling
155 * convention. But stdcall functions cannot take a variable number of
156 * arguments so this does not make sense. The stdcall specification is
157 * probably ignored by Microsoft's compiler in this case. So declare it
158 * correctly in Wine so it works with all compilers.
160 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
162 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
165 /***********************************************************************
166 * SnmpUtilMemAlloc (SNMPAPI.@)
168 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
170 TRACE("(%d)\n", nbytes);
171 return HeapAlloc(GetProcessHeap(), 0, nbytes);
174 /***********************************************************************
175 * SnmpUtilMemReAlloc (SNMPAPI.@)
177 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
179 TRACE("(%p, %d)\n", mem, nbytes);
180 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
183 /***********************************************************************
184 * SnmpUtilMemFree (SNMPAPI.@)
186 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
188 TRACE("(%p)\n", mem);
189 HeapFree(GetProcessHeap(), 0, mem);
192 /***********************************************************************
193 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
195 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
197 TRACE("(%p, %p)\n", dst, src);
198 return asn_any_copy(dst, src);
201 /***********************************************************************
202 * SnmpUtilAsnAnyFree (SNMPAPI.@)
204 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
206 TRACE("(%p)\n", any);
207 asn_any_free(any);
210 /***********************************************************************
211 * SnmpUtilOctetsCpy (SNMPAPI.@)
213 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
215 TRACE("(%p, %p)\n", dst, src);
217 if (!dst) return SNMPAPI_ERROR;
218 if (!src)
220 dst->dynamic = FALSE;
221 dst->length = 0;
222 dst->stream = NULL;
223 return SNMPAPI_NOERROR;
225 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
227 unsigned int i;
229 dst->dynamic = TRUE;
230 dst->length = src->length;
231 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
232 return SNMPAPI_NOERROR;
234 return SNMPAPI_ERROR;
237 /***********************************************************************
238 * SnmpUtilOctetsFree (SNMPAPI.@)
240 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
242 TRACE("(%p)\n", octets);
244 if (octets)
246 octets->length = 0;
247 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
248 octets->stream = NULL;
249 octets->dynamic = FALSE;
253 /***********************************************************************
254 * SnmpUtilOctetsNCmp (SNMPAPI.@)
256 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
258 INT ret;
259 unsigned int i;
261 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
263 if (!octets1 || !octets2) return 0;
265 for (i = 0; i < count; i++)
266 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
268 return 0;
271 /***********************************************************************
272 * SnmpUtilOctetsCmp (SNMPAPI.@)
274 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
276 TRACE("(%p, %p)\n", octets1, octets2);
278 if (octets1->length < octets2->length) return -1;
279 if (octets1->length > octets2->length) return 1;
281 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
284 /***********************************************************************
285 * SnmpUtilOidAppend (SNMPAPI.@)
287 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
289 UINT *ids, i, size;
291 TRACE("(%p, %p)\n", dst, src);
293 if (!dst) return SNMPAPI_ERROR;
294 if (!src) return SNMPAPI_NOERROR;
296 size = (src->idLength + dst->idLength) * sizeof(UINT);
297 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
299 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
301 SetLastError(SNMP_MEM_ALLOC_ERROR);
302 return SNMPAPI_ERROR;
304 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
307 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
308 dst->idLength = dst->idLength + src->idLength;
309 dst->ids = ids;
311 return SNMPAPI_NOERROR;
314 /***********************************************************************
315 * SnmpUtilOidCpy (SNMPAPI.@)
317 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
319 TRACE("(%p, %p)\n", dst, src);
321 if (!dst) return SNMPAPI_ERROR;
322 if (!src)
324 dst->idLength = 0;
325 dst->ids = NULL;
326 return SNMPAPI_NOERROR;
328 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
330 unsigned int i;
332 dst->idLength = src->idLength;
333 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
334 return SNMPAPI_NOERROR;
336 return SNMPAPI_ERROR;
339 /***********************************************************************
340 * SnmpUtilOidFree (SNMPAPI.@)
342 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
344 TRACE("(%p)\n", oid);
346 if (!oid) return;
348 oid->idLength = 0;
349 HeapFree(GetProcessHeap(), 0, oid->ids);
350 oid->ids = NULL;
353 /***********************************************************************
354 * SnmpUtilOidNCmp (SNMPAPI.@)
356 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
358 unsigned int i, len;
360 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
362 if (!oid1 || !oid2) return 0;
364 len = min(count, oid1->idLength);
365 len = min(len, oid2->idLength);
366 for (i = 0; i < len; i++)
368 if (oid1->ids[i] > oid2->ids[i]) return 1;
369 if (oid1->ids[i] < oid2->ids[i]) return -1;
371 if (i == count) return 0;
372 if (oid1->idLength < oid2->idLength) return -1;
373 if (oid1->idLength > oid2->idLength) return 1;
374 return 0;
377 /***********************************************************************
378 * SnmpUtilOidCmp (SNMPAPI.@)
380 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
382 TRACE("(%p, %p)\n", oid1, oid2);
384 if (oid1->idLength < oid2->idLength) return -1;
385 if (oid1->idLength > oid2->idLength) return 1;
387 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
390 /***********************************************************************
391 * SnmpUtilVarBindCpy (SNMPAPI.@)
393 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
395 unsigned int i, size;
397 TRACE("(%p, %p)\n", dst, src);
399 if (!dst) return SNMPAPI_ERROR;
400 if (!src)
402 dst->value.asnType = ASN_NULL;
403 return SNMPAPI_NOERROR;
406 size = src->name.idLength * sizeof(UINT);
407 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
409 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
410 dst->name.idLength = src->name.idLength;
412 if (!asn_any_copy(&dst->value, &src->value))
414 HeapFree(GetProcessHeap(), 0, dst->name.ids);
415 return SNMPAPI_ERROR;
417 return SNMPAPI_NOERROR;
420 /***********************************************************************
421 * SnmpUtilVarBindFree (SNMPAPI.@)
423 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
425 TRACE("(%p)\n", vb);
427 if (!vb) return;
429 asn_any_free(&vb->value);
430 HeapFree(GetProcessHeap(), 0, vb->name.ids);
431 vb->name.idLength = 0;
432 vb->name.ids = NULL;
435 /***********************************************************************
436 * SnmpUtilVarBindListCpy (SNMPAPI.@)
438 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
440 unsigned int i, size;
441 SnmpVarBind *src_entry, *dst_entry;
443 TRACE("(%p, %p)\n", dst, src);
445 if (!src)
447 dst->list = NULL;
448 dst->len = 0;
449 return SNMPAPI_NOERROR;
451 size = src->len * sizeof(SnmpVarBind);
452 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
453 return SNMPAPI_ERROR;
455 src_entry = src->list;
456 dst_entry = dst->list;
457 for (i = 0; i < src->len; i++)
459 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
461 src_entry++;
462 dst_entry++;
464 else
466 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
467 HeapFree(GetProcessHeap(), 0, dst->list);
468 return SNMPAPI_ERROR;
471 dst->len = src->len;
472 return SNMPAPI_NOERROR;
475 /***********************************************************************
476 * SnmpUtilVarBindListFree (SNMPAPI.@)
478 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
480 unsigned int i;
481 SnmpVarBind *entry;
483 TRACE("(%p)\n", vb);
485 entry = vb->list;
486 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
487 HeapFree(GetProcessHeap(), 0, vb->list);
488 vb->list = NULL;
489 vb->len = 0;
492 /***********************************************************************
493 * SnmpUtilIdsToA (SNMPAPI.@)
495 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
497 static char one[10], oid[514], null_oid[] = "<null oid>";
498 unsigned int i, len, left = sizeof(oid) - 1;
500 TRACE("(%p, %d)\n", ids, length);
502 if (!ids || !length) return null_oid;
504 *oid = 0;
505 for (i = 0; i < length; i++)
507 sprintf(one, "%d", ids[i]);
508 len = strlen(one);
509 if (left >= len)
511 strcat(oid, one);
512 left -= len;
514 else return oid;
516 if (i < length - 1)
518 if (left > 0)
520 strcat(oid, ".");
521 left--;
523 else return oid;
526 return oid;
529 /***********************************************************************
530 * SnmpUtilOidToA (SNMPAPI.@)
532 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
534 static char null_oid[] = "<null oid>";
536 TRACE("(%p)\n", oid);
538 if (oid)
539 return SnmpUtilIdsToA(oid->ids, oid->idLength);
540 else
541 return null_oid;
544 /***********************************************************************
545 * SnmpUtilPrintOid (SNMPAPI.@)
547 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
549 unsigned int i;
551 TRACE("(%p)\n", oid);
553 if (!oid) return;
555 for (i = 0; i < oid->idLength; i++)
557 TRACE("%u", oid->ids[i]);
558 if (i < oid->idLength - 1) TRACE(".");
560 TRACE("\n");
563 /***********************************************************************
564 * SnmpUtilPrintAsnAny (SNMPAPI.@)
566 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
568 unsigned int i;
570 TRACE("(%p)\n", any);
572 switch (any->asnType)
574 case ASN_NULL: TRACE("Null value\n"); return;
575 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
576 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
577 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
578 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
579 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
580 case ASN_COUNTER64:
582 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
583 return;
585 case ASN_OCTETSTRING:
587 TRACE("String ");
588 for (i = 0; i < any->asnValue.string.length; i++)
589 TRACE("%c", any->asnValue.string.stream[i]);
590 TRACE("\n");
591 return;
593 case ASN_IPADDRESS:
595 TRACE("IpAddress ");
596 if (any->asnValue.string.length < 4)
598 TRACE("Invalid\n");
599 return;
601 for (i = 0; i < 4; i++)
603 TRACE("%u", any->asnValue.string.stream[i]);
604 if (i < 3) TRACE(".");
606 TRACE("\n");
607 return;
609 case ASN_BITS:
611 TRACE("Bits ");
612 for (i = 0; i < any->asnValue.string.length; i++)
614 TRACE("0x%02x", any->asnValue.string.stream[i]);
615 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
617 TRACE("\n");
618 return;
620 case ASN_OPAQUE:
622 TRACE("Opaque ");
623 for (i = 0; i < any->asnValue.string.length; i++)
625 TRACE("0x%02x", any->asnValue.string.stream[i]);
626 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
628 TRACE("\n");
629 return;
631 case ASN_OBJECTIDENTIFIER:
633 TRACE("ObjectID ");
634 for (i = 0; i < any->asnValue.object.idLength; i++)
636 TRACE("%u", any->asnValue.object.ids[i]);
637 if (i < any->asnValue.object.idLength - 1) TRACE(".");
639 TRACE("\n");
640 return;
642 default:
644 TRACE("Invalid type %d\n", any->asnType);
645 return;