private IPPacketIdentifier(ushort key, IPHeader ipHeader, SystemModule module)
 {
     Key = key;
     _ttl = ipHeader.TTL;
     Daddr = ipHeader.DestinationAddress;
     _module = module;
 }
 public static IPPacketIdentifier Create(ushort key, IPHeader ipHeader, SystemModule module)
 {
     var id = new IPPacketIdentifier(key, ipHeader, module);
     id._timer = new Timer
                 {
                     Interval = 1000
                 };
     id._timer.Elapsed += (s, e) =>
                          {
                              id._ttl--;
                              if (id._ttl == 0)
                              {
                                  Log.Write("IPPacketIdentifier", "Timeout", string.Format("addr={0}, id={1}", id.Daddr.StandardFormat, id.Key));
                                  id._timer.Stop();
                                  module.KillIPPacketIdentifier(id);
                              }
                          };
     return id;
 }
Exemple #3
0
        public override Handler Parse()
        {
            var header = new IPHeader();

            header.VersionIHL = LoadAndScroll<Byte>();
            header.TypeOfService = LoadAndScroll<Byte>();
            header.TotalLength = LoadUInt16ReversingEndian();
            header.Identification = LoadUInt16ReversingEndian();
            header.FlagsFragmentOffset = LoadUInt16ReversingEndian();
            header.TTL = LoadAndScroll<Byte>();
            header.Protocol = LoadAndScroll<Byte>();
            header.Crc = LoadUInt16ReversingEndian();
            header.SourceAddress = LoadAndScroll<IPAddress>();
            header.DestinationAddress = LoadAndScroll<IPAddress>();

            Offset += header.InternetHeaderLength - 20;

            SetValue(IPv4HeaderProperty, header);

            return GetNextHandler();
        }
Exemple #4
0
 private void HandleResponse(string deviceID, RipPreambleHeader ripPreambleHeader, RipDataHeader[] ripDataHeaderArray, IPHeader ipHeader)
 {
     foreach (RipDataHeader ripDataHeader in ripDataHeaderArray)
     {
         if (RipDatabase.ContainsKey(ripDataHeader.Network))
         {
             RipRoute route = RipDatabase[ripDataHeader.Network];
             if (ripDataHeader.Metric < route.Metric)
             {
                 RipDatabase[ripDataHeader.Network].UpdateRipHeaderData(ipHeader.SourceAddress, ripDataHeader);
             }
             else if (ripDataHeader.Metric > route.Metric && ripDataHeader.NextHop == ipHeader.SourceAddress)
             {
                 if (route.AcceptPotentialNewMetricOnNextSighting &&
                     route.PotentialNewMetric == ripDataHeader.Metric)
                 {
                     route.AcceptPotentialNewMetric();
                 }
                 else
                 {
                     route.BeginHoldDownTime(ripDataHeader);
                 }
             }
             route.TimeUpdated = DateTime.Now;
             route.RaiseEvent(RipEvent.Sighted);
         }
         else
         {
             RipRoute route = RipRoute.Create(ipHeader.SourceAddress, ripDataHeader, deviceID);
             route.GCCollect += (s, e) =>
                                {
                                    RipDatabase.Remove((s as RipRoute).Network);
                                    Controller.RemoveRoute(this, route);
                                };
             Log.Write("RIP", "RipDatabaseAdd", ripDataHeader.Network.StandardFormat);
             RipDatabase.Add(ripDataHeader.Network, route);
             Controller.RegisterRoute(this, route);
         }
     }
 }
