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

Simon Kelley simon at thekelleys.org.uk
Fri Jan 26 15:22:54 GMT 2018


Patch applied. Many thanks.


Cheers,

Simon.



On 21/12/17 04:01, Kurt H Maier wrote:
> 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);
>  }
> 
> 
> _______________________________________________
> Dnsmasq-discuss mailing list
> Dnsmasq-discuss at lists.thekelleys.org.uk
> http://lists.thekelleys.org.uk/mailman/listinfo/dnsmasq-discuss
> 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.thekelleys.org.uk/pipermail/dnsmasq-discuss/attachments/20180126/91de0935/attachment.sig>


More information about the Dnsmasq-discuss mailing list