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,%ld,%p)\n", hInstDLL
, fdwReason
, lpvReserved
);
126 case DLL_PROCESS_ATTACH
:
127 DisableThreadLibraryCalls(hInstDLL
);
128 startTime
= GetTickCount64();
135 /***********************************************************************
136 * SnmpSvcGetUptime (SNMPAPI.@)
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.@)
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
);
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
;
220 dst
->dynamic
= FALSE
;
223 return SNMPAPI_NOERROR
;
225 if ((dst
->stream
= HeapAlloc(GetProcessHeap(), 0, src
->length
)))
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
);
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
)
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
;
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
)
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
;
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
;
326 return SNMPAPI_NOERROR
;
328 if ((dst
->ids
= HeapAlloc(GetProcessHeap(), 0, src
->idLength
* sizeof(UINT
))))
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
);
349 HeapFree(GetProcessHeap(), 0, oid
->ids
);
353 /***********************************************************************
354 * SnmpUtilOidNCmp (SNMPAPI.@)
356 INT WINAPI
SnmpUtilOidNCmp(AsnObjectIdentifier
*oid1
, AsnObjectIdentifier
*oid2
, UINT count
)
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;
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
;
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
)
429 asn_any_free(&vb
->value
);
430 HeapFree(GetProcessHeap(), 0, vb
->name
.ids
);
431 vb
->name
.idLength
= 0;
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
);
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
))
466 for (--i
; i
> 0; i
--) SnmpUtilVarBindFree(--dst_entry
);
467 HeapFree(GetProcessHeap(), 0, dst
->list
);
468 return SNMPAPI_ERROR
;
472 return SNMPAPI_NOERROR
;
475 /***********************************************************************
476 * SnmpUtilVarBindListFree (SNMPAPI.@)
478 VOID WINAPI
SnmpUtilVarBindListFree(SnmpVarBindList
*vb
)
486 for (i
= 0; i
< vb
->len
; i
++) SnmpUtilVarBindFree(entry
++);
487 HeapFree(GetProcessHeap(), 0, vb
->list
);
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
;
505 for (i
= 0; i
< length
; i
++)
507 sprintf(one
, "%d", ids
[i
]);
529 /***********************************************************************
530 * SnmpUtilOidToA (SNMPAPI.@)
532 LPSTR WINAPI
SnmpUtilOidToA(AsnObjectIdentifier
*oid
)
534 static char null_oid
[] = "<null oid>";
536 TRACE("(%p)\n", oid
);
539 return SnmpUtilIdsToA(oid
->ids
, oid
->idLength
);
544 /***********************************************************************
545 * SnmpUtilPrintOid (SNMPAPI.@)
547 VOID WINAPI
SnmpUtilPrintOid(AsnObjectIdentifier
*oid
)
551 TRACE("(%p)\n", oid
);
555 for (i
= 0; i
< oid
->idLength
; i
++)
557 TRACE("%u", oid
->ids
[i
]);
558 if (i
< oid
->idLength
- 1) TRACE(".");
563 /***********************************************************************
564 * SnmpUtilPrintAsnAny (SNMPAPI.@)
566 VOID WINAPI
SnmpUtilPrintAsnAny(AsnAny
*any
)
570 TRACE("(%p)\n", any
);
572 switch (any
->asnType
)
574 case ASN_NULL
: TRACE("Null value\n"); return;
575 case ASN_INTEGER32
: TRACE("Integer32 %ld\n", any
->asnValue
.number
); return;
576 case ASN_UNSIGNED32
: TRACE("Unsigned32 %lu\n", any
->asnValue
.unsigned32
); return;
577 case ASN_COUNTER32
: TRACE("Counter32 %lu\n", any
->asnValue
.counter
); return;
578 case ASN_GAUGE32
: TRACE("Gauge32 %lu\n", any
->asnValue
.gauge
); return;
579 case ASN_TIMETICKS
: TRACE("Timeticks %lu\n", any
->asnValue
.ticks
); return;
582 TRACE("Counter64 %I64x\n", any
->asnValue
.counter64
.QuadPart
);
585 case ASN_OCTETSTRING
:
588 for (i
= 0; i
< any
->asnValue
.string
.length
; i
++)
589 TRACE("%c", any
->asnValue
.string
.stream
[i
]);
596 if (any
->asnValue
.string
.length
< 4)
601 for (i
= 0; i
< 4; i
++)
603 TRACE("%u", any
->asnValue
.string
.stream
[i
]);
604 if (i
< 3) TRACE(".");
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(" ");
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(" ");
631 case ASN_OBJECTIDENTIFIER
:
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(".");
644 TRACE("Invalid type %d\n", any
->asnType
);