PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/inet/dotnet/IpAddrPeer.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 191 lines | 135 code | 29 blank | 27 comment | 9 complexity | 85b9582a12e8ce3789edcb101907930a MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2007, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 21 Nov 07 Andy Frank Ported to .NET
  7. //
  8. using System;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using Fan.Sys;
  12. namespace Fan.Inet
  13. {
  14. public class IpAddrPeer
  15. {
  16. //////////////////////////////////////////////////////////////////////////
  17. // Peer Factory
  18. //////////////////////////////////////////////////////////////////////////
  19. public static IpAddrPeer make(IpAddr fan)
  20. {
  21. return new IpAddrPeer();
  22. }
  23. //////////////////////////////////////////////////////////////////////////
  24. // Constructors
  25. //////////////////////////////////////////////////////////////////////////
  26. public static IpAddr make(string str)
  27. {
  28. try
  29. {
  30. return make(str, Dns.GetHostAddresses(str)[0]);
  31. }
  32. catch (SocketException e)
  33. {
  34. throw UnknownHostErr.make(e.Message).val;
  35. }
  36. }
  37. public static List makeAll(string str)
  38. {
  39. try
  40. {
  41. IPAddress[] addr = Dns.GetHostAddresses(str);
  42. List list = new List(Fan.Sys.Sys.ObjType, addr.Length); //IpAddr.$Type, addr.length);
  43. for (int i=0; i<addr.Length; i++)
  44. list.add(make(str, addr[i]));
  45. return list;
  46. }
  47. catch (SocketException e)
  48. {
  49. throw UnknownHostErr.make(e.Message).val;
  50. }
  51. }
  52. public static IpAddr makeBytes(Buf bytes)
  53. {
  54. try
  55. {
  56. MemBuf mb = bytes as MemBuf;
  57. IPAddress dotnet = new IPAddress(mb.bytes());
  58. return make(dotnet.ToString(), dotnet);
  59. }
  60. catch (SocketException e)
  61. {
  62. throw ArgErr.make(e.Message).val;
  63. }
  64. }
  65. public static IpAddr local()
  66. {
  67. if (m_local == null)
  68. {
  69. try
  70. {
  71. string hostName = Dns.GetHostName();
  72. // TODO - not sure the correct behavoir here, but we seem
  73. // to get IPv6 addresses first, so for now at least, lets
  74. // attempt to use the IPv4 address
  75. IPAddress dotnet = null;
  76. IPAddress[] addr = Dns.GetHostAddresses(hostName);
  77. for (int i=0; i<addr.Length; i++)
  78. if (addr[i].AddressFamily == AddressFamily.InterNetwork)
  79. dotnet = addr[i];
  80. m_local = make(hostName, dotnet);
  81. }
  82. catch (Exception)
  83. {
  84. try
  85. {
  86. // fallback to explicit loopback
  87. IPAddress dotnet = new IPAddress(new byte[] {127, 0, 0, 1});
  88. m_local = make(dotnet.ToString(), dotnet);
  89. }
  90. catch (Exception ignore)
  91. {
  92. // should never happen
  93. Err.dumpStack(ignore);
  94. }
  95. }
  96. }
  97. return m_local;
  98. }
  99. public static IpAddr make(IPAddress dotnet)
  100. {
  101. return make(dotnet.ToString(), dotnet);
  102. }
  103. public static IpAddr make(string str, IPAddress dotnet)
  104. {
  105. IpAddr fan = IpAddr.internalMake();
  106. fan.m_peer.m_str = str;
  107. fan.m_peer.m_dotnet = dotnet;
  108. return fan;
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. // Identity
  112. //////////////////////////////////////////////////////////////////////////
  113. public long hash(IpAddr fan)
  114. {
  115. return m_dotnet.GetHashCode();
  116. }
  117. public bool Equals(IpAddr fan, object obj)
  118. {
  119. if (obj is IpAddr)
  120. return this.m_dotnet.Equals(((IpAddr)obj).m_peer.m_dotnet);
  121. else
  122. return false;
  123. }
  124. public string toStr(IpAddr fan)
  125. {
  126. return m_str;
  127. }
  128. //////////////////////////////////////////////////////////////////////////
  129. // Methods
  130. //////////////////////////////////////////////////////////////////////////
  131. public bool isIPv4(IpAddr fan)
  132. {
  133. return m_dotnet.AddressFamily == AddressFamily.InterNetwork;
  134. }
  135. public bool isIPv6(IpAddr fan)
  136. {
  137. return m_dotnet.AddressFamily == AddressFamily.InterNetworkV6;
  138. }
  139. public Buf bytes(IpAddr fan)
  140. {
  141. return new MemBuf(m_dotnet.GetAddressBytes());
  142. }
  143. public string numeric(IpAddr fan)
  144. {
  145. return m_dotnet.ToString();
  146. }
  147. public string hostname(IpAddr fan)
  148. {
  149. return Dns.GetHostEntry(m_dotnet).HostName;
  150. }
  151. public Object toNative(IpAddr fan)
  152. {
  153. return m_dotnet;
  154. }
  155. //////////////////////////////////////////////////////////////////////////
  156. // Fields
  157. //////////////////////////////////////////////////////////////////////////
  158. private static IpAddr m_local;
  159. public string m_str;
  160. public IPAddress m_dotnet;
  161. }
  162. }