Exemple #5
0
        private List<PacketData> CreatePacketData(PacketData packetData, Handler layer3Handler, Value[] parameterArray, out IPHeader baseIPHeader, out IPPacketIdentifier id)
        {
            const int ipMtu = 1500;
            var list = new List<PacketData>();
            var ipHeader = (IPHeader)layer3Handler.Build(parameterArray);
            baseIPHeader = ipHeader;
            var headerLength = ipHeader.VersionIHL * 5;
            ipHeader.TotalLength = (ushort)((ipHeader.VersionIHL * 5) + packetData.Length);

            id = CreateIPPacketIdentifier(ipHeader);
            ipHeader.Identification = id.Key;

            var remainingData = packetData.Data;
            var totalTransmitCount = remainingData.Count;
            ushort fragmentOffset = 0;

            while (remainingData.Count > 0)
            {
                if (remainingData.Count + headerLength > ipMtu)
                {
                    var firstFragmentSize = ipMtu - ipHeader.InternetHeaderLength;
                    var blockCount = (int)Math.Floor((double)firstFragmentSize / 8);
                    var copyCount = blockCount * 8;
                    var pendingTransmitData = new List<byte>(remainingData.GetRange(0, copyCount));
                    remainingData = new List<byte>(remainingData.GetRange(copyCount, remainingData.Count - copyCount));
                    ipHeader.FlagsFragmentOffset = (ushort)((fragmentOffset >> 3) | 0x2000);
                    Log.Write("SYSTEM", "IPFRAGMENT", fragmentOffset.ToString());
                    fragmentOffset += (ushort)copyCount;
                    ipHeader.TotalLength = (ushort)((ipHeader.VersionIHL * 5) + copyCount);
                    //++ contains MF bit
                    //ipHeader.FlagsFragmentOffset = (ushort)(ipHeader.FlagsFragmentOffset << 3);
                    var segmentData = PacketData.Create(pendingTransmitData);
                    segmentData = layer3Handler.GetBytes(ipHeader, segmentData);
                    list.Add(segmentData);
                }
                else
                {
                    var pendingTransmitData = remainingData;
                    ipHeader.FlagsFragmentOffset = (ushort)(fragmentOffset >> 3);
                    //ipHeader.FlagsFragmentOffset = (ushort)(fragmentOffset);
                    Log.Write("SYSTEM", "IPFRAGMENT", fragmentOffset.ToString());
                    //ipHeader.FlagsFragmentOffset = (ushort)(ipHeader.FlagsFragmentOffset << 3);
                    ipHeader.TotalLength = (ushort)((ipHeader.VersionIHL * 5) + remainingData.Count);
                    var segmentData = PacketData.Create(pendingTransmitData);
                    segmentData = layer3Handler.GetBytes(ipHeader, segmentData);
                    list.Add(segmentData);
                    remainingData.Clear();
                }
            }
            return list;
        }
Exemple #6
0
        private IPPacketIdentifier CreateIPPacketIdentifier(IPHeader ipHeader)
        {
            lock (_ipPacketIdentifierLock)
            {
                if (!_ipPacketIdentifierMap.ContainsKey(ipHeader.DestinationAddress))
                {
                    _ipPacketIdentifierMap[ipHeader.DestinationAddress] = new IPPacketPerDestinationIdentifier();
                }
                IPPacketPerDestinationIdentifier destinationMap = _ipPacketIdentifierMap[ipHeader.DestinationAddress];
                var key = (ushort)(destinationMap.MaxKey + 1);
                if (key == 65536)
                {
                    key = 0;
                }
                destinationMap.MaxKey = key;

                IPPacketIdentifier id = IPPacketIdentifier.Create(key, ipHeader, this);
                destinationMap.Data.Add(key, id);

                return id;
            }
        }
Exemple #7
0
 internal void OnHelloReceive(IPHeader ipHeader, OspfHeader ospfHeader, OspfHelloHeader ospfHelloHeader)
 {
     lock (Lock)
     {
         LastSeen = DateTime.Now;
         Log.Write("OSPF", "NEIGHBOR", string.Format("Hello received. From: {0}, State: {1}", ospfHeader.RouterID, OspfNeighborState));
         RaiseEvent(NeighborEventType.HelloReceived);
         if (ospfHelloHeader.Neighbor.Any(p => p == Module.RouterID))
         {
             var tupleData = new Tuple<IPHeader, OspfHeader, Tuple<IPAddress, IPAddress>>(ipHeader, ospfHeader,
                                                                                              new Tuple<IPAddress, IPAddress>(ospfHelloHeader.Dr, ospfHelloHeader.Bdr));
             RaiseEvent(NeighborEventType.TwoWayReceived, tupleData);
         }
         else
         {
             var tupleData = new Tuple<IPHeader, OspfHeader, object>(ipHeader, ospfHeader, ospfHelloHeader);
             RaiseEvent(NeighborEventType.OneWayReceived, tupleData);
         }
     }
 }
