Author Topic: IPv6 structures missing?  (Read 344 times)

pcMike

  • Regular Member
  • *
  • Posts: 46
IPv6 structures missing?
« on: November 15, 2017, 10:21:32 AM »
I wrote a tcp server which I want to update to support IPv6, so I need to replace the sockaddr_in structure with sockaddr_in6.
Unfortunately sockaddr_in6 is not defined in windows.inc. Can anyone tell me what's needed to add it?

Also, if anyone has example code for an IPv6 tcp server it would be very helpful. Ideally I want to accept both IPv4 and IPv6 connections using dual stack by creating an IPv6 socket and turning off the socket option IPV6_V6ONLY, (which is not defined in MASM32 either, but it is 027h), and then invoke bind and listen to allow inbound connections.

It appears that other then changing sockaddr_in to sockaddr_in6 and changing AF_INET to AF_INET6 (or AF_UNSPEC for dual stack), I will just need to adapt to using getaddrinfo and getnameinfo in place of the IPv4-only functions.





hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4883
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: IPv6 structures missing?
« Reply #1 on: November 15, 2017, 11:32:37 AM »
Its handy in MSDN.

https://msdn.microsoft.com/en-us/library/windows/hardware/ff570824(v=vs.85).aspx

typedef struct sockaddr_in {
  ADDRESS_FAMILY sin6_family;
  USHORT         sin6_port;
  ULONG          sin6_flowinfo;
  IN6_ADDR       sin6_addr;
  union {
    ULONG    sin6_scope_id;
    SCOPE_ID sin6_scope_struct;
  };
} SOCKADDR_IN6, *PSOCKADDR_IN6;
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

pcMike

  • Regular Member
  • *
  • Posts: 46
Re: IPv6 structures missing?
« Reply #2 on: November 15, 2017, 12:14:54 PM »
Thanks Hutch, but how would I create the same structure in MASM?

Is ADDRESS_FAMILY 8 bytes?
What does UNION do?
It appears that sin6_scope_struct is not defined in WINDOWS.INC either.



jj2007

  • Member
  • *****
  • Posts: 7662
  • Assembler is fun ;-)
    • MasmBasic
Re: IPv6 structures missing?
« Reply #3 on: November 15, 2017, 01:12:03 PM »
Pick what you need:
Code: [Select]
ADDRINFOA STRUCT
 ai_flags  DWORD ? ; AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
 ai_family DWORD ? ; PF_xxx
 ai_socktype DWORD ? ; SOCK_xxx
 ai_protocol DWORD ? ; 0 or IPPROTO_xxx for IPv4 and IPv6
 ai_addrlen dd ? ; Length of ai_addr
 ai_canonname LPVOID ? ; Canonical name for nodename
 ai_addr dd ? ; Binary address
 ai_next  dd ? ; Next structure in linked list
ADDRINFOA ENDS

Code: [Select]
SOCKADDR_IN6 STRUCT
 sin6_family DWORD ?
 sin6_port USHORT ?
 sin6_flowinfo ULONG ?
 sin6_addr DWORD ?
 UNION
   sin6_scope_id ULONG ?
   sin6_scope_struct SCOPE_ID <>
 ENDS
SOCKADDR_IN6 ENDS

PSOCKADDR_IN6 typedef ptr SOCKADDR_IN6

For the definition of SCOPE_ID, see here. Looks like a RECORD.

pcMike

  • Regular Member
  • *
  • Posts: 46
Re: IPv6 structures missing?
« Reply #4 on: November 15, 2017, 03:44:36 PM »
Thanks JJ, that was exactly what I needed.

aw27

  • Member
  • ****
  • Posts: 801
Re: IPv6 structures missing?
« Reply #5 on: November 15, 2017, 04:46:50 PM »
The structure is more likely something like this:

SOCKADDR_IN6 STRUCT
 sin6_family   WORD ?
 sin6_port    WORD ?
 sin6_flowinfo   DWORD ?
 sin6_addr    BYTE 16 DUP (?)
 ;UNION
   sin6_scope_id   DWORD ?
   ;sin6_scope_struct   SCOPE_ID <>
 ;ENDS
