Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

std.socket cleanup #260

Merged
merged 86 commits into from over 2 years ago

6 participants

Vladimir Panteleev JakobOvrum Andrei Alexandrescu Brad Roberts Jonathan M Davis millerlogic
Vladimir Panteleev
Collaborator

Take three. Previous pull request: #211, newsgroup discussion.

Vladimir Panteleev
Collaborator

I would like to add something like this for Posix as well, however it requires a change in druntime and is not as trivial.

For the record, creating fd_sets with a capacity higher than the default FD_SETSIZE in C-like languages is done by #define-ing FD_SETSIZE to the desired value before including the appropriate header files. Of course, this is not possible in D. It would also not be possible to simply use the same approach as I've done here for Windows, because on Posix FD_ZERO needs to know the capacity of the fd_set structure.

Technically, the exact implementation method isn't as important as it will be abstracted by std.socket anyway.

Vladimir Panteleev

Should this be outright deprecated? Or do we need to schedule it for deprecation, like everything else, despite how minor it is?

Collaborator

Very little should ever be deprecated immediately, because doing so breaks code. Unless errorCode was very recently added, it needs to be scheduled for deprecation before it's deprecated.

Vladimir Panteleev

This change supersedes 956decd entirely.

Vladimir Panteleev
Collaborator

Of note: technically, this wasn't a real merge resolution (in the sense that I edited the merge conflicts by hand) - rather, I decided to apply this year's std.socket history on top of Chris's update, so git could track individual changes. The end result is the same with considerably less work.

CyberShadow added some commits
Vladimir Panteleev CyberShadow Issue 3484 - std.socket.Address hierarchy not const-safe aec1d8e
Vladimir Panteleev CyberShadow std.socket: add getErrorText() to Socket 8fee67f
Vladimir Panteleev CyberShadow std.socket: verify capacity in SocketSet operations for memory safety 8be7c18
Vladimir Panteleev CyberShadow std.socket: Refactor Service.getServiceBy* to use optional arguments …
…instead of overloads
8716f74
Vladimir Panteleev CyberShadow Merge branch 'master' into new-std-socket 5fb6c4e
Vladimir Panteleev CyberShadow std.socket: Refactor common code in InternetHost methods 0e94b20
Vladimir Panteleev CyberShadow std.socket: use thread-safe name resolution where it's available 8d8726b
Vladimir Panteleev CyberShadow std.socket: use stdint types for network numbers 48c3c82
Vladimir Panteleev CyberShadow std.socket: Linger struct - D naming conventions and automatic field …
…typing
c42e414
Vladimir Panteleev CyberShadow std.socket: fix reverse IPv4 lookup via gethostbyaddr (with test) 7aa9e5f
Vladimir Panteleev CyberShadow std.socket: Mark SocketException.errorCode as scheduled for deprecation 6313c40
Vladimir Panteleev CyberShadow std.socket: Add const name() method to Address covariant with non-con…
…st method
f4a629b
Vladimir Panteleev CyberShadow std.socket: Implement Address.addressFamily in the base class bbbf60d
Vladimir Panteleev CyberShadow std.socket: Fix documentation of InternetHost.getHostByAddr d6ccb34
Vladimir Panteleev CyberShadow std.socket: use INADDR_ANY 193fffc
Vladimir Panteleev CyberShadow std.socket: Address-family-agnostic getnameinfo support
This commit refactors getnameinfo usage in toHostNameString into a new
method in the base Address class, and provides an IPv4-specific override
in InternetAddress which uses the deprecated getHostByAddr function.
1ee33a4
Vladimir Panteleev CyberShadow std.socket: Address DDoc improvements 57159cd
Vladimir Panteleev CyberShadow std.socket: Update module authors eb7d8d9
Vladimir Panteleev CyberShadow std.socket: Require hostname when using getnameinfo, for consistency …
…with gethostbyaddr
3cc1436
Vladimir Panteleev CyberShadow std.socket: Add toAddrString, toPortString, toServiceNameString to Ad…
…dress
94b782e
Vladimir Panteleev CyberShadow std.socket: Fix typo in documentation 5528b31
Vladimir Panteleev CyberShadow std.socket: Add UnknownAddressReference class cb8d20b
Vladimir Panteleev CyberShadow std.socket: Add getAddressInfo cf9c945
Vladimir Panteleev CyberShadow std.socket: Drop AddressInfoHints, DDoc updates 093c6f1
Vladimir Panteleev CyberShadow std.socket: Make getAddressInfo template-variadic 15b8d62
Vladimir Panteleev CyberShadow std.socket: reword InternetAddress constructor DDoc 88a9025
Vladimir Panteleev CyberShadow std.socket: add Internet6Address d9f43ab
Vladimir Panteleev CyberShadow std.socket: add getAddress function aefbe78
Vladimir Panteleev CyberShadow std.socket: check for invalid IP addresses in InternetHost.getHostByAddr ad2d046
Vladimir Panteleev CyberShadow std.socket: Refactor all *Address.toString implementations to a singl…
…e base one
65c7f6b
Vladimir Panteleev CyberShadow std.socket, .socketstream: Link to examples on GitHub d81efd4
Vladimir Panteleev CyberShadow std.socket: Exception class documentation a36ba86
Vladimir Panteleev CyberShadow std.socket: DDoc grammar a65072d
Vladimir Panteleev CyberShadow std.socket: Wrap C structs for TimeVal and Linger
Instead of trying to mimic the internal layout of the C timeval and linger
structures (and risking silent breakages due to differences in types,
field order and alignment issues), properly wrap them and provide proxy
properties for accessing their fields. Unfortunately I couldn't find a
neat way to do this neatly while preserving documentation, the deprecation
of the C field names and the new aliases. If Issue 6619 is implemented,
the implementation could be simplified greatly.

This commit also renames std.socket.timeval to TimeVal and marks the old
name as scheduled for deprecation.
a22d88f
Vladimir Panteleev CyberShadow std.socket: @property 3a33e01
Vladimir Panteleev CyberShadow std.socket: Add Socket.this(AddressInfo) constructor 75fcdfa
Vladimir Panteleev CyberShadow std.socket: Use socklen_t e1960bd
Vladimir Panteleev CyberShadow std.socket: Check sockaddr length in local/remoteAddress 864d566
Vladimir Panteleev CyberShadow std.socket: Mention error diagnostic functions in send*/receive* DDoc 1c0eea1
Vladimir Panteleev CyberShadow std.socket: Timeout option refactoring and overflow checks 4a5b955
Vladimir Panteleev CyberShadow std.socket: Change delay type from int to long in Socket.select
0x7FFFFFFF microseconds is about 36 minutes. An application which uses
the select() timeout for event timing may specify a value larger than that,
if it needs to wake up hourly or less often.
b7fea71
Vladimir Panteleev CyberShadow std.socket: Allow specifying Socket.select timeout as Duration 1732544
Vladimir Panteleev CyberShadow std.socket: Tcp/UdpSocket constructor DDoc 2575a0b
Vladimir Panteleev CyberShadow std.socket: More constness f8846a4
Vladimir Panteleev CyberShadow std.socket: Replace Socket.newFamilyObject with createAddress
Socket.newFamilyObject was a private method which simply called
createAddress().
4698949
Vladimir Panteleev CyberShadow std.socket: Throw appropriate exception in local/remoteAddress 6d710cc
Vladimir Panteleev CyberShadow std.socket: Add AF_INET6 support to Socket.createAddress 28540d0
Vladimir Panteleev CyberShadow std.socket: Disambiguate usage of "address" term in DDoc d5b6117
Vladimir Panteleev CyberShadow std.socket: Add parseAddress function e22af4c
Vladimir Panteleev CyberShadow std.socket: Recommend new functions instead of IPv4-only classes 00affa3
Vladimir Panteleev CyberShadow std.socket: Drop printf, add and use softUnittest c4514e9
Vladimir Panteleev CyberShadow std.socket: Socket.send*/receive* DDoc af32eda
Vladimir Panteleev CyberShadow std.socket: Add examples for DNS functions/classes, DDoc fixes f64c68b
Vladimir Panteleev CyberShadow std.socket, .socketstream: Remove ws2_32 notice, a pragma(lib) exists afc14ed
Vladimir Panteleev CyberShadow std.socket: POSIX fixes 0ed0372
Vladimir Panteleev CyberShadow std.socket: Add static InternetAddress.toString method fc402b1
Vladimir Panteleev CyberShadow std.socket: Add Socket.setKeepAlive for Windows and Linux 72490a5
Vladimir Panteleev CyberShadow std.socket: Add IPV6_V6ONLY SocketOption e974479
Vladimir Panteleev CyberShadow std.socket: Add UnixAddress 7dab11c
Vladimir Panteleev CyberShadow std.socket: Enumeration clean-up, documentation, completion d21e98e
Vladimir Panteleev CyberShadow std.socket: Avoid reallocating address in Socket.receiveFrom 2fbb7d6
Vladimir Panteleev CyberShadow std.socket: Fix failing unittests 34e4346
Vladimir Panteleev CyberShadow std.socket: Failed reverse lookups return null, not throw 5fac9e2
Vladimir Panteleev CyberShadow std.socket: Add unittest for WINSOCK_TIMEOUT_SKEW 89feff7
JakobOvrum

Why did you switch to uint16_t instead of ushort? ushort is also guaranteed to be 16 bits. Wouldn't this change just confuse D programmers who aren't familiar with the C way?

The constructor this(AddressFamily af, SocketType type, string protocolName) should be this(AddressFamily af, SocketType type, in char[] protocolName) by the looks of it.

The Service class has this problem too. And Protocol. And InternetHost. Ok it looks like pretty much the whole module uses string everywhere. I don't know if that's a holdover from the old D1 module or not, but it should be changed.

In serviceToPort, service == "" should be changed to service.empty.

Vladimir Panteleev
Collaborator

The constructor this(AddressFamily af, SocketType type, string protocolName) should be this(AddressFamily af, SocketType type, in char[] protocolName) by the looks of it.

This shouldn't break any code, right? What about other methods, such as Service.getServiceByName?

And why did you switch to uint16_t instead of ushort? ushort is also guaranteed to be 16 bits. Wouldn't this change just confuse D programmers who aren't familiar with the C way?

I wanted to be consistent, but I didn't think of the time about the impact on the documentation. I'll take care of it.

JakobOvrum

This shouldn't break any code, right? What about other methods, such as Service.getServiceByName?

servent.s_name is a char*, so to!string(serv.s_name) creates a copy. getservbyname takes a const char*, so const(char)[] is sufficient.

Vladimir Panteleev
Collaborator

I understand the intent of the change, but that doesn't answer my question. Your observation also applies to similar methods which accept string parameters, such as Service.getServiceByName, correct?

JakobOvrum

Yes, they should not ask for the strong immutable guarantee from the user when they don't use it. Service.getServiceByName (which is a deliciously redundant name) does not need the immutable guarantee on its parameters because they are not escaped. This applies to all instances of string parameters I have seen in this module so far.

Vladimir Panteleev
Collaborator

Done, thanks.

Andrei Alexandrescu

Is there a reason for not going with the classic naming convention? Also, did you mean to return a fd_set[]?

Collaborator

The naming convention of FD_CREATE is in line with the FD_SET/FD_CLR/FD_ZERO/FD_ISSET functions (which are macros in C). And no, a fd_set is a set of file descriptors (socket handles on Windows).

Andrei Alexandrescu
Owner

Guess this is ready for committing. Does anyone have issues with it?

Vladimir Panteleev
Collaborator

For the record, I noticed that a DMD unit test (runnable/testsocket.d) fails on the pull request auto-tester, but I can't reproduce the problem on my machine.

Edit: never mind, I see the problem. It's caused by fixing enforcement of SocketSet capacity.

Vladimir Panteleev
Collaborator

That test is invalid: a class destructor accesses a heap-allocated field, which is what causes the segfault. The Socket class already has a destructor which closes the socket. I'll send a pull request.

Edit: #410.

Brad Roberts
Owner

I want that test yanked from the dmd suite and moved to a standard phobos unittest. It's from the era where the dmd suite contained tests for the whole codebase. Doesn't need to be done as part of this pull request though.

Vladimir Panteleev
Collaborator

Does anyone know what that test is supposed to be testing? It's already catching all SocketExceptions, so I guess it's testing for something else? Bad codegen? A segfault? What's PERMUTE_ARGS?

Brad Roberts
Owner

It dates back to a very old bug: http://d.puremagic.com/issues/show_bug.cgi?id=268

Obviously a number of issues weren't well handled back then.

Vladimir Panteleev
Collaborator

Ping?

Andrei Alexandrescu andralex merged commit 4d8c6a1 into from
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 86 unique commits by 2 authors.

Aug 22, 2011
Vladimir Panteleev CyberShadow std.socket: Add SocketOSException and SocketParameterException classes d3493b9
Vladimir Panteleev CyberShadow std.socket: Ddoc formatting and minor improvements 6ecc85b
Vladimir Panteleev CyberShadow std.socket: Refactor error message retrieval to own function, add las…
…tSocketError
4dfe306
Vladimir Panteleev CyberShadow std.socket: Add support for error message formatting on Windows 0ac3b8c
Vladimir Panteleev CyberShadow std.socket: add wouldHaveBlocked function 1963269
Vladimir Panteleev CyberShadow std.socket: replace version(BsdSockets) with version(Posix) 9b5aa12
Vladimir Panteleev CyberShadow std.socket: re-enabled Windows getProtocolByType test (works for me) 956decd
Vladimir Panteleev CyberShadow std.c.windows.winsock: fix alignment ef3fd73
Vladimir Panteleev CyberShadow std.c.windows.winsock: add FD_CREATE for allocating fd_sets of arbitr…
…ary capacity
98ea66e
Vladimir Panteleev CyberShadow std.socket: Add support for variable set sizes on Windows 1f2cc3a
Aug 24, 2011
Vladimir Panteleev CyberShadow std.socketstream: Clean up Ddoc formatting and code indentation. a8992d8
Vladimir Panteleev CyberShadow std.socketstream: document and @disable SocketStream.seek. 96c6a72
Vladimir Panteleev CyberShadow Issue 5177 - std.socketstream's close() should call super.close() 29f5491
millerlogic millerlogic std.socket updates and boost license
Committed on behalf of Christopher Miller by Vladimir Panteleev <thecybershadow@gmail.com>
8e743ad
Vladimir Panteleev CyberShadow Merge branch 'std-socket-chris' into new-std-socket
Conflicts:
	std/socket.d
65e6bbe
Vladimir Panteleev CyberShadow std.c.windows.winsock: const-correctness in API function declarations fb9d56f
Vladimir Panteleev CyberShadow Issue 3484 - std.socket.Address hierarchy not const-safe aec1d8e
Aug 25, 2011
Vladimir Panteleev CyberShadow std.socket: add getErrorText() to Socket 8fee67f
Vladimir Panteleev CyberShadow std.socket: verify capacity in SocketSet operations for memory safety 8be7c18
Aug 26, 2011
Vladimir Panteleev CyberShadow std.socket: Refactor Service.getServiceBy* to use optional arguments …
…instead of overloads
8716f74
Sep 03, 2011
Vladimir Panteleev CyberShadow Merge branch 'master' into new-std-socket 5fb6c4e
Vladimir Panteleev CyberShadow std.socket: Refactor common code in InternetHost methods 0e94b20
Sep 04, 2011
Vladimir Panteleev CyberShadow std.socket: use thread-safe name resolution where it's available 8d8726b
Vladimir Panteleev CyberShadow std.socket: use stdint types for network numbers 48c3c82
Vladimir Panteleev CyberShadow std.socket: Linger struct - D naming conventions and automatic field …
…typing
c42e414
Vladimir Panteleev CyberShadow std.socket: fix reverse IPv4 lookup via gethostbyaddr (with test) 7aa9e5f
Sep 05, 2011
Vladimir Panteleev CyberShadow std.socket: Mark SocketException.errorCode as scheduled for deprecation 6313c40
Vladimir Panteleev CyberShadow std.socket: Add const name() method to Address covariant with non-con…
…st method
f4a629b
Vladimir Panteleev CyberShadow std.socket: Implement Address.addressFamily in the base class bbbf60d
Vladimir Panteleev CyberShadow std.socket: Fix documentation of InternetHost.getHostByAddr d6ccb34
Vladimir Panteleev CyberShadow std.socket: use INADDR_ANY 193fffc
Vladimir Panteleev CyberShadow std.socket: Address-family-agnostic getnameinfo support
This commit refactors getnameinfo usage in toHostNameString into a new
method in the base Address class, and provides an IPv4-specific override
in InternetAddress which uses the deprecated getHostByAddr function.
1ee33a4
Vladimir Panteleev CyberShadow std.socket: Address DDoc improvements 57159cd
Vladimir Panteleev CyberShadow std.socket: Update module authors eb7d8d9
Vladimir Panteleev CyberShadow std.socket: Require hostname when using getnameinfo, for consistency …
…with gethostbyaddr
3cc1436
Vladimir Panteleev CyberShadow std.socket: Add toAddrString, toPortString, toServiceNameString to Ad…
…dress
94b782e
Vladimir Panteleev CyberShadow std.socket: Fix typo in documentation 5528b31
Vladimir Panteleev CyberShadow std.socket: Add UnknownAddressReference class cb8d20b
Vladimir Panteleev CyberShadow std.socket: Add getAddressInfo cf9c945
Sep 07, 2011
Vladimir Panteleev CyberShadow std.socket: Drop AddressInfoHints, DDoc updates 093c6f1
Vladimir Panteleev CyberShadow std.socket: Make getAddressInfo template-variadic 15b8d62
Vladimir Panteleev CyberShadow std.socket: reword InternetAddress constructor DDoc 88a9025
Vladimir Panteleev CyberShadow std.socket: add Internet6Address d9f43ab
Vladimir Panteleev CyberShadow std.socket: add getAddress function aefbe78
Vladimir Panteleev CyberShadow std.socket: check for invalid IP addresses in InternetHost.getHostByAddr ad2d046
Vladimir Panteleev CyberShadow std.socket: Refactor all *Address.toString implementations to a singl…
…e base one
65c7f6b
Vladimir Panteleev CyberShadow std.socket, .socketstream: Link to examples on GitHub d81efd4
Vladimir Panteleev CyberShadow std.socket: Exception class documentation a36ba86
Vladimir Panteleev CyberShadow std.socket: DDoc grammar a65072d
Vladimir Panteleev CyberShadow std.socket: Wrap C structs for TimeVal and Linger
Instead of trying to mimic the internal layout of the C timeval and linger
structures (and risking silent breakages due to differences in types,
field order and alignment issues), properly wrap them and provide proxy
properties for accessing their fields. Unfortunately I couldn't find a
neat way to do this neatly while preserving documentation, the deprecation
of the C field names and the new aliases. If Issue 6619 is implemented,
the implementation could be simplified greatly.

This commit also renames std.socket.timeval to TimeVal and marks the old
name as scheduled for deprecation.
a22d88f
Vladimir Panteleev CyberShadow std.socket: @property 3a33e01
Vladimir Panteleev CyberShadow std.socket: Add Socket.this(AddressInfo) constructor 75fcdfa
Vladimir Panteleev CyberShadow std.socket: Use socklen_t e1960bd
Vladimir Panteleev CyberShadow std.socket: Check sockaddr length in local/remoteAddress 864d566
Vladimir Panteleev CyberShadow std.socket: Mention error diagnostic functions in send*/receive* DDoc 1c0eea1
Vladimir Panteleev CyberShadow std.socket: Timeout option refactoring and overflow checks 4a5b955
Vladimir Panteleev CyberShadow std.socket: Change delay type from int to long in Socket.select
0x7FFFFFFF microseconds is about 36 minutes. An application which uses
the select() timeout for event timing may specify a value larger than that,
if it needs to wake up hourly or less often.
b7fea71
Vladimir Panteleev CyberShadow std.socket: Allow specifying Socket.select timeout as Duration 1732544
Vladimir Panteleev CyberShadow std.socket: Tcp/UdpSocket constructor DDoc 2575a0b
Vladimir Panteleev CyberShadow std.socket: More constness f8846a4
Vladimir Panteleev CyberShadow std.socket: Replace Socket.newFamilyObject with createAddress
Socket.newFamilyObject was a private method which simply called
createAddress().
4698949
Vladimir Panteleev CyberShadow std.socket: Throw appropriate exception in local/remoteAddress 6d710cc
Vladimir Panteleev CyberShadow std.socket: Add AF_INET6 support to Socket.createAddress 28540d0
Vladimir Panteleev CyberShadow std.socket: Disambiguate usage of "address" term in DDoc d5b6117
Vladimir Panteleev CyberShadow std.socket: Add parseAddress function e22af4c
Vladimir Panteleev CyberShadow std.socket: Recommend new functions instead of IPv4-only classes 00affa3
Sep 08, 2011
Vladimir Panteleev CyberShadow std.socket: Drop printf, add and use softUnittest c4514e9
Vladimir Panteleev CyberShadow std.socket: Socket.send*/receive* DDoc af32eda
Vladimir Panteleev CyberShadow std.socket: Add examples for DNS functions/classes, DDoc fixes f64c68b
Vladimir Panteleev CyberShadow std.socket, .socketstream: Remove ws2_32 notice, a pragma(lib) exists afc14ed
Vladimir Panteleev CyberShadow std.socket: POSIX fixes 0ed0372
Vladimir Panteleev CyberShadow std.socket: Add static InternetAddress.toString method fc402b1
Vladimir Panteleev CyberShadow std.socket: Add Socket.setKeepAlive for Windows and Linux 72490a5
Vladimir Panteleev CyberShadow std.socket: Add IPV6_V6ONLY SocketOption e974479
Sep 11, 2011
Vladimir Panteleev CyberShadow std.socket: Add UnixAddress 7dab11c
Sep 12, 2011
Vladimir Panteleev CyberShadow std.socket: Enumeration clean-up, documentation, completion d21e98e
Vladimir Panteleev CyberShadow std.socket: Avoid reallocating address in Socket.receiveFrom 2fbb7d6
Sep 14, 2011
Vladimir Panteleev CyberShadow std.socket: Fix failing unittests 34e4346
Vladimir Panteleev CyberShadow std.socket: Failed reverse lookups return null, not throw 5fac9e2
Vladimir Panteleev CyberShadow std.socket: Add unittest for WINSOCK_TIMEOUT_SKEW 89feff7
Sep 15, 2011
Vladimir Panteleev CyberShadow std.socket: Remove user-facing socket-specific numeric address types. 9a86fb7
Sep 17, 2011
Vladimir Panteleev CyberShadow std.socket: Use "in char[]" instead of string arguments d6d2ceb
Vladimir Panteleev CyberShadow std.socket: Remove redundant string duplication d7c771a
Sep 19, 2011
Vladimir Panteleev CyberShadow Merge branch 'master' into new-std-socket
Conflicts:
	std/socket.d
3cd801b
Vladimir Panteleev CyberShadow std.socket: Fix build failure on POSIX
More weird constness compiler behavior...
2fc2a1b
Vladimir Panteleev CyberShadow std.socket: There is no such thing as IN6ADDR_NONE. 89461b1
Something went wrong with that request. Please try again.