On ter?a-feira, 29 de novembro de 2016 04:31:11 PST Arpit Agarwal wrote:
> In the following code in CAReceiveMessage function:
> 
>     if (flags & CA_IPV6)
>     {
>         namelen = sizeof (struct sockaddr_in6);
>         level = IPPROTO_IPV6;
>         type = IPV6_PKTINFO;
>         len = sizeof (struct in6_pktinfo);
>     }
>     else
>     {
>         namelen = sizeof (struct sockaddr_in);
>         level = IPPROTO_IP;
>         type = IP_PKTINFO;
>         len = sizeof (struct in6_pktinfo);
>     }
> 
> May I know why the len is set to sizeof (struct in6_pktinfo) in case of IPv4
> packet? Is it intentional with a purpose? Shouldn't it be set to sizeof
> (struct in_pktinfo) ? It will be helpful if someone can clear my doubt.

Hello Arpit

Yes and no. This looks like a copy & paste error and it should have been 
sizeof(struct in_pktinfo). However, this is harmless. This part of the code is 
simply telling the OS the size of the buffer, which was defined above as;

    union control
    {
        struct cmsghdr cmsg;
        unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
    } cmsg;

If it set the len to sizeof(struct in_pktinfo), then it needs to assert 
(hopefully static_assert) that the size of in6_pktinfo is greater than or 
equal to the size of in_pktinfo.

Alternatively, it can do what the Windows code below does and simply pass the 
size of the buffer:

    union control
    {
        WSACMSGHDR cmsg;
        uint8_t data[WSA_CMSG_SPACE(sizeof (IN6_PKTINFO))];
    } cmsg;
        // ....
                  .Control = {.buf = cmsg.data, .len = sizeof (cmsg)}

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

Reply via email to