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
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
));
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;
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
;
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
];
77 WARN("unknown ASN type: %d\n", src
->asnType
);
81 dst
->asnType
= src
->asnType
;
82 return SNMPAPI_NOERROR
;
85 static void asn_any_free(AsnAny
*any
)
95 if (any
->asnValue
.string
.dynamic
)
97 HeapFree(GetProcessHeap(), 0, any
->asnValue
.string
.stream
);
98 any
->asnValue
.string
.stream
= NULL
;
102 case ASN_OBJECTIDENTIFIER
:
104 HeapFree(GetProcessHeap(), 0, any
->asnValue
.object
.ids
);
105 any
->asnValue
.object
.ids
= NULL
;
110 any
->asnType
= ASN_NULL
;
113 static ULONGLONG startTime
;
115 /***********************************************************************
116 * DllMain for SNMPAPI
123 TRACE("(%p,%d,%p)\n", hInstDLL
, fdwReason
, lpvReserved
);
126 case DLL_WINE_PREATTACH
:
127 return FALSE
; /* prefer native version */
128 case DLL_PROCESS_ATTACH
:
129 DisableThreadLibraryCalls(hInstDLL
);
130 startTime
= GetTickCount64();
137 /***********************************************************************
138 * SnmpSvcGetUptime (SNMPAPI.@)
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.@)
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
);
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
;
222 dst
->dynamic
= FALSE
;
225 return SNMPAPI_NOERROR
;
227 if ((dst
->stream
= HeapAlloc(GetProcessHeap(), 0, src
->length
)))
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
);
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
)
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
;
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
)
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
;
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
;
328 return SNMPAPI_NOERROR
;
330 if ((dst
->ids
= HeapAlloc(GetProcessHeap(), 0, src
->idLength
* sizeof(UINT
))))
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
);
351 HeapFree(GetProcessHeap(), 0, oid
->ids
);
355 /***********************************************************************
356 * SnmpUtilOidNCmp (SNMPAPI.@)
358 INT WINAPI
SnmpUtilOidNCmp(AsnObjectIdentifier
*oid1
, AsnObjectIdentifier
*oid2
, UINT count
)
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;
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
;
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
)
431 asn_any_free(&vb
->value
);
432 HeapFree(GetProcessHeap(), 0, vb
->name
.ids
);
433 vb
->name
.idLength
= 0;
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
);
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
))
468 for (--i
; i
> 0; i
--) SnmpUtilVarBindFree(--dst_entry
);
469 HeapFree(GetProcessHeap(), 0, dst
->list
);
470 return SNMPAPI_ERROR
;
474 return SNMPAPI_NOERROR
;
477 /***********************************************************************
478 * SnmpUtilVarBindListFree (SNMPAPI.@)
480 VOID WINAPI
SnmpUtilVarBindListFree(SnmpVarBindList
*vb
)
488 for (i
= 0; i
< vb
->len
; i
++) SnmpUtilVarBindFree(entry
++);
489 HeapFree(GetProcessHeap(), 0, vb
->list
);
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
;
507 for (i
= 0; i
< length
; i
++)
509 sprintf(one
, "%d", ids
[i
]);
531 /***********************************************************************
532 * SnmpUtilOidToA (SNMPAPI.@)
534 LPSTR WINAPI
SnmpUtilOidToA(AsnObjectIdentifier
*oid
)
536 static char null_oid
[] = "<null oid>";
538 TRACE("(%p)\n", oid
);
541 return SnmpUtilIdsToA(oid
->ids
, oid
->idLength
);
546 /***********************************************************************
547 * SnmpUtilPrintOid (SNMPAPI.@)
549 VOID WINAPI
SnmpUtilPrintOid(AsnObjectIdentifier
*oid
)
553 TRACE("(%p)\n", oid
);
557 for (i
= 0; i
< oid
->idLength
; i
++)
559 TRACE("%u", oid
->ids
[i
]);
560 if (i
< oid
->idLength
- 1) TRACE(".");
565 /***********************************************************************
566 * SnmpUtilPrintAsnAny (SNMPAPI.@)
568 VOID WINAPI
SnmpUtilPrintAsnAny(AsnAny
*any
)
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;
584 TRACE("Counter64 %x%08x\n", (DWORD
)(any
->asnValue
.counter64
.QuadPart
>>32),(DWORD
)any
->asnValue
.counter64
.QuadPart
);
587 case ASN_OCTETSTRING
:
590 for (i
= 0; i
< any
->asnValue
.string
.length
; i
++)
591 TRACE("%c", any
->asnValue
.string
.stream
[i
]);
598 if (any
->asnValue
.string
.length
< 4)
603 for (i
= 0; i
< 4; i
++)
605 TRACE("%u", any
->asnValue
.string
.stream
[i
]);
606 if (i
< 3) TRACE(".");
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(" ");
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(" ");
633 case ASN_OBJECTIDENTIFIER
:
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(".");
646 TRACE("Invalid type %d\n", any
->asnType
);