]> git.meshlink.io Git - catta/blob - src/iface-windows.c
5624f739fed2c2644fbb3aa7e91c302c5f505831
[catta] / src / iface-windows.c
1 /***
2   This file is part of catta.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
20 #include "iface-windows.h"
21 #include "iface.h"
22
23 #include <stdlib.h> // wcstombs
24 #include <catta/malloc.h>
25 #include <catta/log.h>
26 #include <iphlpapi.h>
27 #include "hashmap.h"
28 #include "util.h"   // catta_format_mac_address
29
30
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,
34                                        CattaHwInterface *hw,
35                                        IP_ADAPTER_UNICAST_ADDRESS *a)
36 {
37     CattaInterface *iface;
38     CattaAddress addr;
39     CattaInterfaceAddress *ifaddr;
40     struct sockaddr *sa = a->Address.lpSockaddr;
41
42     // fill addr struct for address lookup
43     switch(sa->sa_family) {
44     case AF_INET:
45         memcpy(addr.data.data, &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr));
46         break;
47     case AF_INET6:
48         memcpy(addr.data.data, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr));
49         break;
50     default:
51         catta_log_debug("unexpected address family on interface %d: %u", hw->index, sa->sa_family);
52         return;
53     }
54     addr.proto = catta_af_to_proto(sa->sa_family);
55
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);
59         return;
60     }
61
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");
66             return;
67         }
68     }
69
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));
74     else
75         ifaddr->global_scope = 1;
76
77     // XXX debugging, remove
78     {
79         char s[CATTA_ADDRESS_STR_MAX];
80         catta_log_debug(" address: %s\n"
81                         "   global_scope: %d",
82             catta_address_snprint(s, sizeof(s), &addr),
83             ifaddr->global_scope);
84     }
85 }
86
87 // integrate the information from an IP_ADAPTER_ADDRESSES structure
88 // as returned by GetAdaptersAddresses into the CattaInterfaceMonitor
89 static void ip_adapter(CattaInterfaceMonitor *m, IP_ADAPTER_ADDRESSES *p)
90 {
91     IP_ADAPTER_UNICAST_ADDRESS *a;
92     CattaIfIndex idx;
93     CattaHwInterface *hw;
94     size_t n;
95
96     // we want an index specific to the hardware interface, but Windows
97     // has one for IPv4 and one for IPv6. it seems like these are always the
98     // same unless one of the protocols is not available. let's have a bunch of
99     // checks...
100     if(!p->IfIndex && !p->Ipv6IfIndex) {
101         return; // no usable protocols
102     } else if(!p->IfIndex) {
103         idx = p->Ipv6IfIndex;   // IPv6 but no IPv4 (huh!)
104     } else if(!p->Ipv6IfIndex) {
105         idx = p->IfIndex;       // IPv4 but no IPv6
106     } else if(p->IfIndex == p->Ipv6IfIndex) {
107         idx = p->IfIndex;       // same index for both protocols
108     } else {
109         // both indexes valid but not equal
110         catta_log_error("unsupported interface: %ls (IfIndex and Ipv6IfIndex differ: %u/%u)",
111             p->FriendlyName, (unsigned int)p->IfIndex, (unsigned int)p->Ipv6IfIndex);
112         return;
113     }
114
115     // find the CattaHwInterface by index or allocate a new one
116     if((hw = catta_interface_monitor_get_hw_interface(m, idx)) == NULL) {
117         if((hw = catta_hw_interface_new(m, idx)) == NULL) {
118             catta_log_error("catta_hw_interface_new failed in ip_adapter_address");
119             return;
120         }
121     }
122
123     // fill the CattaHwInterface struct with data
124     hw->flags_ok =
125         (p->OperStatus == IfOperStatusUp) &&
126         !(p->IfType == IF_TYPE_SOFTWARE_LOOPBACK) &&
127         !(p->Flags & IP_ADAPTER_NO_MULTICAST) &&
128         (m->server->config.allow_point_to_point || !(p->IfType == IF_TYPE_PPP));
129             // XXX what about IF_TYPE_TUNNEL?
130
131     n = wcstombs(NULL, p->FriendlyName, 0) + 1;
132     catta_free(hw->name);
133     hw->name = catta_new(char, n);
134     wcstombs(hw->name, p->FriendlyName, n);
135
136     hw->mtu = p->Mtu;
137
138     hw->mac_address_size = p->PhysicalAddressLength;
139     if(hw->mac_address_size > CATTA_MAC_ADDRESS_MAX)
140         hw->mac_address_size = CATTA_MAC_ADDRESS_MAX;
141     memcpy(hw->mac_address, p->PhysicalAddress, hw->mac_address_size);
142
143     // XXX debugging, remove
144     {
145         char mac[256];
146         catta_log_debug(" name: %s\n"
147                         " index: %d\n"
148                         "   IfIndex: %u\n"
149                         "   Ipv6IfIndex: %u\n"
150                         " mtu: %d\n"
151                         " mac: %s\n"
152                         " flags_ok: %d\n"
153                         "   type: %u\n"
154                         "   status: %u\n"
155                         "   multicast: %d\n"
156                         "   flags: 0x%.4x",
157             hw->name, hw->index,
158             (unsigned int)p->IfIndex, (unsigned int)p->Ipv6IfIndex,
159             hw->mtu,
160             catta_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size),
161             hw->flags_ok,
162             (unsigned int)p->IfType,
163             (unsigned int)p->OperStatus,
164             !(p->Flags & IP_ADAPTER_NO_MULTICAST),
165             (unsigned int)p->Flags);
166     }
167
168     // process addresses
169     // XXX remove addresses that are no longer in the list
170     for(a=p->FirstUnicastAddress; a; a=a->Next)
171         ip_adapter_unicast_address(m, hw, a);
172     catta_log_debug("=====");
173 }
174
175
176 int catta_interface_monitor_init_osdep(CattaInterfaceMonitor *m)
177 {
178     (void)*m;   // silence "unused paramter" warning
179
180     // XXX register callbacks to get notified of interface/address changes
181
182     return 0;
183 }
184
185 void catta_interface_monitor_free_osdep(CattaInterfaceMonitor *m)
186 {
187     (void)*m;   // silence "unused paramter" warning
188 }
189
190 void catta_interface_monitor_sync(CattaInterfaceMonitor *m)
191 {
192     IP_ADAPTER_ADDRESSES *buf = NULL;
193     IP_ADAPTER_ADDRESSES *p;
194     ULONG bufsize = 15000;
195     ULONG r;
196
197     // allocate a buffer and call GetAdaptersAddresses
198     // retry with the correct size if the buffer was too small
199     do {
200         catta_free(buf);    // no-op on first iteration
201         if((buf = catta_malloc(bufsize)) == NULL) {
202             catta_log_error("malloc failed in catta_interface_monitor_sync");
203             return;
204         }
205
206         r = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, buf, &bufsize);
207     } while(r == ERROR_BUFFER_OVERFLOW);
208
209     if(r != NO_ERROR) {
210         catta_log_error("GetAdaptersAddresses failed: %u", (unsigned int)r);
211         return;
212     }
213
214     // XXX remove interfaces for adapters that are no longer in the list
215
216     // create 'CattaInterface's for every adapter
217     for(p=buf; p; p=p->Next)
218         ip_adapter(m, p);
219
220     catta_free(buf);
221
222     m->list_complete = 1;
223     catta_interface_monitor_check_relevant(m);
224     catta_interface_monitor_update_rrs(m, 0);
225     catta_log_info("Network interface enumeration completed.");
226 }