www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - sockaddr_in and InternetAddress

reply "Andre Kostur" <andre kostur.net> writes:
I'm looking for one of two things:

1) If I have a sockaddr_in, how do I get it into an 
InternetAddress?   I can do it with sufficient handwaving using 
bigEndianToNative and such (pulling out the 4-byte address in 
there), but I was expecting an easier/shorter method to get the 
sockaddr_in into an InternetAddress (after all, it's exactly what 
an InternetAddress is holding internally anyway)?

2) If it is agreed that there should be a shorter way, I'm 
willing to write the code, but would need a little guidance as to 
where would be the appropriate place to put the code (New 
overload to parseAddress?  New constructor for InternetAddress?)

And... a similar issue for sockaddr_in6 and Internet6Address 
(although this one's easier since you don't need to deal with the 
endianness issue).
May 28 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
I would add it as a new constructor to InternetAddress.

In the mean time, you could work around the lack of it by making 
a new subclass of InternetAddress that sets the member with your 
constructor. Like:

class MyInternetAddress : InternetAddress {
    this(sockaddr_in addr) {
       this.sin = addr;
    }
}


and I think that will work. (Looking at the source, the sin 
member is protected which means a subclass can get at it.)

Then you just pass it everywhere InternetAddress is expected and 
it ought to just work.
May 28 2015
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 29 May 2015 at 00:08:10 UTC, Andre Kostur wrote:
 I'm looking for one of two things:

 1) If I have a sockaddr_in, how do I get it into an 
 InternetAddress?
First of all, you may not actually need an InternetAddress. Modern code working with addresses should be address-agnostic, so it should take an Address instead. Ideally, you should avoid doing anything address-family-specific - then, it will work with any address family, incl. any introduced in the future. You could: - Create an UnknownAddressReference which uses a pointer to your sockaddr_in. - Create an InternetAddress, then copy the address over its "name" property.
 2) If it is agreed that there should be a shorter way, I'm 
 willing to write the code, but would need a little guidance as 
 to where would be the appropriate place to put the code (New 
 overload to parseAddress?  New constructor for InternetAddress?)
A new constructor for InternetAddress won't hurt.
May 28 2015
parent Andre Kostur <andre kostur.net> writes:
On 2015-05-28 11:10 PM, Vladimir Panteleev wrote:
 On Friday, 29 May 2015 at 00:08:10 UTC, Andre Kostur wrote:
 I'm looking for one of two things:

 1) If I have a sockaddr_in, how do I get it into an InternetAddress?
First of all, you may not actually need an InternetAddress. Modern code working with addresses should be address-agnostic, so it should take an Address instead. Ideally, you should avoid doing anything address-family-specific - then, it will work with any address family, incl. any introduced in the future.
Generally speaking, sure. But I live down at the protocol layers of networking where I do need to be aware of whether I'm using IPv4 or IPv6.
 You could:

 - Create an UnknownAddressReference which uses a pointer to your
 sockaddr_in.

 - Create an InternetAddress, then copy the address over its "name"
 property.
Seems inconvenient to construct then copy over an object when I have the data ready at the time of construction.
 2) If it is agreed that there should be a shorter way, I'm willing to
 write the code, but would need a little guidance as to where would be
 the appropriate place to put the code (New overload to parseAddress?
 New constructor for InternetAddress?)
A new constructor for InternetAddress won't hurt.
Done (for both InternetAddress and Internet6Address), pull request is in progress. Thanks for the input! (And to Adam too...)
May 29 2015