Esempio n. 1
0
 public static void ProcessBytes(byte[] inputData, IPEndPoint endpoint, Dictionary <int, Action <byte[], Guid, IPEndPoint> > callbacks)
 {
     if (inputData.Length >= 20)
     {
         if (inputData[4] == 33 && inputData[5] == 18 && inputData[6] == 164 && inputData[7] == 66)
         {
             UdpStun.ProcessStun(inputData);
         }
     }
     for (int i = 0; i < magicBytes.Length; i++)
     {
         if (magicBytes[i] != inputData[i])
         {
             return;
         }
     }
     lock (tempGuidBytes)
     {
         int bytesToProcess = inputData.Length - 4;
         if (bytesToProcess < 16)
         {
             return;
         }
         bytesToProcess -= 16;
         Array.Copy(inputData, 4, tempGuidBytes, 0, 16);
         Guid recvGuid = new Guid(tempGuidBytes);
         if (bytesToProcess < 4)
         {
             return;
         }
         Array.Copy(inputData, 20, tempType, 0, 4);
         FlipEndian(ref tempType);
         int dataType = BitConverter.ToInt32(tempType, 0);
         //Console.WriteLine("Processed " + inputData.Length + " type: " + dataType);
         if (callbacks.ContainsKey(dataType))
         {
             callbacks[dataType](inputData, recvGuid, endpoint);
         }
     }
 }
Esempio n. 2
0
 public void Run()
 {
     try
     {
         clientSocketv4 = new UdpClient(0, AddressFamily.InterNetwork);
     }
     catch (Exception e)
     {
         DebugLog("Error setting up v4 socket: " + e);
         clientSocketv4 = null;
     }
     try
     {
         clientSocketv6 = new UdpClient(0, AddressFamily.InterNetworkV6);
     }
     catch (Exception e)
     {
         DebugLog("Error setting up v6 socket: " + e);
         clientSocketv6 = null;
     }
     if (clientSocketv4 != null)
     {
         clientSocketv4.BeginReceive(HandleReceive, clientSocketv4);
     }
     if (clientSocketv6 != null)
     {
         clientSocketv6.BeginReceive(HandleReceive, clientSocketv6);
     }
     if (clientSocketv4 != null)
     {
         int v4portNumber = ((IPEndPoint)clientSocketv4.Client.LocalEndPoint).Port;
         DebugLog("Listening on port v4:" + v4portNumber);
         foreach (IPAddress addr in myAddresses)
         {
             if (UdpMeshCommon.IsIPv4(addr))
             {
                 me.AddRemoteEndpoint(new IPEndPoint(addr, v4portNumber));
             }
         }
     }
     if (clientSocketv6 != null)
     {
         int v6portNumber = ((IPEndPoint)clientSocketv6.Client.LocalEndPoint).Port;
         DebugLog("Listening on port v6:" + v6portNumber);
         foreach (IPAddress addr in myAddresses)
         {
             if (UdpMeshCommon.IsIPv6(addr))
             {
                 me.AddRemoteEndpoint(new IPEndPoint(addr, v6portNumber));
             }
         }
     }
     while (!shutdown && !error && clientSocketv4 != null && clientSocketv6 != null)
     {
         //Restun every 5 minutes
         if ((DateTime.UtcNow.Ticks - receivedStun) > (TimeSpan.TicksPerMinute * 5))
         {
             if (clientSocketv4 != null)
             {
                 UdpStun.RequestRemoteIPv4(clientSocketv4);
                 UdpStun.RequestRemoteIPv6(clientSocketv6);
             }
         }
         SendServerInfo();
         SendClientsHello();
         System.Threading.Thread.Sleep(10000);
     }
     Shutdown();
 }