Debian Codegraph @

agent/mibgroup/if-mib/data_access/interface_iphlpapi.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-features.h>
3 #include <net-snmp/library/container.h>
4 #include <net-snmp/library/snmp.h>
5 #include <net-snmp/agent/snmp_agent.h>
6 #include <net-snmp/agent/snmp_vars.h>
7 #include "interface_private.h"
8 #include "net-snmp/data_access/interface.h"
9 #include <ws2tcpip.h>
10 #include <iphlpapi.h>
11
12 /* For Cygwin, MinGW32 and MSYS */
13 #ifndef NETIO_STATUS
14 #define NETIO_STATUS DWORD
15 #endif
16
17 #ifdef HAVE_MIB_IF_TABLE2
18 static void (*pFreeMibTable)(void *Table);
19 static NETIO_STATUS (*pGetIfEntry2)(MIB_IF_ROW2 *Row);
20 static NETIO_STATUS (*pGetIfTable2)(MIB_IF_TABLE2 **Table);
21 static NETIO_STATUS (*pSetIfEntry2)(MIB_IF_ROW2 *Row);
22 #endif
23
24 void
25 netsnmp_arch_interface_init(void)
26 {
27 #ifdef HAVE_MIB_IF_TABLE2
28 HANDLE dll_handle = LoadLibrary("iphlpapi.dll");
29
30 pFreeMibTable = (void(*)(void*))(uintptr_t)
31 GetProcAddress(dll_handle, "FreeMibTable");
32 pGetIfEntry2 = (NETIO_STATUS(*)(MIB_IF_ROW2*))(uintptr_t)
33 GetProcAddress(dll_handle, "GetIfEntry2");
34 pGetIfTable2 = (NETIO_STATUS(*)(MIB_IF_TABLE2**))(uintptr_t)
35 GetProcAddress(dll_handle, "GetIfTable2");
36 pSetIfEntry2 = (NETIO_STATUS(*)(MIB_IF_ROW2*))(uintptr_t)
37 GetProcAddress(dll_handle, "SetIfEntry2");
38 #endif
39 }
40
41 /* Load interface data using GetIfTable() (Windows 2000 and later). */
42 static int netsnmp_arch_interface_load_old(netsnmp_container *container)
43 {
44 DWORD status;
45 DWORD dwActualSize = 0;
46 MIB_IFTABLE *iftable = NULL;
47 MIB_IFROW *row, *end;
48
49 status = GetIfTable(iftable, &dwActualSize, /*bOrder=*/FALSE);
50 if (status == NO_ERROR)
51 return 0;
52 if (status != ERROR_INSUFFICIENT_BUFFER)
53 return SNMP_ERR_GENERR;
54 iftable = malloc(dwActualSize);
55 if (!iftable)
56 return SNMP_ERR_GENERR;
57 status = GetIfTable(iftable, &dwActualSize, /*bOrder=*/FALSE);
58 if (status != NO_ERROR)
59 return SNMP_ERR_GENERR;
60 end = iftable->table + iftable->dwNumEntries;
61 for (row = iftable->table; row < end; row++) {
62 netsnmp_interface_entry *entry;
63
64 entry = calloc(1, sizeof(*entry));
65 if (!entry)
66 continue;
67 entry->name = strdup((const char *) row->wszName);
68 entry->oid_index.len = 1;
69 entry->oid_index.oids = &entry->index;
70 entry->index = row->dwIndex;
71 entry->descr = netsnmp_memdup(row->bDescr, row->dwDescrLen);
72 entry->type = row->dwType;
73 entry->mtu = row->dwMtu;
74 entry->speed = row->dwSpeed;
75 entry->paddr_len = row->dwPhysAddrLen;
76 entry->paddr = netsnmp_memdup(row->bPhysAddr, row->dwPhysAddrLen);
77 entry->admin_status = row->dwAdminStatus;
78 entry->oper_status = row->dwOperStatus ==
79 MIB_IF_OPER_STATUS_OPERATIONAL ? 1/*up*/ : 2/*down*/;
80 entry->lastchange = 0; /* row->dwLastChange is not a Unix time */
81 entry->stats.ibytes.low = row->dwInOctets;
82 entry->stats.iucast.low = row->dwInUcastPkts;
83 entry->stats.inucast = row->dwInNUcastPkts;
84 entry->stats.ierrors = row->dwInErrors;
85 entry->stats.idiscards = row->dwInDiscards;
86 entry->stats.iunknown_protos = row->dwInUnknownProtos;
87 entry->stats.obytes.low = row->dwOutOctets;
88 entry->stats.oucast.low = row->dwOutUcastPkts;
89 entry->stats.oerrors = row->dwOutErrors;
90 entry->stats.oqlen = row->dwOutQLen;
91 entry->stats.onucast = row->dwOutNUcastPkts;
92 CONTAINER_INSERT(container, entry);
93 }
94
95 free(iftable);
96 return 0;
97 }
98
99 #ifdef HAVE_MIB_IF_TABLE2
100 /*
101 * Load interface data using GetIfTable2(). GetIfTable2() is available on
102 * Windows Vista, Windows Server 2008 and later.
103 */
104 static int netsnmp_arch_interface_load_new(netsnmp_container *container)
105 {
106 DWORD status;
107 MIB_IF_TABLE2 *iftable = NULL;
108 MIB_IF_ROW2 *row, *end;
109 char *descr;
110 unsigned descr_len;
111
112 status = pGetIfTable2(&iftable);
113 if (status != NO_ERROR)
114 return SNMP_ERR_GENERR;
115 end = iftable->Table + iftable->NumEntries;
116 for (row = iftable->Table; row < end; row++) {
117 netsnmp_interface_entry *entry;
118
119 entry = calloc(1, sizeof(*entry));
120 if (!entry)
121 continue;
122 entry->name = strdup((const char *) row->Alias);
123 entry->oid_index.len = 1;
124 entry->oid_index.oids = &entry->index;
125 entry->index = row->InterfaceIndex;
126 descr_len = WideCharToMultiByte(CP_UTF8, 0, row->Description, -1, NULL,
127 0, NULL, NULL);
128 descr = malloc(descr_len);
129 if (row && WideCharToMultiByte(CP_UTF8, 0, row->Description, -1, descr,
130 descr_len, NULL, NULL) > 0) {
131 entry->descr = descr;
132 } else {
133 free(row);
134 }
135 entry->type = row->Type;
136 entry->mtu = row->Mtu;
137 entry->speed = row->TransmitLinkSpeed;
138 entry->paddr_len = row->PhysicalAddressLength;
139 entry->paddr = netsnmp_memdup(row->PhysicalAddress,
140 row->PhysicalAddressLength);
141 entry->admin_status = row->AdminStatus;
142 entry->oper_status = row->OperStatus;
143 entry->lastchange = 0; /* row->dwLastChange is not a Unix time */
144 entry->stats.ibytes.low = row->InOctets;
145 entry->stats.iucast.low = row->InUcastPkts;
146 entry->stats.inucast = row->InNUcastPkts;
147 entry->stats.ierrors = row->InErrors;
148 entry->stats.idiscards = row->InDiscards;
149 entry->stats.iunknown_protos = row->InUnknownProtos;
150 entry->stats.obytes.low = row->OutOctets;
151 entry->stats.oucast.low = row->OutUcastPkts;
152 entry->stats.oerrors = row->OutErrors;
153 entry->stats.oqlen = row->OutQLen;
154 entry->stats.onucast = row->OutNUcastPkts;
155 CONTAINER_INSERT(container, entry);
156 }
157
158 pFreeMibTable(iftable);
159 return 0;
160 }
161 #else
162 static int netsnmp_arch_interface_load_new(netsnmp_container *container)
163 {
164 return SNMP_ERR_GENERR;
165 }
166 #endif
167
168 int
169 netsnmp_arch_interface_container_load(netsnmp_container *container,
170 u_int load_flags)
171 {
172 #ifdef HAVE_MIB_IF_TABLE2
173 return pGetIfTable2 ? netsnmp_arch_interface_load_new(container) :
174 netsnmp_arch_interface_load_old(container);
175 #else
176 return netsnmp_arch_interface_load_old(container);
177 #endif
178 }
179
180 oid
181 netsnmp_arch_interface_index_find(const char *name)
182 {
183 /* To do: implement this function. */
184 return 0;
185 }
186
187 int
188 netsnmp_arch_set_admin_status(struct netsnmp_interface_entry_s *entry,
189 int ifAdminStatus_val)
190 {
191 MIB_IFROW row;
192
193 memset(&row, 0, sizeof(row));
194 row.dwIndex = entry->index;
195 if (GetIfEntry(&row) != NO_ERROR) {
196 snmp_log(LOG_ERR, "GetIfEntry() failed for index %" NETSNMP_PRIo "u",
197 entry->index);
198 return SNMP_ERR_GENERR;
199 }
200 row.dwAdminStatus = ifAdminStatus_val;
201 if (SetIfEntry(&row) != NO_ERROR) {
202 snmp_log(LOG_ERR,
203 "SetIfEntry() failed for index %" NETSNMP_PRIo "u and admin status %u\n",
204 entry->index, ifAdminStatus_val);
205 return SNMP_ERR_GENERR;
206 }
207 return 0;
208 }