On Tue, 19 Dec 2017 11:01:55 +0100
Adrien Mazarguil <adrien.mazarg...@6wind.com> wrote:

> > Why not use sscanf which would be safer in this case.  
> 
> Right, this is indeed the obvious implementation, however not only the fixed
> MAC-48 format is not the most convenient to use for user input (somewhat
> like forcing them to enter fully expanded IPv6 addresses every time),
> sscanf() also ignores leading white spaces and successfully parses weird
> expressions like "  -42:    0x66:   0af: 0: 44:-6", which I think is a
> problem.

There is a standard for ethernet representation, that is all you need to
accept.  The only simplifications are optional leading zeros 02 vs 2
and upper and lower case a-f.

Don't overthink this. The FreeBSD version of ether_aton_r is:

struct ether_addr *
ether_aton_r(const char *a, struct ether_addr *e)
{
        int i;
        unsigned int o0, o1, o2, o3, o4, o5;

        i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
        if (i != 6)
                return (NULL);
        e->octet[0]=o0;
        e->octet[1]=o1;
        e->octet[2]=o2;
        e->octet[3]=o3;
        e->octet[4]=o4;
        e->octet[5]=o5;
        return (e);
}

Reply via email to