mscorlib(4.0.0.0) API with additions
SocketAddress.cs
2 using System.Net.Sockets;
3 using System.Text;
4 
5 namespace System.Net
6 {
8  [global::__DynamicallyInvokable]
9  public class SocketAddress
10  {
11  internal const int IPv6AddressSize = 28;
12 
13  internal const int IPv4AddressSize = 16;
14 
15  internal int m_Size;
16 
17  internal byte[] m_Buffer;
18 
19  private const int WriteableOffset = 2;
20 
21  private const int MaxSize = 32;
22 
23  private bool m_changed = true;
24 
25  private int m_hash;
26 
29  [global::__DynamicallyInvokable]
30  public AddressFamily Family
31  {
32  [global::__DynamicallyInvokable]
33  get
34  {
35  return (AddressFamily)(m_Buffer[0] | (m_Buffer[1] << 8));
36  }
37  }
38 
41  [global::__DynamicallyInvokable]
42  public int Size
43  {
44  [global::__DynamicallyInvokable]
45  get
46  {
47  return m_Size;
48  }
49  }
50 
55  [global::__DynamicallyInvokable]
56  public byte this[int offset]
57  {
58  [global::__DynamicallyInvokable]
59  get
60  {
61  if (offset < 0 || offset >= Size)
62  {
63  throw new IndexOutOfRangeException();
64  }
65  return m_Buffer[offset];
66  }
67  [global::__DynamicallyInvokable]
68  set
69  {
70  if (offset < 0 || offset >= Size)
71  {
72  throw new IndexOutOfRangeException();
73  }
74  if (m_Buffer[offset] != value)
75  {
76  m_changed = true;
77  }
78  m_Buffer[offset] = value;
79  }
80  }
81 
84  [global::__DynamicallyInvokable]
86  : this(family, 32)
87  {
88  }
89 
95  [global::__DynamicallyInvokable]
96  public SocketAddress(AddressFamily family, int size)
97  {
98  if (size < 2)
99  {
100  throw new ArgumentOutOfRangeException("size");
101  }
102  m_Size = size;
103  m_Buffer = new byte[(size / IntPtr.Size + 2) * IntPtr.Size];
104  m_Buffer[0] = (byte)family;
105  m_Buffer[1] = (byte)((int)family >> 8);
106  }
107 
108  internal SocketAddress(IPAddress ipAddress)
109  : this(ipAddress.AddressFamily, (ipAddress.AddressFamily == AddressFamily.InterNetwork) ? 16 : 28)
110  {
111  m_Buffer[2] = 0;
112  m_Buffer[3] = 0;
113  if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
114  {
115  m_Buffer[4] = 0;
116  m_Buffer[5] = 0;
117  m_Buffer[6] = 0;
118  m_Buffer[7] = 0;
119  long scopeId = ipAddress.ScopeId;
120  m_Buffer[24] = (byte)scopeId;
121  m_Buffer[25] = (byte)(scopeId >> 8);
122  m_Buffer[26] = (byte)(scopeId >> 16);
123  m_Buffer[27] = (byte)(scopeId >> 24);
124  byte[] addressBytes = ipAddress.GetAddressBytes();
125  for (int i = 0; i < addressBytes.Length; i++)
126  {
127  m_Buffer[8 + i] = addressBytes[i];
128  }
129  }
130  else
131  {
132  m_Buffer[4] = (byte)ipAddress.m_Address;
133  m_Buffer[5] = (byte)(ipAddress.m_Address >> 8);
134  m_Buffer[6] = (byte)(ipAddress.m_Address >> 16);
135  m_Buffer[7] = (byte)(ipAddress.m_Address >> 24);
136  }
137  }
138 
139  internal SocketAddress(IPAddress ipaddress, int port)
140  : this(ipaddress)
141  {
142  m_Buffer[2] = (byte)(port >> 8);
143  m_Buffer[3] = (byte)port;
144  }
145 
146  internal IPAddress GetIPAddress()
147  {
148  if (Family == AddressFamily.InterNetworkV6)
149  {
150  byte[] array = new byte[16];
151  for (int i = 0; i < array.Length; i++)
152  {
153  array[i] = m_Buffer[i + 8];
154  }
155  long scopeid = (m_Buffer[27] << 24) + (m_Buffer[26] << 16) + (m_Buffer[25] << 8) + m_Buffer[24];
156  return new IPAddress(array, scopeid);
157  }
158  if (Family == AddressFamily.InterNetwork)
159  {
160  long newAddress = ((m_Buffer[4] & 0xFF) | ((m_Buffer[5] << 8) & 0xFF00) | ((m_Buffer[6] << 16) & 0xFF0000) | (m_Buffer[7] << 24)) & uint.MaxValue;
161  return new IPAddress(newAddress);
162  }
163  throw new SocketException(SocketError.AddressFamilyNotSupported);
164  }
165 
166  internal IPEndPoint GetIPEndPoint()
167  {
168  IPAddress iPAddress = GetIPAddress();
169  int port = ((m_Buffer[2] << 8) & 0xFF00) | m_Buffer[3];
170  return new IPEndPoint(iPAddress, port);
171  }
172 
173  internal void CopyAddressSizeIntoBuffer()
174  {
175  m_Buffer[m_Buffer.Length - IntPtr.Size] = (byte)m_Size;
176  m_Buffer[m_Buffer.Length - IntPtr.Size + 1] = (byte)(m_Size >> 8);
177  m_Buffer[m_Buffer.Length - IntPtr.Size + 2] = (byte)(m_Size >> 16);
178  m_Buffer[m_Buffer.Length - IntPtr.Size + 3] = (byte)(m_Size >> 24);
179  }
180 
181  internal int GetAddressSizeOffset()
182  {
183  return m_Buffer.Length - IntPtr.Size;
184  }
185 
186  internal unsafe void SetSize(IntPtr ptr)
187  {
188  m_Size = *(int*)(void*)ptr;
189  }
190 
195  [global::__DynamicallyInvokable]
196  public override bool Equals(object comparand)
197  {
198  SocketAddress socketAddress = comparand as SocketAddress;
199  if (socketAddress == null || Size != socketAddress.Size)
200  {
201  return false;
202  }
203  for (int i = 0; i < Size; i++)
204  {
205  if (this[i] != socketAddress[i])
206  {
207  return false;
208  }
209  }
210  return true;
211  }
212 
215  [global::__DynamicallyInvokable]
216  public override int GetHashCode()
217  {
218  if (m_changed)
219  {
220  m_changed = false;
221  m_hash = 0;
222  int num = Size & -4;
223  int i;
224  for (i = 0; i < num; i += 4)
225  {
226  m_hash ^= (m_Buffer[i] | (m_Buffer[i + 1] << 8) | (m_Buffer[i + 2] << 16) | (m_Buffer[i + 3] << 24));
227  }
228  if ((Size & 3) != 0)
229  {
230  int num2 = 0;
231  int num3 = 0;
232  for (; i < Size; i++)
233  {
234  num2 |= m_Buffer[i] << num3;
235  num3 += 8;
236  }
237  m_hash ^= num2;
238  }
239  }
240  return m_hash;
241  }
242 
245  [global::__DynamicallyInvokable]
246  public override string ToString()
247  {
248  StringBuilder stringBuilder = new StringBuilder();
249  for (int i = 2; i < Size; i++)
250  {
251  if (i > 2)
252  {
253  stringBuilder.Append(",");
254  }
255  stringBuilder.Append(this[i].ToString(NumberFormatInfo.InvariantInfo));
256  }
257  return Family.ToString() + ":" + Size.ToString(NumberFormatInfo.InvariantInfo) + ":{" + stringBuilder.ToString() + "}";
258  }
259  }
260 }
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
The exception that is thrown when a socket error occurs.
AddressFamily AddressFamily
Gets the address family of the IP address.
Definition: IPAddress.cs:111
SocketAddress(AddressFamily family, int size)
Creates a new instance of the T:System.Net.SocketAddress class using the specified address family and...
override string ToString()
Returns information about the socket address.
Provides an Internet Protocol (IP) address.
Definition: IPAddress.cs:10
AddressFamily Family
Gets the T:System.Net.Sockets.AddressFamily enumerated value of the current T:System....
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
The exception that is thrown when an attempt is made to access an element of an array or collection w...
AddressFamily
Specifies the addressing scheme that an instance of the T:System.Net.Sockets.Socket class can use.
Definition: AddressFamily.cs:5
override bool Equals(object comparand)
Determines whether the specified Object is equal to the current Object.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
SocketAddress(AddressFamily family)
Creates a new instance of the T:System.Net.SocketAddress class for the given address family.
Stores serialized information from T:System.Net.EndPoint derived classes.
Definition: SocketAddress.cs:9
long ScopeId
Gets or sets the IPv6 address scope identifier.
Definition: IPAddress.cs:128
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
static int Size
Gets the size of this instance.
Definition: IntPtr.cs:27
byte [] GetAddressBytes()
Provides a copy of the T:System.Net.IPAddress as an array of bytes.
Definition: IPAddress.cs:473
SocketError
Defines error codes for the T:System.Net.Sockets.Socket class.
Definition: SocketError.cs:5
static NumberFormatInfo InvariantInfo
Gets a read-only T:System.Globalization.NumberFormatInfo object that is culture-independent (invarian...
int Size
Gets the underlying buffer size of the T:System.Net.SocketAddress.
Provides culture-specific information for formatting and parsing numeric values.
override int GetHashCode()
Serves as a hash function for a particular type, suitable for use in hashing algorithms and data stru...