public void Stop() { var tmp = new List<ICaptureDevice>(_devices); foreach (var device in tmp) StopCapture(device); tmp.Clear(); _devices.Clear(); FoundConnection = false; if (_currentSession != null) { _currentSession.Dispose(); _currentSession = null; } }
void device_OnPacketArrival(object sender, CaptureEventArgs e) { var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data); // Check if IP packet IpPacket ipPacket = packet.Extract(typeof(IpPacket)) as IpPacket; if (ipPacket == null) return; if (!CheckIfCorrectIP(ipPacket.SourceAddress.ToString()) && !CheckIfCorrectIP(ipPacket.DestinationAddress.ToString())) { return; } TcpPacket tcpPacket = packet.Extract(typeof(TcpPacket)) as TcpPacket; if (tcpPacket == null) return; if (!FoundConnection) { if (tcpPacket.Syn && tcpPacket.Ack) { // Found new connection. // Check version... if (frmMain.Instance.CheckRunningEXEVersion()) { // Correct version FoundConnection = true; _currentPortMap = new KeyValuePair<ushort, ushort>(tcpPacket.DestinationPort, tcpPacket.SourcePort); _currentSession = new Session(); MasterThread.Instance.AddCallback((a) => { using (MaplePacket p = new MaplePacket(0xEE00)) { p.WriteBool(true); p.WriteString(ipPacket.SourceAddress.ToString()); p.WriteUShort(tcpPacket.SourcePort); p.SwitchOver(); p.Reset(0); ServerConnection.Instance.ForwardPacket(MaplePacket.CommunicationType.ClientPacket, p); } }); Logger.WriteLine("[CON] New connection found on {0}!", e.Device.Description); if (cache != 0) { _currentSession.SetOutboundSequence(cache); cache = 0; } _currentSession.BufferTCPPacket(tcpPacket, !(_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort)); } else { Logger.WriteLine("Incorrect MapleStory version. Ignoring connection."); } } else if (tcpPacket.Syn && !tcpPacket.Ack) // Heh fix { cache = (int)(tcpPacket.SequenceNumber + 1); } } else if (FoundConnection && ( (_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort) || (_currentPortMap.Value == tcpPacket.SourcePort && _currentPortMap.Key == tcpPacket.DestinationPort) ) ) { if (tcpPacket.Fin || tcpPacket.Rst) { FoundConnection = false; Logger.WriteLine("[CON] Connection Lost"); MasterThread.Instance.AddCallback((a) => { using (MaplePacket p = new MaplePacket(0xEE00)) { p.WriteBool(false); p.SwitchOver(); p.Reset(0); ServerConnection.Instance.ForwardPacket(MaplePacket.CommunicationType.ClientPacket, p); } }); return; } bool result = _currentSession.BufferTCPPacket(tcpPacket, !(_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort)); if (!result) { FoundConnection = false; _currentSession.Dispose(); _currentSession = null; } } else { // Logger.WriteLine("[DEBUG] {0} - {1} {2}", FoundConnection, tcpPacket.SourcePort, tcpPacket.DestinationPort); } }