Esempio n. 1
0
 private void W_PopPacket(object sender, byte[] e)
 {
     if (DebugLogger.LogNeeded())
     {
         var _ = DebugLogger.LogPacketWithTimestamp(e);
     }
     PacketPoped?.Invoke(sender, e);
 }
Esempio n. 2
0
        public async void Init()
        {
            if (running)
            {
                return;
            }
            adapterFactory = await AdapterConfig.GetAdapterFactoryFromDefaultFile();

            running     = true;
            taskChannel = Channel.CreateUnbounded <Action>(new UnboundedChannelOptions()
            {
                SingleReader = true
            });
            var _ = Task.Run(() => doWork());

            wintun.PacketPoped       += W_PopPacket;
            TcpSocket.EstablishedTcp += W_EstablishTcp;

            wintun.Init();
            int i = 0;

            while (running)
            {
                i++;
                await executeLwipTask(() =>
                {
                    wintun.CheckTimeout();
                    return(0);
                }).ConfigureAwait(false);

                if (i % 10 == 0)
                {
                    tunAdapters.RemoveAll(w => !w.TryGetTarget(out var a) /* TODO: || a.IsShutdown == 1 */);
                    if (DebugLogger.LogNeeded())
                    {
                        DebugLogger.Log("# of connections in local stack: " + ConnectionCount);
                        DebugLogger.Log($"# of open/all adapters: {TunSocketAdapter.OpenCount} {tunAdapters.Count}");
                        DebugLogger.Log($"# of recv/send: {TunSocketAdapter.RecvingCount} {TunSocketAdapter.SendingCount}");
                    }
                }
                await Task.Delay(250).ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        public void PushPacket([ReadOnlyArray] byte[] packet)
        {
            // Packets must contain valid IPv4 headers
            if (packet.Length < 20 || packet[0] >> 4 != 4)
            {
                return;
            }
            var proto = packet[9];

            switch (proto)
            {
            case 6:     // TCP
                break;

            case 17:     // UDP
                break;

            default:
                return;
            }
            if (DebugLogger.LogNeeded())
            {
                var _ = DebugLogger.LogPacketWithTimestamp(packet);
            }
            if (proto == 6)
            {
                _ = executeLwipTask(() => wintun.PushPacket(packet));
            }
            else
            {
                try
                {
                    TunDatagramAdapter.ProcessIpPayload(packet, this);
                }
                catch (Exception ex)
                {
                    DebugLogger.Log("Error processing udp ip packet: " + ex.ToString());
                }
            }
        }