makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / snmpapi / main.c
blob3c90ca20ec9f101d49a67f29fdeabb4e489438c0
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_WINE_PREATTACH:
127 return FALSE; /* prefer native version */
128 case DLL_PROCESS_ATTACH:
129 DisableThreadLibraryCalls(hInstDLL);
130 startTime = GetTickCount64();
131 break;
134 return TRUE;
137 /***********************************************************************
138 * SnmpSvcGetUptime (SNMPAPI.@)
140 * BUGS
141 * This returns the number of centiseconds since the DLL was loaded,
142 * rather than the number of centiseconds since the SNMP service was
143 * started, since there isn't yet any SNMP service in Wine.
145 DWORD WINAPI SnmpSvcGetUptime(void)
147 ULONGLONG now = GetTickCount64();
149 return (now - startTime) / 10;
152 /***********************************************************************
153 * SnmpUtilDbgPrint (SNMPAPI.@)
155 * NOTES
156 * The Microsoft headers claim this function uses the stdcall calling
157 * convention. But stdcall functions cannot take a variable number of
158 * arguments so this does not make sense. The stdcall specification is
159 * probably ignored by Microsoft's compiler in this case. So declare it
160 * correctly in Wine so it works with all compilers.
162 VOID WINAPIV SnmpUtilDbgPrint(INT loglevel, LPSTR format, ...)
164 FIXME("(%d, %s)\n", loglevel, debugstr_a(format));
167 /***********************************************************************
168 * SnmpUtilMemAlloc (SNMPAPI.@)
170 LPVOID WINAPI SnmpUtilMemAlloc(UINT nbytes)
172 TRACE("(%d)\n", nbytes);
173 return HeapAlloc(GetProcessHeap(), 0, nbytes);
176 /***********************************************************************
177 * SnmpUtilMemReAlloc (SNMPAPI.@)
179 LPVOID WINAPI SnmpUtilMemReAlloc(LPVOID mem, UINT nbytes)
181 TRACE("(%p, %d)\n", mem, nbytes);
182 return HeapReAlloc(GetProcessHeap(), 0, mem, nbytes);
185 /***********************************************************************
186 * SnmpUtilMemFree (SNMPAPI.@)
188 VOID WINAPI SnmpUtilMemFree(LPVOID mem)
190 TRACE("(%p)\n", mem);
191 HeapFree(GetProcessHeap(), 0, mem);
194 /***********************************************************************
195 * SnmpUtilAsnAnyCpy (SNMPAPI.@)
197 INT WINAPI SnmpUtilAsnAnyCpy(AsnAny *dst, AsnAny *src)
199 TRACE("(%p, %p)\n", dst, src);
200 return asn_any_copy(dst, src);
203 /***********************************************************************
204 * SnmpUtilAsnAnyFree (SNMPAPI.@)
206 VOID WINAPI SnmpUtilAsnAnyFree(AsnAny *any)
208 TRACE("(%p)\n", any);
209 asn_any_free(any);
212 /***********************************************************************
213 * SnmpUtilOctetsCpy (SNMPAPI.@)
215 INT WINAPI SnmpUtilOctetsCpy(AsnOctetString *dst, AsnOctetString *src)
217 TRACE("(%p, %p)\n", dst, src);
219 if (!dst) return SNMPAPI_ERROR;
220 if (!src)
222 dst->dynamic = FALSE;
223 dst->length = 0;
224 dst->stream = NULL;
225 return SNMPAPI_NOERROR;
227 if ((dst->stream = HeapAlloc(GetProcessHeap(), 0, src->length)))
229 unsigned int i;
231 dst->dynamic = TRUE;
232 dst->length = src->length;
233 for (i = 0; i < dst->length; i++) dst->stream[i] = src->stream[i];
234 return SNMPAPI_NOERROR;
236 return SNMPAPI_ERROR;
239 /***********************************************************************
240 * SnmpUtilOctetsFree (SNMPAPI.@)
242 VOID WINAPI SnmpUtilOctetsFree(AsnOctetString *octets)
244 TRACE("(%p)\n", octets);
246 if (octets)
248 octets->length = 0;
249 if (octets->dynamic) HeapFree(GetProcessHeap(), 0, octets->stream);
250 octets->stream = NULL;
251 octets->dynamic = FALSE;
255 /***********************************************************************
256 * SnmpUtilOctetsNCmp (SNMPAPI.@)
258 INT WINAPI SnmpUtilOctetsNCmp(AsnOctetString *octets1, AsnOctetString *octets2, UINT count)
260 INT ret;
261 unsigned int i;
263 TRACE("(%p, %p, %d)\n", octets1, octets2, count);
265 if (!octets1 || !octets2) return 0;
267 for (i = 0; i < count; i++)
268 if ((ret = octets1->stream[i] - octets2->stream[i])) return ret;
270 return 0;
273 /***********************************************************************
274 * SnmpUtilOctetsCmp (SNMPAPI.@)
276 INT WINAPI SnmpUtilOctetsCmp(AsnOctetString *octets1, AsnOctetString *octets2)
278 TRACE("(%p, %p)\n", octets1, octets2);
280 if (octets1->length < octets2->length) return -1;
281 if (octets1->length > octets2->length) return 1;
283 return SnmpUtilOctetsNCmp(octets1, octets2, octets1->length);
286 /***********************************************************************
287 * SnmpUtilOidAppend (SNMPAPI.@)
289 INT WINAPI SnmpUtilOidAppend(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
291 UINT *ids, i, size;
293 TRACE("(%p, %p)\n", dst, src);
295 if (!dst) return SNMPAPI_ERROR;
296 if (!src) return SNMPAPI_NOERROR;
298 size = (src->idLength + dst->idLength) * sizeof(UINT);
299 if (!(ids = HeapReAlloc(GetProcessHeap(), 0, dst->ids, size)))
301 if (!(ids = HeapAlloc(GetProcessHeap(), 0, size)))
303 SetLastError(SNMP_MEM_ALLOC_ERROR);
304 return SNMPAPI_ERROR;
306 else memcpy(ids, dst->ids, dst->idLength * sizeof(UINT));
309 for (i = 0; i < src->idLength; i++) ids[i + dst->idLength] = src->ids[i];
310 dst->idLength = dst->idLength + src->idLength;
311 dst->ids = ids;
313 return SNMPAPI_NOERROR;
316 /***********************************************************************
317 * SnmpUtilOidCpy (SNMPAPI.@)
319 INT WINAPI SnmpUtilOidCpy(AsnObjectIdentifier *dst, AsnObjectIdentifier *src)
321 TRACE("(%p, %p)\n", dst, src);
323 if (!dst) return SNMPAPI_ERROR;
324 if (!src)
326 dst->idLength = 0;
327 dst->ids = NULL;
328 return SNMPAPI_NOERROR;
330 if ((dst->ids = HeapAlloc(GetProcessHeap(), 0, src->idLength * sizeof(UINT))))
332 unsigned int i;
334 dst->idLength = src->idLength;
335 for (i = 0; i < dst->idLength; i++) dst->ids[i] = src->ids[i];
336 return SNMPAPI_NOERROR;
338 return SNMPAPI_ERROR;
341 /***********************************************************************
342 * SnmpUtilOidFree (SNMPAPI.@)
344 VOID WINAPI SnmpUtilOidFree(AsnObjectIdentifier *oid)
346 TRACE("(%p)\n", oid);
348 if (!oid) return;
350 oid->idLength = 0;
351 HeapFree(GetProcessHeap(), 0, oid->ids);
352 oid->ids = NULL;
355 /***********************************************************************
356 * SnmpUtilOidNCmp (SNMPAPI.@)
358 INT WINAPI SnmpUtilOidNCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2, UINT count)
360 unsigned int i, len;
362 TRACE("(%p, %p, %d)\n", oid1, oid2, count);
364 if (!oid1 || !oid2) return 0;
366 len = min(count, oid1->idLength);
367 len = min(len, oid2->idLength);
368 for (i = 0; i < len; i++)
370 if (oid1->ids[i] > oid2->ids[i]) return 1;
371 if (oid1->ids[i] < oid2->ids[i]) return -1;
373 if (i == count) return 0;
374 if (oid1->idLength < oid2->idLength) return -1;
375 if (oid1->idLength > oid2->idLength) return 1;
376 return 0;
379 /***********************************************************************
380 * SnmpUtilOidCmp (SNMPAPI.@)
382 INT WINAPI SnmpUtilOidCmp(AsnObjectIdentifier *oid1, AsnObjectIdentifier *oid2)
384 TRACE("(%p, %p)\n", oid1, oid2);
386 if (oid1->idLength < oid2->idLength) return -1;
387 if (oid1->idLength > oid2->idLength) return 1;
389 return SnmpUtilOidNCmp(oid1, oid2, oid1->idLength);
392 /***********************************************************************
393 * SnmpUtilVarBindCpy (SNMPAPI.@)
395 INT WINAPI SnmpUtilVarBindCpy(SnmpVarBind *dst, SnmpVarBind *src)
397 unsigned int i, size;
399 TRACE("(%p, %p)\n", dst, src);
401 if (!dst) return SNMPAPI_ERROR;
402 if (!src)
404 dst->value.asnType = ASN_NULL;
405 return SNMPAPI_NOERROR;
408 size = src->name.idLength * sizeof(UINT);
409 if (!(dst->name.ids = HeapAlloc(GetProcessHeap(), 0, size))) return SNMPAPI_ERROR;
411 for (i = 0; i < src->name.idLength; i++) dst->name.ids[i] = src->name.ids[i];
412 dst->name.idLength = src->name.idLength;
414 if (!asn_any_copy(&dst->value, &src->value))
416 HeapFree(GetProcessHeap(), 0, dst->name.ids);
417 return SNMPAPI_ERROR;
419 return SNMPAPI_NOERROR;
422 /***********************************************************************
423 * SnmpUtilVarBindFree (SNMPAPI.@)
425 VOID WINAPI SnmpUtilVarBindFree(SnmpVarBind *vb)
427 TRACE("(%p)\n", vb);
429 if (!vb) return;
431 asn_any_free(&vb->value);
432 HeapFree(GetProcessHeap(), 0, vb->name.ids);
433 vb->name.idLength = 0;
434 vb->name.ids = NULL;
437 /***********************************************************************
438 * SnmpUtilVarBindListCpy (SNMPAPI.@)
440 INT WINAPI SnmpUtilVarBindListCpy(SnmpVarBindList *dst, SnmpVarBindList *src)
442 unsigned int i, size;
443 SnmpVarBind *src_entry, *dst_entry;
445 TRACE("(%p, %p)\n", dst, src);
447 if (!src)
449 dst->list = NULL;
450 dst->len = 0;
451 return SNMPAPI_NOERROR;
453 size = src->len * sizeof(SnmpVarBind);
454 if (!(dst->list = HeapAlloc(GetProcessHeap(), 0, size)))
455 return SNMPAPI_ERROR;
457 src_entry = src->list;
458 dst_entry = dst->list;
459 for (i = 0; i < src->len; i++)
461 if (SnmpUtilVarBindCpy(dst_entry, src_entry))
463 src_entry++;
464 dst_entry++;
466 else
468 for (--i; i > 0; i--) SnmpUtilVarBindFree(--dst_entry);
469 HeapFree(GetProcessHeap(), 0, dst->list);
470 return SNMPAPI_ERROR;
473 dst->len = src->len;
474 return SNMPAPI_NOERROR;
477 /***********************************************************************
478 * SnmpUtilVarBindListFree (SNMPAPI.@)
480 VOID WINAPI SnmpUtilVarBindListFree(SnmpVarBindList *vb)
482 unsigned int i;
483 SnmpVarBind *entry;
485 TRACE("(%p)\n", vb);
487 entry = vb->list;
488 for (i = 0; i < vb->len; i++) SnmpUtilVarBindFree(entry++);
489 HeapFree(GetProcessHeap(), 0, vb->list);
490 vb->list = NULL;
491 vb->len = 0;
494 /***********************************************************************
495 * SnmpUtilIdsToA (SNMPAPI.@)
497 LPSTR WINAPI SnmpUtilIdsToA(UINT *ids, UINT length)
499 static char one[10], oid[514], null_oid[] = "<null oid>";
500 unsigned int i, len, left = sizeof(oid) - 1;
502 TRACE("(%p, %d)\n", ids, length);
504 if (!ids || !length) return null_oid;
506 *oid = 0;
507 for (i = 0; i < length; i++)
509 sprintf(one, "%d", ids[i]);
510 len = strlen(one);
511 if (left >= len)
513 strcat(oid, one);
514 left -= len;
516 else return oid;
518 if (i < length - 1)
520 if (left > 0)
522 strcat(oid, ".");
523 left--;
525 else return oid;
528 return oid;
531 /***********************************************************************
532 * SnmpUtilOidToA (SNMPAPI.@)
534 LPSTR WINAPI SnmpUtilOidToA(AsnObjectIdentifier *oid)
536 static char null_oid[] = "<null oid>";
538 TRACE("(%p)\n", oid);
540 if (oid)
541 return SnmpUtilIdsToA(oid->ids, oid->idLength);
542 else
543 return null_oid;
546 /***********************************************************************
547 * SnmpUtilPrintOid (SNMPAPI.@)
549 VOID WINAPI SnmpUtilPrintOid(AsnObjectIdentifier *oid)
551 unsigned int i;
553 TRACE("(%p)\n", oid);
555 if (!oid) return;
557 for (i = 0; i < oid->idLength; i++)
559 TRACE("%u", oid->ids[i]);
560 if (i < oid->idLength - 1) TRACE(".");
562 TRACE("\n");
565 /***********************************************************************
566 * SnmpUtilPrintAsnAny (SNMPAPI.@)
568 VOID WINAPI SnmpUtilPrintAsnAny(AsnAny *any)
570 unsigned int i;
572 TRACE("(%p)\n", any);
574 switch (any->asnType)
576 case ASN_NULL: TRACE("Null value\n"); return;
577 case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return;
578 case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return;
579 case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return;
580 case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return;
581 case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return;
582 case ASN_COUNTER64:
584 TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart);
585 return;
587 case ASN_OCTETSTRING:
589 TRACE("String ");
590 for (i = 0; i < any->asnValue.string.length; i++)
591 TRACE("%c", any->asnValue.string.stream[i]);
592 TRACE("\n");
593 return;
595 case ASN_IPADDRESS:
597 TRACE("IpAddress ");
598 if (any->asnValue.string.length < 4)
600 TRACE("Invalid\n");
601 return;
603 for (i = 0; i < 4; i++)
605 TRACE("%u", any->asnValue.string.stream[i]);
606 if (i < 3) TRACE(".");
608 TRACE("\n");
609 return;
611 case ASN_BITS:
613 TRACE("Bits ");
614 for (i = 0; i < any->asnValue.string.length; i++)
616 TRACE("0x%02x", any->asnValue.string.stream[i]);
617 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
619 TRACE("\n");
620 return;
622 case ASN_OPAQUE:
624 TRACE("Opaque ");
625 for (i = 0; i < any->asnValue.string.length; i++)
627 TRACE("0x%02x", any->asnValue.string.stream[i]);
628 if (i < any->asnValue.object.idLength - 1) TRACE(" ");
630 TRACE("\n");
631 return;
633 case ASN_OBJECTIDENTIFIER:
635 TRACE("ObjectID ");
636 for (i = 0; i < any->asnValue.object.idLength; i++)
638 TRACE("%u", any->asnValue.object.ids[i]);
639 if (i < any->asnValue.object.idLength - 1) TRACE(".");
641 TRACE("\n");
642 return;
644 default:
646 TRACE("Invalid type %d\n", any->asnType);
647 return;