IPEndPoint.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // System.Net.IPEndPoint.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Net.Sockets;
  30. namespace System.Net {
  31. [Serializable]
  32. public class IPEndPoint : EndPoint {
  33. private IPAddress address;
  34. private int port;
  35. public const int MaxPort = 65535;
  36. public const int MinPort = 0;
  37. public IPEndPoint (IPAddress address, int port)
  38. {
  39. if (address == null)
  40. throw new ArgumentNullException ("Value cannot be null");
  41. Address = address;
  42. Port = port;
  43. }
  44. public IPEndPoint (long iaddr, int port) : this (new IPAddress (iaddr), port)
  45. {
  46. }
  47. public IPAddress Address {
  48. get {
  49. return (address);
  50. }
  51. set {
  52. address=value;
  53. }
  54. }
  55. public override AddressFamily AddressFamily {
  56. get {
  57. return address.AddressFamily;
  58. }
  59. }
  60. public int Port {
  61. get {
  62. return port;
  63. }
  64. set {
  65. // LAMESPEC: no mention of sanity checking
  66. // PS: MS controls the range when setting the value
  67. if (value < MinPort || value > MaxPort)
  68. throw new ArgumentOutOfRangeException ("Invalid port");
  69. port = value;
  70. }
  71. }
  72. // bytes 2 and 3 store the port, the rest
  73. // stores the address
  74. public override EndPoint Create(SocketAddress sockaddr) {
  75. int size=sockaddr.Size;
  76. // LAMESPEC: no mention of what to do if
  77. // sockaddr is bogus
  78. if(size<8) {
  79. // absolute minimum amount needed for
  80. // an address family, buffer size,
  81. // port and address
  82. return(null);
  83. }
  84. AddressFamily family=(AddressFamily)sockaddr[0];
  85. int port;
  86. IPEndPoint ipe = null;
  87. switch(family)
  88. {
  89. case AddressFamily.InterNetwork:
  90. port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
  91. long address=(((long)sockaddr[7])<<24) +
  92. (((long)sockaddr[6])<<16) +
  93. (((long)sockaddr[5])<<8) +
  94. (long)sockaddr[4];
  95. ipe = new IPEndPoint(address, port);
  96. break;
  97. #if NET_1_1
  98. case AddressFamily.InterNetworkV6:
  99. port = (((int)sockaddr[2])<<8) + (int)sockaddr[3];
  100. /// maybe flowid ?
  101. /*
  102. int unknown = (int)sockaddr[4] +
  103. (((int)sockaddr[5])<<8) +
  104. (((int)sockaddr[6])<<16) +
  105. (((int)sockaddr[7])<<24);
  106. */
  107. int scopeId = (int)sockaddr[24] +
  108. (((int)sockaddr[25])<<8) +
  109. (((int)sockaddr[26])<<16) +
  110. (((int)sockaddr[27])<<24);
  111. ushort[] addressData = new ushort[8];
  112. for(int i=0; i<8; i++)
  113. addressData[i] = (ushort)((sockaddr[8+i*2] << 8) + sockaddr[8+i*2+1]);
  114. ipe = new IPEndPoint (new IPAddress(addressData, scopeId), port);
  115. break;
  116. #endif
  117. default:
  118. return null;
  119. }
  120. return(ipe);
  121. }
  122. public override SocketAddress Serialize() {
  123. SocketAddress sockaddr = null;
  124. switch (address.AddressFamily)
  125. {
  126. case AddressFamily.InterNetwork:
  127. // .net produces a 16 byte buffer, even though
  128. // only 8 bytes are used. I guess its just a
  129. // holdover from struct sockaddr padding.
  130. sockaddr = new SocketAddress(AddressFamily.InterNetwork, 16);
  131. // bytes 2 and 3 store the port, the rest
  132. // stores the address
  133. sockaddr [2] = (byte) ((port>>8) & 0xff);
  134. sockaddr [3] = (byte) (port & 0xff);
  135. long addr = address.InternalIPv4Address;
  136. sockaddr [4] = (byte) (addr & 0xff);
  137. sockaddr [5] = (byte) ((addr >> 8) & 0xff);
  138. sockaddr [6] = (byte) ((addr >> 16) & 0xff);
  139. sockaddr [7] = (byte) ((addr >> 24) & 0xff);
  140. break;
  141. #if NET_1_1
  142. case AddressFamily.InterNetworkV6:
  143. sockaddr = new SocketAddress(AddressFamily.InterNetworkV6, 28);
  144. sockaddr [2] = (byte) ((port>>8) & 0xff);
  145. sockaddr [3] = (byte) (port & 0xff);
  146. byte[] addressBytes = address.GetAddressBytes();
  147. for(int i=0; i<16; i++)
  148. sockaddr[8+i] = addressBytes[i];
  149. sockaddr [24] = (byte) (address.ScopeId & 0xff);
  150. sockaddr [25] = (byte) ((address.ScopeId >> 8) & 0xff);
  151. sockaddr [26] = (byte) ((address.ScopeId >> 16) & 0xff);
  152. sockaddr [27] = (byte) ((address.ScopeId >> 24) & 0xff);
  153. break;
  154. #endif
  155. }
  156. return(sockaddr);
  157. }
  158. public override string ToString() {
  159. return(address.ToString() + ":" + port);
  160. }
  161. public override bool Equals (Object obj)
  162. {
  163. IPEndPoint p = obj as IPEndPoint;
  164. return p != null &&
  165. p.port == port &&
  166. p.address.Equals (address);
  167. }
  168. public override int GetHashCode ()
  169. {
  170. return address.GetHashCode () + port;
  171. }
  172. }
  173. }