2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 #include "iface-windows.h"
23 #include <stdlib.h> // wcstombs
24 #include <catta/malloc.h>
25 #include <catta/log.h>
28 #include "util.h" // catta_format_mac_address
31 // integrate the information from an IP_ADAPTER_UNICAST_ADDRESS structure for
32 // given CattaHwInterface into the CattaInterfaceMonitor
33 static void ip_adapter_unicast_address(CattaInterfaceMonitor *m,
35 IP_ADAPTER_UNICAST_ADDRESS *a)
37 CattaInterface *iface;
39 CattaInterfaceAddress *ifaddr;
40 struct sockaddr *sa = a->Address.lpSockaddr;
42 // fill addr struct for address lookup
43 switch(sa->sa_family) {
45 memcpy(addr.data.data, &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr));
48 memcpy(addr.data.data, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr));
51 catta_log_debug("unexpected address family on interface %d: %u", hw->index, sa->sa_family);
54 addr.proto = catta_af_to_proto(sa->sa_family);
56 // get protocol-specific CattaInterface object
57 if(!(iface = catta_interface_monitor_get_interface(m, hw->index, addr.proto))) {
58 catta_log_error("CattaInterface (index %d, proto %d) not found", hw->index, addr.proto);
62 // find or allocate a CattaInterfaceAddress struct for this address
63 if(!(ifaddr = catta_interface_monitor_get_address(m, iface, &addr))) {
64 if(!(ifaddr = catta_interface_address_new(m, iface, &addr, a->OnLinkPrefixLength))) {
65 catta_log_error("out of memory in ip_adapter_unicast_address");
70 // set global scope flag
71 if(addr.proto == CATTA_PROTO_INET6)
72 ifaddr->global_scope = !(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr.data.data)
73 || IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr.data.data));
75 ifaddr->global_scope = 1;
77 // XXX debugging, remove
79 char s[CATTA_ADDRESS_STR_MAX];
80 catta_log_debug(" address: %s\n"
83 catta_address_snprint(s, sizeof(s), &addr),
85 (unsigned int)a->Flags);
89 // integrate the information from an IP_ADAPTER_ADDRESSES structure
90 // as returned by GetAdaptersAddresses into the CattaInterfaceMonitor
91 static void ip_adapter(CattaInterfaceMonitor *m, IP_ADAPTER_ADDRESSES *p)
93 IP_ADAPTER_UNICAST_ADDRESS *a;
98 // we want an index specific to the hardware interface, but Windows
99 // has one for IPv4 and one for IPv6. it seems like these are always the
100 // same unless one of the protocols is not available. let's have a bunch of
102 if(!p->IfIndex && !p->Ipv6IfIndex) {
103 return; // no usable protocols
104 } else if(!p->IfIndex) {
105 idx = p->Ipv6IfIndex; // IPv6 but no IPv4 (huh!)
106 } else if(!p->Ipv6IfIndex) {
107 idx = p->IfIndex; // IPv4 but no IPv6
108 } else if(p->IfIndex == p->Ipv6IfIndex) {
109 idx = p->IfIndex; // same index for both protocols
111 // both indexes valid but not equal
112 catta_log_error("unsupported interface: %ls (IfIndex and Ipv6IfIndex differ: %u/%u)",
113 p->FriendlyName, (unsigned int)p->IfIndex, (unsigned int)p->Ipv6IfIndex);
117 // find the CattaHwInterface by index or allocate a new one
118 if((hw = catta_interface_monitor_get_hw_interface(m, idx)) == NULL) {
119 if((hw = catta_hw_interface_new(m, idx)) == NULL) {
120 catta_log_error("catta_hw_interface_new failed in ip_adapter_address");
125 // fill the CattaHwInterface struct with data
127 (p->OperStatus == IfOperStatusUp) &&
128 !(p->IfType == IF_TYPE_SOFTWARE_LOOPBACK) &&
129 !(p->Flags & IP_ADAPTER_NO_MULTICAST) &&
130 (m->server->config.allow_point_to_point || !(p->IfType == IF_TYPE_PPP));
131 // XXX what about IF_TYPE_TUNNEL?
133 n = wcstombs(NULL, p->FriendlyName, 0) + 1;
134 catta_free(hw->name);
135 hw->name = catta_new(char, n);
136 wcstombs(hw->name, p->FriendlyName, n);
140 hw->mac_address_size = p->PhysicalAddressLength;
141 if(hw->mac_address_size > CATTA_MAC_ADDRESS_MAX)
142 hw->mac_address_size = CATTA_MAC_ADDRESS_MAX;
143 memcpy(hw->mac_address, p->PhysicalAddress, hw->mac_address_size);
145 // XXX debugging, remove
148 catta_log_debug(" name: %s\n"
160 (unsigned int)p->IfIndex, (unsigned int)p->Ipv6IfIndex,
162 catta_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size),
164 (unsigned int)p->IfType,
165 (unsigned int)p->OperStatus,
166 !(p->Flags & IP_ADAPTER_NO_MULTICAST),
167 (unsigned int)p->Flags);
171 // XXX remove addresses that are no longer in the list
172 for(a=p->FirstUnicastAddress; a; a=a->Next)
173 ip_adapter_unicast_address(m, hw, a);
174 catta_log_debug("=====");
178 int catta_interface_monitor_init_osdep(CattaInterfaceMonitor *m)
180 (void)*m; // silence "unused paramter" warning
182 // XXX register callbacks to get notified of interface/address changes
187 void catta_interface_monitor_free_osdep(CattaInterfaceMonitor *m)
189 (void)*m; // silence "unused paramter" warning
192 void catta_interface_monitor_sync(CattaInterfaceMonitor *m)
194 IP_ADAPTER_ADDRESSES *buf = NULL;
195 IP_ADAPTER_ADDRESSES *p;
196 ULONG bufsize = 15000;
199 // allocate a buffer and call GetAdaptersAddresses
200 // retry with the correct size if the buffer was too small
202 catta_free(buf); // no-op on first iteration
203 if((buf = catta_malloc(bufsize)) == NULL) {
204 catta_log_error("malloc failed in catta_interface_monitor_sync");
208 r = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, buf, &bufsize);
209 } while(r == ERROR_BUFFER_OVERFLOW);
212 catta_log_error("GetAdaptersAddresses failed: %u", (unsigned int)r);
216 // XXX remove interfaces for adapters that are no longer in the list
218 // create 'CattaInterface's for every adapter
219 for(p=buf; p; p=p->Next)
224 m->list_complete = 1;
225 catta_interface_monitor_check_relevant(m);
226 catta_interface_monitor_update_rrs(m, 0);
227 catta_log_info("Network interface enumeration completed.");