Esempio n. 1
0
 public EthernetAddress GetHardwareAddress()
 {
     if (htype != (byte)HType.Ethernet)
     {
         // XXX Throw some nasty exception
     }
     return(EthernetAddress.ParseBytes(chaddr, 0));
 }
Esempio n. 2
0
 // parse "pkt" from "start" bytes in, and return the
 // "src" and "dst" Ethernet addresses, and the protocol
 // "type" Returns the byte offset of the next layer's
 // header.
 public static int Read(byte [] !pkt,
                        int start,
                        out EthernetAddress src,
                        out EthernetAddress dst,
                        out ushort type)
 {
     dst  = EthernetAddress.ParseBytes(pkt, start);
     src  = EthernetAddress.ParseBytes(pkt, start + 6);
     type = (ushort)((pkt[start + 12] << 8) | (pkt[start + 13]));
     return(start + 14);
 }
Esempio n. 3
0
 public bool ReadEthernetAddress(out EthernetAddress address)
 {
     try {
         address   = EthernetAddress.ParseBytes(data, position);
         position += EthernetAddress.Length;
     }
     catch (ArgumentException) {
         address = EthernetAddress.Zero;
         return(false);
     }
     return(true);
 }
Esempio n. 4
0
        public ArpHeader(Bytes packet, ushort index)
        {
            //since this is already known to be an arp request
            //we skip the sanity checks...

            VTable.Assert(packet.Length - index >= 96);

            // check hardware type == 0x0001 (Ethernet)
            htype = NetworkBitConverter.ToUInt16(packet, index);
            DebugStub.Assert(htype == 0x001);
            index += 2;

            // check protocol type == 0x0800 (IP)
            ptype = NetworkBitConverter.ToUInt16(packet, index);
            DebugStub.Assert(ptype == 0x0800);
            index += 2;

            // check hardware address len is 6 bytes
            hlen = packet[index++];
            DebugStub.Assert(hlen == 6);


            // check IP address len is 4 bytes
            plen = packet[index++];
            DebugStub.Assert(plen == 4);

            op     = NetworkBitConverter.ToUInt16(packet, index);
            index += 2;

            senderEthernetAddr = EthernetAddress.ParseBytes(packet.Array, packet.Start + index);
            index += EthernetAddress.Length;

            uint addr = NetworkBitConverter.ToUInt32(packet, index);

            index       += 4;
            senderIPAddr = new IPv4(addr);


            destEthernetAddr = EthernetAddress.ParseBytes(packet.Array, packet.Start + index);
            index           += EthernetAddress.Length;

            addr       = NetworkBitConverter.ToUInt32(packet, index);
            index     += 4;
            destIPAddr = new IPv4(addr);
            //sgc complains
            pad0 = 0;
            pad1 = 0;
        }
Esempio n. 5
0
        // creates an arp reply using the received packet
        // the packet must be an arp request
        // the responder should pass its own IP and MAC address
        // we assume that we have the same hw type
        // (return offset_arp size)
        public static int CreateArpReply(ref byte[] !pkt,
                                         IPv4 selfIP,
                                         EthernetAddress selfMACAddr)
        {
            int offset = 14;           // arp message starts here.
            int o      = offset;

            // check we have enough packet
            if (pkt.Length - offset < Size)
            {
                return(offset);
            }

            // check hardware type == 0x0001 (Ethernet)
            if (pkt[o++] != 0x00 || pkt[o++] != 0x01)
            {
                return(offset);
            }

            // check protocol type == 0x0800 (IP)
            if (pkt[o++] != 0x08 || pkt[o++] != 0x00)
            {
                return(offset); // error
            }
            // check addresses len
            if (pkt[o++] != 0x06)
            {
                return(offset);
            }

            if (pkt[o++] != 0x04)
            {
                return(offset);
            }

            // operation code
            Type opcode = (Type)(((int)pkt[o++] << 8) | (int)pkt[o++]);

            // we need a request message.
            if (opcode != Type.ARP_REQUEST)
            {
                return(offset);
            }

            // sender HW & IP
            EthernetAddress senderhw = EthernetAddress.ParseBytes(pkt, o);

            o += EthernetAddress.Length;

            IPv4 senderIP = IPv4.ParseBytes(pkt, o);

            o += IPv4.Length;

            // target HW & IP
            // EthernetAddress targethw = EthernetAddress.ParseBytes(pkt, o);
            o += EthernetAddress.Length;

            // IPv4 targetIP = IPv4.ParseBytes(pkt, o);
            o += IPv4.Length;

            // all is well, do our stuff...
            // 1. set local hw address to arp's source ether address + fix dest addresses
            // 2. set arp type = ARP_REPLY
            // 3. set the destination's ethernet address to self
            //    and target is the source of the request.

            o        = offset + 6; // opcode entry
            pkt[o++] = (byte)(((ushort)Type.ARP_REPLY) >> 8);
            pkt[o++] = (byte)Type.ARP_REPLY;

            // set hw and IP address
            selfMACAddr.CopyOut(pkt, o);
            o += EthernetAddress.Length;

            selfIP.CopyOut(pkt, o);
            o += IPv4.Length;

            // setup dest address to the requesting host address
            senderhw.CopyOut(pkt, o);
            o += EthernetAddress.Length;

            senderIP.CopyOut(pkt, o);
            o += IPv4.Length;

            // set ethernet level addresses
            Array.Copy(pkt, 6, pkt, 0, 6);  // src  -> dest
            selfMACAddr.CopyOut(pkt, 6);    // self -> src

            return(offset + Size);
        }