[Dnsmasq-discuss] [PATCH] simplify bindtodevice()

Kurt H Maier khm at sciops.net
Thu Dec 21 04:01:10 GMT 2017


Right now bindtodevice() declares an ifreq, copies the device name into
it, and passes a pointer to the entire structure as the optval for
setsockopt.  This only works because ifr_name happens to be the first
element in the ifreq data structure.  What actually gets passed to
setsockopt looks like 
  "wlp3s0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\255nzHmU\0\0"...
and is 40 bytes long.

We could change it so we pass ifr.ifr_name, but this is wasteful too,
since device is already in the format we want.

Attached is a patch that uses device directly, and passes IFNAMSIZ as
optlen.

Thanks,
khm


diff --git a/src/dhcp-common.c b/src/dhcp-common.c
index eae9ae3..8e128fa 100644
--- a/src/dhcp-common.c
+++ b/src/dhcp-common.c
@@ -485,11 +485,8 @@ char *whichdevice(void)
  
 void  bindtodevice(char *device, int fd)
 {
-  struct ifreq ifr;
-  
-  strcpy(ifr.ifr_name, device);
   /* only allowed by root. */
-  if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) == -1 &&
+  if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, device, IFNAMSIZ) == -1 &&
       errno != EPERM)
     die(_("failed to set SO_BINDTODEVICE on DHCP socket: %s"), NULL, EC_BADNET);
 }




More information about the Dnsmasq-discuss mailing list