public override bool Equals(System.Object obj) { // If parameter is null return false. if (obj == null) { return(false); } // If parameter cannot be cast to Point return false. TcpSessionKey session = obj as TcpSessionKey; if ((System.Object)session == null) { return(false); } // Return true if the fields match: return((session.DestIP == this.DestIP && session.DestPort == this.DestPort) && (session.SrcIP == this.SrcIP && session.SrcPort == this.SrcPort)); }
private static void HandleTcp(Packet packet, int threadID) { TcpLayer tcpLayer = (TcpLayer)packet.Ethernet.IpV4.Tcp.ExtractLayer(); MyDevice newDev = GetDevice(threadID, "other"); IpV4Layer ipLayer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer(); PayloadLayer tcpPayload = (PayloadLayer)packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer(); TcpSessionKey tcpKey1 = new TcpSessionKey(ipLayer.Source.ToString(), tcpLayer.SourcePort, ipLayer.Destination.ToString(), tcpLayer.DestinationPort); TcpSessionKey tcpKey2 = new TcpSessionKey(ipLayer.Destination.ToString(), tcpLayer.DestinationPort, ipLayer.Source.ToString(), tcpLayer.SourcePort); bool newSession = false; TcpSessionValue currentSession = null; try { currentSession = sessionDic[tcpKey1]; } catch { try { currentSession = sessionDic[tcpKey2]; } catch { newSession = true; TcpSessionKey tcpKeyN = new TcpSessionKey(ipLayer.Source.ToString(), tcpLayer.SourcePort, ipLayer.Destination.ToString(), tcpLayer.DestinationPort); TcpSessionValue tcpValueN = new TcpSessionValue(tcpLayer.SequenceNumber, tcpLayer.AcknowledgmentNumber, 64000, tcpLayer.SequenceNumber); currentSession = tcpValueN; sessionDic.Add(tcpKeyN, tcpValueN); } } if (!newSession) { int prevAckNum = (int)currentSession.ackNum; int prevSeqNum = (int)currentSession.seqNum; int curAckNum = (int)tcpLayer.AcknowledgmentNumber; int curSeqNum = (int)tcpLayer.SequenceNumber; int oldSeqNum = (int)currentSession.prevseq; if (curSeqNum - oldSeqNum < currentSession.windowSize && curSeqNum - oldSeqNum >= 0 && (curSeqNum == prevSeqNum || curAckNum == prevSeqNum)) { currentSession.prevseq = currentSession.seqNum; currentSession.ackNum = tcpLayer.AcknowledgmentNumber; currentSession.seqNum = tcpLayer.SequenceNumber; Packet newPacket = null; string targetMac = null; targetMac = GetTargetAddress(packet, newDev).Mac; if (targetMac != null) { newPacket = BuildTcpPacket(new MacAddress(newDev.MacAddressWithDots()), new MacAddress(targetMac), ipLayer, tcpLayer, tcpPayload); } else { Console.WriteLine("No target MAC found"); } Console.WriteLine("packet built"); if (newPacket.IsValid) { Console.WriteLine("TCP packet sent"); newDev.Communicator.SendPacket(newPacket); } else { Console.WriteLine("Packet not valid"); } } else { Console.WriteLine("TCP packet not valid"); } } }