Re: struct sockaddr_XYZ formats portable?

From: Malte Starostik (malte_at_starostik.de)
Date: 06/02/05


Date: 2 Jun 2005 04:38:15 -0700

Barry Margolin schrieb:
> In article <1117695956.122774.283810@z14g2000cwz.googlegroups.com>,
> "vishal" <vishal.ag@gmail.com> wrote:
>
> > If however, for some reasons, you want to send a particular
> > sockaddr_xyz structure over to some other hosts on the network, it
> > would be treated simply as data/payload and there won't be any issues
> > as long as you maintain byte ordering on both ends.

Byte order isn't the problem as the multi-byte members like port are
explicitly in "network" order already. Thanks anyway for your
elaborate response.

> But what if the recipient's sockaddr_xyz structure has different
> members, or they're in a different order, than those on the sender?
> E.g. on my system, the sockaddr_in structure is:
>
> struct sockaddr_in {
> u_char sin_len;

Alright, neither Linux nor Windows have a sin_len member, so there we
go already ...

> u_char sin_family;
> u_short sin_port;
> struct in_addr sin_addr;
> char sin_zero[8];
> };
>
> but on the recipient's system it could easily be:

[snip different layout example]

> I think that's the essence of the OP's question: are these structure
> layouts standard?

Exactly that was my question. I did a quick check on Linux and Windows
and saw that member sizes, order and overall struct sizes were equal so
I thought maybe they are standardised.

> Actually, even if they are, I don't think you can depend on being able
> to send them over the wire. C allows for padding between structure
> members, and different C implementations may pad the structures
> differently.

And forgot about this one. Indeed the systems I checked on are both
x86, so no surprise the padding is the same. And on a closer look no
explicit alignment is specified in the headers.

> If you want to send one of these over the network, you should use
> something like XDR to define your own structures and ensure
> compatibility.

I'll definately have to use something like that then. Reason I wanted
to avoid that is that the class encapsulating endpoint addresses
currently only exposes the raw data cast to const sockaddr* as that's
all I need for bind(), connect() and sendto() - internally it's a
sockaddr_storage and thus large enough for both protocol families. And
in order to exchange <family, ip, port> tuples with other nodes using
the struct as given seemed easiest since extracting members would be
the first time to introduce explicit handling of IPv4 vs. IPv6 into the
app and protocol.

Cheers,
Malte



Relevant Pages

  • Re: Structure size directives
    ... When you target a specific architecture, the alignment requirements are know apriori, so system developers do make use of explicit alignment and knows what they are doing. ... struct foo obj; ... offset of 4 bytes, and with the #pragma pack, it would be allocated at ... There are really two different cases here, you talk about the usage of "pack" to minimize padding in a struct, while I talk about the "pack " usage, which specify alignmentof struct members. ...
    (comp.lang.c)
  • Re: procedure parameter struct order
    ... the members of this struct would always have the same order on ... >> members in the order they are declared. ... Or if there even is a stack as such, ... > compiler would be quite correct if, behind the scenes, all it pushed on ...
    (comp.lang.c)
  • Re: Uninitialized memory, malloc and unsigned char
    ... the structure or union object may be a trap representation. ... I think this ensures that the holes between sturct members do not ... affect validity of the struct. ...
    (comp.std.c)
  • Re: BN AGM Motions
    ... sometimes the real reasons why someone has been ... I was involved in a, non naturist, clubs AGM were we had to vote ... committee set up to investigate John's alleged misconduct didn't meet ... limited punishments the governing committee or members through an AGM ...
    (uk.rec.naturist)
  • Re: "common initial sequence" rule = non-obvious constraints on padding?
    ... of any member struct of a union which currently contains such a struct ... anywhere that union type is visible. ... I'm sure that on the DS9000, the linker searches the complete program for unions and adds random padding between structure members in a way that breaks this assumption. ... compatible, you can declare the same object twice, using both types, without breaking the program. ...
    (comp.std.c)