File: SystemNetHelpers.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (90 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace System.Net.PeerToPeer
{
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Permissions;

    internal static class SystemNetHelpers
    {
        internal const int IPv6AddressSize = 28;
        internal const int IPv4AddressSize = 16;
        internal static byte[] SOCKADDRFromIPEndPoint(IPEndPoint ipEndPoint)
        {
            byte[] buffer = new byte[ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ? IPv6AddressSize : IPv4AddressSize];
#if BIGENDIAN
            buffer[0] = unchecked((byte)((int)family>>8));
            buffer[1] = unchecked((byte)((int)family   ));
#else
            buffer[0] = unchecked((byte)((int)ipEndPoint.AddressFamily));
            buffer[1] = unchecked((byte)((int)ipEndPoint.AddressFamily >> 8));
#endif
            if (ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
            {
                buffer[2] = (byte)(ipEndPoint.Port >> 8);
                buffer[3] = (byte)ipEndPoint.Port;

                buffer[4] = (byte)0;
                buffer[5] = (byte)0;
                buffer[6] = (byte)0;
                buffer[7] = (byte)0;

                long scope = ipEndPoint.Address.ScopeId;
                buffer[24] = (byte)scope;
                buffer[25] = (byte)(scope >> 8);
                buffer[26] = (byte)(scope >> 16);
                buffer[27] = (byte)(scope >> 24);

                byte[] addressBytes = ipEndPoint.Address.GetAddressBytes();
                for (int i = 0; i < addressBytes.Length; i++)
                {
                    buffer[8 + i] = addressBytes[i];
                }
            }
            else
            {
                buffer[2] = unchecked((byte)(ipEndPoint.Port >> 8));
                buffer[3] = unchecked((byte)(ipEndPoint.Port));
                byte[] addressBytes = ipEndPoint.Address.GetAddressBytes();
                for (int i = 0; i < addressBytes.Length; i++)
                {
                    buffer[4 + i] = addressBytes[i];
                }
            }
            return buffer;
        }
        internal static IPEndPoint IPEndPointFromSOCKADDRBuffer(byte[] buffer)
        {
            IPAddress ip = null;
            int addressFamily = 0;
#if BIGENDIAN
            addressFamily = buffer[1] + ((int)buffer[0] << 8);
#else
            addressFamily = buffer[0] + ((int)buffer[1] << 8);
#endif
            //Get port
            int port = buffer[3] + ((int)buffer[2] << 8);

            if ((AddressFamily)addressFamily == AddressFamily.InterNetwork)
            {
                byte[] v4bytes = new byte[] { buffer[4], buffer[5], buffer[6], buffer[7] };
                ip = new IPAddress(v4bytes);
            }
            else if ((AddressFamily)addressFamily == AddressFamily.InterNetworkV6)
            {
                byte[] v6Bytes = new byte[16];
                for (int i = 0; i < 16; i++)
                    v6Bytes[i] = buffer[8 + i];
                long scope = ((long)(long)buffer[24] + ((long)buffer[25] << 8) + ((long)buffer[26] << 16) + ((long)buffer[27] << 24));
                ip = new IPAddress(v6Bytes);
                ip.ScopeId = scope;
            }

            return new IPEndPoint(ip, port);

        }
    }
}