Re: struct sockaddr_XYZ formats portable?
From: Malte Starostik (malte_at_starostik.de)
Date: 06/02/05
- Next message: bobroberts: "Re: socket read behavior"
- Previous message: Barry Margolin: "Re: struct sockaddr_XYZ formats portable?"
- In reply to: Barry Margolin: "Re: struct sockaddr_XYZ formats portable?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: bobroberts: "Re: socket read behavior"
- Previous message: Barry Margolin: "Re: struct sockaddr_XYZ formats portable?"
- In reply to: Barry Margolin: "Re: struct sockaddr_XYZ formats portable?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|