Skip to content

Commit

Permalink
Merge pull request #3300 from dcarp/Fix_14611
Browse files Browse the repository at this point in the history
Fix 14611 - socket.localAddress fails on Unix sockets with longer path (> 13 characters)
  • Loading branch information
schveiguy committed May 22, 2015
2 parents ae9706a + a6ed886 commit b8bc7c1
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions std/socket.d
Original file line number Diff line number Diff line change
Expand Up @@ -1921,45 +1921,47 @@ static if (is(sockaddr_un))
class UnixAddress: Address
{
protected:
sockaddr_un* sun;
socklen_t len;

struct
{
align (1):
sockaddr_un sun;
char unused = '\0'; // placeholder for a terminating '\0'
}

this() pure nothrow @nogc
{
sun.sun_family = AF_UNIX;
sun.sun_path = '?';
}


public:
override @property sockaddr* name()
{
return cast(sockaddr*)sun;
return cast(sockaddr*)&sun;
}

override @property const(sockaddr)* name() const
{
return cast(const(sockaddr)*)sun;
return cast(const(sockaddr)*)&sun;
}


override @property socklen_t nameLen() const
override @property socklen_t nameLen() @trusted const
{
return len;
return cast(socklen_t) (sockaddr_un.init.sun_path.offsetof +
strlen(cast(const(char*)) sun.sun_path.ptr) + 1);
}


this(in char[] path) @trusted pure nothrow
this(in char[] path) @trusted pure
{
len = cast(socklen_t)(sockaddr_un.init.sun_path.offsetof + path.length + 1);
sun = cast(sockaddr_un*) (new ubyte[len]).ptr;
enforce(path.length <= sun.sun_path.sizeof, new SocketParameterException("Path too long"));
sun.sun_family = AF_UNIX;
sun.sun_path.ptr[0..path.length] = (cast(byte[]) path)[];
sun.sun_path.ptr[path.length] = 0;
}

@property string path() const pure
@property string path() @trusted const pure
{
return to!string(sun.sun_path.ptr);
return to!string(cast(const(char)*)sun.sun_path.ptr);
}

override string toString() const pure
Expand All @@ -1984,6 +1986,7 @@ static if (is(sockaddr_un))

listener.bind(address);
scope(exit) () @trusted { remove(name.tempCString()); } ();
assert(listener.localAddress.toString == name);

listener.listen(1);

Expand Down Expand Up @@ -3405,6 +3408,13 @@ public:
Address result;
switch(_family)
{
static if (is(sockaddr_un))
{
case AddressFamily.UNIX:
result = new UnixAddress;
break;
}

case AddressFamily.INET:
result = new InternetAddress;
break;
Expand Down

0 comments on commit b8bc7c1

Please sign in to comment.