Fri, Jan 07, 2011 at 01:57:21PM +0100, joris dedieu wrote:
> What do you think about it ?
[...]
> +static int     bindany = 0; /* 1 allows to bind a non local ip */
> +SYSCTL_INT(_net_inet_ip, OID_AUTO, bindany, CTLFLAG_RW, &bindany, 0,
> +    "Allow to bind a non local ip");

On at least 8.x, you will likely want to use VNET_* macros to enable
your new sysctl to be virtualized.  Something like this:
{{{
VNET_DEFINE(int, inp_bindany) = 0;
SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, bindany, CTLFLAG_RW,
        &VNET_NAME(inp_bindany), 0, "Force INP_BINDANY on all sockets");
}}}
and use VNET(inp_bindany) in subsequent code.

>                         if ((inp->inp_flags & INP_BINDANY) == 0 &&
> -                           ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
> -                               return (EADDRNOTAVAIL);
> +                           ifa_ifwithaddr_check((struct sockaddr *)sin) == 
> 0) {
> +                               if(bindany > 0)
> +                                       inp->inp_flags |= INP_BINDANY;
> +                               else
> +                                       return (EADDRNOTAVAIL);
> +                       }

The check is better to be rewritten as
{{{
                        if (VNET(inp_bindany) > 0)
                                inp->inp_flags |= INP_BINDANY;
                        else if ((inp->inp_flags & INP_BINDANY) == 0 &&
                            ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
                                return (EADDRNOTAVAIL);
}}}
it will save you two conditionals if bindany is enabled.  On the other
hand, if you will set the value of VNET(inp_bindany) to INP_BINDANY
instead of 1, you can even do
{{{
                        inp->inp_flags |= VNET(inp_bindany);
                        if ((inp->inp_flags & INP_BINDANY) == 0 &&
                            ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
                                return (EADDRNOTAVAIL);
}}}
and this will eliminate one branching instruction at the expense of
memory access and fast logical operation.
-- 
Eygene Ryabinkin                                        ,,,^..^,,,
[ Life's unfair - but root password helps!           | codelabs.ru ]
[ 82FE 06BC D497 C0DE 49EC  4FF0 16AF 9EAE 8152 ECFB | freebsd.org ]

Attachment: pgpbHhA7Pc69T.pgp
Description: PGP signature

Reply via email to