English 中文(简体)
How to initialize struct in6_addr?
原标题:

I do know one method to do this,

const struct in6_addr naddr6 = { { 
                0x3f, 0xfe, 0x05, 0x01,
                0x00, 0x08, 0x00, 0x00, 
                0x02, 0x60, 0x97, 0xff,
                0xfe, 0x40, 0xef, 0xab
}};

but could not with this,

const struct in6_addr naddr6 = 
                { { { 0x3ffe0501, 0x00080000, 0x026097ff, 0xfe40efab } } }; 

and it seems that I could either 1/2/3 paris of brackets.Why?

thanks.

最佳回答

Because one need to indicate which form of the address it is addressing (shown using C99):

const struct in6_addr naddr6 = 
    { { .u6_addr32 = { 0x3ffe0501, 0x00080000, 0x026097ff, 0xfe40efab } } };

The first bracket pair is for the in6_addr struct, the second for the union:

struct in6_addr
  {
    union
      {
 uint8_t u6_addr8[16];
 uint16_t u6_addr16[8];
 uint32_t u6_addr32[4];
      } in6_u;
  };
问题回答

The portable way to do this is like so:

struct in6_addr s6 = { };
if (!IN6_IS_ADDR_UNSPECIFIED(&s6))
  inet_pton(AF_INET6, "2001:db8::1", &s6);




相关问题
route a custom url to a web-application

I have a Vista Home PC with computer-name say office_pc1 and with IP say 192.168.11.40. I have a web-application, in that pc, which can be successfully accessed via the IP itself. Now, my question is ...

Why would a blocking socket repeatedly return 0-length data?

I m having a significant problem using a standard BSD-style socket in a C++ program. In the code below, I connect to a local web server, send a request, and simply create a loop waiting for data to ...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

How to initialize struct in6_addr?

I do know one method to do this, const struct in6_addr naddr6 = { { 0x3f, 0xfe, 0x05, 0x01, 0x00, 0x08, 0x00, 0x00, 0x02, 0x60, 0x97, 0xff, ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

热门标签