Browse code

Remove count_netmask_bits(), convert users to use netmask_to_netbits2()

The previous Linux/iproute2 code converted binary netmasks to string
representation (print_in_addr_t()), just to immediately scanf() it back
to binary to count bits. netmask_to_netbits2() directly works on the
in_addr_t.

Signed-off-by: Gert Doering <gert@greenie.muc.de>
Acked-by: Steffan Karger <steffan.karger@fox-it.com>
Message-Id: <1419713983-16272-1-git-send-email-gert@greenie.muc.de>
URL: http://article.gmane.org/gmane.network.openvpn.devel/9360

Gert Doering authored on 2014/12/28 05:59:43
Showing 5 changed files
... ...
@@ -827,32 +827,6 @@ setenv_str_i (struct env_set *es, const char *name, const char *value, const int
827 827
   gc_free (&gc);
828 828
 }
829 829
 
830
-/*
831
- * taken from busybox networking/ifupdown.c
832
- */
833
-unsigned int
834
-count_bits(unsigned int a)
835
-{
836
-  unsigned int result;
837
-  result = (a & 0x55) + ((a >> 1) & 0x55);
838
-  result = (result & 0x33) + ((result >> 2) & 0x33);
839
-  return((result & 0x0F) + ((result >> 4) & 0x0F));
840
-}
841
-
842
-int
843
-count_netmask_bits(const char *dotted_quad)
844
-{
845
-  unsigned int result, a, b, c, d;
846
-  /* Found a netmask...  Check if it is dotted quad */
847
-  if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
848
-    return -1;
849
-  result = count_bits(a);
850
-  result += count_bits(b);
851
-  result += count_bits(c);
852
-  result += count_bits(d);
853
-  return ((int)result);
854
-}
855
-
856 830
 /* return true if filename can be opened for read */
857 831
 bool
858 832
 test_file (const char *filename)
... ...
@@ -166,10 +166,6 @@ const char **make_env_array (const struct env_set *es,
166 166
 const char **make_arg_array (const char *first, const char *parms, struct gc_arena *gc);
167 167
 const char **make_extended_arg_array (char **p, struct gc_arena *gc);
168 168
 
169
-/* convert netmasks for iproute2 */
170
-int count_netmask_bits(const char *);
171
-unsigned int count_bits(unsigned int );
172
-
173 169
 /* an analogue to the random() function, but use OpenSSL functions if available */
174 170
 #ifdef ENABLE_CRYPTO
175 171
 long int get_random(void);
... ...
@@ -1288,7 +1288,7 @@ add_route (struct route_ipv4 *r,
1288 1288
   argv_printf (&argv, "%s route add %s/%d",
1289 1289
   	      iproute_path,
1290 1290
 	      network,
1291
-             count_netmask_bits(netmask));
1291
+              netmask_to_netbits2(r->netmask));
1292 1292
 
1293 1293
   if (r->flags & RT_METRIC_DEFINED)
1294 1294
     argv_printf_cat (&argv, "metric %d", r->metric);
... ...
@@ -1765,7 +1765,7 @@ delete_route (struct route_ipv4 *r,
1765 1765
   argv_printf (&argv, "%s route del %s/%d",
1766 1766
   	      iproute_path,
1767 1767
 	      network,
1768
-	      count_netmask_bits(netmask));
1768
+              netmask_to_netbits2(r->netmask));
1769 1769
 #else
1770 1770
   argv_printf (&argv, "%s del -net %s netmask %s",
1771 1771
 	       ROUTE_PATH,
... ...
@@ -305,6 +305,7 @@ static inline bool test_routes (const struct route_list *rl, const struct tuntap
305 305
 #endif
306 306
 
307 307
 bool netmask_to_netbits (const in_addr_t network, const in_addr_t netmask, int *netbits);
308
+int netmask_to_netbits2 (in_addr_t netmask);
308 309
 
309 310
 static inline in_addr_t
310 311
 netbits_to_netmask (const int netbits)
... ...
@@ -722,7 +722,7 @@ do_ifconfig (struct tuntap *tt,
722 722
 				  iproute_path,
723 723
 				  actual,
724 724
 				  ifconfig_local,
725
-				  count_netmask_bits(ifconfig_remote_netmask),
725
+				  netmask_to_netbits2(tt->remote_netmask),
726 726
 				  ifconfig_broadcast
727 727
 				  );
728 728
 		  argv_msg (M_INFO, &argv);
... ...
@@ -1819,7 +1819,7 @@ close_tun (struct tuntap *tt)
1819 1819
 			iproute_path,
1820 1820
 			tt->actual_name,
1821 1821
 			print_in_addr_t (tt->local, 0, &gc),
1822
-			count_netmask_bits(print_in_addr_t (tt->remote_netmask, 0, &gc))
1822
+		        netmask_to_netbits2(tt->remote_netmask)
1823 1823
 			);
1824 1824
 	      }
1825 1825
 #else