Example #1
0
        /// <summary>
        /// Returns the embedded Wake-On-LAN packet
        ///  or null if there is none
        /// </summary>
        /// <returns>
        /// A Wake-On-LAN packet
        /// </returns>
        public static WakeOnLanPacket GetEncapsulated(Packet p)
        {
            if (p is EthernetPacket)
            {
                var payload = EthernetPacket.GetInnerPayload((InternetLinkLayerPacket)p);
                if (((EthernetPacket)p).Type == EthernetPacketType.WakeOnLan)
                {
                    var payloadBas = new ByteArraySegment(p.PayloadData);
                    if (WakeOnLanPacket.IsValid(payloadBas))
                    {
                        return(new WakeOnLanPacket(payloadBas));
                    }
                }

                if (payload != null && payload is IpPacket)
                {
                    var innerPayload = payload.PayloadPacket;

                    if ((innerPayload != null) &&
                        (innerPayload.PayloadData != null) &&
                        (innerPayload is UdpPacket))
                    {
                        var innerPayloadBas = new ByteArraySegment(innerPayload.PayloadData);
                        if (WakeOnLanPacket.IsValid(innerPayloadBas))
                        {
                            return(new WakeOnLanPacket(innerPayloadBas));
                        }
                    }
                }
            }
            return(null);
        }
Example #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="byteArraySegment">
        /// A <see cref="ByteArraySegment" />
        /// </param>
        public UdpPacket(ByteArraySegment byteArraySegment)
        {
            Log.DebugFormat("ByteArraySegment {0}", byteArraySegment);

            // set the header field, header field values are retrieved from this byte array
            Header = new ByteArraySegment(byteArraySegment)
            {
                Length = UdpFields.HeaderLength
            };

            PayloadPacketOrData = new Lazy <PacketOrByteArraySegment>(() =>
            {
                const int wakeOnLanPort0 = 0;
                const int wakeOnLanPort7 = 7;
                const int wakeOnLanPort9 = 9;
                const int l2TpPort       = 1701;
                const int teredoPort     = 3544;

                var result          = new PacketOrByteArraySegment();
                var destinationPort = DestinationPort;
                var sourcePort      = SourcePort;
                var payload         = Header.NextSegment();

                // If this packet is going to port 0, 7 or 9, then it might be a WakeOnLan packet.
                if ((destinationPort == wakeOnLanPort0) || (destinationPort == wakeOnLanPort7) || (destinationPort == wakeOnLanPort9))
                {
                    if (WakeOnLanPacket.IsValid(payload))
                    {
                        result.Packet = new WakeOnLanPacket(payload);
                        return(result);
                    }
                }

                if ((destinationPort == l2TpPort) || (sourcePort == l2TpPort))
                {
                    result.Packet = new L2tpPacket(payload, this);
                    return(result);
                }

                // Teredo encapsulates IPv6 traffic into UDP packets, parse out the bytes in the payload into packets.
                // If it contains a IPV6 packet, it to this current packet as a payload.
                // https://tools.ietf.org/html/rfc4380#section-5.1.1
                if ((destinationPort == teredoPort) || (sourcePort == teredoPort))
                {
                    if (ContainsIPv6Packet(payload))
                    {
                        result.Packet = new IPv6Packet(payload);
                        return(result);
                    }
                }

                // store the payload bytes
                result.ByteArraySegment = payload;
                return(result);
            },
                                                                      LazyThreadSafetyMode.PublicationOnly);
        }
Example #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="bas">
 /// A <see cref="ByteArraySegment"/>
 /// </param>
 public WakeOnLanPacket(ByteArraySegment bas)
 {
     if (WakeOnLanPacket.IsValid(bas))
     {
         // set the header field, header field values are retrieved from this byte array
         header        = new ByteArraySegment(bas);
         header.Length = Bytes.Length;
     }
 }
Example #4
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public WakeOnLanPacket(ByteArraySegment bas)
        {
            log.Debug("");

            RandomUtils.EnsurePacketLength(this, packetLength, bas.Length);

            if (WakeOnLanPacket.IsValid(bas))
            {
                // set the header field, header field values are retrieved from this byte array
                header        = new ByteArraySegment(bas);
                header.Length = Bytes.Length;
            }
        }
Example #5
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="byteArraySegment">
        /// A <see cref="ByteArraySegment" />
        /// </param>
        public UdpPacket(ByteArraySegment byteArraySegment)
        {
            Log.DebugFormat("ByteArraySegment {0}", byteArraySegment);

            // set the header field, header field values are retrieved from this byte array
            Header = new ByteArraySegment(byteArraySegment)
            {
                Length = UdpFields.HeaderLength
            };

            PayloadPacketOrData = new LazySlim <PacketOrByteArraySegment>(() =>
            {
                const int wakeOnLanPort0 = 0;
                const int wakeOnLanPort7 = 7;
                const int wakeOnLanPort9 = 9;

                var result          = new PacketOrByteArraySegment();
                var destinationPort = DestinationPort;
                var sourcePort      = SourcePort;
                var payload         = Header.NextSegment();

                // If this packet is going to port 0, 7 or 9, then it might be a WakeOnLan packet.
                if (destinationPort == wakeOnLanPort0 || destinationPort == wakeOnLanPort7 || destinationPort == wakeOnLanPort9)
                {
                    if (WakeOnLanPacket.IsValid(payload))
                    {
                        result.Packet = new WakeOnLanPacket(payload);
                        return(result);
                    }
                }

                if (destinationPort == L2tpFields.Port || sourcePort == L2tpFields.Port)
                {
                    result.Packet = new L2tpPacket(payload, this);
                    return(result);
                }

                if ((sourcePort == DhcpV4Fields.ClientPort || sourcePort == DhcpV4Fields.ServerPort) &&
                    (destinationPort == DhcpV4Fields.ClientPort || destinationPort == DhcpV4Fields.ServerPort))
                {
                    var nextSegmentLength = byteArraySegment.Length - Header.Length;
                    if (nextSegmentLength >= DhcpV4Fields.MinimumSize)
                    {
                        var nextSegment = new ByteArraySegment(byteArraySegment.Bytes, byteArraySegment.Offset + Header.Length, nextSegmentLength);

                        var magicNumber = EndianBitConverter.Big.ToUInt32(nextSegment.Bytes, nextSegment.Offset + DhcpV4Fields.MagicNumberPosition);
                        if (magicNumber == DhcpV4Fields.MagicNumber)
                        {
                            result.Packet = new DhcpV4Packet(nextSegment, this);
                            return(result);
                        }
                    }
                }

                // Teredo encapsulates IPv6 traffic into UDP packets, parse out the bytes in the payload into packets.
                // If it contains a IPV6 packet, it to this current packet as a payload.
                // https://tools.ietf.org/html/rfc4380#section-5.1.1
                if (destinationPort == IPv6Fields.TeredoPort || sourcePort == IPv6Fields.TeredoPort)
                {
                    if (ContainsIPv6Packet(payload))
                    {
                        result.Packet = new IPv6Packet(payload);
                        return(result);
                    }
                }

                // store the payload bytes
                result.ByteArraySegment = payload;
                return(result);
            });
        }