Ipv6

From usenix.org.uk
Jump to: navigation, search

After struggling with some IPv6 functions in C, I've come up with this, to convert from inet_pton format. In perl, the returned data is already in the format that you're after.

my $data = inet_pton( AF_INET6, "2001:0db8:85a3:0000:0000:8a2e:0370:7334" );
my @ints = unpack 'N*', $data;

That's really helpful, but in C, the returned data is not in the format that equates to the number. It's in network address format (the format on the wire).

So, I don't know if there's something already out there to do the conversion, but I've knocked something up that does it.

static int switch_bits_intel( unsigned int *ip_src, unsigned int *ip_dst ) {
	int i;
	unsigned int *src, *dst;

	for( i = 0 ; i < 4 ; i++ ) {
		unsigned int x;
		src = &ip_src[i];
		dst = &ip_dst[i];

		*dst = 0;

		x = *src >> 16;

		*dst |= ( ( *src & 0x000000ff ) << 8 | ( *src & 0x0000ff00 ) >> 8 ) << 16;
		src = &x;
		*dst |= ( ( *src & 0x000000ff ) << 8 | ( *src & 0x0000ff00 ) >> 8 );
	}
	return(0);
}

Supply the function like so:

unsigned int parts[4];
unsigned int *ip_space = (void*)apr_palloc( r->pool, sizeof(struct in6_addr) );
if( apr_inet_pton( APR_INET6, "2001:0db8:85a3:0000:0000:8a2e:0370:7334", parts ) == 0 ) {
	ap_log_error(APLOG_MARK, APLOG_ALERT, 0, NULL, "Not a valid address" );
	return( DECLINED );
}

switch_bits_intel( (void*)&parts, (void*)ip_space );

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox