1 // Written in the D programming language
3 // NOTE: When working on this module, be sure to run tests with -debug=std_socket
4 // E.g.: dmd -version=StdUnittest -debug=std_socket -unittest -main -run socket
5 // This will enable some tests which are too slow or flaky to run as part of CI.
8 Copyright (C) 2004-2011 Christopher E. Miller
13 Thanks to Benjamin Herr for his assistance.
18 * Example: See $(SAMPLESRC listener.d) and $(SAMPLESRC htmlget.d)
19 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
20 * Authors: Christopher E. Miller, $(HTTP klickverbot.at, David Nadlinger),
21 * $(HTTP thecybershadow.net, Vladimir Panteleev)
22 * Source: $(PHOBOSSRC std/socket.d)
27 import core.stdc.stdint, core.stdc.stdlib, core.stdc.string, std.conv, std.string;
29 import core.stdc.config;
30 import core.time : dur, Duration;
33 import std.internal.cstring;
39 else version (WatchOS)
46 pragma (lib, "ws2_32.lib");
47 pragma (lib, "wsock32.lib");
49 import core.sys.windows.winbase, std.windows.syserror;
50 public import core.sys.windows.winsock2;
51 private alias _ctimeval = core.sys.windows.winsock2.timeval;
52 private alias _clinger = core.sys.windows.winsock2.linger;
54 enum socket_t : SOCKET { INVALID_SOCKET }
55 private const int _SOCKET_ERROR = SOCKET_ERROR;
58 private int _lasterr() nothrow @nogc
60 return WSAGetLastError();
74 public import core.sys.posix.netinet.in_;
75 import core.sys.posix.arpa.inet;
76 import core.sys.posix.fcntl;
77 import core.sys.posix.netdb;
78 import core.sys.posix.netinet.tcp;
79 import core.sys.posix.sys.select;
80 import core.sys.posix.sys.socket;
81 import core.sys.posix.sys.time;
82 import core.sys.posix.sys.un : sockaddr_un;
83 import core.sys.posix.unistd;
84 private alias _ctimeval = core.sys.posix.sys.time.timeval;
85 private alias _clinger = core.sys.posix.sys.socket.linger;
87 import core.stdc.errno;
89 enum socket_t : int32_t { _init = -1 }
90 private const int _SOCKET_ERROR = -1;
99 private int _lasterr() nothrow @nogc
106 static assert(0, "No socket support for this platform yet.");
109 version (StdUnittest)
111 // Print a message on exception instead of failing the unittest.
112 private void softUnittest(void delegate() @safe test, int line = __LINE__) @trusted
118 import std.stdio : writefln;
122 writefln("Ignoring std.socket(%d) test failure (likely caused by flaky environment): %s", line, e.msg);
127 /// Base exception thrown by `std.socket`.
128 class SocketException: Exception
130 mixin basicExceptionCtors;
133 version (CRuntime_Glibc) version = GNU_STRERROR;
134 version (CRuntime_UClibc) version = GNU_STRERROR;
137 * Needs to be public so that SocketOSException can be thrown outside of
138 * std.socket (since it uses it as a default argument), but it probably doesn't
139 * need to actually show up in the docs, since there's not really any public
140 * need for it outside of being a default argument.
142 string formatSocketError(int err) @trusted
148 version (GNU_STRERROR)
150 cs = strerror_r(err, buf.ptr, buf.length);
154 auto errs = strerror_r(err, buf.ptr, buf.length);
158 return "Socket error " ~ to!string(err);
161 auto len = strlen(cs);
163 if (cs[len - 1] == '\n')
165 if (cs[len - 1] == '\r')
167 return cs[0 .. len].idup;
172 return sysErrorString(err);
175 return "Socket error " ~ to!string(err);
178 /// Retrieve the error message for the most recently encountered network error.
179 @property string lastSocketError()
181 return formatSocketError(_lasterr());
185 * Socket exceptions representing network errors reported by the operating
188 class SocketOSException: SocketException
190 int errorCode; /// Platform-specific error code.
194 string file = __FILE__,
195 size_t line = __LINE__,
196 Throwable next = null,
197 int err = _lasterr(),
198 string function(int) @trusted errorFormatter = &formatSocketError)
203 super(msg ~ ": " ~ errorFormatter(err), file, line, next);
205 super(errorFormatter(err), file, line, next);
211 string file = __FILE__,
212 size_t line = __LINE__,
213 int err = _lasterr(),
214 string function(int) @trusted errorFormatter = &formatSocketError)
216 this(msg, file, line, next, err, errorFormatter);
222 string function(int) @trusted errorFormatter = &formatSocketError,
223 string file = __FILE__,
224 size_t line = __LINE__,
225 Throwable next = null)
227 this(msg, file, line, next, err, errorFormatter);
231 /// Socket exceptions representing invalid parameters specified by user code.
232 class SocketParameterException: SocketException
234 mixin basicExceptionCtors;
238 * Socket exceptions representing attempts to use network capabilities not
239 * available on the current system.
241 class SocketFeatureException: SocketException
243 mixin basicExceptionCtors;
249 * `true` if the last socket operation failed because the socket
250 * was in non-blocking mode and the operation would have blocked,
251 * or if the socket is in blocking mode and set a SNDTIMEO or RCVTIMEO,
252 * and the operation timed out.
254 bool wouldHaveBlocked() nothrow @nogc
257 return _lasterr() == WSAEWOULDBLOCK || _lasterr() == WSAETIMEDOUT;
259 return _lasterr() == EAGAIN;
261 static assert(0, "No socket support for this platform yet.");
266 auto sockets = socketPair();
268 s.setOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, dur!"msecs"(10));
269 ubyte[] buffer = new ubyte[](16);
270 auto rec = s.receive(buffer);
271 assert(rec == -1 && wouldHaveBlocked());
277 typeof(&getnameinfo) getnameinfoPointer;
278 typeof(&getaddrinfo) getaddrinfoPointer;
279 typeof(&freeaddrinfo) freeaddrinfoPointer;
282 shared static this() @system
288 // Winsock will still load if an older version is present.
289 // The version is just a request.
291 val = WSAStartup(0x2020, &wd);
292 if (val) // Request Winsock 2.2 for IPv6.
293 throw new SocketOSException("Unable to initialize socket library", val);
295 // These functions may not be present on older Windows versions.
296 // See the comment in InternetAddress.toHostNameString() for details.
297 auto ws2Lib = GetModuleHandleA("ws2_32.dll");
300 getnameinfoPointer = cast(typeof(getnameinfoPointer))
301 GetProcAddress(ws2Lib, "getnameinfo");
302 getaddrinfoPointer = cast(typeof(getaddrinfoPointer))
303 GetProcAddress(ws2Lib, "getaddrinfo");
304 freeaddrinfoPointer = cast(typeof(freeaddrinfoPointer))
305 GetProcAddress(ws2Lib, "freeaddrinfo");
310 getnameinfoPointer = &getnameinfo;
311 getaddrinfoPointer = &getaddrinfo;
312 freeaddrinfoPointer = &freeaddrinfo;
317 shared static ~this() @system nothrow @nogc
326 * The communication domain used to resolve an address.
328 enum AddressFamily: ushort
330 UNSPEC = AF_UNSPEC, /// Unspecified address family
331 UNIX = AF_UNIX, /// Local communication
332 INET = AF_INET, /// Internet Protocol version 4
333 IPX = AF_IPX, /// Novell IPX
334 APPLETALK = AF_APPLETALK, /// AppleTalk
335 INET6 = AF_INET6, /// Internet Protocol version 6
340 * Communication semantics
344 STREAM = SOCK_STREAM, /// Sequenced, reliable, two-way communication-based byte streams
345 DGRAM = SOCK_DGRAM, /// Connectionless, unreliable datagrams with a fixed maximum length; data may be lost or arrive out of order
346 RAW = SOCK_RAW, /// Raw protocol access
347 RDM = SOCK_RDM, /// Reliably-delivered message datagrams
348 SEQPACKET = SOCK_SEQPACKET, /// Sequenced, reliable, two-way connection-based datagrams with a fixed maximum length
355 enum ProtocolType: int
357 IP = IPPROTO_IP, /// Internet Protocol version 4
358 ICMP = IPPROTO_ICMP, /// Internet Control Message Protocol
359 IGMP = IPPROTO_IGMP, /// Internet Group Management Protocol
360 GGP = IPPROTO_GGP, /// Gateway to Gateway Protocol
361 TCP = IPPROTO_TCP, /// Transmission Control Protocol
362 PUP = IPPROTO_PUP, /// PARC Universal Packet Protocol
363 UDP = IPPROTO_UDP, /// User Datagram Protocol
364 IDP = IPPROTO_IDP, /// Xerox NS protocol
365 RAW = IPPROTO_RAW, /// Raw IP packets
366 IPV6 = IPPROTO_IPV6, /// Internet Protocol version 6
371 * `Protocol` is a class for retrieving protocol information.
375 * auto proto = new Protocol;
376 * writeln("About protocol TCP:");
377 * if (proto.getProtocolByType(ProtocolType.TCP))
379 * writefln(" Name: %s", proto.name);
380 * foreach (string s; proto.aliases)
381 * writefln(" Alias: %s", s);
384 * writeln(" No information found");
389 /// These members are populated when one of the following functions are called successfully:
391 string name; /// ditto
392 string[] aliases; /// ditto
395 void populate(protoent* proto) @system pure nothrow
397 type = cast(ProtocolType) proto.p_proto;
398 name = to!string(proto.p_name);
403 if (!proto.p_aliases[i])
409 aliases = new string[i];
410 for (i = 0; i != aliases.length; i++)
413 to!string(proto.p_aliases[i]);
422 /** Returns: false on failure */
423 bool getProtocolByName(scope const(char)[] name) @trusted nothrow
426 proto = getprotobyname(name.tempCString());
434 /** Returns: false on failure */
435 // Same as getprotobynumber().
436 bool getProtocolByType(ProtocolType type) @trusted nothrow
439 proto = getprotobynumber(type);
448 // Skip this test on Android because getprotobyname/number are
449 // unimplemented in bionic.
450 version (CRuntime_Bionic) {} else
453 // import std.stdio : writefln;
455 Protocol proto = new Protocol;
456 assert(proto.getProtocolByType(ProtocolType.TCP));
457 //writeln("About protocol TCP:");
458 //writefln("\tName: %s", proto.name);
459 // foreach (string s; proto.aliases)
461 // writefln("\tAlias: %s", s);
463 assert(proto.name == "tcp");
464 assert(proto.aliases.length == 1 && proto.aliases[0] == "TCP");
470 * `Service` is a class for retrieving service information.
474 * auto serv = new Service;
475 * writeln("About service epmap:");
476 * if (serv.getServiceByName("epmap", "tcp"))
478 * writefln(" Service: %s", serv.name);
479 * writefln(" Port: %d", serv.port);
480 * writefln(" Protocol: %s", serv.protocolName);
481 * foreach (string s; serv.aliases)
482 * writefln(" Alias: %s", s);
485 * writefln(" No service for epmap.");
490 /// These members are populated when one of the following functions are called successfully:
492 string[] aliases; /// ditto
493 ushort port; /// ditto
494 string protocolName; /// ditto
497 void populate(servent* serv) @system pure nothrow
499 name = to!string(serv.s_name);
500 port = ntohs(cast(ushort) serv.s_port);
501 protocolName = to!string(serv.s_proto);
506 if (!serv.s_aliases[i])
512 aliases = new string[i];
513 for (i = 0; i != aliases.length; i++)
516 to!string(serv.s_aliases[i]);
526 * If a protocol name is omitted, any protocol will be matched.
527 * Returns: false on failure.
529 bool getServiceByName(scope const(char)[] name, scope const(char)[] protocolName = null) @trusted nothrow
532 serv = getservbyname(name.tempCString(), protocolName.tempCString());
541 bool getServiceByPort(ushort port, scope const(char)[] protocolName = null) @trusted nothrow
544 serv = getservbyport(port, protocolName.tempCString());
555 import std.stdio : writefln;
557 Service serv = new Service;
558 if (serv.getServiceByName("epmap", "tcp"))
560 // writefln("About service epmap:");
561 // writefln("\tService: %s", serv.name);
562 // writefln("\tPort: %d", serv.port);
563 // writefln("\tProtocol: %s", serv.protocolName);
564 // foreach (string s; serv.aliases)
566 // writefln("\tAlias: %s", s);
568 // For reasons unknown this is loc-srv on Wine and epmap on Windows
569 assert(serv.name == "loc-srv" || serv.name == "epmap", serv.name);
570 assert(serv.port == 135);
571 assert(serv.protocolName == "tcp");
575 writefln("No service for epmap.");
581 private mixin template socketOSExceptionCtors()
584 this(string msg, string file = __FILE__, size_t line = __LINE__,
585 Throwable next = null, int err = _lasterr())
587 super(msg, file, line, next, err);
591 this(string msg, Throwable next, string file = __FILE__,
592 size_t line = __LINE__, int err = _lasterr())
594 super(msg, next, file, line, err);
598 this(string msg, int err, string file = __FILE__, size_t line = __LINE__,
599 Throwable next = null)
601 super(msg, next, file, line, err);
607 * Class for exceptions thrown from an `InternetHost`.
609 class HostException: SocketOSException
611 mixin socketOSExceptionCtors;
615 * `InternetHost` is a class for resolving IPv4 addresses.
617 * Consider using `getAddress`, `parseAddress` and `Address` methods
618 * instead of using this class directly.
622 /// These members are populated when one of the following functions are called successfully:
624 string[] aliases; /// ditto
625 uint[] addrList; /// ditto
628 void validHostent(in hostent* he)
630 if (he.h_addrtype != cast(int) AddressFamily.INET || he.h_length != 4)
631 throw new HostException("Address family mismatch");
635 void populate(hostent* he) @system pure nothrow
640 name = to!string(he.h_name);
651 aliases = new string[i];
652 for (i = 0; i != aliases.length; i++)
655 to!string(he.h_aliases[i]);
665 p = he.h_addr_list[i];
672 addrList = new uint[i];
673 for (i = 0; i != addrList.length; i++)
675 addrList[i] = ntohl(*(cast(uint*) he.h_addr_list[i]));
684 private bool getHostNoSync(string opMixin, T)(T param) @system
695 alias getHost = getHostNoSync;
698 // posix systems use global state for return value, so we
699 // must synchronize across all threads
700 private bool getHost(string opMixin, T)(T param) @system
702 synchronized(this.classinfo)
703 return getHostNoSync!(opMixin, T)(param);
709 * Returns: false if unable to resolve.
711 bool getHostByName(scope const(char)[] name) @trusted
713 static if (is(typeof(gethostbyname_r)))
715 return getHostNoSync!q{
718 ubyte[256] buffer_v = void;
719 auto buffer = buffer_v[];
720 auto param_zTmp = param.tempCString();
725 if (gethostbyname_r(param_zTmp, he, buffer.ptr, buffer.length, &he, &errno) == ERANGE)
726 buffer.length = buffer.length * 2;
735 auto he = gethostbyname(param.tempCString());
741 * Resolve IPv4 address number.
744 * addr = The IPv4 address to resolve, in host byte order.
746 * false if unable to resolve.
748 bool getHostByAddr(uint addr) @trusted
751 auto x = htonl(param);
752 auto he = gethostbyaddr(&x, 4, cast(int) AddressFamily.INET);
757 * Same as previous, but addr is an IPv4 address string in the
758 * dotted-decimal form $(I a.b.c.d).
759 * Returns: false if unable to resolve.
761 bool getHostByAddr(scope const(char)[] addr) @trusted
764 auto x = inet_addr(param.tempCString());
765 enforce(x != INADDR_NONE,
766 new SocketParameterException("Invalid IPv4 address"));
767 auto he = gethostbyaddr(&x, 4, cast(int) AddressFamily.INET);
775 InternetHost ih = new InternetHost;
777 ih.getHostByAddr(0x7F_00_00_01);
778 assert(ih.addrList[0] == 0x7F_00_00_01);
779 ih.getHostByAddr("127.0.0.1");
780 assert(ih.addrList[0] == 0x7F_00_00_01);
782 if (!ih.getHostByName("www.digitalmars.com"))
783 return; // don't fail if not connected to internet
785 assert(ih.addrList.length);
786 InternetAddress ia = new InternetAddress(ih.addrList[0], InternetAddress.PORT_ANY);
787 assert(ih.name == "www.digitalmars.com" || ih.name == "digitalmars.com",
790 assert(ih.getHostByAddr(ih.addrList[0]));
791 string getHostNameFromInt = ih.name.dup;
793 assert(ih.getHostByAddr(ia.toAddrString()));
794 string getHostNameFromStr = ih.name.dup;
796 assert(getHostNameFromInt == getHostNameFromStr);
800 /// Holds information about a socket _address retrieved by `getAddressInfo`.
803 AddressFamily family; /// Address _family
804 SocketType type; /// Socket _type
805 ProtocolType protocol; /// Protocol
806 Address address; /// Socket _address
807 string canonicalName; /// Canonical name, when `AddressInfoFlags.CANONNAME` is used.
811 * A subset of flags supported on all platforms with getaddrinfo.
812 * Specifies option flags for `getAddressInfo`.
814 enum AddressInfoFlags: int
816 /// The resulting addresses will be used in a call to `Socket.bind`.
817 PASSIVE = AI_PASSIVE,
819 /// The canonical name is returned in `canonicalName` member in the first `AddressInfo`.
820 CANONNAME = AI_CANONNAME,
823 * The `node` parameter passed to `getAddressInfo` must be a numeric string.
824 * This will suppress any potentially lengthy network host address lookups.
826 NUMERICHOST = AI_NUMERICHOST,
831 * On POSIX, getaddrinfo uses its own error codes, and thus has its own
832 * formatting function.
834 private string formatGaiError(int err) @trusted
838 return sysErrorString(err);
843 return to!string(gai_strerror(err));
848 * Provides _protocol-independent translation from host names to socket
849 * addresses. If advanced functionality is not required, consider using
850 * `getAddress` for compatibility with older systems.
852 * Returns: Array with one `AddressInfo` per socket address.
854 * Throws: `SocketOSException` on failure, or `SocketFeatureException`
855 * if this functionality is not available on the current system.
858 * node = string containing host name or numeric address
859 * options = optional additional parameters, identified by type:
860 * $(UL $(LI `string` - service name or port number)
861 * $(LI `AddressInfoFlags` - option flags)
862 * $(LI `AddressFamily` - address family to filter by)
863 * $(LI `SocketType` - socket type to filter by)
864 * $(LI `ProtocolType` - protocol to filter by))
868 * // Roundtrip DNS resolution
869 * auto results = getAddressInfo("www.digitalmars.com");
870 * assert(results[0].address.toHostNameString() ==
871 * "digitalmars.com");
874 * results = getAddressInfo("www.digitalmars.com",
875 * AddressInfoFlags.CANONNAME);
876 * assert(results[0].canonicalName == "digitalmars.com");
879 * results = getAddressInfo("ipv6.google.com");
880 * assert(results[0].family == AddressFamily.INET6);
882 * // Multihomed resolution
883 * results = getAddressInfo("google.com");
884 * assert(results.length > 1);
887 * results = getAddressInfo("127.0.0.1",
888 * AddressInfoFlags.NUMERICHOST);
889 * assert(results.length && results[0].family ==
890 * AddressFamily.INET);
893 * results = getAddressInfo("::1",
894 * AddressInfoFlags.NUMERICHOST);
895 * assert(results.length && results[0].family ==
896 * AddressFamily.INET6);
899 AddressInfo[] getAddressInfo(T...)(scope const(char)[] node, scope T options)
901 const(char)[] service = null;
903 hints.ai_family = AF_UNSPEC;
905 foreach (i, option; options)
907 static if (is(typeof(option) : const(char)[]))
908 service = options[i];
910 static if (is(typeof(option) == AddressInfoFlags))
911 hints.ai_flags |= option;
913 static if (is(typeof(option) == AddressFamily))
914 hints.ai_family = option;
916 static if (is(typeof(option) == SocketType))
917 hints.ai_socktype = option;
919 static if (is(typeof(option) == ProtocolType))
920 hints.ai_protocol = option;
922 static assert(0, "Unknown getAddressInfo option type: " ~ typeof(option).stringof);
925 return () @trusted { return getAddressInfoImpl(node, service, &hints); }();
932 const(char[]) breakSafety()
934 *cast(int*) 0xcafebabe = 0xdeadbeef;
937 alias breakSafety this;
939 assert(!__traits(compiles, () {
940 getAddressInfo("", Oops.init);
941 }), "getAddressInfo breaks @safe");
944 private AddressInfo[] getAddressInfoImpl(scope const(char)[] node, scope const(char)[] service, addrinfo* hints) @system
946 import std.array : appender;
948 if (getaddrinfoPointer && freeaddrinfoPointer)
952 int ret = getaddrinfoPointer(
954 service.tempCString(),
956 enforce(ret == 0, new SocketOSException("getaddrinfo error", ret, &formatGaiError));
957 scope(exit) freeaddrinfoPointer(ai_res);
959 auto result = appender!(AddressInfo[])();
961 // Use const to force UnknownAddressReference to copy the sockaddr.
962 for (const(addrinfo)* ai = ai_res; ai; ai = ai.ai_next)
963 result ~= AddressInfo(
964 cast(AddressFamily) ai.ai_family,
965 cast(SocketType ) ai.ai_socktype,
966 cast(ProtocolType ) ai.ai_protocol,
967 new UnknownAddressReference(ai.ai_addr, cast(socklen_t) ai.ai_addrlen),
968 ai.ai_canonname ? to!string(ai.ai_canonname) : null);
970 assert(result.data.length > 0);
974 throw new SocketFeatureException("Address info lookup is not available " ~
982 if (getaddrinfoPointer)
984 // Roundtrip DNS resolution
985 auto results = getAddressInfo("www.digitalmars.com");
986 assert(results[0].address.toHostNameString() == "digitalmars.com");
989 results = getAddressInfo("www.digitalmars.com",
990 AddressInfoFlags.CANONNAME);
991 assert(results[0].canonicalName == "digitalmars.com");
994 //results = getAddressInfo("ipv6.google.com");
995 //assert(results[0].family == AddressFamily.INET6);
997 // Multihomed resolution
998 //results = getAddressInfo("google.com");
999 //assert(results.length > 1);
1002 results = getAddressInfo("127.0.0.1", AddressInfoFlags.NUMERICHOST);
1003 assert(results.length && results[0].family == AddressFamily.INET);
1006 results = getAddressInfo("::1", AddressInfoFlags.NUMERICHOST);
1007 assert(results.length && results[0].family == AddressFamily.INET6);
1011 if (getaddrinfoPointer)
1013 auto results = getAddressInfo(null, "1234", AddressInfoFlags.PASSIVE,
1014 SocketType.STREAM, ProtocolType.TCP, AddressFamily.INET);
1015 assert(results.length == 1 && results[0].address.toString() == "0.0.0.0:1234");
1020 private ushort serviceToPort(scope const(char)[] service)
1023 return InternetAddress.PORT_ANY;
1025 if (isNumeric(service))
1026 return to!ushort(service);
1029 auto s = new Service();
1030 s.getServiceByName(service);
1036 * Provides _protocol-independent translation from host names to socket
1037 * addresses. Uses `getAddressInfo` if the current system supports it,
1038 * and `InternetHost` otherwise.
1040 * Returns: Array with one `Address` instance per socket address.
1042 * Throws: `SocketOSException` on failure.
1046 * writeln("Resolving www.digitalmars.com:");
1049 * auto addresses = getAddress("www.digitalmars.com");
1050 * foreach (address; addresses)
1051 * writefln(" IP: %s", address.toAddrString());
1053 * catch (SocketException e)
1054 * writefln(" Lookup failed: %s", e.msg);
1057 Address[] getAddress(scope const(char)[] hostname, scope const(char)[] service = null)
1059 if (getaddrinfoPointer && freeaddrinfoPointer)
1061 // use getAddressInfo
1062 auto infos = getAddressInfo(hostname, service);
1064 results.length = infos.length;
1065 foreach (i, ref result; results)
1066 result = infos[i].address;
1070 return getAddress(hostname, serviceToPort(service));
1074 Address[] getAddress(scope const(char)[] hostname, ushort port)
1076 if (getaddrinfoPointer && freeaddrinfoPointer)
1077 return getAddress(hostname, to!string(port));
1080 // use getHostByName
1081 auto ih = new InternetHost;
1082 if (!ih.getHostByName(hostname))
1083 throw new AddressException(
1084 text("Unable to resolve host '", hostname, "'"));
1087 foreach (uint addr; ih.addrList)
1088 results ~= new InternetAddress(addr, port);
1097 auto addresses = getAddress("63.105.9.61");
1098 assert(addresses.length && addresses[0].toAddrString() == "63.105.9.61");
1100 if (getaddrinfoPointer)
1102 // test via gethostbyname
1103 auto getaddrinfoPointerBackup = getaddrinfoPointer;
1104 cast() getaddrinfoPointer = null;
1105 scope(exit) cast() getaddrinfoPointer = getaddrinfoPointerBackup;
1107 addresses = getAddress("63.105.9.61");
1108 assert(addresses.length && addresses[0].toAddrString() == "63.105.9.61");
1115 * Provides _protocol-independent parsing of network addresses. Does not
1116 * attempt name resolution. Uses `getAddressInfo` with
1117 * `AddressInfoFlags.NUMERICHOST` if the current system supports it, and
1118 * `InternetAddress` otherwise.
1120 * Returns: An `Address` instance representing specified address.
1122 * Throws: `SocketException` on failure.
1126 * writeln("Enter IP address:");
1127 * string ip = readln().chomp();
1130 * Address address = parseAddress(ip);
1131 * writefln("Looking up reverse of %s:",
1132 * address.toAddrString());
1135 * string reverse = address.toHostNameString();
1137 * writefln(" Reverse name: %s", reverse);
1139 * writeln(" Reverse hostname not found.");
1141 * catch (SocketException e)
1142 * writefln(" Lookup error: %s", e.msg);
1144 * catch (SocketException e)
1146 * writefln(" %s is not a valid IP address: %s",
1151 Address parseAddress(scope const(char)[] hostaddr, scope const(char)[] service = null)
1153 if (getaddrinfoPointer && freeaddrinfoPointer)
1154 return getAddressInfo(hostaddr, service, AddressInfoFlags.NUMERICHOST)[0].address;
1156 return parseAddress(hostaddr, serviceToPort(service));
1160 Address parseAddress(scope const(char)[] hostaddr, ushort port)
1162 if (getaddrinfoPointer && freeaddrinfoPointer)
1163 return parseAddress(hostaddr, to!string(port));
1166 auto in4_addr = InternetAddress.parse(hostaddr);
1167 enforce(in4_addr != InternetAddress.ADDR_NONE,
1168 new SocketParameterException("Invalid IP address"));
1169 return new InternetAddress(in4_addr, port);
1177 auto address = parseAddress("63.105.9.61");
1178 assert(address.toAddrString() == "63.105.9.61");
1180 if (getaddrinfoPointer)
1182 // test via inet_addr
1183 auto getaddrinfoPointerBackup = getaddrinfoPointer;
1184 cast() getaddrinfoPointer = null;
1185 scope(exit) cast() getaddrinfoPointer = getaddrinfoPointerBackup;
1187 address = parseAddress("63.105.9.61");
1188 assert(address.toAddrString() == "63.105.9.61");
1191 assert(collectException!SocketException(parseAddress("Invalid IP address")));
1197 * Class for exceptions thrown from an `Address`.
1199 class AddressException: SocketOSException
1201 mixin socketOSExceptionCtors;
1206 * `Address` is an abstract class for representing a socket addresses.
1210 * writeln("About www.google.com port 80:");
1213 * Address[] addresses = getAddress("www.google.com", 80);
1214 * writefln(" %d addresses found.", addresses.length);
1215 * foreach (int i, Address a; addresses)
1217 * writefln(" Address %d:", i+1);
1218 * writefln(" IP address: %s", a.toAddrString());
1219 * writefln(" Hostname: %s", a.toHostNameString());
1220 * writefln(" Port: %s", a.toPortString());
1221 * writefln(" Service name: %s",
1222 * a.toServiceNameString());
1225 * catch (SocketException e)
1226 * writefln(" Lookup error: %s", e.msg);
1229 abstract class Address
1231 /// Returns pointer to underlying `sockaddr` structure.
1232 abstract @property sockaddr* name() pure nothrow @nogc;
1233 abstract @property const(sockaddr)* name() const pure nothrow @nogc; /// ditto
1235 /// Returns actual size of underlying `sockaddr` structure.
1236 abstract @property socklen_t nameLen() const pure nothrow @nogc;
1238 // Socket.remoteAddress, Socket.localAddress, and Socket.receiveFrom
1239 // use setNameLen to set the actual size of the address as returned by
1240 // getsockname, getpeername, and recvfrom, respectively.
1241 // The following implementation is sufficient for fixed-length addresses,
1242 // and ensures that the length is not changed.
1243 // Must be overridden for variable-length addresses.
1244 protected void setNameLen(socklen_t len)
1246 if (len != this.nameLen)
1247 throw new AddressException(
1248 format("%s expects address of length %d, not %d", typeid(this),
1249 this.nameLen, len), 0);
1252 /// Family of this address.
1253 @property AddressFamily addressFamily() const pure nothrow @nogc
1255 return cast(AddressFamily) name.sa_family;
1258 // Common code for toAddrString and toHostNameString
1259 private string toHostString(bool numeric) @trusted const
1261 // getnameinfo() is the recommended way to perform a reverse (name)
1262 // lookup on both Posix and Windows. However, it is only available
1263 // on Windows XP and above, and not included with the WinSock import
1264 // libraries shipped with DMD. Thus, we check for getnameinfo at
1265 // runtime in the shared module constructor, and use it if it's
1266 // available in the base class method. Classes for specific network
1267 // families (e.g. InternetHost) override this method and use a
1268 // deprecated, albeit commonly-available method when getnameinfo()
1269 // is not available.
1270 // http://technet.microsoft.com/en-us/library/aa450403.aspx
1271 if (getnameinfoPointer)
1273 auto buf = new char[NI_MAXHOST];
1274 auto ret = getnameinfoPointer(
1276 buf.ptr, cast(uint) buf.length,
1278 numeric ? NI_NUMERICHOST : NI_NAMEREQD);
1282 if (ret == EAI_NONAME)
1285 if (ret == WSANO_DATA)
1289 enforce(ret == 0, new AddressException("Could not get " ~
1290 (numeric ? "host address" : "host name")));
1291 return assumeUnique(buf[0 .. strlen(buf.ptr)]);
1294 throw new SocketFeatureException((numeric ? "Host address" : "Host name") ~
1295 " lookup for this address family is not available on this system.");
1298 // Common code for toPortString and toServiceNameString
1299 private string toServiceString(bool numeric) @trusted const
1301 // See toHostNameString() for details about getnameinfo().
1302 if (getnameinfoPointer)
1304 auto buf = new char[NI_MAXSERV];
1305 enforce(getnameinfoPointer(
1308 buf.ptr, cast(uint) buf.length,
1309 numeric ? NI_NUMERICSERV : NI_NAMEREQD
1310 ) == 0, new AddressException("Could not get " ~
1311 (numeric ? "port number" : "service name")));
1312 return assumeUnique(buf[0 .. strlen(buf.ptr)]);
1315 throw new SocketFeatureException((numeric ? "Port number" : "Service name") ~
1316 " lookup for this address family is not available on this system.");
1320 * Attempts to retrieve the host address as a human-readable string.
1322 * Throws: `AddressException` on failure, or `SocketFeatureException`
1323 * if address retrieval for this address family is not available on the
1326 string toAddrString() const
1328 return toHostString(true);
1332 * Attempts to retrieve the host name as a fully qualified domain name.
1334 * Returns: The FQDN corresponding to this `Address`, or `null` if
1335 * the host name did not resolve.
1337 * Throws: `AddressException` on error, or `SocketFeatureException`
1338 * if host name lookup for this address family is not available on the
1341 string toHostNameString() const
1343 return toHostString(false);
1347 * Attempts to retrieve the numeric port number as a string.
1349 * Throws: `AddressException` on failure, or `SocketFeatureException`
1350 * if port number retrieval for this address family is not available on the
1353 string toPortString() const
1355 return toServiceString(true);
1359 * Attempts to retrieve the service name as a string.
1361 * Throws: `AddressException` on failure, or `SocketFeatureException`
1362 * if service name lookup for this address family is not available on the
1365 string toServiceNameString() const
1367 return toServiceString(false);
1370 /// Human readable string representing this address.
1371 override string toString() const
1375 string host = toAddrString();
1376 string port = toPortString();
1377 if (host.indexOf(':') >= 0)
1378 return "[" ~ host ~ "]:" ~ port;
1380 return host ~ ":" ~ port;
1382 catch (SocketException)
1388 * `UnknownAddress` encapsulates an unknown socket address.
1390 class UnknownAddress: Address
1397 override @property sockaddr* name() return
1402 override @property const(sockaddr)* name() const return
1408 override @property socklen_t nameLen() const
1410 return cast(socklen_t) sa.sizeof;
1417 * `UnknownAddressReference` encapsulates a reference to an arbitrary
1420 class UnknownAddressReference: Address
1427 /// Constructs an `Address` with a reference to the specified `sockaddr`.
1428 this(sockaddr* sa, socklen_t len) pure nothrow @nogc
1434 /// Constructs an `Address` with a copy of the specified `sockaddr`.
1435 this(const(sockaddr)* sa, socklen_t len) @system pure nothrow
1437 this.sa = cast(sockaddr*) (cast(ubyte*) sa)[0 .. len].dup.ptr;
1441 override @property sockaddr* name()
1446 override @property const(sockaddr)* name() const
1452 override @property socklen_t nameLen() const
1454 return cast(socklen_t) len;
1460 * `InternetAddress` encapsulates an IPv4 (Internet Protocol version 4)
1463 * Consider using `getAddress`, `parseAddress` and `Address` methods
1464 * instead of using this class directly.
1466 class InternetAddress: Address
1472 this() pure nothrow @nogc
1478 override @property sockaddr* name() return
1480 return cast(sockaddr*)&sin;
1483 override @property const(sockaddr)* name() const return
1485 return cast(const(sockaddr)*)&sin;
1489 override @property socklen_t nameLen() const
1491 return cast(socklen_t) sin.sizeof;
1495 enum uint ADDR_ANY = INADDR_ANY; /// Any IPv4 host address.
1496 enum uint ADDR_NONE = INADDR_NONE; /// An invalid IPv4 host address.
1497 enum ushort PORT_ANY = 0; /// Any IPv4 port number.
1499 /// Returns the IPv4 _port number (in host byte order).
1500 @property ushort port() const pure nothrow @nogc
1502 return ntohs(sin.sin_port);
1505 /// Returns the IPv4 address number (in host byte order).
1506 @property uint addr() const pure nothrow @nogc
1508 return ntohl(sin.sin_addr.s_addr);
1512 * Construct a new `InternetAddress`.
1514 * addr = an IPv4 address string in the dotted-decimal form a.b.c.d,
1515 * or a host name which will be resolved using an `InternetHost`
1517 * port = port number, may be `PORT_ANY`.
1519 this(scope const(char)[] addr, ushort port)
1521 uint uiaddr = parse(addr);
1522 if (ADDR_NONE == uiaddr)
1524 InternetHost ih = new InternetHost;
1525 if (!ih.getHostByName(addr))
1526 //throw new AddressException("Invalid internet address");
1527 throw new AddressException(
1528 text("Unable to resolve host '", addr, "'"));
1529 uiaddr = ih.addrList[0];
1531 sin.sin_family = AddressFamily.INET;
1532 sin.sin_addr.s_addr = htonl(uiaddr);
1533 sin.sin_port = htons(port);
1537 * Construct a new `InternetAddress`.
1539 * addr = (optional) an IPv4 address in host byte order, may be `ADDR_ANY`.
1540 * port = port number, may be `PORT_ANY`.
1542 this(uint addr, ushort port) pure nothrow @nogc
1544 sin.sin_family = AddressFamily.INET;
1545 sin.sin_addr.s_addr = htonl(addr);
1546 sin.sin_port = htons(port);
1550 this(ushort port) pure nothrow @nogc
1552 sin.sin_family = AddressFamily.INET;
1553 sin.sin_addr.s_addr = ADDR_ANY;
1554 sin.sin_port = htons(port);
1558 * Construct a new `InternetAddress`.
1560 * addr = A sockaddr_in as obtained from lower-level API calls such as getifaddrs.
1562 this(sockaddr_in addr) pure nothrow @nogc
1564 assert(addr.sin_family == AddressFamily.INET, "Socket address is not of INET family.");
1568 /// Human readable string representing the IPv4 address in dotted-decimal form.
1569 override string toAddrString() @trusted const
1571 return to!string(inet_ntoa(sin.sin_addr));
1574 /// Human readable string representing the IPv4 port.
1575 override string toPortString() const
1577 return std.conv.to!string(port);
1581 * Attempts to retrieve the host name as a fully qualified domain name.
1583 * Returns: The FQDN corresponding to this `InternetAddress`, or
1584 * `null` if the host name did not resolve.
1586 * Throws: `AddressException` on error.
1588 override string toHostNameString() const
1590 // getnameinfo() is the recommended way to perform a reverse (name)
1591 // lookup on both Posix and Windows. However, it is only available
1592 // on Windows XP and above, and not included with the WinSock import
1593 // libraries shipped with DMD. Thus, we check for getnameinfo at
1594 // runtime in the shared module constructor, and fall back to the
1595 // deprecated getHostByAddr() if it could not be found. See also:
1596 // http://technet.microsoft.com/en-us/library/aa450403.aspx
1598 if (getnameinfoPointer)
1599 return super.toHostNameString();
1602 auto host = new InternetHost();
1603 if (!host.getHostByAddr(ntohl(sin.sin_addr.s_addr)))
1610 * Compares with another InternetAddress of same type for equality
1611 * Returns: true if the InternetAddresses share the same address and
1614 override bool opEquals(Object o) const
1616 auto other = cast(InternetAddress) o;
1617 return other && this.sin.sin_addr.s_addr == other.sin.sin_addr.s_addr &&
1618 this.sin.sin_port == other.sin.sin_port;
1624 auto addr1 = new InternetAddress("127.0.0.1", 80);
1625 auto addr2 = new InternetAddress("127.0.0.2", 80);
1627 assert(addr1 == addr1);
1628 assert(addr1 != addr2);
1632 * Parse an IPv4 address string in the dotted-decimal form $(I a.b.c.d)
1633 * and return the number.
1634 * Returns: If the string is not a legitimate IPv4 address,
1635 * `ADDR_NONE` is returned.
1637 static uint parse(scope const(char)[] addr) @trusted nothrow
1639 return ntohl(inet_addr(addr.tempCString()));
1643 * Convert an IPv4 address number in host byte order to a human readable
1644 * string representing the IPv4 address in dotted-decimal form.
1646 static string addrToString(uint addr) @trusted nothrow
1649 sin_addr.s_addr = htonl(addr);
1650 return to!string(inet_ntoa(sin_addr));
1658 const InternetAddress ia = new InternetAddress("63.105.9.61", 80);
1659 assert(ia.toString() == "63.105.9.61:80");
1663 // test construction from a sockaddr_in
1666 sin.sin_addr.s_addr = htonl(0x7F_00_00_01); // 127.0.0.1
1667 sin.sin_family = AddressFamily.INET;
1668 sin.sin_port = htons(80);
1670 const InternetAddress ia = new InternetAddress(sin);
1671 assert(ia.toString() == "127.0.0.1:80");
1675 // test reverse lookup
1676 auto ih = new InternetHost;
1677 if (ih.getHostByName("digitalmars.com"))
1679 const ia = new InternetAddress(ih.addrList[0], 80);
1680 assert(ia.toHostNameString() == "digitalmars.com");
1682 if (getnameinfoPointer)
1684 // test reverse lookup, via gethostbyaddr
1685 auto getnameinfoPointerBackup = getnameinfoPointer;
1686 cast() getnameinfoPointer = null;
1687 scope(exit) cast() getnameinfoPointer = getnameinfoPointerBackup;
1689 assert(ia.toHostNameString() == "digitalmars.com");
1696 // test failing reverse lookup
1697 const InternetAddress ia = new InternetAddress("255.255.255.255", 80);
1698 assert(ia.toHostNameString() is null);
1700 if (getnameinfoPointer)
1702 // test failing reverse lookup, via gethostbyaddr
1703 auto getnameinfoPointerBackup = getnameinfoPointer;
1704 cast() getnameinfoPointer = null;
1705 scope(exit) cast() getnameinfoPointer = getnameinfoPointerBackup;
1707 assert(ia.toHostNameString() is null);
1714 * `Internet6Address` encapsulates an IPv6 (Internet Protocol version 6)
1717 * Consider using `getAddress`, `parseAddress` and `Address` methods
1718 * instead of using this class directly.
1720 class Internet6Address: Address
1726 this() pure nothrow @nogc
1732 override @property sockaddr* name() return
1734 return cast(sockaddr*)&sin6;
1737 override @property const(sockaddr)* name() const return
1739 return cast(const(sockaddr)*)&sin6;
1743 override @property socklen_t nameLen() const
1745 return cast(socklen_t) sin6.sizeof;
1749 /// Any IPv6 host address.
1750 static @property ref const(ubyte)[16] ADDR_ANY() pure nothrow @nogc
1752 static if (is(typeof(IN6ADDR_ANY)))
1756 static immutable addr = IN6ADDR_ANY.s6_addr;
1760 return IN6ADDR_ANY.s6_addr;
1762 else static if (is(typeof(in6addr_any)))
1764 return in6addr_any.s6_addr;
1770 /// Any IPv6 port number.
1771 enum ushort PORT_ANY = 0;
1773 /// Returns the IPv6 port number.
1774 @property ushort port() const pure nothrow @nogc
1776 return ntohs(sin6.sin6_port);
1779 /// Returns the IPv6 address.
1780 @property ubyte[16] addr() const pure nothrow @nogc
1782 return sin6.sin6_addr.s6_addr;
1786 * Construct a new `Internet6Address`.
1788 * addr = an IPv6 host address string in the form described in RFC 2373,
1789 * or a host name which will be resolved using `getAddressInfo`.
1790 * service = (optional) service name.
1792 this(scope const(char)[] addr, scope const(char)[] service = null) @trusted
1794 auto results = getAddressInfo(addr, service, AddressFamily.INET6);
1795 assert(results.length && results[0].family == AddressFamily.INET6);
1796 sin6 = *cast(sockaddr_in6*) results[0].address.name;
1800 * Construct a new `Internet6Address`.
1802 * addr = an IPv6 host address string in the form described in RFC 2373,
1803 * or a host name which will be resolved using `getAddressInfo`.
1804 * port = port number, may be `PORT_ANY`.
1806 this(scope const(char)[] addr, ushort port)
1808 if (port == PORT_ANY)
1811 this(addr, to!string(port));
1815 * Construct a new `Internet6Address`.
1817 * addr = (optional) an IPv6 host address in host byte order, or
1819 * port = port number, may be `PORT_ANY`.
1821 this(ubyte[16] addr, ushort port) pure nothrow @nogc
1823 sin6.sin6_family = AddressFamily.INET6;
1824 sin6.sin6_addr.s6_addr = addr;
1825 sin6.sin6_port = htons(port);
1829 this(ushort port) pure nothrow @nogc
1831 sin6.sin6_family = AddressFamily.INET6;
1832 sin6.sin6_addr.s6_addr = ADDR_ANY;
1833 sin6.sin6_port = htons(port);
1837 * Construct a new `Internet6Address`.
1839 * addr = A sockaddr_in6 as obtained from lower-level API calls such as getifaddrs.
1841 this(sockaddr_in6 addr) pure nothrow @nogc
1843 assert(addr.sin6_family == AddressFamily.INET6);
1848 * Parse an IPv6 host address string as described in RFC 2373, and return the
1850 * Throws: `SocketException` on error.
1852 static ubyte[16] parse(scope const(char)[] addr) @trusted
1854 // Although we could use inet_pton here, it's only available on Windows
1855 // versions starting with Vista, so use getAddressInfo with NUMERICHOST
1857 auto results = getAddressInfo(addr, AddressInfoFlags.NUMERICHOST);
1858 if (results.length && results[0].family == AddressFamily.INET6)
1859 return (cast(sockaddr_in6*) results[0].address.name).sin6_addr.s6_addr;
1860 throw new AddressException("Not an IPv6 address", 0);
1868 const Internet6Address ia = new Internet6Address("::1", 80);
1869 assert(ia.toString() == "[::1]:80");
1873 // test construction from a sockaddr_in6
1876 sin.sin6_addr.s6_addr = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; // [::1]
1877 sin.sin6_family = AddressFamily.INET6;
1878 sin.sin6_port = htons(80);
1880 const Internet6Address ia = new Internet6Address(sin);
1881 assert(ia.toString() == "[::1]:80");
1888 static if (!is(sockaddr_un))
1890 // This exists only to allow the constructor taking
1891 // a sockaddr_un to be compilable for documentation
1892 // on platforms that don't supply a sockaddr_un.
1899 * `UnixAddress` encapsulates an address for a Unix domain socket
1900 * (`AF_UNIX`), i.e. a socket bound to a path name in the file system.
1901 * Available only on supported systems.
1903 * Linux also supports an abstract address namespace, in which addresses
1904 * are independent of the file system. A socket address is abstract
1905 * iff `path` starts with a _null byte (`'\0'`). Null bytes in other
1906 * positions of an abstract address are allowed and have no special
1911 * auto addr = new UnixAddress("/var/run/dbus/system_bus_socket");
1912 * auto abstractAddr = new UnixAddress("\0/tmp/dbus-OtHLWmCLPR");
1915 * See_Also: $(HTTP http://man7.org/linux/man-pages/man7/unix.7.html, UNIX(7))
1917 class UnixAddress: Address
1919 private this() pure nothrow @nogc {}
1921 /// Construct a new `UnixAddress` from the specified path.
1922 this(scope const(char)[] path) { }
1925 * Construct a new `UnixAddress`.
1927 * addr = A sockaddr_un as obtained from lower-level API calls.
1929 this(sockaddr_un addr) pure nothrow @nogc { }
1931 /// Get the underlying _path.
1932 @property string path() const { return null; }
1935 override string toString() const { return null; }
1937 override @property sockaddr* name() { return null; }
1938 override @property const(sockaddr)* name() const { return null; }
1939 override @property socklen_t nameLen() const { return 0; }
1943 static if (is(sockaddr_un))
1945 class UnixAddress: Address
1954 char unused = '\0'; // placeholder for a terminating '\0'
1957 this() pure nothrow @nogc
1959 sun.sun_family = AddressFamily.UNIX;
1961 _nameLen = sun.sizeof;
1964 override void setNameLen(socklen_t len) @trusted
1966 if (len > sun.sizeof)
1967 throw new SocketParameterException("Not enough socket address storage");
1972 override @property sockaddr* name() return
1974 return cast(sockaddr*)&sun;
1977 override @property const(sockaddr)* name() const return
1979 return cast(const(sockaddr)*)&sun;
1982 override @property socklen_t nameLen() @trusted const
1987 this(scope const(char)[] path) @trusted pure
1989 enforce(path.length <= sun.sun_path.sizeof, new SocketParameterException("Path too long"));
1990 sun.sun_family = AddressFamily.UNIX;
1991 sun.sun_path.ptr[0 .. path.length] = (cast(byte[]) path)[];
1992 _nameLen = cast(socklen_t)
1994 auto len = sockaddr_un.init.sun_path.offsetof + path.length;
1995 // Pathname socket address must be terminated with '\0'
1996 // which must be included in the address length.
1997 if (sun.sun_path.ptr[0])
1999 sun.sun_path.ptr[path.length] = 0;
2006 this(sockaddr_un addr) pure nothrow @nogc
2008 assert(addr.sun_family == AddressFamily.UNIX);
2012 @property string path() @trusted const pure
2014 auto len = _nameLen - sockaddr_un.init.sun_path.offsetof;
2016 return null; // An empty path may be returned from getpeername
2017 // For pathname socket address we need to strip off the terminating '\0'
2018 if (sun.sun_path.ptr[0])
2020 return (cast(const(char)*) sun.sun_path.ptr)[0 .. len].idup;
2023 override string toString() const pure
2031 import core.stdc.stdio : remove;
2033 version (iOSDerived)
2035 // Slightly different version of `std.file.deleteme` to reduce the path
2036 // length on iOS derived platforms. Due to the sandbox, the length
2037 // of paths can quickly become too long.
2038 static string deleteme()
2040 import std.conv : text;
2041 import std.process : thisProcessID;
2042 import std.file : tempDir;
2044 return text(tempDir, thisProcessID);
2049 import std.file : deleteme;
2051 immutable ubyte[] data = [1, 2, 3, 4];
2054 const basePath = deleteme;
2055 auto names = [ basePath ~ "-socket" ];
2057 names ~= "\0" ~ basePath ~ "-abstract\0unix\0socket";
2059 foreach (name; names)
2061 auto address = new UnixAddress(name);
2063 auto listener = new Socket(AddressFamily.UNIX, SocketType.STREAM);
2064 scope(exit) listener.close();
2065 listener.bind(address);
2066 scope(exit) () @trusted { if (name[0]) remove(name.tempCString()); } ();
2067 assert(listener.localAddress.toString == name);
2071 pair[0] = new Socket(AddressFamily.UNIX, SocketType.STREAM);
2072 scope(exit) listener.close();
2074 pair[0].connect(address);
2075 scope(exit) pair[0].close();
2077 pair[1] = listener.accept();
2078 scope(exit) pair[1].close();
2082 auto buf = new ubyte[data.length];
2083 pair[1].receive(buf);
2084 assert(buf == data);
2086 // getpeername is free to return an empty name for a unix
2087 // domain socket pair or unbound socket. Let's confirm it
2088 // returns successfully and doesn't throw anything.
2089 // See https://issues.dlang.org/show_bug.cgi?id=20544
2090 assertNotThrown(pair[1].remoteAddress().toString());
2097 * Class for exceptions thrown by `Socket.accept`.
2099 class SocketAcceptException: SocketOSException
2101 mixin socketOSExceptionCtors;
2104 /// How a socket is shutdown:
2105 enum SocketShutdown: int
2107 RECEIVE = SD_RECEIVE, /// socket receives are disallowed
2108 SEND = SD_SEND, /// socket sends are disallowed
2109 BOTH = SD_BOTH, /// both RECEIVE and SEND
2113 /// Flags may be OR'ed together:
2114 enum SocketFlags: int
2116 NONE = 0, /// no flags specified
2118 OOB = MSG_OOB, /// out-of-band stream data
2119 PEEK = MSG_PEEK, /// peek at incoming data without removing it from the queue, only for receiving
2120 DONTROUTE = MSG_DONTROUTE, /// data should not be subject to routing; this flag may be ignored. Only for sending
2124 private mixin template FieldProxy(string target, string field)
2127 @property typeof(`~target~`) `~field~`() const pure nothrow @nogc
2133 @property typeof(`~target~`) `~field~`(typeof(`~target~`) value) pure nothrow @nogc
2135 return `~target~` = value;
2141 /// Duration timeout value.
2145 alias tv_sec_t = typeof(ctimeval.tv_sec);
2146 alias tv_usec_t = typeof(ctimeval.tv_usec);
2148 version (StdDdoc) // no DDoc for string mixins, can't forward individual fields
2150 tv_sec_t seconds; /// Number of _seconds.
2151 tv_usec_t microseconds; /// Number of additional _microseconds.
2156 mixin FieldProxy!(`ctimeval.tv_sec`, `seconds`);
2157 mixin FieldProxy!(`ctimeval.tv_usec`, `microseconds`);
2163 * A collection of sockets for use with `Socket.select`.
2165 * `SocketSet` wraps the platform `fd_set` type. However, unlike
2166 * `fd_set`, `SocketSet` is not statically limited to `FD_SETSIZE`
2167 * or any other limit, and grows as needed.
2174 // On Windows, fd_set is an array of socket handles,
2175 // following a word containing the fd_set instance size.
2176 // We use one dynamic array for everything, and use its first
2177 // element(s) for the count.
2179 alias fd_set_count_type = typeof(fd_set.init.fd_count);
2180 alias fd_set_type = typeof(fd_set.init.fd_array[0]);
2181 static assert(fd_set_type.sizeof == socket_t.sizeof);
2183 // Number of fd_set_type elements at the start of our array that are
2184 // used for the socket count and alignment
2186 enum FD_SET_OFFSET = fd_set.fd_array.offsetof / fd_set_type.sizeof;
2187 static assert(FD_SET_OFFSET);
2188 static assert(fd_set.fd_count.offsetof % fd_set_type.sizeof == 0);
2192 void resize(size_t size) pure nothrow
2194 set.length = FD_SET_OFFSET + size;
2197 ref inout(fd_set_count_type) count() @trusted @property inout pure nothrow @nogc
2200 return *cast(inout(fd_set_count_type)*)set.ptr;
2203 size_t capacity() @property const pure nothrow @nogc
2205 return set.length - FD_SET_OFFSET;
2208 inout(socket_t)[] fds() @trusted inout @property pure nothrow @nogc
2210 return cast(inout(socket_t)[])set[FD_SET_OFFSET .. FD_SET_OFFSET+count];
2216 // On Posix, fd_set is a bit array. We assume that the fd_set
2217 // type (declared in core.sys.posix.sys.select) is a structure
2218 // containing a single field, a static array.
2220 static assert(fd_set.tupleof.length == 1);
2222 // This is the type used in the fd_set array.
2223 // Using the type of the correct size is important for big-endian
2226 alias fd_set_type = typeof(fd_set.init.tupleof[0][0]);
2228 // Number of file descriptors represented by one fd_set_type
2230 enum FD_NFDBITS = 8 * fd_set_type.sizeof;
2232 static fd_set_type mask(uint n) pure nothrow @nogc
2234 return (cast(fd_set_type) 1) << (n % FD_NFDBITS);
2237 // Array size to fit that many sockets
2239 static size_t lengthFor(size_t size) pure nothrow @nogc
2241 return (size + (FD_NFDBITS-1)) / FD_NFDBITS;
2246 void resize(size_t size) pure nothrow
2248 set.length = lengthFor(size);
2251 // Make sure we can fit that many sockets
2253 void setMinCapacity(size_t size) pure nothrow
2255 auto length = lengthFor(size);
2256 if (set.length < length)
2257 set.length = length;
2260 size_t capacity() @property const pure nothrow @nogc
2262 return set.length * FD_NFDBITS;
2268 static assert(false, "Unknown platform");
2273 * Create a SocketSet with a specific initial capacity (defaults to
2274 * `FD_SETSIZE`, the system's default capacity).
2276 this(size_t size = FD_SETSIZE) pure nothrow
2282 /// Reset the `SocketSet` so that there are 0 `Socket`s in the collection.
2283 void reset() pure nothrow @nogc
2295 void add(socket_t s) @trusted pure nothrow
2299 if (count == capacity)
2302 set.length = set.capacity;
2309 auto index = s / FD_NFDBITS;
2310 auto length = set.length;
2311 if (index >= length)
2313 while (index >= length)
2315 set.length = length;
2316 set.length = set.capacity;
2318 set[index] |= mask(s);
2325 * Add a `Socket` to the collection.
2326 * The socket must not already be in the collection.
2328 void add(Socket s) pure nothrow
2333 void remove(socket_t s) pure nothrow
2337 import std.algorithm.searching : countUntil;
2339 auto p = fds.countUntil(s);
2341 fds[p] = fds[--count];
2345 auto index = s / FD_NFDBITS;
2346 if (index >= set.length)
2348 set[index] &= ~mask(s);
2349 // note: adjusting maxfd would require scanning the set, not worth it
2355 * Remove this `Socket` from the collection.
2356 * Does nothing if the socket is not in the collection already.
2358 void remove(Socket s) pure nothrow
2363 int isSet(socket_t s) const pure nothrow @nogc
2367 import std.algorithm.searching : canFind;
2368 return fds.canFind(s) ? 1 : 0;
2374 auto index = s / FD_NFDBITS;
2375 return (set[index] & mask(s)) ? 1 : 0;
2380 /// Return nonzero if this `Socket` is in the collection.
2381 int isSet(Socket s) const pure nothrow @nogc
2383 return isSet(s.sock);
2389 * The current capacity of this `SocketSet`. The exact
2390 * meaning of the return value varies from platform to platform.
2393 * Since D 2.065, this value does not indicate a
2394 * restriction, and `SocketSet` will grow its capacity as
2395 * needed automatically.
2397 @property uint max() const pure nothrow @nogc
2399 return cast(uint) capacity;
2403 fd_set* toFd_set() @trusted pure nothrow @nogc
2405 return cast(fd_set*) set.ptr;
2409 int selectn() const pure nothrow @nogc
2415 else version (Posix)
2424 auto fds = cast(socket_t[])
2425 [cast(socket_t) 1, 2, 0, 1024, 17, 42, 1234, 77, 77+32, 77+64];
2426 auto set = new SocketSet();
2427 foreach (fd; fds) assert(!set.isSet(fd));
2428 foreach (fd; fds) set.add(fd);
2429 foreach (fd; fds) assert(set.isSet(fd));
2431 // Make sure SocketSet reimplements fd_set correctly
2432 auto fdset = set.toFd_set();
2433 foreach (fd; fds[0]..cast(socket_t)(fds[$-1]+1))
2434 assert(cast(bool) set.isSet(fd) == cast(bool)(() @trusted => FD_ISSET(fd, fdset))());
2438 assert(set.isSet(fd));
2440 assert(!set.isSet(fd));
2446 version (iOSDerived)
2461 static assert(LIMIT > PAIRS*2);
2462 import core.sys.posix.sys.resource;
2464 getrlimit(RLIMIT_NOFILE, &fileLimit);
2465 assert(fileLimit.rlim_max > LIMIT, "Open file hard limit too low");
2466 fileLimit.rlim_cur = LIMIT;
2467 setrlimit(RLIMIT_NOFILE, &fileLimit);
2470 Socket[2][PAIRS] pairs;
2471 foreach (ref pair; pairs)
2472 pair = socketPair();
2475 foreach (pair; pairs)
2483 auto rng = Xorshift(42);
2484 pairs[].randomShuffle(rng);
2486 auto readSet = new SocketSet();
2487 auto writeSet = new SocketSet();
2488 auto errorSet = new SocketSet();
2490 foreach (testPair; pairs)
2497 foreach (ref pair; pairs)
2507 auto n = Socket.select(readSet, writeSet, errorSet);
2508 assert(n == PAIRS*2); // All in writeSet
2509 assert(writeSet.isSet(testPair[0]));
2510 assert(writeSet.isSet(testPair[1]));
2511 assert(!readSet.isSet(testPair[0]));
2512 assert(!readSet.isSet(testPair[1]));
2513 assert(!errorSet.isSet(testPair[0]));
2514 assert(!errorSet.isSet(testPair[1]));
2517 // Socket.send can't be marked with `scope`
2518 // -> @safe DIP1000 code can't use it - see https://github.com/dlang/phobos/pull/6204
2520 testPair[0].send(b[]);
2523 n = Socket.select(readSet, null, null);
2524 assert(n == 1); // testPair[1]
2525 assert(readSet.isSet(testPair[1]));
2526 assert(!readSet.isSet(testPair[0]));
2527 // Socket.receive can't be marked with `scope`
2528 // -> @safe DIP1000 code can't use it - see https://github.com/dlang/phobos/pull/6204
2530 testPair[1].receive(b[]);
2536 // https://issues.dlang.org/show_bug.cgi?id=14012
2537 // https://issues.dlang.org/show_bug.cgi?id=14013
2540 auto set = new SocketSet(1);
2541 assert(set.max >= 0);
2544 foreach (n; 0 .. LIMIT)
2545 set.add(cast(socket_t) n);
2546 assert(set.max >= LIMIT);
2549 /// The level at which a socket option is defined:
2550 enum SocketOptionLevel: int
2552 SOCKET = SOL_SOCKET, /// Socket level
2553 IP = ProtocolType.IP, /// Internet Protocol version 4 level
2554 ICMP = ProtocolType.ICMP, /// Internet Control Message Protocol level
2555 IGMP = ProtocolType.IGMP, /// Internet Group Management Protocol level
2556 GGP = ProtocolType.GGP, /// Gateway to Gateway Protocol level
2557 TCP = ProtocolType.TCP, /// Transmission Control Protocol level
2558 PUP = ProtocolType.PUP, /// PARC Universal Packet Protocol level
2559 UDP = ProtocolType.UDP, /// User Datagram Protocol level
2560 IDP = ProtocolType.IDP, /// Xerox NS protocol level
2561 RAW = ProtocolType.RAW, /// Raw IP packet level
2562 IPV6 = ProtocolType.IPV6, /// Internet Protocol version 6 level
2565 /// _Linger information for use with SocketOption.LINGER.
2570 version (StdDdoc) // no DDoc for string mixins, can't forward individual fields
2572 private alias l_onoff_t = typeof(_clinger.init.l_onoff );
2573 private alias l_linger_t = typeof(_clinger.init.l_linger);
2574 l_onoff_t on; /// Nonzero for _on.
2575 l_linger_t time; /// Linger _time.
2580 mixin FieldProxy!(`clinger.l_onoff`, `on`);
2581 mixin FieldProxy!(`clinger.l_linger`, `time`);
2585 /// Specifies a socket option:
2586 enum SocketOption: int
2588 DEBUG = SO_DEBUG, /// Record debugging information
2589 BROADCAST = SO_BROADCAST, /// Allow transmission of broadcast messages
2590 REUSEADDR = SO_REUSEADDR, /// Allow local reuse of address
2591 LINGER = SO_LINGER, /// Linger on close if unsent data is present
2592 OOBINLINE = SO_OOBINLINE, /// Receive out-of-band data in band
2593 SNDBUF = SO_SNDBUF, /// Send buffer size
2594 RCVBUF = SO_RCVBUF, /// Receive buffer size
2595 DONTROUTE = SO_DONTROUTE, /// Do not route
2596 SNDTIMEO = SO_SNDTIMEO, /// Send timeout
2597 RCVTIMEO = SO_RCVTIMEO, /// Receive timeout
2598 ERROR = SO_ERROR, /// Retrieve and clear error status
2599 KEEPALIVE = SO_KEEPALIVE, /// Enable keep-alive packets
2600 ACCEPTCONN = SO_ACCEPTCONN, /// Listen
2601 RCVLOWAT = SO_RCVLOWAT, /// Minimum number of input bytes to process
2602 SNDLOWAT = SO_SNDLOWAT, /// Minimum number of output bytes to process
2603 TYPE = SO_TYPE, /// Socket type
2605 // SocketOptionLevel.TCP:
2606 TCP_NODELAY = .TCP_NODELAY, /// Disable the Nagle algorithm for send coalescing
2608 // SocketOptionLevel.IPV6:
2609 IPV6_UNICAST_HOPS = .IPV6_UNICAST_HOPS, /// IP unicast hop limit
2610 IPV6_MULTICAST_IF = .IPV6_MULTICAST_IF, /// IP multicast interface
2611 IPV6_MULTICAST_LOOP = .IPV6_MULTICAST_LOOP, /// IP multicast loopback
2612 IPV6_MULTICAST_HOPS = .IPV6_MULTICAST_HOPS, /// IP multicast hops
2613 IPV6_JOIN_GROUP = .IPV6_JOIN_GROUP, /// Add an IP group membership
2614 IPV6_LEAVE_GROUP = .IPV6_LEAVE_GROUP, /// Drop an IP group membership
2615 IPV6_V6ONLY = .IPV6_V6ONLY, /// Treat wildcard bind as AF_INET6-only
2620 * `Socket` is a class that creates a network communication endpoint using
2621 * the Berkeley sockets interface.
2627 AddressFamily _family;
2630 bool _blocking = false; /// Property to get or set whether the socket is blocking or nonblocking.
2632 // The WinSock timeouts seem to be effectively skewed by a constant
2633 // offset of about half a second (value in milliseconds). This has
2634 // been confirmed on updated (as of Jun 2011) Windows XP, Windows 7
2635 // and Windows Server 2008 R2 boxes. The unittest below tests this
2637 enum WINSOCK_TIMEOUT_SKEW = 500;
2643 import std.datetime.stopwatch;
2644 import std.typecons;
2647 auto pair = socketPair();
2648 auto sock = pair[0];
2649 sock.setOption(SocketOptionLevel.SOCKET,
2650 SocketOption.RCVTIMEO, dur!"msecs"(msecs));
2652 auto sw = StopWatch(Yes.autoStart);
2657 Duration readBack = void;
2658 sock.getOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, readBack);
2660 assert(readBack.total!"msecs" == msecs);
2661 assert(sw.peek().total!"msecs" > msecs - 100 && sw.peek().total!"msecs" < msecs + 100);
2665 void setSock(socket_t handle)
2667 assert(handle != socket_t.init);
2670 // Set the option to disable SIGPIPE on send() if the platform
2671 // has it (e.g. on OS X).
2672 static if (is(typeof(SO_NOSIGPIPE)))
2674 setOption(SocketOptionLevel.SOCKET, cast(SocketOption) SO_NOSIGPIPE, true);
2679 // For use with accepting().
2680 protected this() pure nothrow @nogc
2688 * Create a blocking socket. If a single protocol type exists to support
2689 * this socket type within the address family, the `ProtocolType` may be
2692 this(AddressFamily af, SocketType type, ProtocolType protocol) @trusted
2695 auto handle = cast(socket_t) socket(af, type, protocol);
2696 if (handle == socket_t.init)
2697 throw new SocketOSException("Unable to create socket");
2702 this(AddressFamily af, SocketType type)
2704 /* A single protocol exists to support this socket type within the
2705 * protocol family, so the ProtocolType is assumed.
2707 this(af, type, cast(ProtocolType) 0); // Pseudo protocol number.
2712 this(AddressFamily af, SocketType type, scope const(char)[] protocolName) @trusted
2715 proto = getprotobyname(protocolName.tempCString());
2717 throw new SocketOSException("Unable to find the protocol");
2718 this(af, type, cast(ProtocolType) proto.p_proto);
2723 * Create a blocking socket using the parameters from the specified
2724 * `AddressInfo` structure.
2726 this(const scope AddressInfo info)
2728 this(info.family, info.type, info.protocol);
2731 /// Use an existing socket handle.
2732 this(socket_t sock, AddressFamily af) pure nothrow @nogc
2734 assert(sock != socket_t.init);
2740 ~this() nothrow @nogc
2746 /// Get underlying socket handle.
2747 @property socket_t handle() const pure nothrow @nogc
2753 * Get/set socket's blocking flag.
2755 * When a socket is blocking, calls to receive(), accept(), and send()
2756 * will block and wait for data/action.
2757 * A non-blocking socket will immediately return instead of blocking.
2759 @property bool blocking() @trusted const nothrow @nogc
2765 else version (Posix)
2767 return !(fcntl(handle, F_GETFL, 0) & O_NONBLOCK);
2772 @property void blocking(bool byes) @trusted
2777 if (_SOCKET_ERROR == ioctlsocket(sock, FIONBIO, &num))
2781 else version (Posix)
2783 int x = fcntl(sock, F_GETFL, 0);
2790 if (-1 == fcntl(sock, F_SETFL, x))
2796 throw new SocketOSException("Unable to set socket blocking");
2800 /// Get the socket's address family.
2801 @property AddressFamily addressFamily()
2806 /// Property that indicates if this is a valid, alive socket.
2807 @property bool isAlive() @trusted const
2810 socklen_t typesize = cast(socklen_t) type.sizeof;
2811 return !getsockopt(sock, SOL_SOCKET, SO_TYPE, cast(char*)&type, &typesize);
2815 * Associate a local address with this socket.
2818 * addr = The $(LREF Address) to associate this socket with.
2820 * Throws: $(LREF SocketOSException) when unable to bind the socket.
2822 void bind(Address addr) @trusted
2824 if (_SOCKET_ERROR == .bind(sock, addr.name, addr.nameLen))
2825 throw new SocketOSException("Unable to bind socket");
2829 * Establish a connection. If the socket is blocking, connect waits for
2830 * the connection to be made. If the socket is nonblocking, connect
2831 * returns immediately and the connection attempt is still in progress.
2833 void connect(Address to) @trusted
2835 if (_SOCKET_ERROR == .connect(sock, to.name, to.nameLen))
2844 if (WSAEWOULDBLOCK == err)
2847 else version (Posix)
2849 if (EINPROGRESS == err)
2857 throw new SocketOSException("Unable to connect socket", err);
2862 * Listen for an incoming connection. `bind` must be called before you
2863 * can `listen`. The `backlog` is a request of how many pending
2864 * incoming connections are queued until `accept`ed.
2866 void listen(int backlog) @trusted
2868 if (_SOCKET_ERROR == .listen(sock, backlog))
2869 throw new SocketOSException("Unable to listen on socket");
2873 * Called by `accept` when a new `Socket` must be created for a new
2874 * connection. To use a derived class, override this method and return an
2875 * instance of your class. The returned `Socket`'s handle must not be
2876 * set; `Socket` has a protected constructor `this()` to use in this
2879 * Override to use a derived class.
2880 * The returned socket's handle must not be set.
2882 protected Socket accepting() pure nothrow
2888 * Accept an incoming connection. If the socket is blocking, `accept`
2889 * waits for a connection request. Throws `SocketAcceptException` if
2890 * unable to _accept. See `accepting` for use with derived classes.
2892 Socket accept() @trusted
2894 auto newsock = cast(socket_t).accept(sock, null, null);
2895 if (socket_t.init == newsock)
2896 throw new SocketAcceptException("Unable to accept socket connection");
2901 newSocket = accepting();
2902 assert(newSocket.sock == socket_t.init);
2904 newSocket.setSock(newsock);
2906 newSocket._blocking = _blocking; //inherits blocking mode
2907 newSocket._family = _family; //same family
2918 /// Disables sends and/or receives.
2919 void shutdown(SocketShutdown how) @trusted nothrow @nogc
2921 .shutdown(sock, cast(int) how);
2925 private static void _close(socket_t sock) @system nothrow @nogc
2931 else version (Posix)
2939 * Immediately drop any connections and release socket resources.
2940 * The `Socket` object is no longer usable after `close`.
2941 * Calling `shutdown` before `close` is recommended
2942 * for connection-oriented sockets.
2944 void close() @trusted nothrow @nogc
2947 sock = socket_t.init;
2952 * Returns: the local machine's host name
2954 static @property string hostName() @trusted // getter
2956 char[256] result; // Host names are limited to 255 chars.
2957 if (_SOCKET_ERROR == .gethostname(result.ptr, result.length))
2958 throw new SocketOSException("Unable to obtain host name");
2959 return to!string(result.ptr);
2962 /// Remote endpoint `Address`.
2963 @property Address remoteAddress() @trusted
2965 Address addr = createAddress();
2966 socklen_t nameLen = addr.nameLen;
2967 if (_SOCKET_ERROR == .getpeername(sock, addr.name, &nameLen))
2968 throw new SocketOSException("Unable to obtain remote socket address");
2969 addr.setNameLen(nameLen);
2970 assert(addr.addressFamily == _family);
2974 /// Local endpoint `Address`.
2975 @property Address localAddress() @trusted
2977 Address addr = createAddress();
2978 socklen_t nameLen = addr.nameLen;
2979 if (_SOCKET_ERROR == .getsockname(sock, addr.name, &nameLen))
2980 throw new SocketOSException("Unable to obtain local socket address");
2981 addr.setNameLen(nameLen);
2982 assert(addr.addressFamily == _family);
2987 * Send or receive error code. See `wouldHaveBlocked`,
2988 * `lastSocketError` and `Socket.getErrorText` for obtaining more
2989 * information about the error.
2991 enum int ERROR = _SOCKET_ERROR;
2993 private static int capToInt(size_t size) nothrow @nogc
2995 // Windows uses int instead of size_t for length arguments.
2996 // Luckily, the send/recv functions make no guarantee that
2997 // all the data is sent, so we use that to send at most
2999 return size > size_t(int.max) ? int.max : cast(int) size;
3003 * Send data on the connection. If the socket is blocking and there is no
3004 * buffer space left, `send` waits.
3005 * Returns: The number of bytes actually sent, or `Socket.ERROR` on
3008 ptrdiff_t send(const(void)[] buf, SocketFlags flags) @trusted
3010 static if (is(typeof(MSG_NOSIGNAL)))
3012 flags = cast(SocketFlags)(flags | MSG_NOSIGNAL);
3015 auto sent = .send(sock, buf.ptr, capToInt(buf.length), cast(int) flags);
3017 auto sent = .send(sock, buf.ptr, buf.length, cast(int) flags);
3022 ptrdiff_t send(const(void)[] buf)
3024 return send(buf, SocketFlags.NONE);
3028 * Send data to a specific destination Address. If the destination address is
3029 * not specified, a connection must have been made and that address is used.
3030 * If the socket is blocking and there is no buffer space left, `sendTo` waits.
3031 * Returns: The number of bytes actually sent, or `Socket.ERROR` on
3034 ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags, Address to) @trusted
3036 static if (is(typeof(MSG_NOSIGNAL)))
3038 flags = cast(SocketFlags)(flags | MSG_NOSIGNAL);
3042 sock, buf.ptr, capToInt(buf.length),
3043 cast(int) flags, to.name, to.nameLen
3046 return .sendto(sock, buf.ptr, buf.length, cast(int) flags, to.name, to.nameLen);
3050 ptrdiff_t sendTo(const(void)[] buf, Address to)
3052 return sendTo(buf, SocketFlags.NONE, to);
3056 //assumes you connect()ed
3058 ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags) @trusted
3060 static if (is(typeof(MSG_NOSIGNAL)))
3062 flags = cast(SocketFlags)(flags | MSG_NOSIGNAL);
3065 return .sendto(sock, buf.ptr, capToInt(buf.length), cast(int) flags, null, 0);
3067 return .sendto(sock, buf.ptr, buf.length, cast(int) flags, null, 0);
3071 //assumes you connect()ed
3073 ptrdiff_t sendTo(const(void)[] buf)
3075 return sendTo(buf, SocketFlags.NONE);
3080 * Receive data on the connection. If the socket is blocking, `receive`
3081 * waits until there is data to be received.
3082 * Returns: The number of bytes actually received, `0` if the remote side
3083 * has closed the connection, or `Socket.ERROR` on failure.
3085 ptrdiff_t receive(void[] buf, SocketFlags flags) @trusted
3087 version (Windows) // Does not use size_t
3090 ? .recv(sock, buf.ptr, capToInt(buf.length), cast(int) flags)
3096 ? .recv(sock, buf.ptr, buf.length, cast(int) flags)
3102 ptrdiff_t receive(void[] buf)
3104 return receive(buf, SocketFlags.NONE);
3108 * Receive data and get the remote endpoint `Address`.
3109 * If the socket is blocking, `receiveFrom` waits until there is data to
3111 * Returns: The number of bytes actually received, `0` if the remote side
3112 * has closed the connection, or `Socket.ERROR` on failure.
3114 ptrdiff_t receiveFrom(void[] buf, SocketFlags flags, ref Address from) @trusted
3116 if (!buf.length) //return 0 and don't think the connection closed
3118 if (from is null || from.addressFamily != _family)
3119 from = createAddress();
3120 socklen_t nameLen = from.nameLen;
3123 auto read = .recvfrom(sock, buf.ptr, capToInt(buf.length), cast(int) flags, from.name, &nameLen);
3124 from.setNameLen(nameLen);
3125 assert(from.addressFamily == _family);
3126 // if (!read) //connection closed
3131 auto read = .recvfrom(sock, buf.ptr, buf.length, cast(int) flags, from.name, &nameLen);
3132 from.setNameLen(nameLen);
3133 assert(from.addressFamily == _family);
3134 // if (!read) //connection closed
3141 ptrdiff_t receiveFrom(void[] buf, ref Address from)
3143 return receiveFrom(buf, SocketFlags.NONE, from);
3147 //assumes you connect()ed
3149 ptrdiff_t receiveFrom(void[] buf, SocketFlags flags) @trusted
3151 if (!buf.length) //return 0 and don't think the connection closed
3155 auto read = .recvfrom(sock, buf.ptr, capToInt(buf.length), cast(int) flags, null, null);
3156 // if (!read) //connection closed
3161 auto read = .recvfrom(sock, buf.ptr, buf.length, cast(int) flags, null, null);
3162 // if (!read) //connection closed
3168 //assumes you connect()ed
3170 ptrdiff_t receiveFrom(void[] buf)
3172 return receiveFrom(buf, SocketFlags.NONE);
3177 * Get a socket option.
3178 * Returns: The number of bytes written to `result`.
3179 * The length, in bytes, of the actual result - very different from getsockopt()
3181 int getOption(SocketOptionLevel level, SocketOption option, void[] result) @trusted
3183 socklen_t len = cast(socklen_t) result.length;
3184 if (_SOCKET_ERROR == .getsockopt(sock, cast(int) level, cast(int) option, result.ptr, &len))
3185 throw new SocketOSException("Unable to get socket option");
3190 /// Common case of getting integer and boolean options.
3191 int getOption(SocketOptionLevel level, SocketOption option, out int32_t result) @trusted
3193 return getOption(level, option, (&result)[0 .. 1]);
3197 /// Get the linger option.
3198 int getOption(SocketOptionLevel level, SocketOption option, out Linger result) @trusted
3200 //return getOption(cast(SocketOptionLevel) SocketOptionLevel.SOCKET, SocketOption.LINGER, (&result)[0 .. 1]);
3201 return getOption(level, option, (&result.clinger)[0 .. 1]);
3204 /// Get a timeout (duration) option.
3205 void getOption(SocketOptionLevel level, SocketOption option, out Duration result) @trusted
3207 enforce(option == SocketOption.SNDTIMEO || option == SocketOption.RCVTIMEO,
3208 new SocketParameterException("Not a valid timeout option: " ~ to!string(option)));
3209 // WinSock returns the timeout values as a milliseconds DWORD,
3210 // while Linux and BSD return a timeval struct.
3214 getOption(level, option, (&msecs)[0 .. 1]);
3215 if (option == SocketOption.RCVTIMEO)
3216 msecs += WINSOCK_TIMEOUT_SKEW;
3217 result = dur!"msecs"(msecs);
3219 else version (Posix)
3222 getOption(level, option, (&tv.ctimeval)[0 .. 1]);
3223 result = dur!"seconds"(tv.seconds) + dur!"usecs"(tv.microseconds);
3225 else static assert(false);
3228 /// Set a socket option.
3229 void setOption(SocketOptionLevel level, SocketOption option, void[] value) @trusted
3231 if (_SOCKET_ERROR == .setsockopt(sock, cast(int) level,
3232 cast(int) option, value.ptr, cast(uint) value.length))
3233 throw new SocketOSException("Unable to set socket option");
3237 /// Common case for setting integer and boolean options.
3238 void setOption(SocketOptionLevel level, SocketOption option, int32_t value) @trusted
3240 setOption(level, option, (&value)[0 .. 1]);
3244 /// Set the linger option.
3245 void setOption(SocketOptionLevel level, SocketOption option, Linger value) @trusted
3247 //setOption(cast(SocketOptionLevel) SocketOptionLevel.SOCKET, SocketOption.LINGER, (&value)[0 .. 1]);
3248 setOption(level, option, (&value.clinger)[0 .. 1]);
3252 * Sets a timeout (duration) option, i.e. `SocketOption.SNDTIMEO` or
3253 * `RCVTIMEO`. Zero indicates no timeout.
3255 * In a typical application, you might also want to consider using
3256 * a non-blocking socket instead of setting a timeout on a blocking one.
3258 * Note: While the receive timeout setting is generally quite accurate
3259 * on *nix systems even for smaller durations, there are two issues to
3260 * be aware of on Windows: First, although undocumented, the effective
3261 * timeout duration seems to be the one set on the socket plus half
3262 * a second. `setOption()` tries to compensate for that, but still,
3263 * timeouts under 500ms are not possible on Windows. Second, be aware
3264 * that the actual amount of time spent until a blocking call returns
3265 * randomly varies on the order of 10ms.
3268 * level = The level at which a socket option is defined.
3269 * option = Either `SocketOption.SNDTIMEO` or `SocketOption.RCVTIMEO`.
3270 * value = The timeout duration to set. Must not be negative.
3272 * Throws: `SocketException` if setting the options fails.
3276 * import std.datetime;
3277 * import std.typecons;
3278 * auto pair = socketPair();
3279 * scope(exit) foreach (s; pair) s.close();
3281 * // Set a receive timeout, and then wait at one end of
3282 * // the socket pair, knowing that no data will arrive.
3283 * pair[0].setOption(SocketOptionLevel.SOCKET,
3284 * SocketOption.RCVTIMEO, dur!"seconds"(1));
3286 * auto sw = StopWatch(Yes.autoStart);
3288 * pair[0].receive(buffer);
3289 * writefln("Waited %s ms until the socket timed out.",
3293 void setOption(SocketOptionLevel level, SocketOption option, Duration value) @trusted
3295 enforce(option == SocketOption.SNDTIMEO || option == SocketOption.RCVTIMEO,
3296 new SocketParameterException("Not a valid timeout option: " ~ to!string(option)));
3298 enforce(value >= dur!"hnsecs"(0), new SocketParameterException(
3299 "Timeout duration must not be negative."));
3303 import std.algorithm.comparison : max;
3305 auto msecs = to!int(value.total!"msecs");
3306 if (msecs != 0 && option == SocketOption.RCVTIMEO)
3307 msecs = max(1, msecs - WINSOCK_TIMEOUT_SKEW);
3308 setOption(level, option, msecs);
3310 else version (Posix)
3313 value.split!("seconds", "usecs")(tv.tv_sec, tv.tv_usec);
3314 setOption(level, option, (&tv)[0 .. 1]);
3316 else static assert(false);
3320 * Get a text description of this socket's error status, and clear the
3321 * socket's error status.
3323 string getErrorText()
3326 getOption(SocketOptionLevel.SOCKET, SocketOption.ERROR, error);
3327 return formatSocketError(error);
3331 * Enables TCP keep-alive with the specified parameters.
3334 * time = Number of seconds with no activity until the first
3335 * keep-alive packet is sent.
3336 * interval = Number of seconds between when successive keep-alive
3337 * packets are sent if no acknowledgement is received.
3339 * Throws: `SocketOSException` if setting the options fails, or
3340 * `SocketFeatureException` if setting keep-alive parameters is
3341 * unsupported on the current platform.
3343 void setKeepAlive(int time, int interval) @trusted
3347 tcp_keepalive options;
3349 options.keepalivetime = time * 1000;
3350 options.keepaliveinterval = interval * 1000;
3351 uint cbBytesReturned;
3352 enforce(WSAIoctl(sock, SIO_KEEPALIVE_VALS,
3353 &options, options.sizeof,
3355 &cbBytesReturned, null, null) == 0,
3356 new SocketOSException("Error setting keep-alive"));
3359 static if (is(typeof(TCP_KEEPIDLE)) && is(typeof(TCP_KEEPINTVL)))
3361 setOption(SocketOptionLevel.TCP, cast(SocketOption) TCP_KEEPIDLE, time);
3362 setOption(SocketOptionLevel.TCP, cast(SocketOption) TCP_KEEPINTVL, interval);
3363 setOption(SocketOptionLevel.SOCKET, SocketOption.KEEPALIVE, true);
3366 throw new SocketFeatureException("Setting keep-alive options " ~
3367 "is not supported on this platform");
3371 * Wait for a socket to change status. A wait timeout of $(REF Duration, core, time) or
3372 * `TimeVal`, may be specified; if a timeout is not specified or the
3373 * `TimeVal` is `null`, the maximum timeout is used. The `TimeVal`
3374 * timeout has an unspecified value when `select` returns.
3375 * Returns: The number of sockets with status changes, `0` on timeout,
3376 * or `-1` on interruption. If the return value is greater than `0`,
3377 * the `SocketSets` are updated to only contain the sockets having status
3378 * changes. For a connecting socket, a write status change means the
3379 * connection is established and it's able to send. For a listening socket,
3380 * a read status change means there is an incoming connection request and
3381 * it's able to accept.
3383 * `SocketSet`'s updated to include only those sockets which an event occured.
3384 * For a `connect()`ing socket, writeability means connected.
3385 * For a `listen()`ing socket, readability means listening
3386 * `Winsock`; possibly internally limited to 64 sockets per set.
3389 * the number of events, 0 on timeout, or -1 on interruption
3391 static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, Duration timeout) @trusted
3393 auto vals = timeout.split!("seconds", "usecs")();
3395 tv.seconds = cast(tv.tv_sec_t ) vals.seconds;
3396 tv.microseconds = cast(tv.tv_usec_t) vals.usecs;
3397 return select(checkRead, checkWrite, checkError, &tv);
3402 static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError)
3404 return select(checkRead, checkWrite, checkError, null);
3408 static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, TimeVal* timeout) @trusted
3411 //make sure none of the SocketSet's are the same object
3414 assert(checkRead !is checkWrite);
3415 assert(checkRead !is checkError);
3419 assert(checkWrite !is checkError);
3429 // Windows has a problem with empty fd_set`s that aren't null.
3430 fr = checkRead && checkRead.count ? checkRead.toFd_set() : null;
3431 fw = checkWrite && checkWrite.count ? checkWrite.toFd_set() : null;
3432 fe = checkError && checkError.count ? checkError.toFd_set() : null;
3438 fr = checkRead.toFd_set();
3439 n = checkRead.selectn();
3448 fw = checkWrite.toFd_set();
3450 _n = checkWrite.selectn();
3461 fe = checkError.toFd_set();
3463 _n = checkError.selectn();
3472 // Make sure the sets' capacity matches, to avoid select reading
3473 // out of bounds just because one set was bigger than another
3474 if (checkRead ) checkRead .setMinCapacity(n);
3475 if (checkWrite) checkWrite.setMinCapacity(n);
3476 if (checkError) checkError.setMinCapacity(n);
3479 int result = .select(n, fr, fw, fe, &timeout.ctimeval);
3483 if (_SOCKET_ERROR == result && WSAGetLastError() == WSAEINTR)
3486 else version (Posix)
3488 if (_SOCKET_ERROR == result && errno == EINTR)
3496 if (_SOCKET_ERROR == result)
3497 throw new SocketOSException("Socket select error");
3504 * Can be overridden to support other addresses.
3505 * Returns: a new `Address` object for the current address family.
3507 protected Address createAddress() pure nothrow
3512 static if (is(sockaddr_un))
3514 case AddressFamily.UNIX:
3515 result = new UnixAddress;
3519 case AddressFamily.INET:
3520 result = new InternetAddress;
3523 case AddressFamily.INET6:
3524 result = new Internet6Address;
3528 result = new UnknownAddress;
3536 /// `TcpSocket` is a shortcut class for a TCP Socket.
3537 class TcpSocket: Socket
3539 /// Constructs a blocking TCP Socket.
3540 this(AddressFamily family)
3542 super(family, SocketType.STREAM, ProtocolType.TCP);
3545 /// Constructs a blocking IPv4 TCP Socket.
3548 this(AddressFamily.INET);
3553 /// Constructs a blocking TCP Socket and connects to an `Address`.
3554 this(Address connectTo)
3556 this(connectTo.addressFamily);
3562 /// `UdpSocket` is a shortcut class for a UDP Socket.
3563 class UdpSocket: Socket
3565 /// Constructs a blocking UDP Socket.
3566 this(AddressFamily family)
3568 super(family, SocketType.DGRAM, ProtocolType.UDP);
3572 /// Constructs a blocking IPv4 UDP Socket.
3575 this(AddressFamily.INET);
3579 // https://issues.dlang.org/show_bug.cgi?id=16514
3582 void checkAttributes(string attributes)()
3584 mixin(attributes ~ q{ void function() fun = {};});
3588 class TestSocket : Socket
3592 @property pure nothrow @nogc @safe socket_t handle() const
3594 checkAttributes!q{pure nothrow @nogc @safe}; assert(0);
3596 @property nothrow @nogc @trusted bool blocking() const
3598 checkAttributes!q{nothrow @nogc @trusted}; assert(0);
3600 @property @trusted void blocking(bool byes)
3602 checkAttributes!q{@trusted};
3604 @property @safe AddressFamily addressFamily()
3606 checkAttributes!q{@safe}; assert(0);
3608 @property @trusted bool isAlive() const
3610 checkAttributes!q{@trusted}; assert(0);
3612 @trusted void bind(Address addr)
3614 checkAttributes!q{@trusted};
3616 @trusted void connect(Address to)
3618 checkAttributes!q{@trusted};
3620 @trusted void listen(int backlog)
3622 checkAttributes!q{@trusted};
3624 protected pure nothrow @safe Socket accepting()
3626 checkAttributes!q{pure nothrow @safe}; assert(0);
3628 @trusted Socket accept()
3630 checkAttributes!q{@trusted}; assert(0);
3632 nothrow @nogc @trusted void shutdown(SocketShutdown how)
3634 checkAttributes!q{nothrow @nogc @trusted};
3636 nothrow @nogc @trusted void close()
3638 checkAttributes!q{nothrow @nogc @trusted};
3640 @property @trusted Address remoteAddress()
3642 checkAttributes!q{@trusted}; assert(0);
3644 @property @trusted Address localAddress()
3646 checkAttributes!q{@trusted}; assert(0);
3648 @trusted ptrdiff_t send(const(void)[] buf, SocketFlags flags)
3650 checkAttributes!q{@trusted}; assert(0);
3652 @safe ptrdiff_t send(const(void)[] buf)
3654 checkAttributes!q{@safe}; assert(0);
3656 @trusted ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags, Address to)
3658 checkAttributes!q{@trusted}; assert(0);
3660 @safe ptrdiff_t sendTo(const(void)[] buf, Address to)
3662 checkAttributes!q{@safe}; assert(0);
3664 @trusted ptrdiff_t sendTo(const(void)[] buf, SocketFlags flags)
3666 checkAttributes!q{@trusted}; assert(0);
3668 @safe ptrdiff_t sendTo(const(void)[] buf)
3670 checkAttributes!q{@safe}; assert(0);
3672 @trusted ptrdiff_t receive(void[] buf, SocketFlags flags)
3674 checkAttributes!q{@trusted}; assert(0);
3676 @safe ptrdiff_t receive(void[] buf)
3678 checkAttributes!q{@safe}; assert(0);
3680 @trusted ptrdiff_t receiveFrom(void[] buf, SocketFlags flags, ref Address from)
3682 checkAttributes!q{@trusted}; assert(0);
3684 @safe ptrdiff_t receiveFrom(void[] buf, ref Address from)
3686 checkAttributes!q{@safe}; assert(0);
3688 @trusted ptrdiff_t receiveFrom(void[] buf, SocketFlags flags)
3690 checkAttributes!q{@trusted}; assert(0);
3692 @safe ptrdiff_t receiveFrom(void[] buf)
3694 checkAttributes!q{@safe}; assert(0);
3696 @trusted int getOption(SocketOptionLevel level, SocketOption option, void[] result)
3698 checkAttributes!q{@trusted}; assert(0);
3700 @trusted int getOption(SocketOptionLevel level, SocketOption option, out int32_t result)
3702 checkAttributes!q{@trusted}; assert(0);
3704 @trusted int getOption(SocketOptionLevel level, SocketOption option, out Linger result)
3706 checkAttributes!q{@trusted}; assert(0);
3708 @trusted void getOption(SocketOptionLevel level, SocketOption option, out Duration result)
3710 checkAttributes!q{@trusted};
3712 @trusted void setOption(SocketOptionLevel level, SocketOption option, void[] value)
3714 checkAttributes!q{@trusted};
3716 @trusted void setOption(SocketOptionLevel level, SocketOption option, int32_t value)
3718 checkAttributes!q{@trusted};
3720 @trusted void setOption(SocketOptionLevel level, SocketOption option, Linger value)
3722 checkAttributes!q{@trusted};
3724 @trusted void setOption(SocketOptionLevel level, SocketOption option, Duration value)
3726 checkAttributes!q{@trusted};
3728 @safe string getErrorText()
3730 checkAttributes!q{@safe}; assert(0);
3732 @trusted void setKeepAlive(int time, int interval)
3734 checkAttributes!q{@trusted};
3736 protected pure nothrow @safe Address createAddress()
3738 checkAttributes!q{pure nothrow @safe}; assert(0);
3745 * Creates a pair of connected sockets.
3747 * The two sockets are indistinguishable.
3749 * Throws: `SocketException` if creation of the sockets fails.
3751 Socket[2] socketPair() @trusted
3756 if (socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1)
3757 throw new SocketOSException("Unable to create socket pair");
3759 Socket toSocket(size_t id)
3761 auto s = new Socket;
3762 s.setSock(cast(socket_t) socks[id]);
3763 s._family = AddressFamily.UNIX;
3767 return [toSocket(0), toSocket(1)];
3769 else version (Windows)
3771 // We do not have socketpair() on Windows, just manually create a
3772 // pair of sockets connected over some localhost port.
3775 auto listener = new TcpSocket();
3776 listener.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
3777 listener.bind(new InternetAddress(INADDR_LOOPBACK, InternetAddress.PORT_ANY));
3778 auto addr = listener.localAddress;
3781 result[0] = new TcpSocket(addr);
3782 result[1] = listener.accept();
3788 static assert(false);
3794 immutable ubyte[] data = [1, 2, 3, 4];
3795 auto pair = socketPair();
3796 scope(exit) foreach (s; pair) s.close();
3800 auto buf = new ubyte[data.length];
3801 pair[1].receive(buf);
3802 assert(buf == data);