@@ -59,18 +59,14 @@ static void CALLBACK WireGuardLogger(_In_ WIREGUARD_LOGGER_LEVEL Level,
5959/* *
6060 * @brief Assigns an ipv4 address to a network device with a given LUID
6161 *
62- * @param luid - LUID of the Adapter
62+ * @param ifindex - Interface Index of the Adapter
6363 * @param address - Address and NetMask of the Adapter
6464 * @return ulong - nteContext - Call DeleteIPAddress(nteContext) to remove the
6565 * assignment.
6666 */
67- ulong setIPv4AddressAndMask (NET_LUID luid , const IPAddress address) {
67+ ulong setIPv4AddressAndMask (NET_IFINDEX ifindex , const IPAddress address) {
6868 ULONG nteContext = 0 ;
6969 ULONG nteInstance = 0 ;
70- NET_IFINDEX ifIndex;
71- if (ConvertInterfaceLuidToIndex (&luid, &ifIndex) != NO_ERROR) {
72- return 0 ;
73- }
7470 IN_ADDR ipAddrBinary, subnetMaskBinary;
7571 if (InetPtonA (AF_INET, qPrintable (address.address ().toString ()),
7672 &ipAddrBinary) != 1 ) {
@@ -83,7 +79,7 @@ ulong setIPv4AddressAndMask(NET_LUID luid, const IPAddress address) {
8379 // Add IP address and subnet mask
8480 DWORD dwResult =
8581 AddIPAddress (ipAddrBinary.S_un .S_addr , subnetMaskBinary.S_un .S_addr ,
86- ifIndex , &nteContext, &nteInstance);
82+ ifindex , &nteContext, &nteInstance);
8783 if (dwResult != NO_ERROR) {
8884 WindowsUtils::windowsLog (" WELP, failed to add ip address to adapter" );
8985 return 0 ;
@@ -93,19 +89,12 @@ ulong setIPv4AddressAndMask(NET_LUID luid, const IPAddress address) {
9389/* *
9490 * @brief
9591 *
96- * @param luid - LUID of the Adapter
92+ * @param ifindex - Index of the Adapter
9793 * @param ipAddress - Address and NetMask of the Adapter
9894 * @return bool - If the assignment was successful
9995 */
100- bool setIPv6AddressAndMask (NET_LUID luid , const IPAddress ipAddress) {
96+ bool setIPv6AddressAndMask (NET_IFINDEX ifindex , const IPAddress ipAddress) {
10197 MIB_UNICASTIPADDRESS_ROW row;
102-
103- NET_IFINDEX ifIndex;
104- if (ConvertInterfaceLuidToIndex (&luid, &ifIndex) != NO_ERROR) {
105- logger.error ()
106- << " Failed to assign ivp6: Cannot Find Interface for this LUID" ;
107- return false ;
108- }
10998 SOCKADDR_IN6 sockaddr = {};
11099 sockaddr.sin6_family = AF_INET6;
111100 if (InetPtonA (AF_INET6, qPrintable (ipAddress.address ().toString ()),
@@ -116,9 +105,9 @@ bool setIPv6AddressAndMask(NET_LUID luid, const IPAddress ipAddress) {
116105 }
117106
118107 InitializeUnicastIpAddressEntry (&row);
119- row.Address .Ipv6 . sin6_family = AF_INET6;
108+ row.Address .si_family = AF_INET6;
120109 row.Address .Ipv6 = sockaddr;
121- row.InterfaceLuid = luid ;
110+ row.InterfaceIndex = ifindex ;
122111
123112 // Calculate prefix length from subnet mask
124113 unsigned int prefixLength = ipAddress.prefixLength ();
@@ -320,21 +309,25 @@ bool WireguardUtilsWindows::addInterface(const InterfaceConfig& config) {
320309 auto destroyRouteMonitorOnFailure =
321310 qScopeGuard ([this ]() { m_routeMonitor->deleteLater (); });
322311
312+ NET_IFINDEX ifindex;
313+ const auto res = ConvertInterfaceLuidToIndex (&luid, &ifindex);
314+ if (res != NO_ERROR) {
315+ logger.error () << " ConvertInterfaceLuidToIndex failed with error:" << res;
316+ return false ;
317+ }
323318 // Set the Adapters Address:
324319 m_deviceIpv4_Handle =
325- setIPv4AddressAndMask (luid , IPAddress (config.m_deviceIpv4Address ));
320+ setIPv4AddressAndMask (ifindex , IPAddress (config.m_deviceIpv4Address ));
326321 if (m_deviceIpv4_Handle == 0 ) {
327322 logger.error () << " Failed setIPv4AddressAndMask" ;
328323 return false ;
329324 }
330- if (!setIPv6AddressAndMask (luid , IPAddress (config.m_deviceIpv6Address ))) {
325+ if (!setIPv6AddressAndMask (ifindex , IPAddress (config.m_deviceIpv6Address ))) {
331326 logger.error () << " Failed setIPv6AddressAndMask" ;
332327 return false ;
333328 };
334329
335330 // Enable the windows firewall
336- NET_IFINDEX ifindex;
337- ConvertInterfaceLuidToIndex (&luid, &ifindex);
338331 if (!m_firewall->enableInterface (ifindex)) {
339332 logger.error () << " Failed enabling Killswitch" ;
340333 m_firewall->disableKillSwitch ();
0 commit comments