dinput: Clear DIA_APPNOMAP BuildActionMap flag with specific device semantic.
[wine.git] / dlls / inetmib1 / main.c
blob5d9251835051bf7d9b5b454c16e72ba48dd0da13
1 /*
2 * Copyright 2008 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <limits.h>
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winsock2.h"
29 #include "snmp.h"
30 #include "iphlpapi.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(inetmib1);
35 /**
36 * Utility functions
38 static DWORD copyInt(AsnAny *value, void *src)
40 value->asnType = ASN_INTEGER;
41 value->asnValue.number = *(DWORD *)src;
42 return SNMP_ERRORSTATUS_NOERROR;
45 static void setStringValue(AsnAny *value, BYTE type, DWORD len, BYTE *str)
47 AsnAny strValue;
49 strValue.asnType = type;
50 strValue.asnValue.string.stream = str;
51 strValue.asnValue.string.length = len;
52 strValue.asnValue.string.dynamic = FALSE;
53 SnmpUtilAsnAnyCpy(value, &strValue);
56 typedef DWORD (*copyValueFunc)(AsnAny *value, void *src);
58 struct structToAsnValue
60 size_t offset;
61 copyValueFunc copy;
64 static AsnInteger32 mapStructEntryToValue(struct structToAsnValue *map,
65 UINT mapLen, void *record, UINT id, SnmpVarBind *pVarBind)
67 /* OIDs are 1-based */
68 if (!id)
69 return SNMP_ERRORSTATUS_NOSUCHNAME;
70 --id;
71 if (id >= mapLen)
72 return SNMP_ERRORSTATUS_NOSUCHNAME;
73 if (!map[id].copy)
74 return SNMP_ERRORSTATUS_NOSUCHNAME;
75 return map[id].copy(&pVarBind->value, (BYTE *)record + map[id].offset);
78 static DWORD copyIpAddr(AsnAny *value, void *src)
80 setStringValue(value, ASN_IPADDRESS, sizeof(DWORD), src);
81 return SNMP_ERRORSTATUS_NOERROR;
84 static UINT mib2[] = { 1,3,6,1,2,1 };
85 static UINT mib2System[] = { 1,3,6,1,2,1,1 };
87 typedef BOOL (*varqueryfunc)(BYTE bPduType, SnmpVarBind *pVarBind,
88 AsnInteger32 *pErrorStatus);
90 struct mibImplementation
92 AsnObjectIdentifier name;
93 void (*init)(void);
94 varqueryfunc query;
95 void (*cleanup)(void);
98 static UINT mib2IfNumber[] = { 1,3,6,1,2,1,2,1 };
99 static PMIB_IFTABLE ifTable;
101 static void mib2IfNumberInit(void)
103 DWORD size = 0, ret = GetIfTable(NULL, &size, FALSE);
105 if (ret == ERROR_INSUFFICIENT_BUFFER)
107 MIB_IFTABLE *table = HeapAlloc(GetProcessHeap(), 0, size);
108 if (table)
110 if (!GetIfTable(table, &size, FALSE)) ifTable = table;
111 else HeapFree(GetProcessHeap(), 0, table );
116 static void mib2IfNumberCleanup(void)
118 HeapFree(GetProcessHeap(), 0, ifTable);
121 static BOOL mib2IfNumberQuery(BYTE bPduType, SnmpVarBind *pVarBind,
122 AsnInteger32 *pErrorStatus)
124 AsnObjectIdentifier numberOid = DEFINE_OID(mib2IfNumber);
125 BOOL ret = TRUE;
127 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
128 pErrorStatus);
130 switch (bPduType)
132 case SNMP_PDU_GET:
133 case SNMP_PDU_GETNEXT:
134 if ((bPduType == SNMP_PDU_GET &&
135 !SnmpUtilOidNCmp(&pVarBind->name, &numberOid, numberOid.idLength))
136 || SnmpUtilOidNCmp(&pVarBind->name, &numberOid, numberOid.idLength)
137 < 0)
139 DWORD numIfs = ifTable ? ifTable->dwNumEntries : 0;
141 copyInt(&pVarBind->value, &numIfs);
142 if (bPduType == SNMP_PDU_GETNEXT)
144 SnmpUtilOidFree(&pVarBind->name);
145 SnmpUtilOidCpy(&pVarBind->name, &numberOid);
147 *pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
149 else
151 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
152 /* Caller deals with OID if bPduType == SNMP_PDU_GETNEXT, so don't
153 * need to set it here.
156 break;
157 case SNMP_PDU_SET:
158 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
159 ret = FALSE;
160 break;
161 default:
162 FIXME("0x%02x: unsupported PDU type\n", bPduType);
163 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
165 return ret;
168 static DWORD copyOperStatus(AsnAny *value, void *src)
170 value->asnType = ASN_INTEGER;
171 /* The IPHlpApi definition of operational status differs from the MIB2 one,
172 * so map it to the MIB2 value.
174 switch (*(DWORD *)src)
176 case MIB_IF_OPER_STATUS_OPERATIONAL:
177 value->asnValue.number = MIB_IF_ADMIN_STATUS_UP;
178 break;
179 case MIB_IF_OPER_STATUS_CONNECTING:
180 case MIB_IF_OPER_STATUS_CONNECTED:
181 value->asnValue.number = MIB_IF_ADMIN_STATUS_TESTING;
182 break;
183 default:
184 value->asnValue.number = MIB_IF_ADMIN_STATUS_DOWN;
186 return SNMP_ERRORSTATUS_NOERROR;
189 /* Given an OID and a base OID that it must begin with, finds the item and
190 * integer instance from the OID. E.g., given an OID foo.1.2 and a base OID
191 * foo, returns item 1 and instance 2.
192 * If bPduType is not SNMP_PDU_GETNEXT and either the item or instance is
193 * missing, returns SNMP_ERRORSTATUS_NOSUCHNAME.
194 * If bPduType is SNMP_PDU_GETNEXT, returns the successor to the item and
195 * instance, or item 1, instance 1 if either is missing.
197 static AsnInteger32 getItemAndIntegerInstanceFromOid(AsnObjectIdentifier *oid,
198 AsnObjectIdentifier *base, BYTE bPduType, UINT *item, UINT *instance)
200 AsnInteger32 ret = SNMP_ERRORSTATUS_NOERROR;
202 switch (bPduType)
204 case SNMP_PDU_GETNEXT:
205 if (SnmpUtilOidNCmp(oid, base, base->idLength) < 0)
207 *item = 1;
208 *instance = 1;
210 else if (!SnmpUtilOidNCmp(oid, base, base->idLength))
212 if (oid->idLength == base->idLength ||
213 oid->idLength == base->idLength + 1)
215 /* Either the table or an item within the table is specified,
216 * but the instance is not. Get the first instance.
218 *instance = 1;
219 if (oid->idLength == base->idLength + 1)
220 *item = oid->ids[base->idLength];
221 else
222 *item = 1;
224 else
226 *item = oid->ids[base->idLength];
227 *instance = oid->ids[base->idLength + 1] + 1;
230 else
231 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
232 break;
233 default:
234 if (!SnmpUtilOidNCmp(oid, base, base->idLength))
236 if (oid->idLength == base->idLength ||
237 oid->idLength == base->idLength + 1)
239 /* Either the table or an item within the table is specified,
240 * but the instance is not.
242 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
244 else
246 *item = oid->ids[base->idLength];
247 *instance = oid->ids[base->idLength + 1];
250 else
251 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
253 return ret;
256 /* Given an OID and a base OID that it must begin with, finds the item from the
257 * OID. E.g., given an OID foo.1 and a base OID foo, returns item 1.
258 * If bPduType is not SNMP_PDU_GETNEXT and the item is missing, returns
259 * SNMP_ERRORSTATUS_NOSUCHNAME.
260 * If bPduType is SNMP_PDU_GETNEXT, returns the successor to the item, or item
261 * 1 if the item is missing.
263 static AsnInteger32 getItemFromOid(AsnObjectIdentifier *oid,
264 AsnObjectIdentifier *base, BYTE bPduType, UINT *item)
266 AsnInteger32 ret = SNMP_ERRORSTATUS_NOERROR;
268 switch (bPduType)
270 case SNMP_PDU_GETNEXT:
271 if (SnmpUtilOidNCmp(oid, base, base->idLength) < 0)
272 *item = 1;
273 else if (!SnmpUtilOidNCmp(oid, base, base->idLength))
275 if (oid->idLength == base->idLength)
277 /* The item is missing, assume the first item */
278 *item = 1;
280 else
281 *item = oid->ids[base->idLength] + 1;
283 else
284 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
285 break;
286 default:
287 if (!SnmpUtilOidNCmp(oid, base, base->idLength))
289 if (oid->idLength == base->idLength)
291 /* The item is missing */
292 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
294 else
296 *item = oid->ids[base->idLength];
297 if (!*item)
298 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
301 else
302 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
304 return ret;
307 struct GenericTable
309 DWORD numEntries;
310 BYTE entries[1];
313 static DWORD oidToIpAddr(AsnObjectIdentifier *oid)
315 assert(oid && oid->idLength >= 4);
316 /* Map the IDs to an IP address in little-endian order */
317 return (BYTE)oid->ids[3] << 24 | (BYTE)oid->ids[2] << 16 |
318 (BYTE)oid->ids[1] << 8 | (BYTE)oid->ids[0];
321 typedef void (*oidToKeyFunc)(AsnObjectIdentifier *oid, void *dst);
322 typedef int (__cdecl *compareFunc)(const void *key, const void *value);
324 /* Finds the first value in the table that matches key. Returns its 1-based
325 * index if found, or 0 if not found.
327 static UINT findValueInTable(const void *key,
328 struct GenericTable *table, size_t tableEntrySize, compareFunc compare)
330 UINT index = 0;
331 void *value;
333 value = bsearch(key, table->entries, table->numEntries, tableEntrySize,
334 compare);
335 if (value)
336 index = ((BYTE *)value - (BYTE *)table->entries) / tableEntrySize + 1;
337 return index;
340 /* Finds the first value in the table that matches oid, using makeKey to
341 * convert the oid to a key for comparison. Returns the value's 1-based
342 * index if found, or 0 if not found.
344 static UINT findOidInTable(AsnObjectIdentifier *oid,
345 struct GenericTable *table, size_t tableEntrySize, oidToKeyFunc makeKey,
346 compareFunc compare)
348 UINT index = 0;
349 void *key = HeapAlloc(GetProcessHeap(), 0, tableEntrySize);
351 if (key)
353 makeKey(oid, key);
354 index = findValueInTable(key, table, tableEntrySize, compare);
355 HeapFree(GetProcessHeap(), 0, key);
357 return index;
360 /* Finds the first successor to the value in the table that does matches oid,
361 * using makeKey to convert the oid to a key for comparison. A successor is
362 * a value that does not match oid, so if multiple entries match an oid, only
363 * the first will ever be returned using this method.
364 * Returns the successor's 1-based index if found, or 0 if not found.
366 static UINT findNextOidInTable(AsnObjectIdentifier *oid,
367 struct GenericTable *table, size_t tableEntrySize, oidToKeyFunc makeKey,
368 compareFunc compare)
370 UINT index = 0;
371 void *key = HeapAlloc(GetProcessHeap(), 0, tableEntrySize);
373 if (key)
375 makeKey(oid, key);
376 index = findValueInTable(key, table, tableEntrySize, compare);
377 if (index == 0)
379 /* Not found in table. If it's less than the first entry, return
380 * the first index. Otherwise just return 0 and let the caller
381 * handle finding the successor.
383 if (compare(key, table->entries) < 0)
384 index = 1;
386 else
388 /* Skip any entries that match the same key. This enumeration will
389 * be incomplete, but it's what Windows appears to do if there are
390 * multiple entries with the same index in a table, and it avoids
391 * an infinite loop.
393 for (++index; index <= table->numEntries && compare(key,
394 &table->entries[tableEntrySize * (index - 1)]) == 0; ++index)
397 HeapFree(GetProcessHeap(), 0, key);
399 return index;
402 /* Given an OID and a base OID that it must begin with, finds the item and
403 * element of the table whose value matches the instance from the OID.
404 * The OID is converted to a key with the function makeKey, and compared
405 * against entries in the table with the function compare.
406 * If bPduType is not SNMP_PDU_GETNEXT and either the item or instance is
407 * missing, returns SNMP_ERRORSTATUS_NOSUCHNAME.
408 * If bPduType is SNMP_PDU_GETNEXT, returns the successor to the item and
409 * instance, or item 1, instance 1 if either is missing.
411 static AsnInteger32 getItemAndInstanceFromTable(AsnObjectIdentifier *oid,
412 AsnObjectIdentifier *base, UINT instanceLen, BYTE bPduType,
413 struct GenericTable *table, size_t tableEntrySize, oidToKeyFunc makeKey,
414 compareFunc compare, UINT *item, UINT *instance)
416 AsnInteger32 ret = SNMP_ERRORSTATUS_NOERROR;
418 if (!table)
419 return SNMP_ERRORSTATUS_NOSUCHNAME;
421 switch (bPduType)
423 case SNMP_PDU_GETNEXT:
424 if (SnmpUtilOidNCmp(oid, base, base->idLength) < 0)
426 /* Return the first item and instance from the table */
427 *item = 1;
428 *instance = 1;
430 else if (!SnmpUtilOidNCmp(oid, base, base->idLength) &&
431 oid->idLength < base->idLength + instanceLen + 1)
433 /* Either the table or an item is specified, but the instance is
434 * not.
436 *instance = 1;
437 if (oid->idLength >= base->idLength + 1)
439 *item = oid->ids[base->idLength];
440 if (!*item)
441 *item = 1;
443 else
444 *item = 1;
446 else if (!SnmpUtilOidNCmp(oid, base, base->idLength) &&
447 oid->idLength == base->idLength + instanceLen + 1)
449 *item = oid->ids[base->idLength];
450 if (!*item)
452 *instance = 1;
453 *item = 1;
455 else
457 AsnObjectIdentifier instanceOid = { instanceLen,
458 oid->ids + base->idLength + 1 };
460 *instance = findNextOidInTable(&instanceOid, table,
461 tableEntrySize, makeKey, compare);
462 if (!*instance || *instance > table->numEntries)
463 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
466 else
467 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
468 break;
469 default:
470 if (!SnmpUtilOidNCmp(oid, base, base->idLength) &&
471 oid->idLength == base->idLength + instanceLen + 1)
473 *item = oid->ids[base->idLength];
474 if (!*item)
475 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
476 else
478 AsnObjectIdentifier instanceOid = { instanceLen,
479 oid->ids + base->idLength + 1 };
481 *instance = findOidInTable(&instanceOid, table, tableEntrySize,
482 makeKey, compare);
483 if (!*instance)
484 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
487 else
488 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
490 return ret;
493 static INT setOidWithItem(AsnObjectIdentifier *dst, AsnObjectIdentifier *base,
494 UINT item)
496 UINT id;
497 AsnObjectIdentifier oid;
498 INT ret;
500 SnmpUtilOidFree(dst);
501 ret = SnmpUtilOidCpy(dst, base);
502 if (ret)
504 oid.idLength = 1;
505 oid.ids = &id;
506 id = item;
507 ret = SnmpUtilOidAppend(dst, &oid);
509 return ret;
512 static INT setOidWithItemAndIpAddr(AsnObjectIdentifier *dst,
513 AsnObjectIdentifier *base, UINT item, DWORD addr)
515 UINT id;
516 BYTE *ptr;
517 AsnObjectIdentifier oid;
518 INT ret;
520 ret = setOidWithItem(dst, base, item);
521 if (ret)
523 oid.idLength = 1;
524 oid.ids = &id;
525 for (ptr = (BYTE *)&addr; ret && ptr < (BYTE *)&addr + sizeof(DWORD);
526 ptr++)
528 id = *ptr;
529 ret = SnmpUtilOidAppend(dst, &oid);
532 return ret;
535 static INT setOidWithItemAndInteger(AsnObjectIdentifier *dst,
536 AsnObjectIdentifier *base, UINT item, UINT instance)
538 AsnObjectIdentifier oid;
539 INT ret;
541 ret = setOidWithItem(dst, base, item);
542 if (ret)
544 oid.idLength = 1;
545 oid.ids = &instance;
546 ret = SnmpUtilOidAppend(dst, &oid);
548 return ret;
551 static DWORD copyIfRowDescr(AsnAny *value, void *src)
553 PMIB_IFROW row = (PMIB_IFROW)((BYTE *)src -
554 FIELD_OFFSET(MIB_IFROW, dwDescrLen));
555 DWORD ret;
557 if (row->dwDescrLen)
559 setStringValue(value, ASN_OCTETSTRING, row->dwDescrLen, row->bDescr);
560 ret = SNMP_ERRORSTATUS_NOERROR;
562 else
563 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
564 return ret;
567 static DWORD copyIfRowPhysAddr(AsnAny *value, void *src)
569 PMIB_IFROW row = (PMIB_IFROW)((BYTE *)src -
570 FIELD_OFFSET(MIB_IFROW, dwPhysAddrLen));
571 DWORD ret;
573 if (row->dwPhysAddrLen)
575 setStringValue(value, ASN_OCTETSTRING, row->dwPhysAddrLen,
576 row->bPhysAddr);
577 ret = SNMP_ERRORSTATUS_NOERROR;
579 else
580 ret = SNMP_ERRORSTATUS_NOSUCHNAME;
581 return ret;
584 static struct structToAsnValue mib2IfEntryMap[] = {
585 { FIELD_OFFSET(MIB_IFROW, dwIndex), copyInt },
586 { FIELD_OFFSET(MIB_IFROW, dwDescrLen), copyIfRowDescr },
587 { FIELD_OFFSET(MIB_IFROW, dwType), copyInt },
588 { FIELD_OFFSET(MIB_IFROW, dwMtu), copyInt },
589 { FIELD_OFFSET(MIB_IFROW, dwSpeed), copyInt },
590 { FIELD_OFFSET(MIB_IFROW, dwPhysAddrLen), copyIfRowPhysAddr },
591 { FIELD_OFFSET(MIB_IFROW, dwAdminStatus), copyInt },
592 { FIELD_OFFSET(MIB_IFROW, dwOperStatus), copyOperStatus },
593 { FIELD_OFFSET(MIB_IFROW, dwLastChange), copyInt },
594 { FIELD_OFFSET(MIB_IFROW, dwInOctets), copyInt },
595 { FIELD_OFFSET(MIB_IFROW, dwInUcastPkts), copyInt },
596 { FIELD_OFFSET(MIB_IFROW, dwInNUcastPkts), copyInt },
597 { FIELD_OFFSET(MIB_IFROW, dwInDiscards), copyInt },
598 { FIELD_OFFSET(MIB_IFROW, dwInErrors), copyInt },
599 { FIELD_OFFSET(MIB_IFROW, dwInUnknownProtos), copyInt },
600 { FIELD_OFFSET(MIB_IFROW, dwOutOctets), copyInt },
601 { FIELD_OFFSET(MIB_IFROW, dwOutUcastPkts), copyInt },
602 { FIELD_OFFSET(MIB_IFROW, dwOutNUcastPkts), copyInt },
603 { FIELD_OFFSET(MIB_IFROW, dwOutDiscards), copyInt },
604 { FIELD_OFFSET(MIB_IFROW, dwOutErrors), copyInt },
605 { FIELD_OFFSET(MIB_IFROW, dwOutQLen), copyInt },
608 static UINT mib2IfEntry[] = { 1,3,6,1,2,1,2,2,1 };
610 static BOOL mib2IfEntryQuery(BYTE bPduType, SnmpVarBind *pVarBind,
611 AsnInteger32 *pErrorStatus)
613 AsnObjectIdentifier entryOid = DEFINE_OID(mib2IfEntry);
614 BOOL ret = TRUE;
616 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
617 pErrorStatus);
619 switch (bPduType)
621 case SNMP_PDU_GET:
622 case SNMP_PDU_GETNEXT:
623 if (!ifTable)
625 /* There is no interface present, so let the caller deal
626 * with finding the successor.
628 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
630 else
632 UINT tableIndex = 0, item = 0;
634 *pErrorStatus = getItemAndIntegerInstanceFromOid(&pVarBind->name,
635 &entryOid, bPduType, &item, &tableIndex);
636 if (!*pErrorStatus)
638 assert(tableIndex);
639 assert(item);
640 if (tableIndex > ifTable->dwNumEntries)
641 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
642 else
644 *pErrorStatus = mapStructEntryToValue(mib2IfEntryMap,
645 DEFINE_SIZEOF(mib2IfEntryMap),
646 &ifTable->table[tableIndex - 1], item,
647 pVarBind);
648 if (bPduType == SNMP_PDU_GETNEXT)
649 ret = setOidWithItemAndInteger(&pVarBind->name,
650 &entryOid, item, tableIndex);
654 break;
655 case SNMP_PDU_SET:
656 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
657 ret = FALSE;
658 break;
659 default:
660 FIXME("0x%02x: unsupported PDU type\n", bPduType);
661 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
663 return ret;
666 static UINT mib2Ip[] = { 1,3,6,1,2,1,4 };
667 static MIB_IPSTATS ipStats;
669 static void mib2IpStatsInit(void)
671 GetIpStatistics(&ipStats);
674 static struct structToAsnValue mib2IpMap[] = {
675 { FIELD_OFFSET(MIB_IPSTATS, u.dwForwarding), copyInt }, /* 1 */
676 { FIELD_OFFSET(MIB_IPSTATS, dwDefaultTTL), copyInt }, /* 2 */
677 { FIELD_OFFSET(MIB_IPSTATS, dwInReceives), copyInt }, /* 3 */
678 { FIELD_OFFSET(MIB_IPSTATS, dwInHdrErrors), copyInt }, /* 4 */
679 { FIELD_OFFSET(MIB_IPSTATS, dwInAddrErrors), copyInt }, /* 5 */
680 { FIELD_OFFSET(MIB_IPSTATS, dwForwDatagrams), copyInt }, /* 6 */
681 { FIELD_OFFSET(MIB_IPSTATS, dwInUnknownProtos), copyInt }, /* 7 */
682 { FIELD_OFFSET(MIB_IPSTATS, dwInDiscards), copyInt }, /* 8 */
683 { FIELD_OFFSET(MIB_IPSTATS, dwInDelivers), copyInt }, /* 9 */
684 { FIELD_OFFSET(MIB_IPSTATS, dwOutRequests), copyInt }, /* 10 */
685 { FIELD_OFFSET(MIB_IPSTATS, dwOutDiscards), copyInt }, /* 11 */
686 { FIELD_OFFSET(MIB_IPSTATS, dwOutNoRoutes), copyInt }, /* 12 */
687 { FIELD_OFFSET(MIB_IPSTATS, dwReasmTimeout), copyInt }, /* 13 */
688 { FIELD_OFFSET(MIB_IPSTATS, dwReasmReqds), copyInt }, /* 14 */
689 { FIELD_OFFSET(MIB_IPSTATS, dwReasmOks), copyInt }, /* 15 */
690 { FIELD_OFFSET(MIB_IPSTATS, dwReasmFails), copyInt }, /* 16 */
691 { FIELD_OFFSET(MIB_IPSTATS, dwFragOks), copyInt }, /* 17 */
692 { FIELD_OFFSET(MIB_IPSTATS, dwFragFails), copyInt }, /* 18 */
693 { FIELD_OFFSET(MIB_IPSTATS, dwFragCreates), copyInt }, /* 19 */
694 { 0, NULL }, /* 20: not used, IP addr table */
695 { 0, NULL }, /* 21: not used, route table */
696 { 0, NULL }, /* 22: not used, net to media (ARP) table */
697 { FIELD_OFFSET(MIB_IPSTATS, dwRoutingDiscards), copyInt }, /* 23 */
700 static BOOL mib2IpStatsQuery(BYTE bPduType, SnmpVarBind *pVarBind,
701 AsnInteger32 *pErrorStatus)
703 AsnObjectIdentifier myOid = DEFINE_OID(mib2Ip);
704 UINT item = 0;
705 BOOL ret = TRUE;
707 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
708 pErrorStatus);
710 switch (bPduType)
712 case SNMP_PDU_GET:
713 case SNMP_PDU_GETNEXT:
714 *pErrorStatus = getItemFromOid(&pVarBind->name, &myOid, bPduType,
715 &item);
716 if (!*pErrorStatus)
718 *pErrorStatus = mapStructEntryToValue(mib2IpMap,
719 DEFINE_SIZEOF(mib2IpMap), &ipStats, item, pVarBind);
720 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
721 ret = setOidWithItem(&pVarBind->name, &myOid, item);
723 break;
724 case SNMP_PDU_SET:
725 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
726 ret = FALSE;
727 break;
728 default:
729 FIXME("0x%02x: unsupported PDU type\n", bPduType);
730 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
732 return ret;
735 static UINT mib2IpAddr[] = { 1,3,6,1,2,1,4,20,1 };
736 static PMIB_IPADDRTABLE ipAddrTable;
738 static struct structToAsnValue mib2IpAddrMap[] = {
739 { FIELD_OFFSET(MIB_IPADDRROW, dwAddr), copyIpAddr },
740 { FIELD_OFFSET(MIB_IPADDRROW, dwIndex), copyInt },
741 { FIELD_OFFSET(MIB_IPADDRROW, dwMask), copyIpAddr },
742 { FIELD_OFFSET(MIB_IPADDRROW, dwBCastAddr), copyInt },
743 { FIELD_OFFSET(MIB_IPADDRROW, dwReasmSize), copyInt },
746 static void mib2IpAddrInit(void)
748 DWORD size = 0, ret = GetIpAddrTable(NULL, &size, TRUE);
750 if (ret == ERROR_INSUFFICIENT_BUFFER)
752 MIB_IPADDRTABLE *table = HeapAlloc(GetProcessHeap(), 0, size);
753 if (table)
755 if (!GetIpAddrTable(table, &size, TRUE)) ipAddrTable = table;
756 else HeapFree(GetProcessHeap(), 0, table );
761 static void mib2IpAddrCleanup(void)
763 HeapFree(GetProcessHeap(), 0, ipAddrTable);
766 static void oidToIpAddrRow(AsnObjectIdentifier *oid, void *dst)
768 MIB_IPADDRROW *row = dst;
770 row->dwAddr = oidToIpAddr(oid);
773 static int __cdecl DWORD_cmp(DWORD a, DWORD b)
775 return a < b ? -1 : a > b ? 1 : 0; /* a subtraction would overflow */
778 static int __cdecl compareIpAddrRow(const void *a, const void *b)
780 const MIB_IPADDRROW *rowA = a, *rowB = b;
781 return DWORD_cmp(ntohl(rowA->dwAddr), ntohl(rowB->dwAddr));
784 static BOOL mib2IpAddrQuery(BYTE bPduType, SnmpVarBind *pVarBind,
785 AsnInteger32 *pErrorStatus)
787 AsnObjectIdentifier myOid = DEFINE_OID(mib2IpAddr);
788 UINT tableIndex = 0, item = 0;
789 BOOL ret = TRUE;
791 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
792 pErrorStatus);
794 switch (bPduType)
796 case SNMP_PDU_GET:
797 case SNMP_PDU_GETNEXT:
798 *pErrorStatus = getItemAndInstanceFromTable(&pVarBind->name,
799 &myOid, 4, bPduType, (struct GenericTable *)ipAddrTable,
800 sizeof(MIB_IPADDRROW), oidToIpAddrRow, compareIpAddrRow, &item,
801 &tableIndex);
802 if (!*pErrorStatus)
804 assert(tableIndex);
805 assert(item);
806 *pErrorStatus = mapStructEntryToValue(mib2IpAddrMap,
807 DEFINE_SIZEOF(mib2IpAddrMap),
808 &ipAddrTable->table[tableIndex - 1], item, pVarBind);
809 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
810 ret = setOidWithItemAndIpAddr(&pVarBind->name, &myOid, item,
811 ipAddrTable->table[tableIndex - 1].dwAddr);
813 break;
814 case SNMP_PDU_SET:
815 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
816 ret = FALSE;
817 break;
818 default:
819 FIXME("0x%02x: unsupported PDU type\n", bPduType);
820 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
822 return ret;
825 static UINT mib2IpRoute[] = { 1,3,6,1,2,1,4,21,1 };
826 static PMIB_IPFORWARDTABLE ipRouteTable;
828 static struct structToAsnValue mib2IpRouteMap[] = {
829 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardDest), copyIpAddr },
830 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardIfIndex), copyInt },
831 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMetric1), copyInt },
832 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMetric2), copyInt },
833 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMetric3), copyInt },
834 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMetric4), copyInt },
835 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardNextHop), copyIpAddr },
836 { FIELD_OFFSET(MIB_IPFORWARDROW, u1.dwForwardType), copyInt },
837 { FIELD_OFFSET(MIB_IPFORWARDROW, u2.dwForwardProto), copyInt },
838 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardAge), copyInt },
839 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMask), copyIpAddr },
840 { FIELD_OFFSET(MIB_IPFORWARDROW, dwForwardMetric5), copyInt },
843 static void mib2IpRouteInit(void)
845 DWORD size = 0, ret = GetIpForwardTable(NULL, &size, TRUE);
847 if (ret == ERROR_INSUFFICIENT_BUFFER)
849 MIB_IPFORWARDTABLE *table = HeapAlloc(GetProcessHeap(), 0, size);
850 if (table)
852 if (!GetIpForwardTable(table, &size, TRUE)) ipRouteTable = table;
853 else HeapFree(GetProcessHeap(), 0, table );
858 static void mib2IpRouteCleanup(void)
860 HeapFree(GetProcessHeap(), 0, ipRouteTable);
863 static void oidToIpForwardRow(AsnObjectIdentifier *oid, void *dst)
865 MIB_IPFORWARDROW *row = dst;
867 row->dwForwardDest = oidToIpAddr(oid);
870 static int __cdecl compareIpForwardRow(const void *a, const void *b)
872 const MIB_IPFORWARDROW *rowA = a, *rowB = b;
873 return DWORD_cmp(ntohl(rowA->dwForwardDest), ntohl(rowB->dwForwardDest));
876 static BOOL mib2IpRouteQuery(BYTE bPduType, SnmpVarBind *pVarBind,
877 AsnInteger32 *pErrorStatus)
879 AsnObjectIdentifier myOid = DEFINE_OID(mib2IpRoute);
880 UINT tableIndex = 0, item = 0;
881 BOOL ret = TRUE;
883 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
884 pErrorStatus);
886 switch (bPduType)
888 case SNMP_PDU_GET:
889 case SNMP_PDU_GETNEXT:
890 *pErrorStatus = getItemAndInstanceFromTable(&pVarBind->name,
891 &myOid, 4, bPduType, (struct GenericTable *)ipRouteTable,
892 sizeof(MIB_IPFORWARDROW), oidToIpForwardRow, compareIpForwardRow,
893 &item, &tableIndex);
894 if (!*pErrorStatus)
896 assert(tableIndex);
897 assert(item);
898 *pErrorStatus = mapStructEntryToValue(mib2IpRouteMap,
899 DEFINE_SIZEOF(mib2IpRouteMap),
900 &ipRouteTable->table[tableIndex - 1], item, pVarBind);
901 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
902 ret = setOidWithItemAndIpAddr(&pVarBind->name, &myOid, item,
903 ipRouteTable->table[tableIndex - 1].dwForwardDest);
905 break;
906 case SNMP_PDU_SET:
907 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
908 ret = FALSE;
909 break;
910 default:
911 FIXME("0x%02x: unsupported PDU type\n", bPduType);
912 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
914 return ret;
917 static UINT mib2IpNet[] = { 1,3,6,1,2,1,4,22,1 };
918 static PMIB_IPNETTABLE ipNetTable;
920 static DWORD copyIpNetPhysAddr(AsnAny *value, void *src)
922 PMIB_IPNETROW row = (PMIB_IPNETROW)((BYTE *)src - FIELD_OFFSET(MIB_IPNETROW,
923 dwPhysAddrLen));
925 setStringValue(value, ASN_OCTETSTRING, row->dwPhysAddrLen, row->bPhysAddr);
926 return SNMP_ERRORSTATUS_NOERROR;
929 static struct structToAsnValue mib2IpNetMap[] = {
930 { FIELD_OFFSET(MIB_IPNETROW, dwIndex), copyInt },
931 { FIELD_OFFSET(MIB_IPNETROW, dwPhysAddrLen), copyIpNetPhysAddr },
932 { FIELD_OFFSET(MIB_IPNETROW, dwAddr), copyIpAddr },
933 { FIELD_OFFSET(MIB_IPNETROW, u.dwType), copyInt },
936 static void mib2IpNetInit(void)
938 DWORD size = 0, ret = GetIpNetTable(NULL, &size, FALSE);
940 if (ret == ERROR_INSUFFICIENT_BUFFER)
942 MIB_IPNETTABLE *table = HeapAlloc(GetProcessHeap(), 0, size);
943 if (table)
945 if (!GetIpNetTable(table, &size, FALSE)) ipNetTable = table;
946 else HeapFree(GetProcessHeap(), 0, table );
951 static void mib2IpNetCleanup(void)
953 HeapFree(GetProcessHeap(), 0, ipNetTable);
956 static BOOL mib2IpNetQuery(BYTE bPduType, SnmpVarBind *pVarBind,
957 AsnInteger32 *pErrorStatus)
959 AsnObjectIdentifier myOid = DEFINE_OID(mib2IpNet);
960 BOOL ret = TRUE;
962 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
963 pErrorStatus);
965 switch (bPduType)
967 case SNMP_PDU_GET:
968 case SNMP_PDU_GETNEXT:
969 if (!ipNetTable)
970 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
971 else
973 UINT tableIndex = 0, item = 0;
975 *pErrorStatus = getItemAndIntegerInstanceFromOid(&pVarBind->name,
976 &myOid, bPduType, &item, &tableIndex);
977 if (!*pErrorStatus)
979 assert(tableIndex);
980 assert(item);
981 if (tableIndex > ipNetTable->dwNumEntries)
982 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
983 else
985 *pErrorStatus = mapStructEntryToValue(mib2IpNetMap,
986 DEFINE_SIZEOF(mib2IpNetMap),
987 &ipNetTable[tableIndex - 1], item, pVarBind);
988 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
989 ret = setOidWithItemAndInteger(&pVarBind->name, &myOid,
990 item, tableIndex);
994 break;
995 case SNMP_PDU_SET:
996 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
997 ret = FALSE;
998 break;
999 default:
1000 FIXME("0x%02x: unsupported PDU type\n", bPduType);
1001 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1003 return ret;
1006 static UINT mib2Icmp[] = { 1,3,6,1,2,1,5 };
1007 static MIB_ICMP icmpStats;
1009 static void mib2IcmpInit(void)
1011 GetIcmpStatistics(&icmpStats);
1014 static struct structToAsnValue mib2IcmpMap[] = {
1015 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwMsgs), copyInt },
1016 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwErrors), copyInt },
1017 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwDestUnreachs), copyInt },
1018 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwTimeExcds), copyInt },
1019 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwParmProbs), copyInt },
1020 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwSrcQuenchs), copyInt },
1021 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwRedirects), copyInt },
1022 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwEchos), copyInt },
1023 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwEchoReps), copyInt },
1024 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwTimestamps), copyInt },
1025 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwTimestampReps), copyInt },
1026 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwAddrMasks), copyInt },
1027 { FIELD_OFFSET(MIBICMPINFO, icmpInStats.dwAddrMaskReps), copyInt },
1028 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwMsgs), copyInt },
1029 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwErrors), copyInt },
1030 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwDestUnreachs), copyInt },
1031 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwTimeExcds), copyInt },
1032 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwParmProbs), copyInt },
1033 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwSrcQuenchs), copyInt },
1034 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwRedirects), copyInt },
1035 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwEchos), copyInt },
1036 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwEchoReps), copyInt },
1037 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwTimestamps), copyInt },
1038 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwTimestampReps), copyInt },
1039 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwAddrMasks), copyInt },
1040 { FIELD_OFFSET(MIBICMPINFO, icmpOutStats.dwAddrMaskReps), copyInt },
1043 static BOOL mib2IcmpQuery(BYTE bPduType, SnmpVarBind *pVarBind,
1044 AsnInteger32 *pErrorStatus)
1046 AsnObjectIdentifier myOid = DEFINE_OID(mib2Icmp);
1047 UINT item = 0;
1048 BOOL ret = TRUE;
1050 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
1051 pErrorStatus);
1053 switch (bPduType)
1055 case SNMP_PDU_GET:
1056 case SNMP_PDU_GETNEXT:
1057 *pErrorStatus = getItemFromOid(&pVarBind->name, &myOid, bPduType,
1058 &item);
1059 if (!*pErrorStatus)
1061 *pErrorStatus = mapStructEntryToValue(mib2IcmpMap,
1062 DEFINE_SIZEOF(mib2IcmpMap), &icmpStats, item,
1063 pVarBind);
1064 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
1065 ret = setOidWithItem(&pVarBind->name, &myOid, item);
1067 break;
1068 case SNMP_PDU_SET:
1069 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
1070 ret = FALSE;
1071 break;
1072 default:
1073 FIXME("0x%02x: unsupported PDU type\n", bPduType);
1074 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1076 return ret;
1079 static UINT mib2Tcp[] = { 1,3,6,1,2,1,6 };
1080 static MIB_TCPSTATS tcpStats;
1082 static void mib2TcpInit(void)
1084 GetTcpStatistics(&tcpStats);
1087 static struct structToAsnValue mib2TcpMap[] = {
1088 { FIELD_OFFSET(MIB_TCPSTATS, u.dwRtoAlgorithm), copyInt },
1089 { FIELD_OFFSET(MIB_TCPSTATS, dwRtoMin), copyInt },
1090 { FIELD_OFFSET(MIB_TCPSTATS, dwRtoMax), copyInt },
1091 { FIELD_OFFSET(MIB_TCPSTATS, dwMaxConn), copyInt },
1092 { FIELD_OFFSET(MIB_TCPSTATS, dwActiveOpens), copyInt },
1093 { FIELD_OFFSET(MIB_TCPSTATS, dwPassiveOpens), copyInt },
1094 { FIELD_OFFSET(MIB_TCPSTATS, dwAttemptFails), copyInt },
1095 { FIELD_OFFSET(MIB_TCPSTATS, dwEstabResets), copyInt },
1096 { FIELD_OFFSET(MIB_TCPSTATS, dwCurrEstab), copyInt },
1097 { FIELD_OFFSET(MIB_TCPSTATS, dwInSegs), copyInt },
1098 { FIELD_OFFSET(MIB_TCPSTATS, dwOutSegs), copyInt },
1099 { FIELD_OFFSET(MIB_TCPSTATS, dwRetransSegs), copyInt },
1100 { FIELD_OFFSET(MIB_TCPSTATS, dwInErrs), copyInt },
1101 { FIELD_OFFSET(MIB_TCPSTATS, dwOutRsts), copyInt },
1102 { FIELD_OFFSET(MIB_TCPSTATS, dwNumConns), copyInt },
1105 static BOOL mib2TcpQuery(BYTE bPduType, SnmpVarBind *pVarBind,
1106 AsnInteger32 *pErrorStatus)
1108 AsnObjectIdentifier myOid = DEFINE_OID(mib2Tcp);
1109 UINT item = 0;
1110 BOOL ret = TRUE;
1112 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
1113 pErrorStatus);
1115 switch (bPduType)
1117 case SNMP_PDU_GET:
1118 case SNMP_PDU_GETNEXT:
1119 *pErrorStatus = getItemFromOid(&pVarBind->name, &myOid, bPduType,
1120 &item);
1121 if (!*pErrorStatus)
1123 *pErrorStatus = mapStructEntryToValue(mib2TcpMap,
1124 DEFINE_SIZEOF(mib2TcpMap), &tcpStats, item, pVarBind);
1125 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
1126 ret = setOidWithItem(&pVarBind->name, &myOid, item);
1128 break;
1129 case SNMP_PDU_SET:
1130 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
1131 ret = FALSE;
1132 break;
1133 default:
1134 FIXME("0x%02x: unsupported PDU type\n", bPduType);
1135 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1137 return ret;
1140 static UINT mib2Udp[] = { 1,3,6,1,2,1,7 };
1141 static MIB_UDPSTATS udpStats;
1143 static void mib2UdpInit(void)
1145 GetUdpStatistics(&udpStats);
1148 static struct structToAsnValue mib2UdpMap[] = {
1149 { FIELD_OFFSET(MIB_UDPSTATS, dwInDatagrams), copyInt },
1150 { FIELD_OFFSET(MIB_UDPSTATS, dwNoPorts), copyInt },
1151 { FIELD_OFFSET(MIB_UDPSTATS, dwInErrors), copyInt },
1152 { FIELD_OFFSET(MIB_UDPSTATS, dwOutDatagrams), copyInt },
1155 static BOOL mib2UdpQuery(BYTE bPduType, SnmpVarBind *pVarBind,
1156 AsnInteger32 *pErrorStatus)
1158 AsnObjectIdentifier myOid = DEFINE_OID(mib2Udp);
1159 UINT item;
1160 BOOL ret = TRUE;
1162 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
1163 pErrorStatus);
1165 switch (bPduType)
1167 case SNMP_PDU_GET:
1168 case SNMP_PDU_GETNEXT:
1169 *pErrorStatus = getItemFromOid(&pVarBind->name, &myOid, bPduType,
1170 &item);
1171 if (!*pErrorStatus)
1173 *pErrorStatus = mapStructEntryToValue(mib2UdpMap,
1174 DEFINE_SIZEOF(mib2UdpMap), &udpStats, item, pVarBind);
1175 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
1176 ret = setOidWithItem(&pVarBind->name, &myOid, item);
1178 break;
1179 case SNMP_PDU_SET:
1180 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
1181 ret = FALSE;
1182 break;
1183 default:
1184 FIXME("0x%02x: unsupported PDU type\n", bPduType);
1185 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1187 return ret;
1190 static UINT mib2UdpEntry[] = { 1,3,6,1,2,1,7,5,1 };
1191 static PMIB_UDPTABLE udpTable;
1193 static void mib2UdpEntryInit(void)
1195 DWORD size = 0, ret = GetUdpTable(NULL, &size, TRUE);
1197 if (ret == ERROR_INSUFFICIENT_BUFFER)
1199 MIB_UDPTABLE *table = HeapAlloc(GetProcessHeap(), 0, size);
1200 if (table)
1202 if (!GetUdpTable(table, &size, TRUE)) udpTable = table;
1203 else HeapFree(GetProcessHeap(), 0, table);
1208 static void mib2UdpEntryCleanup(void)
1210 HeapFree(GetProcessHeap(), 0, udpTable);
1213 static struct structToAsnValue mib2UdpEntryMap[] = {
1214 { FIELD_OFFSET(MIB_UDPROW, dwLocalAddr), copyIpAddr },
1215 { FIELD_OFFSET(MIB_UDPROW, dwLocalPort), copyInt },
1218 static void oidToUdpRow(AsnObjectIdentifier *oid, void *dst)
1220 MIB_UDPROW *row = dst;
1222 assert(oid && oid->idLength >= 5);
1223 row->dwLocalAddr = oidToIpAddr(oid);
1224 row->dwLocalPort = htons(oid->ids[4]);
1227 static int __cdecl compareUdpRow(const void *a, const void *b)
1229 const MIB_UDPROW *rowA = a, *rowB = b;
1230 return DWORD_cmp(ntohl(rowA->dwLocalAddr), ntohl(rowB->dwLocalAddr)) ||
1231 ntohs(rowA->dwLocalPort) - ntohs(rowB->dwLocalPort);
1234 static BOOL mib2UdpEntryQuery(BYTE bPduType, SnmpVarBind *pVarBind,
1235 AsnInteger32 *pErrorStatus)
1237 AsnObjectIdentifier myOid = DEFINE_OID(mib2UdpEntry);
1238 BOOL ret = TRUE;
1240 TRACE("(0x%02x, %s, %p)\n", bPduType, SnmpUtilOidToA(&pVarBind->name),
1241 pErrorStatus);
1243 switch (bPduType)
1245 case SNMP_PDU_GET:
1246 case SNMP_PDU_GETNEXT:
1247 if (!udpTable)
1248 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1249 else
1251 UINT tableIndex = 0, item = 0;
1253 *pErrorStatus = getItemAndInstanceFromTable(&pVarBind->name, &myOid,
1254 5, bPduType, (struct GenericTable *)udpTable,
1255 sizeof(MIB_UDPROW), oidToUdpRow, compareUdpRow, &item,
1256 &tableIndex);
1257 if (!*pErrorStatus)
1259 assert(tableIndex);
1260 assert(item);
1261 *pErrorStatus = mapStructEntryToValue(mib2UdpEntryMap,
1262 DEFINE_SIZEOF(mib2UdpEntryMap),
1263 &udpTable->table[tableIndex - 1], item, pVarBind);
1264 if (!*pErrorStatus && bPduType == SNMP_PDU_GETNEXT)
1266 AsnObjectIdentifier oid;
1268 ret = setOidWithItemAndIpAddr(&pVarBind->name, &myOid, item,
1269 udpTable->table[tableIndex - 1].dwLocalAddr);
1270 if (ret)
1272 UINT id = ntohs(udpTable->table[tableIndex - 1].dwLocalPort);
1273 oid.idLength = 1;
1274 oid.ids = &id;
1275 ret = SnmpUtilOidAppend(&pVarBind->name, &oid);
1280 break;
1281 case SNMP_PDU_SET:
1282 *pErrorStatus = SNMP_ERRORSTATUS_READONLY;
1283 ret = FALSE;
1284 break;
1285 default:
1286 FIXME("0x%02x: unsupported PDU type\n", bPduType);
1287 *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
1289 return ret;
1292 /* This list MUST BE lexicographically sorted */
1293 static struct mibImplementation supportedIDs[] = {
1294 { DEFINE_OID(mib2IfNumber), mib2IfNumberInit, mib2IfNumberQuery,
1295 mib2IfNumberCleanup },
1296 { DEFINE_OID(mib2IfEntry), NULL, mib2IfEntryQuery, NULL },
1297 { DEFINE_OID(mib2Ip), mib2IpStatsInit, mib2IpStatsQuery, NULL },
1298 { DEFINE_OID(mib2IpAddr), mib2IpAddrInit, mib2IpAddrQuery,
1299 mib2IpAddrCleanup },
1300 { DEFINE_OID(mib2IpRoute), mib2IpRouteInit, mib2IpRouteQuery,
1301 mib2IpRouteCleanup },
1302 { DEFINE_OID(mib2IpNet), mib2IpNetInit, mib2IpNetQuery, mib2IpNetCleanup },
1303 { DEFINE_OID(mib2Icmp), mib2IcmpInit, mib2IcmpQuery, NULL },
1304 { DEFINE_OID(mib2Tcp), mib2TcpInit, mib2TcpQuery, NULL },
1305 { DEFINE_OID(mib2Udp), mib2UdpInit, mib2UdpQuery, NULL },
1306 { DEFINE_OID(mib2UdpEntry), mib2UdpEntryInit, mib2UdpEntryQuery,
1307 mib2UdpEntryCleanup },
1309 static UINT minSupportedIDLength;
1311 /*****************************************************************************
1312 * SnmpExtensionInit [INETMIB1.@]
1314 BOOL WINAPI SnmpExtensionInit(DWORD dwUptimeReference,
1315 HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
1317 AsnObjectIdentifier myOid = DEFINE_OID(mib2System);
1318 UINT i;
1320 TRACE("(%ld, %p, %p)\n", dwUptimeReference, phSubagentTrapEvent,
1321 pFirstSupportedRegion);
1323 minSupportedIDLength = UINT_MAX;
1324 for (i = 0; i < ARRAY_SIZE(supportedIDs); i++)
1326 if (supportedIDs[i].init)
1327 supportedIDs[i].init();
1328 if (supportedIDs[i].name.idLength < minSupportedIDLength)
1329 minSupportedIDLength = supportedIDs[i].name.idLength;
1331 *phSubagentTrapEvent = NULL;
1332 SnmpUtilOidCpy(pFirstSupportedRegion, &myOid);
1333 return TRUE;
1336 static void cleanup(void)
1338 UINT i;
1340 for (i = 0; i < ARRAY_SIZE(supportedIDs); i++)
1341 if (supportedIDs[i].cleanup)
1342 supportedIDs[i].cleanup();
1345 static struct mibImplementation *findSupportedQuery(UINT *ids, UINT idLength,
1346 UINT *matchingIndex)
1348 int indexHigh = DEFINE_SIZEOF(supportedIDs) - 1, indexLow = 0;
1349 AsnObjectIdentifier oid1 = { idLength, ids};
1351 if (!idLength)
1352 return NULL;
1354 while (indexLow <= indexHigh)
1356 INT cmp, i = (indexLow + indexHigh) / 2;
1357 if (!(cmp = SnmpUtilOidNCmp(&oid1, &supportedIDs[i].name, idLength)))
1359 *matchingIndex = i;
1360 return &supportedIDs[i];
1362 if (cmp > 0)
1363 indexLow = i + 1;
1364 else
1365 indexHigh = i - 1;
1367 return NULL;
1370 /*****************************************************************************
1371 * SnmpExtensionQuery [INETMIB1.@]
1373 BOOL WINAPI SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList,
1374 AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
1376 AsnObjectIdentifier mib2oid = DEFINE_OID(mib2);
1377 AsnInteger32 error = SNMP_ERRORSTATUS_NOERROR, errorIndex = 0;
1378 UINT i;
1379 BOOL ret = TRUE;
1381 TRACE("(0x%02x, %p, %p, %p)\n", bPduType, pVarBindList,
1382 pErrorStatus, pErrorIndex);
1384 for (i = 0; !error && i < pVarBindList->len; i++)
1386 /* Ignore any OIDs not in MIB2 */
1387 if (!SnmpUtilOidNCmp(&pVarBindList->list[i].name, &mib2oid,
1388 mib2oid.idLength))
1390 struct mibImplementation *impl = NULL;
1391 UINT len, matchingIndex = 0;
1393 TRACE("%s\n", SnmpUtilOidToA(&pVarBindList->list[i].name));
1394 /* Search for an implementation matching as many octets as possible
1396 for (len = pVarBindList->list[i].name.idLength;
1397 len >= minSupportedIDLength && !impl; len--)
1398 impl = findSupportedQuery(pVarBindList->list[i].name.ids, len,
1399 &matchingIndex);
1400 if (impl && impl->query)
1401 ret = impl->query(bPduType, &pVarBindList->list[i], &error);
1402 else
1403 error = SNMP_ERRORSTATUS_NOSUCHNAME;
1404 if (error == SNMP_ERRORSTATUS_NOSUCHNAME &&
1405 bPduType == SNMP_PDU_GETNEXT)
1407 /* GetNext is special: it finds the successor to the given OID,
1408 * so we have to continue until an implementation handles the
1409 * query or we exhaust the table of supported OIDs.
1411 for (matchingIndex++; error == SNMP_ERRORSTATUS_NOSUCHNAME &&
1412 matchingIndex < DEFINE_SIZEOF(supportedIDs);
1413 matchingIndex++)
1415 error = SNMP_ERRORSTATUS_NOERROR;
1416 impl = &supportedIDs[matchingIndex];
1417 if (impl->query)
1418 ret = impl->query(bPduType, &pVarBindList->list[i],
1419 &error);
1420 else
1421 error = SNMP_ERRORSTATUS_NOSUCHNAME;
1423 /* If the query still isn't resolved, set the OID to the
1424 * successor to the last entry in the table.
1426 if (error == SNMP_ERRORSTATUS_NOSUCHNAME)
1428 SnmpUtilOidFree(&pVarBindList->list[i].name);
1429 ret = SnmpUtilOidCpy(&pVarBindList->list[i].name,
1430 &supportedIDs[matchingIndex - 1].name);
1431 pVarBindList->list[i].name.ids[
1432 pVarBindList->list[i].name.idLength - 1] += 1;
1435 if (error)
1436 errorIndex = i + 1;
1439 *pErrorStatus = error;
1440 *pErrorIndex = errorIndex;
1441 return ret;
1444 /*****************************************************************************
1445 * DllMain [INETMIB1.@]
1447 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1449 TRACE("(0x%p, %ld, %p)\n", hinstDLL, fdwReason, lpvReserved);
1451 switch (fdwReason)
1453 case DLL_PROCESS_ATTACH:
1454 DisableThreadLibraryCalls(hinstDLL);
1455 break;
1456 case DLL_PROCESS_DETACH:
1457 if (lpvReserved) break;
1458 cleanup();
1459 break;
1462 return TRUE;