2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Functions for accessing NT system configuration information. */
12 #include <afs/param.h>
25 static int GetInterfaceList(HKEY skey, char **list);
26 static char *GetNextInterface(char *iflist);
27 static int GetIP(HKEY skey, char *ifname, int *addr, int *mask);
32 * Retrieve IP addresses, subnet masks, MTU sizes, and flags for all
33 * configured interfaces.
37 * count - in is max size of arrays. out is number of elements used.
40 * addrs - array of configured IP addresses, in host order.
41 * masks - array of subnet masks, in host order.
42 * mtus - array of MTU sizes.
43 * flags - array of flags.
46 * Total number of configured interfaces (>= count) or -1 on error.
49 int syscfg_GetIFInfo(int *count, int *addrs, int *masks, int *mtus, int *flags)
51 int maxCount = *count;
52 char *IFListBase = NULL;
53 char *IFList, *ifname;
57 if (RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_IPSRV_KEY,
58 KEY_READ, 0, &skey, NULL))
61 if ((nConfig = GetInterfaceList(skey, &IFListBase)) < 0) {
62 (void) RegCloseKey(skey);
69 while ((n < maxCount) && (ifname = GetNextInterface(IFList))) {
70 if (GetIP(skey, ifname, &addrs[n], &masks[n]) == 0) {
76 /* And until we get mtu's and flags */
77 for (i = 0; i < n; i++) {
82 (void) RegCloseKey(skey);
92 * Get interface list; list is represented as a multistring.
93 * Returns number of elements in interface list or -1 on error.
95 static int GetInterfaceList(HKEY skey, char **list)
103 if (RegOpenKeyAlt(skey, AFSREG_IPSRV_IFACELIST_SUBKEY,
104 KEY_READ, 0, &key, NULL))
107 status = RegQueryValueAlt(key, AFSREG_IPSRV_IFACELIST_BIND_VALUE,
108 &valType, &str, NULL);
109 (void) RegCloseKey(key);
110 if (status || (valType != REG_MULTI_SZ))
113 /* Count strings in multistring. */
120 if (str[i] == '\0') {
121 /* hit end of string */
124 if (str[i] == '\0') {
125 /* hit end of multistring */
138 * Parse interface list. In first call to GetNextInterface(), iflist is
139 * the list returned by GetInterfaceList(); in successive calls, iflist is
140 * the pointer returned by the previous call to GetNextInterface().
142 * Returns pointer to next adapter name, or NULL if done.
145 static char *GetNextInterface(char *iflist)
149 /* interface substrings are assumed to be of form \Device\<adapter name> */
150 ifname = strrchr(iflist, '\\');
153 /* subsequent (not first) call; advance to next interface substring */
154 iflist += strlen(iflist) + 1;
155 /* iflist now points to next interface or end-of-multistring char */
156 ifname = strrchr(iflist, '\\');
160 /* advance to first character of adapter name */
170 * Get IP address associated with interface (adapter name).
171 * Returns 0 on success and -1 on error.
174 static int GetIP(HKEY skey, char *ifname, int *addr, int *mask)
184 len = strlen(ifname) + 1 + sizeof(AFSREG_IPSRV_ADAPTER_PARAM_SUBKEY);
189 sprintf(s, "%s\\%s", ifname, AFSREG_IPSRV_ADAPTER_PARAM_SUBKEY);
191 status = RegOpenKeyAlt(skey, s, KEY_READ, 0, &key, NULL);
197 status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_ADDR_VALUE,
198 &valType, &ipStr, NULL);
199 if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) {
200 if (ipStr) free(ipStr);
201 (void) RegCloseKey(key);
206 status = RegQueryValueAlt(key, AFSREG_IPSRV_ADAPTER_PARAM_MASK_VALUE,
207 &valType, &snMask, NULL);
208 if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) {
209 if (snMask) free(snMask);
213 /* adapter configured via DHCP; address/mask in alternate values */
217 status = RegQueryValueAlt(key,
218 AFSREG_IPSRV_ADAPTER_PARAM_DHCPADDR_VALUE,
219 &valType, &ipStr, NULL);
221 if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) {
222 if (ipStr) free(ipStr);
223 (void) RegCloseKey(key);
227 status = RegQueryValueAlt(key,
228 AFSREG_IPSRV_ADAPTER_PARAM_DHCPMASK_VALUE,
229 &valType, &snMask, NULL);
231 if (status || (valType != REG_SZ && valType != REG_MULTI_SZ)) {
232 if (snMask) free(snMask);
237 /* convert ip and subnet. */
238 *addr = (int)inet_addr(ipStr);
239 *addr = ntohl(*addr);
243 *mask = (int)inet_addr(snMask);
244 *mask = ntohl(*mask);
250 (void) RegCloseKey(key);