Exemple #8
0
 internal void OnDBDReceive(IPHeader ipHeader, OspfHeader ospfHeader, Tuple<OspfDbdHeader, OspfLsaHeader[]> dbdHeaderData)
 {
     lock (Lock)
     {
         Log.Write("OSPF", "NEIGHBOR", string.Format("DBD received. From: {0}, State: {1}", ospfHeader.RouterID, OspfNeighborState));
         var tupleData = new Tuple<IPHeader, OspfHeader, Tuple<OspfDbdHeader, OspfLsaHeader[]>>(ipHeader, ospfHeader, dbdHeaderData);
         var ospfDBDHeader = dbdHeaderData.Item1;
         var ospfLsaHeaderArray = (OspfLsaHeader[])dbdHeaderData.Item2;
         switch (OspfNeighborState)
         {
             case OspfNeighborState.Down:
             case OspfNeighborState.Attempt:
                 return;
             case OspfNeighborState.Init:
                 RaiseEvent(NeighborEventType.TwoWayReceived, new Tuple<IPHeader, OspfHeader, Tuple<IPAddress, IPAddress>>(ipHeader, ospfHeader,
                                                                                                                               new Tuple<IPAddress, IPAddress>(DR, BDR)));
                 return;
             case OspfNeighborState.TwoWay:
                 return;
             case OspfNeighborState.ExStart:
                 if (ospfDBDHeader.IsInitial &&
                     ospfDBDHeader.HasMore &&
                     ospfDBDHeader.IsMaster &&
                     (dbdHeaderData.Item2 == null || ospfLsaHeaderArray.Length == 0) &&
                     ospfHeader.RouterID > Module.RouterID)
                 {
                     Log.Write("OSPF", "NEIGHBOR---------EXSTART", "IS SLAVE TO " + ospfHeader.RouterID);
                     LocalMSState = MasterSlaveState.Slave;
                     _dbdPendingAck = null;
                     DDSequenceNumber = ospfDBDHeader.SequenceNumber;
                     LastOspfDBDSeen = ospfDBDHeader;
                     DiscoveredOptions = ospfDBDHeader.OspfOptions;
                     RaiseEvent(NeighborEventType.NegotiationDone);
                     ProcessDBD(tupleData);
                 }
                 else if (!ospfDBDHeader.IsInitial && !ospfDBDHeader.IsMaster &&
                          DDSequenceNumber == dbdHeaderData.Item1.SequenceNumber &&
                          ospfHeader.RouterID < Module.RouterID)
                 {
                     Log.Write("OSPF", "NEIGHBOR---------EXSTART", "IS MASTER OF" + ospfHeader.RouterID);
                     LocalMSState = MasterSlaveState.Master;
                     _dbdPendingAck = null;
                     LastOspfDBDSeen = ospfDBDHeader;
                     DiscoveredOptions = ospfDBDHeader.OspfOptions;
                     RaiseEvent(NeighborEventType.NegotiationDone);
                     ProcessDBD(tupleData);
                 }
                 return;
             case OspfNeighborState.Exchange:
                 //TODO: duplicate check?
                 if (dbdHeaderData.Item1.IsMaster && LocalMSState == MasterSlaveState.Master)
                 {
                     RaiseEvent(NeighborEventType.SeqNumberMismatch,
                                new Tuple<IPHeader, OspfHeader, Tuple<OspfDbdHeader, OspfLsaHeader[]>>(ipHeader, ospfHeader,
                                                                                                             new Tuple<OspfDbdHeader, OspfLsaHeader[]>(ospfDBDHeader, null)));
                     return;
                 }
                 else if (!dbdHeaderData.Item1.IsMaster && LocalMSState == MasterSlaveState.Slave)
                 {
                     RaiseEvent(NeighborEventType.SeqNumberMismatch,
                                new Tuple<IPHeader, OspfHeader, Tuple<OspfDbdHeader, OspfLsaHeader[]>>(ipHeader, ospfHeader,
                                                                                                             new Tuple<OspfDbdHeader, OspfLsaHeader[]>(ospfDBDHeader, null)));
                     return;
                 }
                 else if (dbdHeaderData.Item1.IsInitial)
                 {
                     RaiseEvent(NeighborEventType.SeqNumberMismatch, tupleData);
                     return;
                 }
                 else if (dbdHeaderData.Item1.OspfOptions != DiscoveredOptions)
                 {
                     RaiseEvent(NeighborEventType.SeqNumberMismatch, tupleData);
                     return;
                 }
                 else if ((LocalMSState == MasterSlaveState.Master && dbdHeaderData.Item1.SequenceNumber == DDSequenceNumber) ||
                          (LocalMSState == MasterSlaveState.Slave && dbdHeaderData.Item1.SequenceNumber == DDSequenceNumber + 1))
                 {
                     LastOspfDBDSeen = dbdHeaderData.Item1;
                     ProcessDBD(tupleData);
                     RaiseEvent(NeighborEventType.ExchangeDone);
                     return;
                 }
                 else
                 {
                     RaiseEvent(NeighborEventType.SeqNumberMismatch, tupleData);
                     return;
                 }
             case OspfNeighborState.Loading:
                 break;
             case OspfNeighborState.Full:
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
     }
 }
Exemple #9
0
 private void GetSourceAndID(OspfHeader ospfHeader, IPHeader ipHeader, out IPAddress source, out IPAddress id)
 {
     switch (OspfNetworkType)
     {
         case OspfNetworkType.PointToMultiPoint:
         case OspfNetworkType.Broadcast:
         case OspfNetworkType.NBMA:
             source = ipHeader.SourceAddress;
             id = ospfHeader.RouterID;
             break;
         case OspfNetworkType.VirtualLink:
             source = ospfHeader.RouterID;
             id = ospfHeader.RouterID;
             break;
         default:
         case OspfNetworkType.PointToPoint:
             source = ospfHeader.RouterID;
             id = ipHeader.SourceAddress;
             break;
     }
 }
Exemple #10
0
 //private void HandleHelloMessage(IPHeader ipHeader, OspfHeader ospfHeader, OspfHelloHeader ospfHelloHeader)
 //{
 //    return;
 //    Neighbor neighbor = NeighborMap[ospfHeader.rid] ?? Neighbor.Create(ipHeader, ospfHeader, ospfHelloHeader);
 //    NeighborEventArgs args = NeighborEventArgs.Create(ipHeader, ospfHeader, ospfHelloHeader);
 //    neighbor.RaiseEvent(NeighborEventType.HelloReceived, args);
 //    if (ospfHelloHeader.neighbor.Any(p => p == RouterID))
 //    {
 //        neighbor.RaiseEvent(NeighborEventType.TwoWayReceived, args);
 //    }
 //    if (neighbor.IsNew && IsPotentialNeighbor(ipHeader, ospfHeader, ospfHelloHeader))
 //    {
 //        NeighborMap.Add(neighbor.RouterId, neighbor);
 //        neighbor.IsNew = false;
 //    }
 //    else
 //    {
 //        Log.Write("OSPF", "HELLO", string.Format("Incompatible OSPF hello message from .", ipHeader.saddr.StandardFormat));
 //    }
 //}
 private bool IsPotentialNeighbor(IPHeader ipHeader, OspfHeader ospfHeader, OspfHelloHeader ospfHelloHeader)
 {
     //if (!ospfHeader.aid.Equals(Area))
     //{
     //    return false;
     //}
     //if (ospfHelloHeader.interval != HelloInterval)
     //{
     //    return false;
     //}
     //if (ospfHelloHeader.routerdeadintvl != RouterDeadInterval)
     //{
     //    return false;
     //}
     //if (((ospfHelloHeader.Options & OspfHelloOptions.ExternalRoutingCapable) == OspfHelloOptions.ExternalRoutingCapable) == IsStub)
     //{
     //    return false;
     //}
     return true;
 }