コード例 #1
0
ファイル: IGMPHeader.cs プロジェクト: bpaziaud/Hermod
    /// <summary>
    /// This routine creates the byte array representation of the IGMP packet as it
    /// would look on the wire.
    /// </summary>
    /// <param name="payLoad">Payload to copy after the IGMP header</param>
    /// <returns>Byte array representing the IGMP header and payload</returns>
    public override byte[] GetProtocolPacketBytes(byte[] payLoad)
    {
        byte[] igmpPacket, addressBytes, byteValue;
        int    offset = 0;

        igmpPacket = new byte[IgmpHeaderLength + payLoad.Length];

        // Build the IGMP packet
        igmpPacket[offset++] = igmpVersionType;
        igmpPacket[offset++] = igmpMaxResponseTime;
        igmpPacket[offset++] = 0;  // Zero the checksum for now
        igmpPacket[offset++] = 0;

        // Copy the group address bytes
        addressBytes = igmpGroupAddress.GetBytes();
        Array.Copy(addressBytes, 0, igmpPacket, offset, addressBytes.Length);
        offset += addressBytes.Length;

        // Copy the payload if specified. Normally, there is no payload to the IGMP
        //    packet -- only the IGMP header.
        if (payLoad.Length > 0)
        {
            Array.Copy(payLoad, 0, igmpPacket, offset, payLoad.Length);
            offset += payLoad.Length;
        }

        // Compute the checksum on the IGMP packet and payload
        Checksum = ComputeChecksum(igmpPacket);

        // Put the checksum value into the packet
        byteValue = BitConverter.GetBytes(igmpChecksum);
        Array.Copy(byteValue, 0, igmpPacket, 2, byteValue.Length);

        return(igmpPacket);
    }
コード例 #2
0
        public static IPv4Address GetIPv4NetworkAddress(this Random random, IPv4SubnetMask mask)
        {
            IPv4Address start = random.GetIPv4Address();

            Byte[] rawResult = ByteHelper.AndArray(start.GetBytes(), mask.GetBytes());

            IPv4Address result = IPv4Address.FromByteArray(rawResult);

            return(result);
        }
コード例 #3
0
        public void IPv4Address_FromString(String input, Boolean parseable, Byte[] expectedByteSequence)
        {
            if (parseable == false)
            {
                Assert.ThrowsAny <Exception>(() => IPv4Address.FromString(input));
                return;
            }

            IPv4Address address = IPv4Address.FromString(input);

            Byte[] actual = address.GetBytes();
            Assert.Equal(expectedByteSequence, actual);
        }
コード例 #4
0
ファイル: IPv4Header.cs プロジェクト: bpaziaud/Hermod
    /// <summary>
    /// This routine takes the properties of the IPv4 header and marhalls them into
    /// a byte array representing the IPv4 header that is to be sent on the wire.
    /// </summary>
    /// <param name="payLoad">The encapsulated headers and data</param>
    /// <returns>A byte array of the IPv4 header and payload</returns>
    public override byte[] GetProtocolPacketBytes(byte[] payLoad)
    {
        byte[] ipv4Packet, byteValue;
        int    index = 0;

        // Allocate space for the IPv4 header plus payload
        ipv4Packet = new byte[Ipv4HeaderLength + payLoad.Length];

        ipv4Packet[index++] = (byte)((ipVersion << 4) | ipLength);
        ipv4Packet[index++] = ipTypeOfService;

        byteValue = BitConverter.GetBytes(ipTotalLength);
        Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
        index += byteValue.Length;

        byteValue = BitConverter.GetBytes(ipId);
        Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
        index += byteValue.Length;

        byteValue = BitConverter.GetBytes(ipOffset);
        Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
        index += byteValue.Length;

        ipv4Packet[index++] = ipTtl;
        ipv4Packet[index++] = ipProtocol;
        ipv4Packet[index++] = 0; // Zero the checksum for now since we will
        ipv4Packet[index++] = 0; // calculate it later

        // Copy the source address
        byteValue = ipSourceAddress.GetBytes();
        Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
        index += byteValue.Length;

        // Copy the destination address
        byteValue = ipDestinationAddress.GetBytes();
        Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
        index += byteValue.Length;

        // Copy the payload
        Array.Copy(payLoad, 0, ipv4Packet, index, payLoad.Length);
        index += payLoad.Length;

        // Compute the checksum over the entire packet (IPv4 header + payload)
        Checksum = ComputeChecksum(ipv4Packet);

        // Set the checksum into the built packet
        byteValue = BitConverter.GetBytes(ipChecksum);
        Array.Copy(byteValue, 0, ipv4Packet, 10, byteValue.Length);

        return(ipv4Packet);
    }
コード例 #5
0
        private DHCPv4InterfaceEntry FindValidInterface(DHCPv4InterfaceOverview interfaces)
        {
            foreach (var item in interfaces.Entries)
            {
                IPv4Address address = IPv4Address.FromString(item.IPv4Address);
                var         bytes   = address.GetBytes();

                if (bytes[0] == 127 || (bytes[0] == 169 && bytes[1] == 254))
                {
                    continue;
                }

                return(item);
            }

            return(null);
        }
コード例 #6
0
 public DHCPv4PacketAddressOption(Byte type, IPv4Address address) : base(type, address.GetBytes())
 {
     Address = address;
 }