[System] Common NetworkInformation code from referencesource
[mono.git] / mcs / class / referencesource / System / net / System / Net / NetworkInformation / SystemTcpConnection.cs
1
2
3 namespace System.Net.NetworkInformation {
4
5     using System.Net;
6     using System.Net.Sockets;
7     using System.Security.Permissions;
8     using System;
9     using System.Runtime.InteropServices;
10     using System.Collections;
11     using System.ComponentModel;
12     using System.Threading;
13
14    
15
16
17     /// <summary>
18     /// Represents an active Tcp connection.</summary>
19     internal class SystemTcpConnectionInformation:TcpConnectionInformation {
20         IPEndPoint localEndPoint;
21         IPEndPoint remoteEndPoint;
22         TcpState state;
23 #if MONO
24         public SystemTcpConnectionInformation (IPEndPoint local, IPEndPoint remote, TcpState state)
25         {
26             localEndPoint = local;
27             remoteEndPoint = remote;
28             this.state = state;
29         }
30 #else
31         internal SystemTcpConnectionInformation(MibTcpRow row) {
32             state = row.state;
33
34             //port is returned in Big-Endian - most significant bit on left
35             //unfortunately, its done at the word level and not the dword level.
36
37             int localPort = row.localPort1<<8|row.localPort2;
38             int remotePort = ((state == TcpState.Listen)?0:row.remotePort1<<8|row.remotePort2);
39
40             localEndPoint = new IPEndPoint(row.localAddr,(int)localPort);
41             remoteEndPoint= new IPEndPoint(row.remoteAddr,(int)remotePort);
42         }
43
44         // IPV6 version of the Tcp row 
45         internal SystemTcpConnectionInformation(MibTcp6RowOwnerPid row) {
46             state = row.state;
47
48             //port is returned in Big-Endian - most significant bit on left
49             //unfortunately, its done at the word level and not the dword level.
50             
51             int localPort = row.localPort1 << 8 | row.localPort2;
52             int remotePort = ((state == TcpState.Listen) ? 0 : row.remotePort1 << 8 | row.remotePort2);
53
54             localEndPoint = new IPEndPoint(new IPAddress(row.localAddr, row.localScopeId), (int)localPort);
55             remoteEndPoint = new IPEndPoint(new IPAddress(row.remoteAddr, row.remoteScopeId), (int)remotePort);
56         }
57 #endif
58
59         public override TcpState State{get {return state;}}
60         public override IPEndPoint LocalEndPoint{get {return localEndPoint;}}
61         public override IPEndPoint RemoteEndPoint{get {return remoteEndPoint;}}
62     }
63  }
64