/std/socket.d

http://github.com/jcd/phobos · D · 3276 lines · 2021 code · 414 blank · 841 comment · 255 complexity · 02a5746ae79a3ff545995366b2eb4268 MD5 · raw file

Large files are truncated click here to view the full file

  1. // Written in the D programming language
  2. /*
  3. Copyright (C) 2004-2011 Christopher E. Miller
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. socket.d 1.4
  25. Jan 2011
  26. Thanks to Benjamin Herr for his assistance.
  27. */
  28. /**
  29. * Example: See $(SAMPLESRC listener.d) and $(SAMPLESRC htmlget.d)
  30. * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  31. * Authors: Christopher E. Miller, $(WEB klickverbot.at, David Nadlinger),
  32. * $(WEB thecybershadow.net, Vladimir Panteleev)
  33. * Source: $(PHOBOSSRC std/_socket.d)
  34. * Macros:
  35. * WIKI=Phobos/StdSocket
  36. */
  37. module std.socket;
  38. import core.stdc.stdint, std.string, std.c.string, std.c.stdlib, std.conv;
  39. import core.stdc.config;
  40. import core.time : dur, Duration;
  41. import std.algorithm : max;
  42. import std.exception : assumeUnique, enforce, collectException;
  43. version(Windows)
  44. {
  45. pragma (lib, "ws2_32.lib");
  46. pragma (lib, "wsock32.lib");
  47. private import std.c.windows.windows, std.c.windows.winsock, std.windows.syserror;
  48. private alias std.c.windows.winsock.timeval _ctimeval;
  49. private alias std.c.windows.winsock.linger _clinger;
  50. enum socket_t : SOCKET { INVALID_SOCKET }
  51. private const int _SOCKET_ERROR = SOCKET_ERROR;
  52. private int _lasterr()
  53. {
  54. return WSAGetLastError();
  55. }
  56. }
  57. else version(Posix)
  58. {
  59. version(linux)
  60. import std.c.linux.socket : AF_IPX, AF_APPLETALK, SOCK_RDM,
  61. IPPROTO_IGMP, IPPROTO_GGP, IPPROTO_PUP, IPPROTO_IDP,
  62. SD_RECEIVE, SD_SEND, SD_BOTH, MSG_NOSIGNAL, INADDR_NONE,
  63. TCP_KEEPIDLE, TCP_KEEPINTVL;
  64. else version(OSX)
  65. import std.c.osx.socket : AF_IPX, AF_APPLETALK, SOCK_RDM,
  66. IPPROTO_IGMP, IPPROTO_GGP, IPPROTO_PUP, IPPROTO_IDP,
  67. SD_RECEIVE, SD_SEND, SD_BOTH, INADDR_NONE;
  68. else version(FreeBSD)
  69. {
  70. import core.sys.posix.sys.socket;
  71. import core.sys.posix.sys.select;
  72. import std.c.freebsd.socket;
  73. private enum SD_RECEIVE = SHUT_RD;
  74. private enum SD_SEND = SHUT_WR;
  75. private enum SD_BOTH = SHUT_RDWR;
  76. }
  77. else
  78. static assert(false);
  79. import core.sys.posix.netdb;
  80. import core.sys.posix.sys.un : sockaddr_un;
  81. private import core.sys.posix.fcntl;
  82. private import core.sys.posix.unistd;
  83. private import core.sys.posix.arpa.inet;
  84. private import core.sys.posix.netinet.tcp;
  85. private import core.sys.posix.netinet.in_;
  86. private import core.sys.posix.sys.time;
  87. //private import core.sys.posix.sys.select;
  88. private import core.sys.posix.sys.socket;
  89. private alias core.sys.posix.sys.time.timeval _ctimeval;
  90. private alias core.sys.posix.sys.socket.linger _clinger;
  91. private import core.stdc.errno;
  92. enum socket_t : int32_t { init = -1 }
  93. private const int _SOCKET_ERROR = -1;
  94. private int _lasterr()
  95. {
  96. return errno;
  97. }
  98. }
  99. else
  100. {
  101. static assert(0); // No socket support yet.
  102. }
  103. version(unittest)
  104. {
  105. static assert(is(uint32_t == uint));
  106. static assert(is(uint16_t == ushort));
  107. private import std.stdio : writefln;
  108. // Print a message on exception instead of failing the unittest.
  109. private void softUnittest(void delegate() test, int line = __LINE__)
  110. {
  111. try
  112. test();
  113. catch (Throwable e)
  114. {
  115. writefln(" --- std.socket(%d) test fails depending on environment ---", line);
  116. writefln(" (%s)", e);
  117. }
  118. }
  119. }
  120. /// Base exception thrown by $(D std.socket).
  121. class SocketException: Exception
  122. {
  123. ///
  124. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
  125. {
  126. super(msg, file, line, next);
  127. }
  128. ///
  129. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
  130. {
  131. super(msg, next, file, line);
  132. }
  133. }
  134. // Needs to be public so that SocketOSException can be thrown outside of
  135. // std.socket (since it uses it as a default argument), but it probably doesn't
  136. // need to actually show up in the docs, since there's not really any public
  137. // need for it outside of being a default argument.
  138. string formatSocketError(int err)
  139. {
  140. version(Posix)
  141. {
  142. char[80] buf;
  143. const(char)* cs;
  144. version (linux)
  145. {
  146. cs = strerror_r(err, buf.ptr, buf.length);
  147. }
  148. else version (OSX)
  149. {
  150. auto errs = strerror_r(err, buf.ptr, buf.length);
  151. if (errs == 0)
  152. cs = buf.ptr;
  153. else
  154. return "Socket error " ~ to!string(err);
  155. }
  156. else version (FreeBSD)
  157. {
  158. auto errs = strerror_r(err, buf.ptr, buf.length);
  159. if (errs == 0)
  160. cs = buf.ptr;
  161. else
  162. return "Socket error " ~ to!string(err);
  163. }
  164. else
  165. static assert(0);
  166. auto len = strlen(cs);
  167. if(cs[len - 1] == '\n')
  168. len--;
  169. if(cs[len - 1] == '\r')
  170. len--;
  171. return cs[0 .. len].idup;
  172. }
  173. else
  174. version(Windows)
  175. {
  176. return sysErrorString(err);
  177. }
  178. else
  179. return "Socket error " ~ to!string(err);
  180. }
  181. /// Retrieve the error message for the most recently encountered network error.
  182. @property string lastSocketError()
  183. {
  184. return formatSocketError(_lasterr());
  185. }
  186. /// Socket exceptions representing network errors reported by the operating
  187. /// system.
  188. class SocketOSException: SocketException
  189. {
  190. int errorCode; /// Platform-specific error code.
  191. ///
  192. this(string msg,
  193. string file = __FILE__,
  194. size_t line = __LINE__,
  195. Throwable next = null,
  196. int err = _lasterr(),
  197. string function(int) errorFormatter = &formatSocketError)
  198. {
  199. errorCode = err;
  200. if (msg.length)
  201. super(msg ~ ": " ~ errorFormatter(err), file, line, next);
  202. else
  203. super(errorFormatter(err), file, line, next);
  204. }
  205. ///
  206. this(string msg,
  207. Throwable next,
  208. string file = __FILE__,
  209. size_t line = __LINE__,
  210. int err = _lasterr(),
  211. string function(int) errorFormatter = &formatSocketError)
  212. {
  213. this(msg, file, line, next, err, errorFormatter);
  214. }
  215. ///
  216. this(string msg,
  217. int err,
  218. string function(int) errorFormatter = &formatSocketError,
  219. string file = __FILE__,
  220. size_t line = __LINE__,
  221. Throwable next = null)
  222. {
  223. this(msg, file, line, next, err, errorFormatter);
  224. }
  225. }
  226. /// Socket exceptions representing invalid parameters specified by user code.
  227. class SocketParameterException: SocketException
  228. {
  229. ///
  230. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
  231. {
  232. super(msg, file, line, next);
  233. }
  234. ///
  235. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
  236. {
  237. super(msg, next, file, line);
  238. }
  239. }
  240. /// Socket exceptions representing attempts to use network capabilities not
  241. /// available on the current system.
  242. class SocketFeatureException: SocketException
  243. {
  244. ///
  245. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
  246. {
  247. super(msg, file, line, next);
  248. }
  249. ///
  250. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
  251. {
  252. super(msg, next, file, line);
  253. }
  254. }
  255. /// Return $(D true) if the last socket operation failed because the socket
  256. /// was in non-blocking mode and the operation would have blocked.
  257. bool wouldHaveBlocked()
  258. {
  259. version(Windows)
  260. return _lasterr() == WSAEWOULDBLOCK;
  261. else version(Posix)
  262. return _lasterr() == EAGAIN;
  263. else
  264. static assert(0);
  265. }
  266. private __gshared typeof(&getnameinfo) getnameinfoPointer;
  267. private __gshared typeof(&getaddrinfo) getaddrinfoPointer;
  268. private __gshared typeof(&freeaddrinfo) freeaddrinfoPointer;
  269. shared static this()
  270. {
  271. version(Windows)
  272. {
  273. WSADATA wd;
  274. // Winsock will still load if an older version is present.
  275. // The version is just a request.
  276. int val;
  277. val = WSAStartup(0x2020, &wd);
  278. if(val) // Request Winsock 2.2 for IPv6.
  279. throw new SocketOSException("Unable to initialize socket library", val);
  280. // These functions may not be present on older Windows versions.
  281. // See the comment in InternetAddress.toHostNameString() for details.
  282. auto ws2Lib = GetModuleHandleA("ws2_32.dll");
  283. if (ws2Lib)
  284. {
  285. getnameinfoPointer = cast(typeof(getnameinfoPointer))
  286. GetProcAddress(ws2Lib, "getnameinfo");
  287. getaddrinfoPointer = cast(typeof(getaddrinfoPointer))
  288. GetProcAddress(ws2Lib, "getaddrinfo");
  289. freeaddrinfoPointer = cast(typeof(freeaddrinfoPointer))
  290. GetProcAddress(ws2Lib, "freeaddrinfo");
  291. }
  292. }
  293. else version(Posix)
  294. {
  295. getnameinfoPointer = &getnameinfo;
  296. getaddrinfoPointer = &getaddrinfo;
  297. freeaddrinfoPointer = &freeaddrinfo;
  298. }
  299. }
  300. shared static ~this()
  301. {
  302. version(Windows)
  303. {
  304. WSACleanup();
  305. }
  306. }
  307. /**
  308. * The communication domain used to resolve an address.
  309. */
  310. enum AddressFamily: int
  311. {
  312. UNSPEC = AF_UNSPEC, /// Unspecified address family
  313. UNIX = AF_UNIX, /// Local communication
  314. INET = AF_INET, /// Internet Protocol version 4
  315. IPX = AF_IPX, /// Novell IPX
  316. APPLETALK = AF_APPLETALK, /// AppleTalk
  317. INET6 = AF_INET6, /// Internet Protocol version 6
  318. }
  319. /**
  320. * Communication semantics
  321. */
  322. enum SocketType: int
  323. {
  324. STREAM = SOCK_STREAM, /// Sequenced, reliable, two-way communication-based byte streams
  325. DGRAM = SOCK_DGRAM, /// Connectionless, unreliable datagrams with a fixed maximum length; data may be lost or arrive out of order
  326. RAW = SOCK_RAW, /// Raw protocol access
  327. RDM = SOCK_RDM, /// Reliably-delivered message datagrams
  328. SEQPACKET = SOCK_SEQPACKET, /// Sequenced, reliable, two-way connection-based datagrams with a fixed maximum length
  329. }
  330. /**
  331. * Protocol
  332. */
  333. enum ProtocolType: int
  334. {
  335. IP = IPPROTO_IP, /// Internet Protocol version 4
  336. ICMP = IPPROTO_ICMP, /// Internet Control Message Protocol
  337. IGMP = IPPROTO_IGMP, /// Internet Group Management Protocol
  338. GGP = IPPROTO_GGP, /// Gateway to Gateway Protocol
  339. TCP = IPPROTO_TCP, /// Transmission Control Protocol
  340. PUP = IPPROTO_PUP, /// PARC Universal Packet Protocol
  341. UDP = IPPROTO_UDP, /// User Datagram Protocol
  342. IDP = IPPROTO_IDP, /// Xerox NS protocol
  343. RAW = IPPROTO_RAW, /// Raw IP packets
  344. IPV6 = IPPROTO_IPV6, /// Internet Protocol version 6
  345. }
  346. /**
  347. * $(D Protocol) is a class for retrieving protocol information.
  348. *
  349. * Example:
  350. * ---
  351. * auto proto = new Protocol;
  352. * writeln("About protocol TCP:");
  353. * if (proto.getProtocolByType(ProtocolType.TCP))
  354. * {
  355. * writefln(" Name: %s", proto.name);
  356. * foreach(string s; proto.aliases)
  357. * writefln(" Alias: %s", s);
  358. * }
  359. * else
  360. * writeln(" No information found");
  361. * ---
  362. */
  363. class Protocol
  364. {
  365. /// These members are populated when one of the following functions are called successfully:
  366. ProtocolType type;
  367. string name; /// ditto
  368. string[] aliases; /// ditto
  369. void populate(protoent* proto)
  370. {
  371. type = cast(ProtocolType)proto.p_proto;
  372. name = to!string(proto.p_name);
  373. int i;
  374. for(i = 0;; i++)
  375. {
  376. if(!proto.p_aliases[i])
  377. break;
  378. }
  379. if(i)
  380. {
  381. aliases = new string[i];
  382. for(i = 0; i != aliases.length; i++)
  383. {
  384. aliases[i] =
  385. to!string(proto.p_aliases[i]);
  386. }
  387. }
  388. else
  389. {
  390. aliases = null;
  391. }
  392. }
  393. /** Returns: false on failure */
  394. bool getProtocolByName(in char[] name)
  395. {
  396. protoent* proto;
  397. proto = getprotobyname(toStringz(name));
  398. if(!proto)
  399. return false;
  400. populate(proto);
  401. return true;
  402. }
  403. /** Returns: false on failure */
  404. // Same as getprotobynumber().
  405. bool getProtocolByType(ProtocolType type)
  406. {
  407. protoent* proto;
  408. proto = getprotobynumber(type);
  409. if(!proto)
  410. return false;
  411. populate(proto);
  412. return true;
  413. }
  414. }
  415. unittest
  416. {
  417. softUnittest({
  418. Protocol proto = new Protocol;
  419. assert(proto.getProtocolByType(ProtocolType.TCP));
  420. //writeln("About protocol TCP:");
  421. //writefln("\tName: %s", proto.name);
  422. // foreach(string s; proto.aliases)
  423. // {
  424. // writefln("\tAlias: %s", s);
  425. // }
  426. assert(proto.name == "tcp");
  427. assert(proto.aliases.length == 1 && proto.aliases[0] == "TCP");
  428. });
  429. }
  430. /**
  431. * $(D Service) is a class for retrieving service information.
  432. *
  433. * Example:
  434. * ---
  435. * auto serv = new Service;
  436. * writeln("About service epmap:");
  437. * if (serv.getServiceByName("epmap", "tcp"))
  438. * {
  439. * writefln(" Service: %s", serv.name);
  440. * writefln(" Port: %d", serv.port);
  441. * writefln(" Protocol: %s", serv.protocolName);
  442. * foreach (string s; serv.aliases)
  443. * writefln(" Alias: %s", s);
  444. * }
  445. * else
  446. * writefln(" No service for epmap.");
  447. * ---
  448. */
  449. class Service
  450. {
  451. /// These members are populated when one of the following functions are called successfully:
  452. string name;
  453. string[] aliases; /// ditto
  454. ushort port; /// ditto
  455. string protocolName; /// ditto
  456. void populate(servent* serv)
  457. {
  458. name = to!string(serv.s_name);
  459. port = ntohs(cast(ushort)serv.s_port);
  460. protocolName = to!string(serv.s_proto);
  461. int i;
  462. for(i = 0;; i++)
  463. {
  464. if(!serv.s_aliases[i])
  465. break;
  466. }
  467. if(i)
  468. {
  469. aliases = new string[i];
  470. for(i = 0; i != aliases.length; i++)
  471. {
  472. aliases[i] =
  473. to!string(serv.s_aliases[i]);
  474. }
  475. }
  476. else
  477. {
  478. aliases = null;
  479. }
  480. }
  481. /**
  482. * If a protocol name is omitted, any protocol will be matched.
  483. * Returns: false on failure.
  484. */
  485. bool getServiceByName(in char[] name, in char[] protocolName = null)
  486. {
  487. servent* serv;
  488. serv = getservbyname(toStringz(name), protocolName !is null ? toStringz(protocolName) : null);
  489. if(!serv)
  490. return false;
  491. populate(serv);
  492. return true;
  493. }
  494. /// ditto
  495. bool getServiceByPort(ushort port, in char[] protocolName = null)
  496. {
  497. servent* serv;
  498. serv = getservbyport(port, protocolName !is null ? toStringz(protocolName) : null);
  499. if(!serv)
  500. return false;
  501. populate(serv);
  502. return true;
  503. }
  504. }
  505. unittest
  506. {
  507. softUnittest({
  508. Service serv = new Service;
  509. if(serv.getServiceByName("epmap", "tcp"))
  510. {
  511. // writefln("About service epmap:");
  512. // writefln("\tService: %s", serv.name);
  513. // writefln("\tPort: %d", serv.port);
  514. // writefln("\tProtocol: %s", serv.protocolName);
  515. // foreach(string s; serv.aliases)
  516. // {
  517. // writefln("\tAlias: %s", s);
  518. // }
  519. // For reasons unknown this is loc-srv on Wine and epmap on Windows
  520. assert(serv.name == "loc-srv" || serv.name == "epmap", serv.name);
  521. assert(serv.port == 135);
  522. assert(serv.protocolName == "tcp");
  523. }
  524. else
  525. {
  526. writefln("No service for epmap.");
  527. }
  528. });
  529. }
  530. /**
  531. * Class for exceptions thrown from an $(D InternetHost).
  532. */
  533. class HostException: SocketOSException
  534. {
  535. ///
  536. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null, int err = _lasterr())
  537. {
  538. super(msg, file, line, next, err);
  539. }
  540. ///
  541. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__, int err = _lasterr())
  542. {
  543. super(msg, next, file, line, err);
  544. }
  545. ///
  546. this(string msg, int err, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
  547. {
  548. super(msg, next, file, line, err);
  549. }
  550. }
  551. /**
  552. * $(D InternetHost) is a class for resolving IPv4 addresses.
  553. *
  554. * Consider using $(D getAddress), $(D parseAddress) and $(D Address) methods
  555. * instead of using this class directly.
  556. *
  557. * Example:
  558. * ---
  559. * auto ih = new InternetHost;
  560. *
  561. * // Forward lookup
  562. * writeln("About www.digitalmars.com:");
  563. * if (ih.getHostByName("www.digitalmars.com"))
  564. * {
  565. * writefln(" Name: %s", ih.name);
  566. * auto ip = InternetAddress.addrToString(ih.addrList[0]);
  567. * writefln(" IP address: %s", ip);
  568. * foreach (string s; ih.aliases)
  569. * writefln(" Alias: %s", s);
  570. * writeln("---");
  571. *
  572. * // Reverse lookup
  573. * writefln("About IP %s:", ip);
  574. * if (ih.getHostByAddr(ih.addrList[0]))
  575. * {
  576. * writefln(" Name: %s", ih.name);
  577. * foreach (string s; ih.aliases)
  578. * writefln(" Alias: %s", s);
  579. * }
  580. * else
  581. * writeln(" Reverse lookup failed");
  582. * }
  583. * else
  584. * writeln(" Can't resolve www.digitalmars.com");
  585. * ---
  586. */
  587. class InternetHost
  588. {
  589. /// These members are populated when one of the following functions are called successfully:
  590. string name;
  591. string[] aliases; /// ditto
  592. uint[] addrList; /// ditto
  593. void validHostent(hostent* he)
  594. {
  595. if(he.h_addrtype != cast(int)AddressFamily.INET || he.h_length != 4)
  596. throw new HostException("Address family mismatch");
  597. }
  598. void populate(hostent* he)
  599. {
  600. int i;
  601. char* p;
  602. name = to!string(he.h_name);
  603. for(i = 0;; i++)
  604. {
  605. p = he.h_aliases[i];
  606. if(!p)
  607. break;
  608. }
  609. if(i)
  610. {
  611. aliases = new string[i];
  612. for(i = 0; i != aliases.length; i++)
  613. {
  614. aliases[i] =
  615. to!string(he.h_aliases[i]);
  616. }
  617. }
  618. else
  619. {
  620. aliases = null;
  621. }
  622. for(i = 0;; i++)
  623. {
  624. p = he.h_addr_list[i];
  625. if(!p)
  626. break;
  627. }
  628. if(i)
  629. {
  630. addrList = new uint[i];
  631. for(i = 0; i != addrList.length; i++)
  632. {
  633. addrList[i] = ntohl(*(cast(uint*)he.h_addr_list[i]));
  634. }
  635. }
  636. else
  637. {
  638. addrList = null;
  639. }
  640. }
  641. private bool getHostNoSync(string opMixin, T)(T param)
  642. {
  643. mixin(opMixin);
  644. if (!he)
  645. return false;
  646. validHostent(he);
  647. populate(he);
  648. return true;
  649. }
  650. version(Windows)
  651. alias getHostNoSync getHost;
  652. else
  653. {
  654. // posix systems use global state for return value, so we
  655. // must synchronize across all threads
  656. private bool getHost(string opMixin, T)(T param)
  657. {
  658. synchronized(this.classinfo)
  659. return getHostNoSync!(opMixin, T)(param);
  660. }
  661. }
  662. /**
  663. * Resolve host name.
  664. * Returns: false if unable to resolve.
  665. */
  666. bool getHostByName(in char[] name)
  667. {
  668. static if (is(typeof(gethostbyname_r)))
  669. {
  670. return getHostNoSync!q{
  671. hostent he_v;
  672. hostent* he;
  673. ubyte[256] buffer_v = void;
  674. auto buffer = buffer_v[];
  675. auto param_z = std.string.toStringz(param);
  676. while (true)
  677. {
  678. he = &he_v;
  679. int errno;
  680. if (gethostbyname_r(param_z, he, buffer.ptr, buffer.length, &he, &errno) == ERANGE)
  681. buffer.length = buffer.length * 2;
  682. else
  683. break;
  684. }
  685. }(name);
  686. }
  687. else
  688. {
  689. return getHost!q{
  690. auto he = gethostbyname(toStringz(param));
  691. }(name);
  692. }
  693. }
  694. /**
  695. * Resolve IPv4 address number.
  696. *
  697. * Params:
  698. * addr = The IPv4 address to resolve, in host byte order.
  699. * Returns:
  700. * false if unable to resolve.
  701. */
  702. bool getHostByAddr(uint addr)
  703. {
  704. return getHost!q{
  705. auto x = htonl(param);
  706. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  707. }(addr);
  708. }
  709. /**
  710. * Same as previous, but addr is an IPv4 address string in the
  711. * dotted-decimal form $(I a.b.c.d).
  712. * Returns: false if unable to resolve.
  713. */
  714. bool getHostByAddr(in char[] addr)
  715. {
  716. return getHost!q{
  717. auto x = inet_addr(std.string.toStringz(param));
  718. enforce(x != INADDR_NONE,
  719. new SocketParameterException("Invalid IPv4 address"));
  720. auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
  721. }(addr);
  722. }
  723. }
  724. unittest
  725. {
  726. InternetHost ih = new InternetHost;
  727. ih.getHostByAddr(0x7F_00_00_01);
  728. assert(ih.addrList[0] == 0x7F_00_00_01);
  729. ih.getHostByAddr("127.0.0.1");
  730. assert(ih.addrList[0] == 0x7F_00_00_01);
  731. softUnittest({
  732. if (!ih.getHostByName("www.digitalmars.com"))
  733. return; // don't fail if not connected to internet
  734. //writefln("addrList.length = %d", ih.addrList.length);
  735. assert(ih.addrList.length);
  736. InternetAddress ia = new InternetAddress(ih.addrList[0], InternetAddress.PORT_ANY);
  737. assert(ih.name == "www.digitalmars.com" || ih.name == "digitalmars.com",
  738. ih.name);
  739. // writefln("IP address = %s", ia.toAddrString());
  740. // writefln("name = %s", ih.name);
  741. // foreach(int i, string s; ih.aliases)
  742. // {
  743. // writefln("aliases[%d] = %s", i, s);
  744. // }
  745. // writefln("---");
  746. //assert(ih.getHostByAddr(ih.addrList[0]));
  747. // writefln("name = %s", ih.name);
  748. // foreach(int i, string s; ih.aliases)
  749. // {
  750. // writefln("aliases[%d] = %s", i, s);
  751. // }
  752. });
  753. }
  754. /// Holds information about a socket _address retrieved by $(D getAddressInfo).
  755. struct AddressInfo
  756. {
  757. AddressFamily family; /// Address _family
  758. SocketType type; /// Socket _type
  759. ProtocolType protocol; /// Protocol
  760. Address address; /// Socket _address
  761. string canonicalName; /// Canonical name, when $(D AddressInfoFlags.CANONNAME) is used.
  762. }
  763. // A subset of flags supported on all platforms with getaddrinfo.
  764. /// Specifies option flags for $(D getAddressInfo).
  765. enum AddressInfoFlags: int
  766. {
  767. /// The resulting addresses will be used in a call to $(D Socket.bind).
  768. PASSIVE = AI_PASSIVE,
  769. /// The canonical name is returned in $(D canonicalName) member in the first $(D AddressInfo).
  770. CANONNAME = AI_CANONNAME,
  771. /// The $(D node) parameter passed to $(D getAddressInfo) must be a numeric string.
  772. /// This will suppress any potentially lengthy network host address lookups.
  773. NUMERICHOST = AI_NUMERICHOST,
  774. }
  775. /// On POSIX, getaddrinfo uses its own error codes, and thus has its own
  776. /// formatting function.
  777. private string formatGaiError(int err)
  778. {
  779. version(Windows)
  780. {
  781. return sysErrorString(err);
  782. }
  783. else
  784. {
  785. synchronized
  786. return to!string(gai_strerror(err));
  787. }
  788. }
  789. /**
  790. * Provides _protocol-independent translation from host names to socket
  791. * addresses. If advanced functionality is not required, consider using
  792. * $(D getAddress) for compatibility with older systems.
  793. *
  794. * Returns: Array with one $(D AddressInfo) per socket address.
  795. *
  796. * Throws: $(D SocketOSException) on failure, or $(D SocketFeatureException)
  797. * if this functionality is not available on the current system.
  798. *
  799. * Params:
  800. * node = string containing host name or numeric address
  801. * options = optional additional parameters, identified by type:
  802. * $(UL $(LI $(D string) - service name or port number)
  803. * $(LI $(D AddressInfoFlags) - option flags)
  804. * $(LI $(D AddressFamily) - address family to filter by)
  805. * $(LI $(D SocketType) - socket type to filter by)
  806. * $(LI $(D ProtocolType) - protocol to filter by))
  807. *
  808. * Example:
  809. * ---
  810. * // Roundtrip DNS resolution
  811. * auto results = getAddressInfo("www.digitalmars.com");
  812. * assert(results[0].address.toHostNameString() ==
  813. * "digitalmars.com");
  814. *
  815. * // Canonical name
  816. * results = getAddressInfo("www.digitalmars.com",
  817. * AddressInfoFlags.CANONNAME);
  818. * assert(results[0].canonicalName == "digitalmars.com");
  819. *
  820. * // IPv6 resolution
  821. * results = getAddressInfo("ipv6.google.com");
  822. * assert(results[0].family == AddressFamily.INET6);
  823. *
  824. * // Multihomed resolution
  825. * results = getAddressInfo("google.com");
  826. * assert(results.length > 1);
  827. *
  828. * // Parsing IPv4
  829. * results = getAddressInfo("127.0.0.1",
  830. * AddressInfoFlags.NUMERICHOST);
  831. * assert(results.length && results[0].family ==
  832. * AddressFamily.INET);
  833. *
  834. * // Parsing IPv6
  835. * results = getAddressInfo("::1",
  836. * AddressInfoFlags.NUMERICHOST);
  837. * assert(results.length && results[0].family ==
  838. * AddressFamily.INET6);
  839. * ---
  840. */
  841. AddressInfo[] getAddressInfo(T...)(in char[] node, T options)
  842. {
  843. const(char)[] service = null;
  844. addrinfo hints;
  845. hints.ai_family = AF_UNSPEC;
  846. foreach (option; options)
  847. {
  848. static if (is(typeof(option) : const(char)[]))
  849. service = option;
  850. else
  851. static if (is(typeof(option) == AddressInfoFlags))
  852. hints.ai_flags |= option;
  853. else
  854. static if (is(typeof(option) == AddressFamily))
  855. hints.ai_family = option;
  856. else
  857. static if (is(typeof(option) == SocketType))
  858. hints.ai_socktype = option;
  859. else
  860. static if (is(typeof(option) == ProtocolType))
  861. hints.ai_protocol = option;
  862. else
  863. static assert(0, "Unknown getAddressInfo option type: " ~ typeof(option).stringof);
  864. }
  865. return getAddressInfoImpl(node, service, &hints);
  866. }
  867. private AddressInfo[] getAddressInfoImpl(in char[] node, in char[] service, addrinfo* hints)
  868. {
  869. if (getaddrinfoPointer && freeaddrinfoPointer)
  870. {
  871. addrinfo* ai_res;
  872. int ret = getaddrinfoPointer(
  873. node is null ? null : std.string.toStringz(node),
  874. service is null ? null : std.string.toStringz(service),
  875. hints, &ai_res);
  876. scope(exit) if (ai_res) freeaddrinfoPointer(ai_res);
  877. enforce(ret == 0, new SocketOSException("getaddrinfo error", ret, &formatGaiError));
  878. AddressInfo[] result;
  879. // Use const to force UnknownAddressReference to copy the sockaddr.
  880. for (const(addrinfo)* ai = ai_res; ai; ai = ai.ai_next)
  881. result ~= AddressInfo(
  882. cast(AddressFamily) ai.ai_family,
  883. cast(SocketType ) ai.ai_socktype,
  884. cast(ProtocolType ) ai.ai_protocol,
  885. new UnknownAddressReference(ai.ai_addr, cast(socklen_t) ai.ai_addrlen),
  886. ai.ai_canonname ? to!string(ai.ai_canonname) : null);
  887. assert(result.length > 0);
  888. return result;
  889. }
  890. throw new SocketFeatureException("Address info lookup is not available " ~
  891. "on this system.");
  892. }
  893. unittest
  894. {
  895. softUnittest({
  896. if (getaddrinfoPointer)
  897. {
  898. // Roundtrip DNS resolution
  899. auto results = getAddressInfo("www.digitalmars.com");
  900. assert(results[0].address.toHostNameString() == "digitalmars.com");
  901. // Canonical name
  902. results = getAddressInfo("www.digitalmars.com",
  903. AddressInfoFlags.CANONNAME);
  904. assert(results[0].canonicalName == "digitalmars.com");
  905. // IPv6 resolution
  906. //results = getAddressInfo("ipv6.google.com");
  907. //assert(results[0].family == AddressFamily.INET6);
  908. // Multihomed resolution
  909. //results = getAddressInfo("google.com");
  910. //assert(results.length > 1);
  911. // Parsing IPv4
  912. results = getAddressInfo("127.0.0.1", AddressInfoFlags.NUMERICHOST);
  913. assert(results.length && results[0].family == AddressFamily.INET);
  914. // Parsing IPv6
  915. results = getAddressInfo("::1", AddressInfoFlags.NUMERICHOST);
  916. assert(results.length && results[0].family == AddressFamily.INET6);
  917. }
  918. });
  919. }
  920. private ushort serviceToPort(in char[] service)
  921. {
  922. if (service == "")
  923. return InternetAddress.PORT_ANY;
  924. else
  925. if (isNumeric(service))
  926. return to!ushort(service);
  927. else
  928. {
  929. auto s = new Service();
  930. s.getServiceByName(service);
  931. return s.port;
  932. }
  933. }
  934. /**
  935. * Provides _protocol-independent translation from host names to socket
  936. * addresses. Uses $(D getAddressInfo) if the current system supports it,
  937. * and $(D InternetHost) otherwise.
  938. *
  939. * Returns: Array with one $(D Address) instance per socket address.
  940. *
  941. * Throws: $(D SocketOSException) on failure.
  942. *
  943. * Example:
  944. * ---
  945. * writeln("Resolving www.digitalmars.com:");
  946. * try
  947. * {
  948. * auto addresses = getAddress("www.digitalmars.com");
  949. * foreach (address; addresses)
  950. * writefln(" IP: %s", address.toAddrString());
  951. * }
  952. * catch (SocketException e)
  953. * writefln(" Lookup failed: %s", e.msg);
  954. * ---
  955. */
  956. Address[] getAddress(in char[] hostname, in char[] service = null)
  957. {
  958. if (getaddrinfoPointer && freeaddrinfoPointer)
  959. {
  960. // use getAddressInfo
  961. Address[] results;
  962. auto infos = getAddressInfo(hostname, service);
  963. foreach (ref info; infos)
  964. results ~= info.address;
  965. return results;
  966. }
  967. else
  968. return getAddress(hostname, serviceToPort(service));
  969. }
  970. /// ditto
  971. Address[] getAddress(in char[] hostname, ushort port)
  972. {
  973. if (getaddrinfoPointer && freeaddrinfoPointer)
  974. return getAddress(hostname, to!string(port));
  975. else
  976. {
  977. // use getHostByName
  978. auto ih = new InternetHost;
  979. if (!ih.getHostByName(hostname))
  980. throw new AddressException(
  981. text("Unable to resolve host '", hostname, "'"));
  982. Address[] results;
  983. foreach (uint addr; ih.addrList)
  984. results ~= new InternetAddress(addr, port);
  985. return results;
  986. }
  987. }
  988. unittest
  989. {
  990. softUnittest({
  991. auto addresses = getAddress("63.105.9.61");
  992. assert(addresses.length && addresses[0].toAddrString() == "63.105.9.61");
  993. if (getaddrinfoPointer)
  994. {
  995. // test via gethostbyname
  996. auto getaddrinfoPointerBackup = getaddrinfoPointer;
  997. getaddrinfoPointer = null;
  998. scope(exit) getaddrinfoPointer = getaddrinfoPointerBackup;
  999. addresses = getAddress("63.105.9.61");
  1000. assert(addresses.length && addresses[0].toAddrString() == "63.105.9.61");
  1001. }
  1002. });
  1003. }
  1004. /**
  1005. * Provides _protocol-independent parsing of network addresses. Does not
  1006. * attempt name resolution. Uses $(D getAddressInfo) with
  1007. * $(D AddressInfoFlags.NUMERICHOST) if the current system supports it, and
  1008. * $(D InternetAddress) otherwise.
  1009. *
  1010. * Returns: An $(D Address) instance representing specified address.
  1011. *
  1012. * Throws: $(D SocketException) on failure.
  1013. *
  1014. * Example:
  1015. * ---
  1016. * writeln("Enter IP address:");
  1017. * string ip = readln().chomp();
  1018. * try
  1019. * {
  1020. * Address address = parseAddress(ip);
  1021. * writefln("Looking up reverse of %s:",
  1022. * address.toAddrString());
  1023. * try
  1024. * {
  1025. * string reverse = address.toHostNameString();
  1026. * if (reverse)
  1027. * writefln(" Reverse name: %s", reverse);
  1028. * else
  1029. * writeln(" Reverse hostname not found.");
  1030. * }
  1031. * catch (SocketException e)
  1032. * writefln(" Lookup error: %s", e.msg);
  1033. * }
  1034. * catch (SocketException e)
  1035. * {
  1036. * writefln(" %s is not a valid IP address: %s",
  1037. * ip, e.msg);
  1038. * }
  1039. * ---
  1040. */
  1041. Address parseAddress(in char[] hostaddr, in char[] service = null)
  1042. {
  1043. if (getaddrinfoPointer && freeaddrinfoPointer)
  1044. return getAddressInfo(hostaddr, service, AddressInfoFlags.NUMERICHOST)[0].address;
  1045. else
  1046. return parseAddress(hostaddr, serviceToPort(service));
  1047. }
  1048. /// ditto
  1049. Address parseAddress(in char[] hostaddr, ushort port)
  1050. {
  1051. if (getaddrinfoPointer && freeaddrinfoPointer)
  1052. return parseAddress(hostaddr, to!string(port));
  1053. else
  1054. {
  1055. auto in4_addr = InternetAddress.parse(hostaddr);
  1056. enforce(in4_addr != InternetAddress.ADDR_NONE,
  1057. new SocketParameterException("Invalid IP address"));
  1058. return new InternetAddress(in4_addr, port);
  1059. }
  1060. }
  1061. unittest
  1062. {
  1063. softUnittest({
  1064. auto address = parseAddress("63.105.9.61");
  1065. assert(address.toAddrString() == "63.105.9.61");
  1066. if (getaddrinfoPointer)
  1067. {
  1068. // test via inet_addr
  1069. auto getaddrinfoPointerBackup = getaddrinfoPointer;
  1070. getaddrinfoPointer = null;
  1071. scope(exit) getaddrinfoPointer = getaddrinfoPointerBackup;
  1072. address = parseAddress("63.105.9.61");
  1073. assert(address.toAddrString() == "63.105.9.61");
  1074. }
  1075. assert(collectException!SocketException(parseAddress("Invalid IP address")));
  1076. });
  1077. }
  1078. /**
  1079. * Class for exceptions thrown from an $(D Address).
  1080. */
  1081. class AddressException: SocketOSException
  1082. {
  1083. ///
  1084. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null, int err = _lasterr())
  1085. {
  1086. super(msg, file, line, next, err);
  1087. }
  1088. ///
  1089. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__, int err = _lasterr())
  1090. {
  1091. super(msg, next, file, line, err);
  1092. }
  1093. ///
  1094. this(string msg, int err, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
  1095. {
  1096. super(msg, next, file, line, err);
  1097. }
  1098. }
  1099. /**
  1100. * $(D Address) is an abstract class for representing a socket addresses.
  1101. *
  1102. * Example:
  1103. * ---
  1104. * writeln("About www.google.com port 80:");
  1105. * try
  1106. * {
  1107. * Address[] addresses = getAddress("www.google.com", 80);
  1108. * writefln(" %d addresses found.", addresses.length);
  1109. * foreach (int i, Address a; addresses)
  1110. * {
  1111. * writefln(" Address %d:", i+1);
  1112. * writefln(" IP address: %s", a.toAddrString());
  1113. * writefln(" Hostname: %s", a.toHostNameString());
  1114. * writefln(" Port: %s", a.toPortString());
  1115. * writefln(" Service name: %s",
  1116. * a.toServiceNameString());
  1117. * }
  1118. * }
  1119. * catch (SocketException e)
  1120. * writefln(" Lookup error: %s", e.msg);
  1121. * ---
  1122. */
  1123. abstract class Address
  1124. {
  1125. /// Returns pointer to underlying $(D sockaddr) structure.
  1126. abstract @property sockaddr* name();
  1127. abstract @property const(sockaddr)* name() const; /// ditto
  1128. /// Returns actual size of underlying $(D sockaddr) structure.
  1129. abstract @property socklen_t nameLen() const;
  1130. /// Family of this address.
  1131. @property AddressFamily addressFamily() const
  1132. {
  1133. return cast(AddressFamily) name.sa_family;
  1134. }
  1135. // Common code for toAddrString and toHostNameString
  1136. private final string toHostString(bool numeric) const
  1137. {
  1138. // getnameinfo() is the recommended way to perform a reverse (name)
  1139. // lookup on both Posix and Windows. However, it is only available
  1140. // on Windows XP and above, and not included with the WinSock import
  1141. // libraries shipped with DMD. Thus, we check for getnameinfo at
  1142. // runtime in the shared module constructor, and use it if it's
  1143. // available in the base class method. Classes for specific network
  1144. // families (e.g. InternetHost) override this method and use a
  1145. // deprecated, albeit commonly-available method when getnameinfo()
  1146. // is not available.
  1147. // http://technet.microsoft.com/en-us/library/aa450403.aspx
  1148. if (getnameinfoPointer)
  1149. {
  1150. auto buf = new char[NI_MAXHOST];
  1151. auto ret = getnameinfoPointer(
  1152. name, nameLen,
  1153. buf.ptr, cast(uint)buf.length,
  1154. null, 0,
  1155. numeric ? NI_NUMERICHOST : NI_NAMEREQD);
  1156. if (!numeric)
  1157. {
  1158. if (ret==EAI_NONAME)
  1159. return null;
  1160. version(Windows)
  1161. if (ret==WSANO_DATA)
  1162. return null;
  1163. }
  1164. enforce(ret == 0, new AddressException("Could not get " ~
  1165. (numeric ? "host address" : "host name")));
  1166. return assumeUnique(buf[0 .. strlen(buf.ptr)]);
  1167. }
  1168. throw new SocketFeatureException((numeric ? "Host address" : "Host name") ~
  1169. " lookup for this address family is not available on this system.");
  1170. }
  1171. // Common code for toPortString and toServiceNameString
  1172. private final string toServiceString(bool numeric) const
  1173. {
  1174. // See toHostNameString() for details about getnameinfo().
  1175. if (getnameinfoPointer)
  1176. {
  1177. auto buf = new char[NI_MAXSERV];
  1178. enforce(getnameinfoPointer(
  1179. name, nameLen,
  1180. null, 0,
  1181. buf.ptr, cast(uint)buf.length,
  1182. numeric ? NI_NUMERICSERV : NI_NAMEREQD
  1183. ) == 0, new AddressException("Could not get " ~
  1184. (numeric ? "port number" : "service name")));
  1185. return assumeUnique(buf[0 .. strlen(buf.ptr)]);
  1186. }
  1187. throw new SocketFeatureException((numeric ? "Port number" : "Service name") ~
  1188. " lookup for this address family is not available on this system.");
  1189. }
  1190. /**
  1191. * Attempts to retrieve the host address as a human-readable string.
  1192. *
  1193. * Throws: $(D AddressException) on failure, or $(D SocketFeatureException)
  1194. * if address retrieval for this address family is not available on the
  1195. * current system.
  1196. */
  1197. string toAddrString() const
  1198. {
  1199. return toHostString(true);
  1200. }
  1201. /**
  1202. * Attempts to retrieve the host name as a fully qualified domain name.
  1203. *
  1204. * Returns: The FQDN corresponding to this $(D Address), or $(D null) if
  1205. * the host name did not resolve.
  1206. *
  1207. * Throws: $(D AddressException) on error, or $(D SocketFeatureException)
  1208. * if host name lookup for this address family is not available on the
  1209. * current system.
  1210. */
  1211. string toHostNameString() const
  1212. {
  1213. return toHostString(false);
  1214. }
  1215. /**
  1216. * Attempts to retrieve the numeric port number as a string.
  1217. *
  1218. * Throws: $(D AddressException) on failure, or $(D SocketFeatureException)
  1219. * if port number retrieval for this address family is not available on the
  1220. * current system.
  1221. */
  1222. string toPortString() const
  1223. {
  1224. return toServiceString(true);
  1225. }
  1226. /**
  1227. * Attempts to retrieve the service name as a string.
  1228. *
  1229. * Throws: $(D AddressException) on failure, or $(D SocketFeatureException)
  1230. * if service name lookup for this address family is not available on the
  1231. * current system.
  1232. */
  1233. string toServiceNameString() const
  1234. {
  1235. return toServiceString(false);
  1236. }
  1237. /// Human readable string representing this address.
  1238. override string toString() const
  1239. {
  1240. try
  1241. {
  1242. string host = toAddrString();
  1243. string port = toPortString();
  1244. if (host.indexOf(':') >= 0)
  1245. return "[" ~ host ~ "]:" ~ port;
  1246. else
  1247. return host ~ ":" ~ port;
  1248. }
  1249. catch (SocketException)
  1250. return "Unknown";
  1251. }
  1252. }
  1253. /**
  1254. * $(D UnknownAddress) encapsulates an unknown socket address.
  1255. */
  1256. class UnknownAddress: Address
  1257. {
  1258. protected:
  1259. sockaddr sa;
  1260. public:
  1261. override @property sockaddr* name()
  1262. {
  1263. return &sa;
  1264. }
  1265. override @property const(sockaddr)* name() const
  1266. {
  1267. return &sa;
  1268. }
  1269. override @property socklen_t nameLen() const
  1270. {
  1271. return cast(socklen_t) sa.sizeof;
  1272. }
  1273. }
  1274. /**
  1275. * $(D UnknownAddressReference) encapsulates a reference to an arbitrary
  1276. * socket address.
  1277. */
  1278. class UnknownAddressReference: Address
  1279. {
  1280. protected:
  1281. sockaddr* sa;
  1282. socklen_t len;
  1283. public:
  1284. /// Constructs an $(D Address) with a reference to the specified $(D sockaddr).
  1285. this(sockaddr* sa, socklen_t len)
  1286. {
  1287. this.sa = sa;
  1288. this.len = len;
  1289. }
  1290. /// Constructs an $(D Address) with a copy of the specified $(D sockaddr).
  1291. this(const(sockaddr)* sa, socklen_t len)
  1292. {
  1293. this.sa = cast(sockaddr*) (cast(ubyte*)sa)[0..len].dup.ptr;
  1294. this.len = len;
  1295. }
  1296. override @property sockaddr* name()
  1297. {
  1298. return sa;
  1299. }
  1300. override @property const(sockaddr)* name() const
  1301. {
  1302. return sa;
  1303. }
  1304. override @property socklen_t nameLen() const
  1305. {
  1306. return cast(socklen_t) len;
  1307. }
  1308. }
  1309. /**
  1310. * $(D InternetAddress) encapsulates an IPv4 (Internet Protocol version 4)
  1311. * socket address.
  1312. *
  1313. * Consider using $(D getAddress), $(D parseAddress) and $(D Address) methods
  1314. * instead of using this class directly.
  1315. */
  1316. class InternetAddress: Address
  1317. {
  1318. protected:
  1319. sockaddr_in sin;
  1320. this()
  1321. {
  1322. }
  1323. public:
  1324. override @property sockaddr* name()
  1325. {
  1326. return cast(sockaddr*)&sin;
  1327. }
  1328. override @property const(sockaddr)* name() const
  1329. {
  1330. return cast(const(sockaddr)*)&sin;
  1331. }
  1332. override @property socklen_t nameLen() const
  1333. {
  1334. return cast(socklen_t) sin.sizeof;
  1335. }
  1336. enum uint ADDR_ANY = INADDR_ANY; /// Any IPv4 host address.
  1337. enum uint ADDR_NONE = INADDR_NONE; /// An invalid IPv4 host address.
  1338. enum ushort PORT_ANY = 0; /// Any IPv4 port number.
  1339. /// Returns the IPv4 _port number (in host byte order).
  1340. @property ushort port() const
  1341. {
  1342. return ntohs(sin.sin_port);
  1343. }
  1344. /// Returns the IPv4 address number (in host byte order).
  1345. @property uint addr() const
  1346. {
  1347. return ntohl(sin.sin_addr.s_addr);
  1348. }
  1349. /**
  1350. * Construct a new $(D InternetAddress).
  1351. * Params:
  1352. * addr = an IPv4 address string in the dotted-decimal form a.b.c.d,
  1353. * or a host name which will be resolved using an $(D InternetHost)
  1354. * object.
  1355. * port = port number, may be $(D PORT_ANY).
  1356. */
  1357. this(in char[] addr, ushort port)
  1358. {
  1359. uint uiaddr = parse(addr);
  1360. if(ADDR_NONE == uiaddr)
  1361. {
  1362. InternetHost ih = new InternetHost;
  1363. if(!ih.getHostByName(addr))
  1364. //throw new AddressException("Invalid internet address");
  1365. throw new AddressException(
  1366. text("Unable to resolve host '", addr, "'"));
  1367. uiaddr = ih.addrList[0];
  1368. }
  1369. sin.sin_family = AddressFamily.INET;
  1370. sin.sin_addr.s_addr = htonl(uiaddr);
  1371. sin.sin_port = htons(port);
  1372. }
  1373. /**
  1374. * Construct a new $(D InternetAddress).
  1375. * Params:
  1376. * addr = (optional) an IPv4 address in host byte order, may be $(D ADDR_ANY).
  1377. * port = port number, may be $(D PORT_ANY).
  1378. */
  1379. this(uint addr, ushort port)
  1380. {
  1381. sin.sin_family = AddressFamily.INET;
  1382. sin.sin_addr.s_addr = htonl(addr);
  1383. sin.sin_port = htons(port);
  1384. }
  1385. /// ditto
  1386. this(ushort port)
  1387. {
  1388. sin.sin_family = AddressFamily.INET;
  1389. sin.sin_addr.s_addr = ADDR_ANY;
  1390. sin.sin_port = htons(port);
  1391. }
  1392. /// Human readable string representing the IPv4 address in dotted-decimal form.
  1393. override string toAddrString() const
  1394. {
  1395. return to!string(inet_ntoa(sin.sin_addr));
  1396. }
  1397. /// Human readable string representing the IPv4 port.
  1398. override string toPortString() const
  1399. {
  1400. return std.conv.to!string(port);
  1401. }
  1402. /**
  1403. * Attempts to retrieve the host name as a fully qualified domain name.
  1404. *
  1405. * Returns: The FQDN corresponding to this $(D InternetAddress), or
  1406. * $(D null) if the host name did not resolve.
  1407. *
  1408. * Throws: $(D AddressException) on error.
  1409. */
  1410. override string toHostNameString() const
  1411. {
  1412. // getnameinfo() is the recommended way to perform a reverse (name)
  1413. // lookup on both Posix and Windows. However, it is only available
  1414. // on Windows XP and above, and not included with the WinSock import
  1415. // libraries shipped with DMD. Thus, we check for getnameinfo at
  1416. // runtime in the shared module constructor, and fall back to the
  1417. // deprecated getHostByAddr() if it could not be found. See also:
  1418. // http://technet.microsoft.com/en-us/library/aa450403.aspx
  1419. if (getnameinfoPointer)
  1420. return super.toHostNameString();
  1421. else
  1422. {
  1423. auto host = new InternetHost();
  1424. if (!host.getHostByAddr(ntohl(sin.sin_addr.s_addr)))
  1425. return null;
  1426. return host.name;
  1427. }
  1428. }
  1429. /**
  1430. * Parse an IPv4 address string in the dotted-decimal form $(I a.b.c.d)
  1431. * and return the number.
  1432. * Returns: If the string is not a legitimate IPv4 address,
  1433. * $(D ADDR_NONE) is returned.
  1434. */
  1435. static uint parse(in char[] addr)
  1436. {
  1437. return ntohl(inet_addr(std.string.toStringz(addr)));
  1438. }
  1439. /**
  1440. * Convert an IPv4 address number in host byte order to a human readable
  1441. * string representing the IPv4 address in dotted-decimal form.
  1442. */
  1443. static string addrToString(uint addr)
  1444. {
  1445. in_addr sin_addr;
  1446. sin_addr.s_addr = htonl(addr);
  1447. return to!string(inet_ntoa(sin_addr));
  1448. }
  1449. }
  1450. unittest
  1451. {
  1452. softUnittest({
  1453. const InternetAddress ia = new InternetAddress("63.105.9.61", 80);
  1454. assert(ia.toString() == "63.105.9.61:80");
  1455. });
  1456. softUnittest({
  1457. // test reverse lookup
  1458. auto ih = new InternetHost;
  1459. if (ih.getHostByName("digitalmars.com"))
  1460. {
  1461. const ia = new InternetAddress(ih.addrList[0], 80);
  1462. assert(ia.toHostNameString() == "digitalmars.com");
  1463. if (getnameinfoPointer)
  1464. {
  1465. // test reverse lookup, via gethostbyaddr
  1466. auto getnameinfoPointerBackup = getnameinfoPointer;
  1467. getnameinfoPointer = null;
  1468. scope(exit) getnameinfoPointer = getnameinfoPointerBackup;
  1469. assert(ia.toHostNameString() == "digitalmars.com");
  1470. }
  1471. }
  1472. });
  1473. version (SlowTests)
  1474. softUnittest({
  1475. // test failing reverse lookup
  1476. const InternetAddress ia = new InternetAddress("127.114.111.120", 80);
  1477. assert(ia.toHostNameString() is null);
  1478. if (getnameinfoPointer)
  1479. {
  1480. // test failing reverse lookup, via gethostbyaddr
  1481. auto getnameinfoPointerBackup = getnameinfoPointer;
  1482. getnameinfoPointer = null;
  1483. scope(exit) getnameinfoPointer = getnameinfoPointerBackup;
  1484. assert(ia.toHostNameString() is null);
  1485. }
  1486. });
  1487. }
  1488. /**
  1489. * $(D Internet6Address) encapsulates an IPv6 (Internet Protocol version 6)
  1490. * socket address.
  1491. *
  1492. * Consider using $(D getAddress), $(D parseAddress) and $(D Address) methods
  1493. * instead of using this class directly.
  1494. */
  1495. class Internet6Address: Address
  1496. {
  1497. protected:
  1498. sockaddr_in6 sin6;
  1499. this()
  1500. {
  1501. }
  1502. public:
  1503. override @property sockaddr* name()
  1504. {
  1505. return cast(sockaddr*)&sin6;
  1506. }
  1507. override @property const(sockaddr)* name() const
  1508. {
  1509. return cast(const(sockaddr)*)&sin6;
  1510. }
  1511. override @property socklen_t nameLen() const
  1512. {
  1513. return cast(socklen_t) sin6.sizeof;
  1514. }
  1515. /// Any IPv6 host address.
  1516. static @property ref const(ubyte)[16] ADDR_ANY()
  1517. {
  1518. const(ubyte)[16]* addr;
  1519. static if (is(typeof(IN6ADDR_ANY)))
  1520. return addr = &IN6ADDR_ANY.s6_addr, *addr;
  1521. else
  1522. static if (is(typeof(in6addr_any)))
  1523. return addr = &i