]> git.meshlink.io Git - catta/blob - src/iface-windows.c
eb6c93cecc536a91c6a43824b837bf621377fd3e
[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 // for the luid-to-idx hashmap
32 static unsigned luid_hash(const void *data)
33 {
34     return ((NET_LUID *)data)->Info.NetLuidIndex;
35 }
36 static int luid_equal(const void *a, const void *b)
37 {
38     return (((NET_LUID *)a)->Value == ((NET_LUID *)b)->Value);
39 }
40
41 static CattaIfIndex find_ifindex(CattaInterfaceMonitor *m, NET_LUID luid)
42 {
43     CattaIfIndex *pi = NULL;
44     NET_LUID *key = NULL;
45
46     if((pi = catta_hashmap_lookup(m->osdep.idxmap, &luid)) == NULL) {
47         // allocate memory for the hashmap key and value
48         key = catta_malloc(sizeof(luid));
49         pi = catta_malloc(sizeof(CattaIfIndex));
50         if(!key || !pi)
51             goto fail;
52
53         *key = luid;
54             
55         // find an index for this luid
56         *pi = m->osdep.nidx;
57         if(*pi < 0)  // overflow
58             goto fail;
59
60         // register the index
61         if(catta_hashmap_replace(m->osdep.idxmap, key, pi) < 0)
62             goto fail;
63         m->osdep.nidx++;
64     }
65
66     return *pi;
67
68 fail:
69     catta_free(key);
70     catta_free(pi);
71     return -1;
72 }
73
74 // integrate the information from an IP_ADAPTER_ADDRESSES structure
75 // as returned by GetAdaptersAddresses into the CattaInterfaceMonitor
76 static void ip_adapter_address(CattaInterfaceMonitor *m, IP_ADAPTER_ADDRESSES *p)
77 {
78     CattaIfIndex idx;
79     CattaHwInterface *hw;
80     size_t n;
81
82     // look up the interface index by LUID
83     if((idx = find_ifindex(m, p->Luid)) < 0) {
84         catta_log_error("could not allocate index ip_adapter_address");
85         return;
86     }
87
88     // find the CattaHwInterface by index or allocate a new one
89     if((hw = catta_interface_monitor_get_hw_interface(m, idx)) == NULL) {
90         if((hw = catta_hw_interface_new(m, idx)) == NULL) {
91             catta_log_error("catta_hw_interface_new failed in ip_adapter_address");
92             return;
93         }
94     }
95
96     // fill the CattaHwInterface struct with data
97     hw->flags_ok =
98         (p->OperStatus == IfOperStatusUp) &&
99         !(p->IfType == IF_TYPE_SOFTWARE_LOOPBACK) &&
100         !(p->Flags & IP_ADAPTER_NO_MULTICAST) &&
101         (m->server->config.allow_point_to_point || !(p->IfType == IF_TYPE_PPP));
102             // XXX what about IF_TYPE_TUNNEL?
103
104     n = wcstombs(NULL, p->FriendlyName, 0) + 1;
105     catta_free(hw->name);
106     hw->name = catta_new(char, n);
107     wcstombs(hw->name, p->FriendlyName, n);
108
109     hw->mtu = p->Mtu;
110
111     hw->mac_address_size = p->PhysicalAddressLength;
112     if(hw->mac_address_size > CATTA_MAC_ADDRESS_MAX)
113         hw->mac_address_size = CATTA_MAC_ADDRESS_MAX;
114     memcpy(hw->mac_address, p->PhysicalAddress, hw->mac_address_size);
115
116     // XXX process addresses
117
118     // XXX debugging, remove
119     {
120         char mac[256];
121         catta_log_debug(" name: %s\n"
122                         " index: %d\n"
123                         " mtu: %d\n"
124                         " mac: %s\n"
125                         " flags_ok: %d\n"
126                         "   type: %u\n"
127                         "   status: %u\n"
128                         "   multicast: %d\n"
129                         "   flags: 0x%.4x\n"
130                         "======",
131             hw->name, hw->index,
132             hw->mtu,
133             catta_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size),
134             hw->flags_ok,
135             (unsigned int)p->IfType,
136             (unsigned int)p->OperStatus,
137             !(p->Flags & IP_ADAPTER_NO_MULTICAST),
138             (unsigned int)p->Flags);
139     }
140 }
141
142
143 int catta_interface_monitor_init_osdep(CattaInterfaceMonitor *m)
144 {
145     m->osdep.nidx = 0;
146     m->osdep.idxmap = catta_hashmap_new(luid_hash, luid_equal, catta_free, catta_free);
147     if(m->osdep.idxmap == NULL) {
148         catta_log_error("out of memory in catta_interface_monitor_init_osdep");
149         return -1;
150     }
151
152     // XXX register callbacks to get notified of interface/address changes
153
154     return 0;
155 }
156
157 void catta_interface_monitor_free_osdep(CattaInterfaceMonitor *m)
158 {
159     catta_hashmap_free(m->osdep.idxmap);
160 }
161
162 void catta_interface_monitor_sync(CattaInterfaceMonitor *m)
163 {
164     IP_ADAPTER_ADDRESSES *buf = NULL;
165     IP_ADAPTER_ADDRESSES *p;
166     ULONG bufsize = 15000;
167     ULONG r;
168
169     // allocate a buffer and call GetAdaptersAddresses
170     // retry with the correct size if the buffer was too small
171     do {
172         catta_free(buf);    // no-op on first iteration
173         if((buf = catta_malloc(bufsize)) == NULL) {
174             catta_log_error("malloc failed in catta_interface_monitor_sync");
175             return;
176         }
177
178         r = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, buf, &bufsize);
179     } while(r == ERROR_BUFFER_OVERFLOW);
180
181     if(r != NO_ERROR) {
182         catta_log_error("GetAdaptersAddresses failed: %u", (unsigned int)r);
183         return;
184     }
185
186     // XXX remove interfaces for adapters that are no longer in the list
187
188     // create 'CattaInterface's for every adapter
189     for(p=buf; p; p=p->Next)
190         ip_adapter_address(m, p);
191
192     catta_free(buf);
193
194     m->list_complete = 1;
195     catta_interface_monitor_check_relevant(m);
196     catta_interface_monitor_update_rrs(m, 0);
197     catta_log_info("Network interface enumeration completed.");
198 }