ipaddr icon indicating copy to clipboard operation
ipaddr copied to clipboard

IPv6 hton layout does not match ipv6_mreq

Open tenderlove opened this issue 2 years ago • 1 comments

Given this C program:

#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[])
{
        struct ipv6_mreq req;
        memset(&req, 0, sizeof(req));
	req.ipv6mr_multiaddr.s6_addr[0] = 0xFF;
	req.ipv6mr_multiaddr.s6_addr[1] = 0x02;
	req.ipv6mr_multiaddr.s6_addr[15] = 0xFB;

        uint8_t * ptr = (uint8_t *)&req;
        for(int i = 0; i < sizeof(req); i++) {
                fprintf(stderr, "\\%#02x", ptr[i]);
        }

        fprintf(stderr, "\n");
        return 0;
}

The bytes of the ipv6_mreq struct are: \0xff\0x2\00\00\00\00\00\00\00\00\00\00\00\00\00\0xfb\00\00\00\00

However, if I use this Ruby program:

require "ipaddr"

p IPAddr.new("ff02::fb").hton

The bytes are: "\xFF\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFB"

I think hton should match the ipv6_mreq layout.

tenderlove avatar Nov 29 '23 18:11 tenderlove

btw I'm not confident that I'm debugging this correctly, but I think the bytes should be the same.

tenderlove avatar Nov 29 '23 18:11 tenderlove