PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/d/phobos2/std/socket.d

https://bitbucket.org/tbone/gdc-fixes
D | 1755 lines | 1181 code | 255 blank | 319 comment | 137 complexity | b738c2809af08c43cf702ff1644367f9 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. // Written in the D programming language
  2. /*
  3. Copyright (C) 2004-2005 Christopher E. Miller
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not
  15. be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. socket.d 1.3
  19. Jan 2005
  20. Thanks to Benjamin Herr for his assistance.
  21. */
  22. /**
  23. * Notes: For Win32 systems, link with ws2_32.lib.
  24. * Example: See /dmd/samples/d/listener.d.
  25. * Authors: Christopher E. Miller
  26. * Macros:
  27. * WIKI=Phobos/StdSocket
  28. */
  29. module std.socket;
  30. import core.stdc.stdint, std.string, std.c.string, std.c.stdlib, std.conv,
  31. std.traits;
  32. version(unittest)
  33. {
  34. private import std.c.stdio : printf;
  35. }
  36. version(Posix)
  37. {
  38. version = BsdSockets;
  39. }
  40. version(Win32)
  41. {
  42. pragma (lib, "ws2_32.lib");
  43. pragma (lib, "wsock32.lib");
  44. private import std.c.windows.windows, std.c.windows.winsock;
  45. private alias std.c.windows.winsock.timeval _ctimeval;
  46. typedef SOCKET socket_t = INVALID_SOCKET;
  47. private const int _SOCKET_ERROR = SOCKET_ERROR;
  48. private int _lasterr()
  49. {
  50. return WSAGetLastError();
  51. }
  52. }
  53. else version(BsdSockets)
  54. {
  55. version(Posix)
  56. {
  57. version(linux)
  58. import std.c.linux.socket : AF_IPX, AF_APPLETALK, SOCK_RDM,
  59. IPPROTO_IGMP, IPPROTO_GGP, IPPROTO_PUP, IPPROTO_IDP,
  60. protoent, servent, hostent, SD_RECEIVE, SD_SEND, SD_BOTH,
  61. MSG_NOSIGNAL, INADDR_NONE, getprotobyname, getprotobynumber,
  62. getservbyname, getservbyport, gethostbyname, gethostbyaddr;
  63. else version(OSX)
  64. private import std.c.osx.socket;
  65. else version(FreeBSD)
  66. {
  67. import core.sys.posix.sys.socket;
  68. import core.sys.posix.sys.select;
  69. import std.c.freebsd.socket;
  70. private enum SD_RECEIVE = SHUT_RD;
  71. private enum SD_SEND = SHUT_WR;
  72. private enum SD_BOTH = SHUT_RDWR;
  73. }
  74. else
  75. static assert(false);
  76. private import core.sys.posix.fcntl;
  77. private import core.sys.posix.unistd;
  78. private import core.sys.posix.arpa.inet;
  79. private import core.sys.posix.netinet.tcp;
  80. private import core.sys.posix.netinet.in_;
  81. private import core.sys.posix.sys.time;
  82. //private import core.sys.posix.sys.select;
  83. private import core.sys.posix.sys.socket;
  84. private alias core.sys.posix.sys.time.timeval _ctimeval;
  85. }
  86. private import core.stdc.errno;
  87. typedef int32_t socket_t = -1;
  88. private const int _SOCKET_ERROR = -1;
  89. private int _lasterr()
  90. {
  91. return errno;
  92. }
  93. }
  94. else
  95. {
  96. static assert(0); // No socket support yet.
  97. }
  98. /// Base exception thrown from a Socket.
  99. class SocketException: Exception
  100. {
  101. int errorCode; /// Platform-specific error code.
  102. this(string msg, int err = 0)
  103. {
  104. errorCode = err;
  105. version(Posix)
  106. {
  107. if(errorCode > 0)
  108. {
  109. char[80] buf;
  110. const(char)* cs;
  111. version (linux)
  112. {
  113. cs = strerror_r(errorCode, buf.ptr, buf.length);
  114. }
  115. else version (OSX)
  116. {
  117. auto errs = strerror_r(errorCode, buf.ptr, buf.length);
  118. if (errs == 0)
  119. cs = buf.ptr;
  120. else
  121. {
  122. cs = "Unknown error";
  123. }
  124. }
  125. else version (FreeBSD)
  126. {
  127. auto errs = strerror_r(errorCode, buf.ptr, buf.length);
  128. if (errs == 0)
  129. cs = buf.ptr;
  130. else
  131. {
  132. cs = "Unknown error";
  133. }
  134. }
  135. else
  136. {
  137. static assert(0);
  138. }
  139. auto len = strlen(cs);
  140. if(cs[len - 1] == '\n')
  141. len--;
  142. if(cs[len - 1] == '\r')
  143. len--;
  144. msg = cast(string) (msg ~ ": " ~ cs[0 .. len]);
  145. }
  146. }
  147. super(msg);
  148. }
  149. }
  150. shared static this()
  151. {
  152. version(Win32)
  153. {
  154. WSADATA wd;
  155. // Winsock will still load if an older version is present.
  156. // The version is just a request.
  157. int val;
  158. val = WSAStartup(0x2020, &wd);
  159. if(val) // Request Winsock 2.2 for IPv6.
  160. throw new SocketException("Unable to initialize socket library", val);
  161. }
  162. }
  163. shared static ~this()
  164. {
  165. version(Win32)
  166. {
  167. WSACleanup();
  168. }
  169. }
  170. /**
  171. * The communication domain used to resolve an address.
  172. */
  173. enum AddressFamily: int
  174. {
  175. UNSPEC = AF_UNSPEC, ///
  176. UNIX = AF_UNIX, /// local communication
  177. INET = AF_INET, /// internet protocol version 4
  178. IPX = AF_IPX, /// novell IPX
  179. APPLETALK = AF_APPLETALK, /// appletalk
  180. INET6 = AF_INET6, // internet protocol version 6
  181. }
  182. /**
  183. * Communication semantics
  184. */
  185. enum SocketType: int
  186. {
  187. STREAM = SOCK_STREAM, /// sequenced, reliable, two-way communication-based byte streams
  188. DGRAM = SOCK_DGRAM, /// connectionless, unreliable datagrams with a fixed maximum length; data may be lost or arrive out of order
  189. RAW = SOCK_RAW, /// raw protocol access
  190. RDM = SOCK_RDM, /// reliably-delivered message datagrams
  191. SEQPACKET = SOCK_SEQPACKET, /// sequenced, reliable, two-way connection-based datagrams with a fixed maximum length
  192. }
  193. /**
  194. * Protocol
  195. */
  196. enum ProtocolType: int
  197. {
  198. IP = IPPROTO_IP, /// internet protocol version 4
  199. ICMP = IPPROTO_ICMP, /// internet control message protocol
  200. IGMP = IPPROTO_IGMP, /// internet group management protocol
  201. GGP = IPPROTO_GGP, /// gateway to gateway protocol
  202. TCP = IPPROTO_TCP, /// transmission control protocol
  203. PUP = IPPROTO_PUP, /// PARC universal packet protocol
  204. UDP = IPPROTO_UDP, /// user datagram protocol
  205. IDP = IPPROTO_IDP, /// Xerox NS protocol
  206. IPV6 = IPPROTO_IPV6, /// internet protocol version 6
  207. }
  208. /**
  209. * Protocol is a class for retrieving protocol information.
  210. */
  211. class Protocol
  212. {
  213. ProtocolType type; /// These members are populated when one of the following functions are called without failure:
  214. string name; /// ditto
  215. string[] aliases; /// ditto
  216. void populate(protoent* proto)
  217. {
  218. type = cast(ProtocolType)proto.p_proto;
  219. name = to!string(proto.p_name).idup;
  220. int i;
  221. for(i = 0;; i++)
  222. {
  223. if(!proto.p_aliases[i])
  224. break;
  225. }
  226. if(i)
  227. {
  228. aliases = new string[i];
  229. for(i = 0; i != aliases.length; i++)
  230. {
  231. aliases[i] =
  232. to!string(proto.p_aliases[i]).idup;
  233. }
  234. }
  235. else
  236. {
  237. aliases = null;
  238. }
  239. }
  240. /** Returns false on failure */
  241. bool getProtocolByName(string name)
  242. {
  243. protoent* proto;
  244. proto = getprotobyname(toStringz(name));
  245. if(!proto)
  246. return false;
  247. populate(proto);
  248. return true;
  249. }
  250. /** Returns false on failure */
  251. // Same as getprotobynumber().
  252. bool getProtocolByType(ProtocolType type)
  253. {
  254. protoent* proto;
  255. proto = getprotobynumber(type);
  256. if(!proto)
  257. return false;
  258. populate(proto);
  259. return true;
  260. }
  261. }
  262. unittest
  263. {
  264. Protocol proto = new Protocol;
  265. version (Windows)
  266. {
  267. // These fail, don't know why
  268. pragma(msg, " --- std.socket(" ~ __LINE__.stringof ~ ") broken test ---");
  269. }
  270. else
  271. {
  272. assert(proto.getProtocolByType(ProtocolType.TCP));
  273. //printf("About protocol TCP:\n\tName: %.*s\n", proto.name);
  274. // foreach(string s; proto.aliases)
  275. // {
  276. // printf("\tAlias: %.*s\n", s);
  277. // }
  278. assert(proto.name == "tcp");
  279. assert(proto.aliases.length == 1 && proto.aliases[0] == "TCP");
  280. }
  281. }
  282. /**
  283. * Service is a class for retrieving service information.
  284. */
  285. class Service
  286. {
  287. /** These members are populated when one of the following functions are called without failure: */
  288. string name;
  289. string[] aliases; /// ditto
  290. ushort port; /// ditto
  291. string protocolName; /// ditto
  292. void populate(servent* serv)
  293. {
  294. name = to!string(serv.s_name);
  295. port = ntohs(cast(ushort)serv.s_port);
  296. protocolName = to!string(serv.s_proto);
  297. int i;
  298. for(i = 0;; i++)
  299. {
  300. if(!serv.s_aliases[i])
  301. break;
  302. }
  303. if(i)
  304. {
  305. aliases = new string[i];
  306. for(i = 0; i != aliases.length; i++)
  307. {
  308. aliases[i] =
  309. to!string(serv.s_aliases[i]).idup;
  310. }
  311. }
  312. else
  313. {
  314. aliases = null;
  315. }
  316. }
  317. /**
  318. * If a protocol name is omitted, any protocol will be matched.
  319. * Returns: false on failure.
  320. */
  321. bool getServiceByName(string name, string protocolName)
  322. {
  323. servent* serv;
  324. serv = getservbyname(toStringz(name), toStringz(protocolName));
  325. if(!serv)
  326. return false;
  327. populate(serv);
  328. return true;
  329. }
  330. // Any protocol name will be matched.
  331. /// ditto
  332. bool getServiceByName(string name)
  333. {
  334. servent* serv;
  335. serv = getservbyname(toStringz(name), null);
  336. if(!serv)
  337. return false;
  338. populate(serv);
  339. return true;
  340. }
  341. /// ditto
  342. bool getServiceByPort(ushort port, string protocolName)
  343. {
  344. servent* serv;
  345. serv = getservbyport(port, toStringz(protocolName));
  346. if(!serv)
  347. return false;
  348. populate(serv);
  349. return true;
  350. }
  351. // Any protocol name will be matched.
  352. /// ditto
  353. bool getServiceByPort(ushort port)
  354. {
  355. servent* serv;
  356. serv = getservbyport(port, null);
  357. if(!serv)
  358. return false;
  359. populate(serv);
  360. return true;
  361. }
  362. }
  363. unittest
  364. {
  365. Service serv = new Service;
  366. if(serv.getServiceByName("epmap", "tcp"))
  367. {
  368. // printf("About service epmap:\n\tService: %.*s\n"
  369. // "\tPort: %d\n\tProtocol: %.*s\n",
  370. // serv.name, serv.port, serv.protocolName);
  371. // foreach(string s; serv.aliases)
  372. // {
  373. // printf("\tAlias: %.*s\n", s);
  374. // }
  375. // For reasons unknown this is loc-srv on Wine and epmap on Windows
  376. assert(serv.name == "loc-srv" || serv.name == "epmap", serv.name);
  377. assert(serv.port == 135);
  378. assert(serv.protocolName == "tcp");
  379. // This assert used to pass, don't know why it fails now
  380. //assert(serv.aliases.length == 1 && serv.aliases[0] == "epmap");
  381. }
  382. else
  383. {
  384. printf("No service for epmap.\n");
  385. }
  386. }
  387. /**
  388. * Base exception thrown from an InternetHost.
  389. */
  390. class HostException: Exception
  391. {
  392. int errorCode; /// Platform-specific error code.
  393. this(string msg, int err = 0)
  394. {
  395. errorCode = err;
  396. super(msg);
  397. }
  398. }
  399. /**
  400. * InternetHost is a class for resolving IPv4 addresses.
  401. */
  402. class InternetHost
  403. {
  404. /** These members are populated when one of the following functions are called without failure: */
  405. string name;
  406. string[] aliases; /// ditto
  407. uint32_t[] addrList; /// ditto
  408. void validHostent(hostent* he)
  409. {
  410. if(he.h_addrtype != cast(int)AddressFamily.INET || he.h_length != 4)
  411. throw new HostException("Address family mismatch", _lasterr());
  412. }
  413. void populate(hostent* he)
  414. {
  415. int i;
  416. char* p;
  417. name = to!string(he.h_name).idup;
  418. for(i = 0;; i++)
  419. {
  420. p = he.h_aliases[i];
  421. if(!p)
  422. break;
  423. }
  424. if(i)
  425. {
  426. aliases = new string[i];
  427. for(i = 0; i != aliases.length; i++)
  428. {
  429. aliases[i] =
  430. to!string(he.h_aliases[i]).idup;
  431. }
  432. }
  433. else
  434. {
  435. aliases = null;
  436. }
  437. for(i = 0;; i++)
  438. {
  439. p = he.h_addr_list[i];
  440. if(!p)
  441. break;
  442. }
  443. if(i)
  444. {
  445. addrList = new uint32_t[i];
  446. for(i = 0; i != addrList.length; i++)
  447. {
  448. addrList[i] = ntohl(*(cast(uint32_t*)he.h_addr_list[i]));
  449. }
  450. }
  451. else
  452. {
  453. addrList = null;
  454. }
  455. }
  456. /**
  457. * Resolve host name. Returns false if unable to resolve.
  458. */
  459. bool getHostByName(string name)
  460. {
  461. version(Windows)
  462. {
  463. // TODO gethostbyname is deprecated in windows, use getaddrinfo
  464. auto he = gethostbyname(toStringz(name));
  465. if(!he)
  466. return false;
  467. validHostent(he);
  468. populate(he);
  469. }
  470. else
  471. {
  472. // posix systems use global state for return value, so we
  473. // must synchronize across all threads
  474. synchronized(this.classinfo)
  475. {
  476. auto he = gethostbyname(toStringz(name));
  477. if(!he)
  478. return false;
  479. validHostent(he);
  480. populate(he);
  481. }
  482. }
  483. return true;
  484. }
  485. /**
  486. * Resolve IPv4 address number. Returns false if unable to resolve.
  487. */
  488. bool getHostByAddr(uint addr)
  489. {
  490. uint x = htonl(addr);
  491. version(Windows)
  492. {
  493. // TODO gethostbyaddr is deprecated in windows, use getnameinfo
  494. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  495. if(!he)
  496. return false;
  497. validHostent(he);
  498. populate(he);
  499. }
  500. else
  501. {
  502. // posix systems use global state for return value, so we
  503. // must synchronize across all threads
  504. synchronized(this.classinfo)
  505. {
  506. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  507. if(!he)
  508. return false;
  509. validHostent(he);
  510. populate(he);
  511. }
  512. }
  513. return true;
  514. }
  515. /**
  516. * Same as previous, but addr is an IPv4 address string in the
  517. * dotted-decimal form $(I a.b.c.d).
  518. * Returns false if unable to resolve.
  519. */
  520. bool getHostByAddr(string addr)
  521. {
  522. uint x = inet_addr(std.string.toStringz(addr));
  523. version(Windows)
  524. {
  525. // TODO gethostbyaddr is deprecated in windows, use getnameinfo
  526. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  527. if(!he)
  528. return false;
  529. validHostent(he);
  530. populate(he);
  531. }
  532. else
  533. {
  534. // posix systems use global state for return value, so we
  535. // must synchronize across all threads
  536. synchronized(this.classinfo)
  537. {
  538. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  539. if(!he)
  540. return false;
  541. validHostent(he);
  542. populate(he);
  543. }
  544. }
  545. return true;
  546. }
  547. }
  548. unittest
  549. {
  550. try
  551. {
  552. InternetHost ih = new InternetHost;
  553. if (!ih.getHostByName("www.digitalmars.com"))
  554. return; // don't fail if not connected to internet
  555. //printf("addrList.length = %d\n", ih.addrList.length);
  556. assert(ih.addrList.length);
  557. InternetAddress ia = new InternetAddress(ih.addrList[0], InternetAddress.PORT_ANY);
  558. assert(ih.name == "www.digitalmars.com" || ih.name == "digitalmars.com",
  559. ih.name);
  560. // printf("IP address = %.*s\nname = %.*s\n", ia.toAddrString(), ih.name);
  561. // foreach(int i, string s; ih.aliases)
  562. // {
  563. // printf("aliases[%d] = %.*s\n", i, s);
  564. // }
  565. // printf("---\n");
  566. assert(ih.getHostByAddr(ih.addrList[0]));
  567. // printf("name = %.*s\n", ih.name);
  568. // foreach(int i, string s; ih.aliases)
  569. // {
  570. // printf("aliases[%d] = %.*s\n", i, s);
  571. // }
  572. }
  573. catch (Throwable e)
  574. {
  575. // Test fails or succeeds depending on environment!
  576. printf(" --- std.socket(%u) broken test ---\n", __LINE__);
  577. printf(" (%.*s)\n", e.toString());
  578. }
  579. }
  580. /**
  581. * Base exception thrown from an Address.
  582. */
  583. class AddressException: Exception
  584. {
  585. this(string msg)
  586. {
  587. super(msg);
  588. }
  589. }
  590. /**
  591. * Address is an abstract class for representing a network addresses.
  592. */
  593. abstract class Address
  594. {
  595. protected sockaddr* name();
  596. protected int nameLen();
  597. AddressFamily addressFamily(); /// Family of this address.
  598. override string toString(); /// Human readable string representing this address.
  599. }
  600. /**
  601. *
  602. */
  603. class UnknownAddress: Address
  604. {
  605. protected:
  606. sockaddr sa;
  607. override sockaddr* name()
  608. {
  609. return &sa;
  610. }
  611. override int nameLen()
  612. {
  613. return sa.sizeof;
  614. }
  615. public:
  616. override AddressFamily addressFamily()
  617. {
  618. return cast(AddressFamily)sa.sa_family;
  619. }
  620. override string toString()
  621. {
  622. return "Unknown";
  623. }
  624. }
  625. /**
  626. * InternetAddress is a class that represents an IPv4 (internet protocol version
  627. * 4) address and port.
  628. */
  629. class InternetAddress: Address
  630. {
  631. protected:
  632. sockaddr_in sin;
  633. override sockaddr* name()
  634. {
  635. return cast(sockaddr*)&sin;
  636. }
  637. override int nameLen()
  638. {
  639. return sin.sizeof;
  640. }
  641. this()
  642. {
  643. }
  644. public:
  645. const uint ADDR_ANY = INADDR_ANY; /// Any IPv4 address number.
  646. const uint ADDR_NONE = INADDR_NONE; /// An invalid IPv4 address number.
  647. const ushort PORT_ANY = 0; /// Any IPv4 port number.
  648. /// Overridden to return AddressFamily.INET.
  649. override AddressFamily addressFamily()
  650. {
  651. return cast(AddressFamily)AddressFamily.INET;
  652. }
  653. /// Returns the IPv4 port number.
  654. ushort port()
  655. {
  656. return ntohs(sin.sin_port);
  657. }
  658. /// Returns the IPv4 address number.
  659. uint addr()
  660. {
  661. return ntohl(sin.sin_addr.s_addr);
  662. }
  663. /**
  664. * Params:
  665. * addr = an IPv4 address string in the dotted-decimal form a.b.c.d,
  666. * or a host name that will be resolved using an InternetHost
  667. * object.
  668. * port = may be PORT_ANY as stated below.
  669. */
  670. this(string addr, ushort port)
  671. {
  672. uint uiaddr = parse(addr);
  673. if(ADDR_NONE == uiaddr)
  674. {
  675. InternetHost ih = new InternetHost;
  676. if(!ih.getHostByName(addr))
  677. //throw new AddressException("Invalid internet address");
  678. throw new AddressException(
  679. "Unable to resolve host '" ~ addr ~ "'");
  680. uiaddr = ih.addrList[0];
  681. }
  682. sin.sin_family = AddressFamily.INET;
  683. sin.sin_addr.s_addr = htonl(uiaddr);
  684. sin.sin_port = htons(port);
  685. }
  686. /**
  687. * Construct a new Address. addr may be ADDR_ANY (default) and port may
  688. * be PORT_ANY, and the actual numbers may not be known until a connection
  689. * is made.
  690. */
  691. this(uint addr, ushort port)
  692. {
  693. sin.sin_family = AddressFamily.INET;
  694. sin.sin_addr.s_addr = htonl(addr);
  695. sin.sin_port = htons(port);
  696. }
  697. /// ditto
  698. this(ushort port)
  699. {
  700. sin.sin_family = AddressFamily.INET;
  701. sin.sin_addr.s_addr = 0; //any, "0.0.0.0"
  702. sin.sin_port = htons(port);
  703. }
  704. /// Human readable string representing the IPv4 address in dotted-decimal form.
  705. string toAddrString()
  706. {
  707. return to!string(inet_ntoa(sin.sin_addr)).idup;
  708. }
  709. /// Human readable string representing the IPv4 port.
  710. string toPortString()
  711. {
  712. return std.conv.to!string(port());
  713. }
  714. /// Human readable string representing the IPv4 address and port in the form $(I a.b.c.d:e).
  715. override string toString()
  716. {
  717. return toAddrString() ~ ":" ~ toPortString();
  718. }
  719. /**
  720. * Parse an IPv4 address string in the dotted-decimal form $(I a.b.c.d)
  721. * and return the number.
  722. * If the string is not a legitimate IPv4 address,
  723. * ADDR_NONE is returned.
  724. */
  725. static uint parse(string addr)
  726. {
  727. return ntohl(inet_addr(std.string.toStringz(addr)));
  728. }
  729. }
  730. unittest
  731. {
  732. try
  733. {
  734. InternetAddress ia = new InternetAddress("63.105.9.61", 80);
  735. assert(ia.toString() == "63.105.9.61:80");
  736. }
  737. catch (Throwable e)
  738. {
  739. printf(" --- std.socket(%u) broken test ---\n", __LINE__);
  740. printf(" (%.*s)\n", e.toString());
  741. }
  742. }
  743. /** */
  744. class SocketAcceptException: SocketException
  745. {
  746. this(string msg, int err = 0)
  747. {
  748. super(msg, err);
  749. }
  750. }
  751. /// How a socket is shutdown:
  752. enum SocketShutdown: int
  753. {
  754. RECEIVE = SD_RECEIVE, /// socket receives are disallowed
  755. SEND = SD_SEND, /// socket sends are disallowed
  756. BOTH = SD_BOTH, /// both RECEIVE and SEND
  757. }
  758. /// Flags may be OR'ed together:
  759. enum SocketFlags: int
  760. {
  761. NONE = 0, /// no flags specified
  762. OOB = MSG_OOB, /// out-of-band stream data
  763. PEEK = MSG_PEEK, /// peek at incoming data without removing it from the queue, only for receiving
  764. DONTROUTE = MSG_DONTROUTE, /// data should not be subject to routing; this flag may be ignored. Only for sending
  765. NOSIGNAL = MSG_NOSIGNAL, /// don't send SIGPIPE signal on socket write error and instead return EPIPE
  766. }
  767. /// Duration timeout value.
  768. extern(C) struct timeval
  769. {
  770. // D interface
  771. int seconds; /// Number of seconds.
  772. int microseconds; /// Number of additional microseconds.
  773. // C interface
  774. deprecated
  775. {
  776. alias seconds tv_sec;
  777. alias microseconds tv_usec;
  778. }
  779. }
  780. /// A collection of sockets for use with Socket.select.
  781. class SocketSet
  782. {
  783. private:
  784. uint maxsockets; /// max desired sockets, the fd_set might be capable of holding more
  785. fd_set set;
  786. version(Win32)
  787. {
  788. uint count()
  789. {
  790. return set.fd_count;
  791. }
  792. }
  793. else version(BsdSockets)
  794. {
  795. int maxfd;
  796. uint count;
  797. }
  798. public:
  799. /// Set the maximum amount of sockets that may be added.
  800. this(uint max)
  801. {
  802. maxsockets = max;
  803. reset();
  804. }
  805. /// Uses the default maximum for the system.
  806. this()
  807. {
  808. this(FD_SETSIZE);
  809. }
  810. /// Reset the SocketSet so that there are 0 Sockets in the collection.
  811. void reset()
  812. {
  813. FD_ZERO(&set);
  814. version(BsdSockets)
  815. {
  816. maxfd = -1;
  817. count = 0;
  818. }
  819. }
  820. void add(socket_t s)
  821. in
  822. {
  823. // Make sure too many sockets don't get added.
  824. assert(count < maxsockets);
  825. }
  826. body
  827. {
  828. FD_SET(s, &set);
  829. version(BsdSockets)
  830. {
  831. ++count;
  832. if(s > maxfd)
  833. maxfd = s;
  834. }
  835. }
  836. /// Add a Socket to the collection. Adding more than the maximum has dangerous side affects.
  837. void add(Socket s)
  838. {
  839. add(s.sock);
  840. }
  841. void remove(socket_t s)
  842. {
  843. FD_CLR(s, &set);
  844. version(BsdSockets)
  845. {
  846. --count;
  847. // note: adjusting maxfd would require scanning the set, not worth it
  848. }
  849. }
  850. /// Remove this Socket from the collection.
  851. void remove(Socket s)
  852. {
  853. remove(s.sock);
  854. }
  855. int isSet(socket_t s)
  856. {
  857. return FD_ISSET(s, &set);
  858. }
  859. /// Returns nonzero if this Socket is in the collection.
  860. int isSet(Socket s)
  861. {
  862. return isSet(s.sock);
  863. }
  864. /// Return maximum amount of sockets that can be added, like FD_SETSIZE.
  865. uint max()
  866. {
  867. return maxsockets;
  868. }
  869. fd_set* toFd_set()
  870. {
  871. return &set;
  872. }
  873. int selectn()
  874. {
  875. version(Win32)
  876. {
  877. return count;
  878. }
  879. else version(BsdSockets)
  880. {
  881. return maxfd + 1;
  882. }
  883. }
  884. }
  885. /// The level at which a socket option is defined:
  886. enum SocketOptionLevel: int
  887. {
  888. SOCKET = SOL_SOCKET, /// socket level
  889. IP = ProtocolType.IP, /// internet protocol version 4 level
  890. ICMP = ProtocolType.ICMP, ///
  891. IGMP = ProtocolType.IGMP, ///
  892. GGP = ProtocolType.GGP, ///
  893. TCP = ProtocolType.TCP, /// transmission control protocol level
  894. PUP = ProtocolType.PUP, ///
  895. UDP = ProtocolType.UDP, /// user datagram protocol level
  896. IDP = ProtocolType.IDP, ///
  897. IPV6 = ProtocolType.IPV6, /// internet protocol version 6 level
  898. }
  899. /// Linger information for use with SocketOption.LINGER.
  900. extern(C) struct linger
  901. {
  902. // D interface
  903. version(Win32)
  904. {
  905. uint16_t on; /// Nonzero for on.
  906. uint16_t time; /// Linger time.
  907. }
  908. else version(BsdSockets)
  909. {
  910. int32_t on;
  911. int32_t time;
  912. }
  913. // C interface
  914. deprecated
  915. {
  916. alias on l_onoff;
  917. alias time l_linger;
  918. }
  919. }
  920. /// Specifies a socket option:
  921. enum SocketOption: int
  922. {
  923. DEBUG = SO_DEBUG, /// record debugging information
  924. BROADCAST = SO_BROADCAST, /// allow transmission of broadcast messages
  925. REUSEADDR = SO_REUSEADDR, /// allow local reuse of address
  926. LINGER = SO_LINGER, /// linger on close if unsent data is present
  927. OOBINLINE = SO_OOBINLINE, /// receive out-of-band data in band
  928. SNDBUF = SO_SNDBUF, /// send buffer size
  929. RCVBUF = SO_RCVBUF, /// receive buffer size
  930. DONTROUTE = SO_DONTROUTE, /// do not route
  931. // SocketOptionLevel.TCP:
  932. TCP_NODELAY = .TCP_NODELAY, /// disable the Nagle algorithm for send coalescing
  933. // SocketOptionLevel.IPV6:
  934. IPV6_UNICAST_HOPS = .IPV6_UNICAST_HOPS, ///
  935. IPV6_MULTICAST_IF = .IPV6_MULTICAST_IF, ///
  936. IPV6_MULTICAST_LOOP = .IPV6_MULTICAST_LOOP, ///
  937. IPV6_JOIN_GROUP = .IPV6_JOIN_GROUP, ///
  938. IPV6_LEAVE_GROUP = .IPV6_LEAVE_GROUP, ///
  939. }
  940. /**
  941. * Socket is a class that creates a network communication endpoint using the
  942. * Berkeley sockets interface.
  943. */
  944. class Socket
  945. {
  946. private:
  947. socket_t sock;
  948. AddressFamily _family;
  949. version(Win32)
  950. bool _blocking = false; /// Property to get or set whether the socket is blocking or nonblocking.
  951. // For use with accepting().
  952. protected this()
  953. {
  954. }
  955. public:
  956. /**
  957. * Create a blocking socket. If a single protocol type exists to support
  958. * this socket type within the address family, the ProtocolType may be
  959. * omitted.
  960. */
  961. this(AddressFamily af, SocketType type, ProtocolType protocol)
  962. {
  963. sock = cast(socket_t)socket(af, type, protocol);
  964. if(sock == socket_t.init)
  965. throw new SocketException("Unable to create socket", _lasterr());
  966. _family = af;
  967. }
  968. // A single protocol exists to support this socket type within the
  969. // protocol family, so the ProtocolType is assumed.
  970. /// ditto
  971. this(AddressFamily af, SocketType type)
  972. {
  973. this(af, type, cast(ProtocolType)0); // Pseudo protocol number.
  974. }
  975. /// ditto
  976. this(AddressFamily af, SocketType type, string protocolName)
  977. {
  978. protoent* proto;
  979. proto = getprotobyname(toStringz(protocolName));
  980. if(!proto)
  981. throw new SocketException("Unable to find the protocol", _lasterr());
  982. this(af, type, cast(ProtocolType)proto.p_proto);
  983. }
  984. ~this()
  985. {
  986. close();
  987. }
  988. /// Get underlying socket handle.
  989. socket_t handle()
  990. {
  991. return sock;
  992. }
  993. /**
  994. * Get/set socket's blocking flag.
  995. *
  996. * When a socket is blocking, calls to receive(), accept(), and send()
  997. * will block and wait for data/action.
  998. * A non-blocking socket will immediately return instead of blocking.
  999. */
  1000. bool blocking()
  1001. {
  1002. version(Win32)
  1003. {
  1004. return _blocking;
  1005. }
  1006. else version(BsdSockets)
  1007. {
  1008. return !(fcntl(handle, F_GETFL, 0) & O_NONBLOCK);
  1009. }
  1010. }
  1011. /// ditto
  1012. void blocking(bool byes)
  1013. {
  1014. version(Win32)
  1015. {
  1016. uint num = !byes;
  1017. if(_SOCKET_ERROR == ioctlsocket(sock, FIONBIO, &num))
  1018. goto err;
  1019. _blocking = byes;
  1020. }
  1021. else version(BsdSockets)
  1022. {
  1023. int x = fcntl(sock, F_GETFL, 0);
  1024. if(-1 == x)
  1025. goto err;
  1026. if(byes)
  1027. x &= ~O_NONBLOCK;
  1028. else
  1029. x |= O_NONBLOCK;
  1030. if(-1 == fcntl(sock, F_SETFL, x))
  1031. goto err;
  1032. }
  1033. return; // Success.
  1034. err:
  1035. throw new SocketException("Unable to set socket blocking", _lasterr());
  1036. }
  1037. /// Get the socket's address family.
  1038. AddressFamily addressFamily() // getter
  1039. {
  1040. return _family;
  1041. }
  1042. /// Property that indicates if this is a valid, alive socket.
  1043. bool isAlive() // getter
  1044. {
  1045. int type;
  1046. socklen_t typesize = cast(socklen_t) type.sizeof;
  1047. return !getsockopt(sock, SOL_SOCKET, SO_TYPE, cast(char*)&type, &typesize);
  1048. }
  1049. /// Associate a local address with this socket.
  1050. void bind(Address addr)
  1051. {
  1052. if(_SOCKET_ERROR == .bind(sock, addr.name(), addr.nameLen()))
  1053. throw new SocketException("Unable to bind socket", _lasterr());
  1054. }
  1055. /**
  1056. * Establish a connection. If the socket is blocking, connect waits for
  1057. * the connection to be made. If the socket is nonblocking, connect
  1058. * returns immediately and the connection attempt is still in progress.
  1059. */
  1060. void connect(Address to)
  1061. {
  1062. if(_SOCKET_ERROR == .connect(sock, to.name(), to.nameLen()))
  1063. {
  1064. int err;
  1065. err = _lasterr();
  1066. if(!blocking)
  1067. {
  1068. version(Win32)
  1069. {
  1070. if(WSAEWOULDBLOCK == err)
  1071. return;
  1072. }
  1073. else version(Posix)
  1074. {
  1075. if(EINPROGRESS == err)
  1076. return;
  1077. }
  1078. else
  1079. {
  1080. static assert(0);
  1081. }
  1082. }
  1083. throw new SocketException("Unable to connect socket", err);
  1084. }
  1085. }
  1086. /**
  1087. * Listen for an incoming connection. bind must be called before you can
  1088. * listen. The backlog is a request of how many pending incoming
  1089. * connections are queued until accept'ed.
  1090. */
  1091. void listen(int backlog)
  1092. {
  1093. if(_SOCKET_ERROR == .listen(sock, backlog))
  1094. throw new SocketException("Unable to listen on socket", _lasterr());
  1095. }
  1096. /**
  1097. * Called by accept when a new Socket must be created for a new
  1098. * connection. To use a derived class, override this method and return an
  1099. * instance of your class. The returned Socket's handle must not be set;
  1100. * Socket has a protected constructor this() to use in this situation.
  1101. */
  1102. // Override to use a derived class.
  1103. // The returned socket's handle must not be set.
  1104. protected Socket accepting()
  1105. {
  1106. return new Socket;
  1107. }
  1108. /**
  1109. * Accept an incoming connection. If the socket is blocking, accept
  1110. * waits for a connection request. Throws SocketAcceptException if unable
  1111. * to accept. See accepting for use with derived classes.
  1112. */
  1113. Socket accept()
  1114. {
  1115. socket_t newsock;
  1116. //newsock = cast(socket_t).accept(sock, null, null); // DMD 0.101 error: found '(' when expecting ';' following 'statement
  1117. alias .accept topaccept;
  1118. newsock = cast(socket_t)topaccept(sock, null, null);
  1119. if(socket_t.init == newsock)
  1120. throw new SocketAcceptException("Unable to accept socket connection", _lasterr());
  1121. Socket newSocket;
  1122. try
  1123. {
  1124. newSocket = accepting();
  1125. assert(newSocket.sock == socket_t.init);
  1126. newSocket.sock = newsock;
  1127. version(Win32)
  1128. newSocket._blocking = _blocking; //inherits blocking mode
  1129. newSocket._family = _family; //same family
  1130. }
  1131. catch(Object o)
  1132. {
  1133. _close(newsock);
  1134. throw o;
  1135. }
  1136. return newSocket;
  1137. }
  1138. /// Disables sends and/or receives.
  1139. void shutdown(SocketShutdown how)
  1140. {
  1141. .shutdown(sock, cast(int)how);
  1142. }
  1143. private static void _close(socket_t sock)
  1144. {
  1145. version(Win32)
  1146. {
  1147. .closesocket(sock);
  1148. }
  1149. else version(BsdSockets)
  1150. {
  1151. .close(sock);
  1152. }
  1153. }
  1154. /**
  1155. * Immediately drop any connections and release socket resources.
  1156. * Calling shutdown before close is recommended for connection-oriented
  1157. * sockets. The Socket object is no longer usable after close.
  1158. */
  1159. //calling shutdown() before this is recommended
  1160. //for connection-oriented sockets
  1161. void close()
  1162. {
  1163. _close(sock);
  1164. sock = socket_t.init;
  1165. }
  1166. private Address newFamilyObject()
  1167. {
  1168. Address result;
  1169. switch(_family)
  1170. {
  1171. case cast(AddressFamily)AddressFamily.INET:
  1172. result = new InternetAddress;
  1173. break;
  1174. default:
  1175. result = new UnknownAddress;
  1176. }
  1177. return result;
  1178. }
  1179. /// Returns the local machine's host name. Idea from mango.
  1180. static string hostName() // getter
  1181. {
  1182. char[256] result; // Host names are limited to 255 chars.
  1183. if(_SOCKET_ERROR == .gethostname(result.ptr, result.length))
  1184. throw new SocketException("Unable to obtain host name", _lasterr());
  1185. return to!string(cast(char*)result).idup;
  1186. }
  1187. /// Remote endpoint Address.
  1188. Address remoteAddress()
  1189. {
  1190. Address addr = newFamilyObject();
  1191. socklen_t nameLen = cast(socklen_t) addr.nameLen();
  1192. if(_SOCKET_ERROR == .getpeername(sock, addr.name(), &nameLen))
  1193. throw new SocketException("Unable to obtain remote socket address", _lasterr());
  1194. assert(addr.addressFamily() == _family);
  1195. return addr;
  1196. }
  1197. /// Local endpoint Address.
  1198. Address localAddress()
  1199. {
  1200. Address addr = newFamilyObject();
  1201. socklen_t nameLen = cast(socklen_t) addr.nameLen();
  1202. if(_SOCKET_ERROR == .getsockname(sock, addr.name(), &nameLen))
  1203. throw new SocketException("Unable to obtain local socket address", _lasterr());
  1204. assert(addr.addressFamily() == _family);
  1205. return addr;
  1206. }
  1207. /// Send or receive error code.
  1208. const int ERROR = _SOCKET_ERROR;
  1209. /**
  1210. * Send data on the connection. Returns the number of bytes actually
  1211. * sent, or ERROR on failure. If the socket is blocking and there is no
  1212. * buffer space left, send waits.
  1213. */
  1214. //returns number of bytes actually sent, or -1 on error
  1215. Select!(size_t.sizeof > 4, long, int)
  1216. send(const(void)[] buf, SocketFlags flags)
  1217. {
  1218. flags |= SocketFlags.NOSIGNAL;
  1219. auto sent = .send(sock, buf.ptr, buf.length, cast(int)flags);
  1220. return sent;
  1221. }
  1222. /// ditto
  1223. Select!(size_t.sizeof > 4, long, int) send(const(void)[] buf)
  1224. {
  1225. return send(buf, SocketFlags.NOSIGNAL);
  1226. }
  1227. /**
  1228. * Send data to a specific destination Address. If the destination address is not specified, a connection must have been made and that address is used. If the socket is blocking and there is no buffer space left, sendTo waits.
  1229. */
  1230. Select!(size_t.sizeof > 4, long, int)
  1231. sendTo(const(void)[] buf, SocketFlags flags, Address to)
  1232. {
  1233. flags |= SocketFlags.NOSIGNAL;
  1234. return .sendto(sock, buf.ptr, buf.length, cast(int)flags, to.name(), to.nameLen());
  1235. }
  1236. /// ditto
  1237. Select!(size_t.sizeof > 4, long, int) sendTo(const(void)[] buf, Address to)
  1238. {
  1239. return sendTo(buf, SocketFlags.NONE, to);
  1240. }
  1241. //assumes you connect()ed
  1242. /// ditto
  1243. Select!(size_t.sizeof > 4, long, int) sendTo(const(void)[] buf, SocketFlags flags)
  1244. {
  1245. flags |= SocketFlags.NOSIGNAL;
  1246. return .sendto(sock, buf.ptr, buf.length, cast(int)flags, null, 0);
  1247. }
  1248. //assumes you connect()ed
  1249. /// ditto
  1250. Select!(size_t.sizeof > 4, long, int) sendTo(const(void)[] buf)
  1251. {
  1252. return sendTo(buf, SocketFlags.NONE);
  1253. }
  1254. /**
  1255. * Receive data on the connection. Returns the number of bytes actually
  1256. * received, 0 if the remote side has closed the connection, or ERROR on
  1257. * failure. If the socket is blocking, receive waits until there is data
  1258. * to be received.
  1259. */
  1260. //returns number of bytes actually received, 0 on connection closure, or -1 on error
  1261. ptrdiff_t receive(void[] buf, SocketFlags flags)
  1262. {
  1263. return buf.length
  1264. ? .recv(sock, buf.ptr, buf.length, cast(int)flags)
  1265. : 0;
  1266. }
  1267. /// ditto
  1268. ptrdiff_t receive(void[] buf)
  1269. {
  1270. return receive(buf, SocketFlags.NONE);
  1271. }
  1272. /**
  1273. * Receive data and get the remote endpoint Address.
  1274. * If the socket is blocking, receiveFrom waits until there is data to
  1275. * be received.
  1276. * Returns: the number of bytes actually received,
  1277. * 0 if the remote side has closed the connection, or ERROR on failure.
  1278. */
  1279. Select!(size_t.sizeof > 4, long, int)
  1280. receiveFrom(void[] buf, SocketFlags flags, out Address from)
  1281. {
  1282. if(!buf.length) //return 0 and don't think the connection closed
  1283. return 0;
  1284. from = newFamilyObject();
  1285. socklen_t nameLen = cast(socklen_t) from.nameLen();
  1286. auto read = .recvfrom(sock, buf.ptr, buf.length, cast(int)flags, from.name(), &nameLen);
  1287. assert(from.addressFamily() == _family);
  1288. // if(!read) //connection closed
  1289. return read;
  1290. }
  1291. /// ditto
  1292. ptrdiff_t receiveFrom(void[] buf, out Address from)
  1293. {
  1294. return receiveFrom(buf, SocketFlags.NONE, from);
  1295. }
  1296. //assumes you connect()ed
  1297. /// ditto
  1298. Select!(size_t.sizeof > 4, long, int)
  1299. receiveFrom(void[] buf, SocketFlags flags)
  1300. {
  1301. if(!buf.length) //return 0 and don't think the connection closed
  1302. return 0;
  1303. auto read = .recvfrom(sock, buf.ptr, buf.length, cast(int)flags, null, null);
  1304. // if(!read) //connection closed
  1305. return read;
  1306. }
  1307. //assumes you connect()ed
  1308. /// ditto
  1309. ptrdiff_t receiveFrom(void[] buf)
  1310. {
  1311. return receiveFrom(buf, SocketFlags.NONE);
  1312. }
  1313. /// Get a socket option. Returns the number of bytes written to result.
  1314. //returns the length, in bytes, of the actual result - very different from getsockopt()
  1315. int getOption(SocketOptionLevel level, SocketOption option, void[] result)
  1316. {
  1317. socklen_t len = cast(socklen_t) result.length;
  1318. if(_SOCKET_ERROR == .getsockopt(sock, cast(int)level, cast(int)option, result.ptr, &len))
  1319. throw new SocketException("Unable to get socket option", _lasterr());
  1320. return len;
  1321. }
  1322. /// Common case of getting integer and boolean options.
  1323. int getOption(SocketOptionLevel level, SocketOption option, out int32_t result)
  1324. {
  1325. return getOption(level, option, (&result)[0 .. 1]);
  1326. }
  1327. /// Get the linger option.
  1328. int getOption(SocketOptionLevel level, SocketOption option, out linger result)
  1329. {
  1330. //return getOption(cast(SocketOptionLevel)SocketOptionLevel.SOCKET, SocketOption.LINGER, (&result)[0 .. 1]);
  1331. return getOption(level, option, (&result)[0 .. 1]);
  1332. }
  1333. // Set a socket option.
  1334. void setOption(SocketOptionLevel level, SocketOption option, void[] value)
  1335. {
  1336. if(_SOCKET_ERROR == .setsockopt(sock, cast(int)level,
  1337. cast(int)option, value.ptr, cast(uint) value.length))
  1338. throw new SocketException("Unable to set socket option", _lasterr());
  1339. }
  1340. /// Common case for setting integer and boolean options.
  1341. void setOption(SocketOptionLevel level, SocketOption option, int32_t value)
  1342. {
  1343. setOption(level, option, (&value)[0 .. 1]);
  1344. }
  1345. /// Set the linger option.
  1346. void setOption(SocketOptionLevel level, SocketOption option, linger value)
  1347. {
  1348. //setOption(cast(SocketOptionLevel)SocketOptionLevel.SOCKET, SocketOption.LINGER, (&value)[0 .. 1]);
  1349. setOption(level, option, (&value)[0 .. 1]);
  1350. }
  1351. /**
  1352. * Wait for a socket to change status. A wait timeout timeval or int microseconds may be specified; if a timeout is not specified or the timeval is null, the maximum timeout is used. The timeval timeout has an unspecified value when select returns. Returns the number of sockets with status changes, 0 on timeout, or -1 on interruption. If the return value is greater than 0, the SocketSets are updated to only contain the sockets having status changes. For a connecting socket, a write status change means the connection is established and it's able to send. For a listening socket, a read status change means there is an incoming connection request and it's able to accept.
  1353. */
  1354. //SocketSet's updated to include only those sockets which an event occured
  1355. //returns the number of events, 0 on timeout, or -1 on interruption
  1356. //for a connect()ing socket, writeability means connected
  1357. //for a listen()ing socket, readability means listening
  1358. //Winsock: possibly internally limited to 64 sockets per set
  1359. static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, timeval* tv)
  1360. in
  1361. {
  1362. //make sure none of the SocketSet's are the same object
  1363. if(checkRead)
  1364. {
  1365. assert(checkRead !is checkWrite);
  1366. assert(checkRead !is checkError);
  1367. }
  1368. if(checkWrite)
  1369. {

Large files files are truncated, but you can click here to view the full file