SOCKADDR_IN6 ENDS

scopeid is usually zero, probably you don't need to complicate things, unless you wish.

jj2007

  • Member
  • *****
  • Posts: 7662
  • Assembler is fun ;-)
    • MasmBasic
Re: IPv6 structures missing?
« Reply #6 on: November 15, 2017, 07:35:09 PM »
José is right, the family is a short. Here is the GCC ws2tcpip.h, much cleaner than the obfuscated M$ crap:
Code: [Select]
struct sockaddr_in6 {
short sin6_family; /* AF_INET6 */
u_short sin6_port; /* transport layer port # */
u_long sin6_flowinfo; /* IPv6 traffic class & flow info */
struct in6_addr sin6_addr;  /* IPv6 address */
u_long sin6_scope_id; /* set of interfaces for a scope */
};

The sin6_addr member is a "struct" that can be one byte, word or dword:
Code: [Select]
struct in6_addr {
    union {
        u_char _S6_u8[16];
        u_short _S6_u16[8];
        u_long _S6_u32[4];
        } _S6_un;
};

And of course, it is a DWORD in practice, what else?

aw27

  • Member
  • ****
  • Posts: 801
Re: IPv6 structures missing?
« Reply #7 on: November 15, 2017, 07:43:11 PM »
and of course, it is a DWORD in practice, what else?

It is never a DWORD, JJ.  :icon_eek:
Have you ever seen a IPv6 address?

jj2007

  • Member
  • *****
  • Posts: 7662
  • Assembler is fun ;-)
    • MasmBasic
Re: IPv6 structures missing?
« Reply #8 on: November 15, 2017, 08:00:55 PM »
Yeah, it's four DWORDs, right, thank you so much and have a nice day.

aw27

  • Member
  • ****
  • Posts: 801
Re: IPv6 structures missing?
« Reply #9 on: November 15, 2017, 08:10:07 PM »
Yeah, it's four DWORDs, right, thank you so much and have a nice day.
No, it is 16 bytes. No function deals with it as an array of dwords.
Now, you can rest in peace.  :t

jj2007

  • Member
  • *****
  • Posts: 7662
  • Assembler is fun ;-)
    • MasmBasic
Re: IPv6 structures missing?
« Reply #10 on: November 15, 2017, 09:10:03 PM »
No function deals with it as an array of dwords.

Then why does GCC provide u_long _S6_u32[4]; in the header file?

aw27

  • Member
  • ****
  • Posts: 801
Re: IPv6 structures missing?
« Reply #11 on: November 15, 2017, 09:45:14 PM »
No function deals with it as an array of dwords.
Then why does GCC provide u_long _S6_u32[4]; in the header file?

Does not make much sense, but VS does the same. 
On top of that values are in network byte order.

dedndave

  • Member
  • *****
  • Posts: 8743
  • Still using Abacus 2.0
    • DednDave
Re: IPv6 structures missing?
« Reply #12 on: November 16, 2017, 01:51:22 AM »
actually, it's 8 words, divided into 3 groups of different sizes  :biggrin:

qWord

  • Member
  • *****
  • Posts: 1468
  • The base type of a type is the type itself
    • SmplMath macros
Re: IPv6 structures missing?
« Reply #13 on: November 16, 2017, 02:12:38 AM »
... and normative it is:
Quote from: RFC4291
IPv6 addresses are 128-bit identifiers for interfaces and sets of interfaces


RFC4291

MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 7662
  • Assembler is fun ;-)
    • MasmBasic
Re: IPv6 structures missing?
« Reply #14 on: November 16, 2017, 02:34:28 AM »
Code: [Select]
struct in6_addr {
    union {
        u_char _S6_u8[16];
        u_short _S6_u16[8];
        u_long _S6_u32[4];
        GUID _S6_uxxl;
        } _S6_un;
};
;)