Browse code

Refactor get_interface_metric to return metric and auto flag separately

- Instead of returning metric = 0 when automatic metric is in use
return the actual metric and flag automatic metric through a
parameter. This makes the function reusable elsewhere.

- Ensure return value can be correctly cast to int and return -1 on
error.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
Acked-by: Simon Rozman <simon@rozman.si>
Message-Id: <1512534521-14760-1-git-send-email-selva.nair@gmail.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16039.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Selva Nair authored on 2017/12/06 13:28:41
Showing 4 changed files
... ...
@@ -344,33 +344,43 @@ delete_block_dns_filters(HANDLE engine_handle)
344 344
 }
345 345
 
346 346
 /*
347
- * Returns interface metric value for specified interface index.
347
+ * Return interface metric value for the specified interface index.
348 348
  *
349 349
  * Arguments:
350 350
  *   index         : The index of TAP adapter.
351 351
  *   family        : Address family (AF_INET for IPv4 and AF_INET6 for IPv6).
352
- * Returns positive metric value or zero for automatic metric on success,
353
- * a less then zero error code on failure.
352
+ *   is_auto       : On return set to true if automatic metric is in use.
353
+ *                   Unused if NULL.
354
+ *
355
+ * Returns positive metric value or -1 on error.
354 356
  */
355
-
356 357
 int
357
-get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family)
358
+get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto)
358 359
 {
359 360
     DWORD err = 0;
360 361
     MIB_IPINTERFACE_ROW ipiface;
361 362
     InitializeIpInterfaceEntry(&ipiface);
362 363
     ipiface.Family = family;
363 364
     ipiface.InterfaceIndex = index;
365
+
366
+    if (is_auto)
367
+    {
368
+        *is_auto = 0;
369
+    }
364 370
     err = GetIpInterfaceEntry(&ipiface);
365
-    if (err == NO_ERROR)
371
+
372
+    /* On Windows metric is never > INT_MAX so return value of int is ok.
373
+     * But we check for overflow nevertheless.
374
+     */
375
+    if (err == NO_ERROR && ipiface.Metric <= INT_MAX)
366 376
     {
367
-        if (ipiface.UseAutomaticMetric)
377
+        if (is_auto)
368 378
         {
369
-            return 0;
379
+            *is_auto = ipiface.UseAutomaticMetric;
370 380
         }
371
-        return ipiface.Metric;
381
+        return (int)ipiface.Metric;
372 382
     }
373
-    return -err;
383
+    return -1;
374 384
 }
375 385
 
376 386
 /*
... ...
@@ -39,17 +39,17 @@ add_block_dns_filters(HANDLE *engine, int iface_index, const WCHAR *exe_path,
39 39
                       block_dns_msg_handler_t msg_handler_callback);
40 40
 
41 41
 /**
42
- * Returns interface metric value for specified interface index.
42
+ * Return interface metric value for the specified interface index.
43 43
  *
44
- * @param index The index of TAP adapter
45
- * @param family Address family (AF_INET for IPv4 and AF_INET6 for IPv6)
44
+ * @param index         The index of TAP adapter.
45
+ * @param family        Address family (AF_INET for IPv4 and AF_INET6 for IPv6).
46
+ * @param is_auto       On return set to true if automatic metric is in use.
47
+ *                      Unused if NULL.
46 48
  *
47
- * @return positive metric value or zero for automatic metric on success,
48
- * a less then zero error code on failure.
49
+ * @return positive interface metric on success or -1 on error
49 50
  */
50
-
51 51
 int
52
-get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family);
52
+get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto);
53 53
 
54 54
 /**
55 55
  * Sets interface metric value for specified interface index.
... ...
@@ -1344,17 +1344,16 @@ win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel)
1344 1344
                                    block_dns_msg_handler);
1345 1345
     if (status == 0)
1346 1346
     {
1347
-        tap_metric_v4 = get_interface_metric(index, AF_INET);
1348
-        tap_metric_v6 = get_interface_metric(index, AF_INET6);
1349
-        if (tap_metric_v4 < 0)
1347
+        int is_auto = 0;
1348
+        tap_metric_v4 = get_interface_metric(index, AF_INET, &is_auto);
1349
+        if (is_auto)
1350 1350
         {
1351
-            /* error, should not restore metric */
1352
-            tap_metric_v4 = -1;
1351
+            tap_metric_v4 = 0;
1353 1352
         }
1354
-        if (tap_metric_v6 < 0)
1353
+        tap_metric_v6 = get_interface_metric(index, AF_INET6, &is_auto);
1354
+        if (is_auto)
1355 1355
         {
1356
-            /* error, should not restore metric */
1357
-            tap_metric_v6 = -1;
1356
+            tap_metric_v6 = 0;
1358 1357
         }
1359 1358
         status = set_interface_metric(index, AF_INET, BLOCK_DNS_IFACE_METRIC);
1360 1359
         if (!status)
... ...
@@ -804,17 +804,18 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists)
804 804
             }
805 805
             interface_data->engine = engine;
806 806
             interface_data->index = msg->iface.index;
807
+            int is_auto = 0;
807 808
             interface_data->metric_v4 = get_interface_metric(msg->iface.index,
808
-                                                             AF_INET);
809
-            if (interface_data->metric_v4 < 0)
809
+                                                             AF_INET, &is_auto);
810
+            if (is_auto)
810 811
             {
811
-                interface_data->metric_v4 = -1;
812
+                interface_data->metric_v4 = 0;
812 813
             }
813 814
             interface_data->metric_v6 = get_interface_metric(msg->iface.index,
814
-                                                             AF_INET6);
815
-            if (interface_data->metric_v6 < 0)
815
+                                                             AF_INET6, &is_auto);
816
+            if (is_auto)
816 817
             {
817
-                interface_data->metric_v6 = -1;
818
+                interface_data->metric_v6 = 0;
818 819
             }
819 820
             err = AddListItem(&(*lists)[block_dns], interface_data);
820 821
             if (!err)