Example #1
0
 public void SetHardwareAddress(EthernetAddress ea)
 {
     hlen   = 6;
     htype  = (byte)HType.Ethernet;
     chaddr = new byte[HardwareAddressLength];
     ea.CopyOut(chaddr, 0);
 }
Example #2
0
        // methods:
        public static int Write(byte [] !pkt,
                                int offset,
                                EthernetAddress srchw,
                                IPv4 srcip,
                                Type operation,
                                EthernetAddress targethw,
                                IPv4 targetip)
        {
            int o = offset;

            pkt[o++] = 0x00; pkt[o++] = 0x01; // hardware type = 0x0001
            pkt[o++] = 0x08; pkt[o++] = 0x00; // protocol type = 0x0800
            pkt[o++] = 0x06;                  // hardware addr len (bytes)
            pkt[o++] = 0x04;                  // protocol address len (bytes)
            pkt[o++] = (byte)(((ushort)operation) >> 8);
            pkt[o++] = (byte)(((ushort)operation) & 0xff);

            srchw.CopyOut(pkt, o);
            o += EthernetAddress.Length;

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

            targethw.CopyOut(pkt, o);
            o += EthernetAddress.Length;

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

            return(o);
        }
Example #3
0
        public static int Write(Bytes pkt,
                                EthernetAddress srchw,
                                IPv4 srcip,
                                ushort operation,
                                EthernetAddress targethw,
                                IPv4 targetip)
        {
            int o = 0;

            pkt[o++] = 0x00; pkt[o++] = 0x01; // hardware type = 0x0001
            pkt[o++] = 0x08; pkt[o++] = 0x00; // protocol type = 0x0800
            pkt[o++] = 0x06;                  // hardware addr len (bytes)
            pkt[o++] = 0x04;                  // protocol address len (bytes)
            pkt[o++] = (byte)(operation >> 8);
            pkt[o++] = (byte)(operation & 0xff);

            srchw.CopyOut(pkt.Array, pkt.Start + o);
            o += EthernetAddress.Length;

            srcip.CopyOut(pkt.Array, pkt.Start + o);
            o += IPv4.Length;

            targethw.CopyOut(pkt.Array, pkt.Start + o);
            o += EthernetAddress.Length;

            targetip.CopyOut(pkt.Array, pkt.Start + o);
            o += IPv4.Length;

            return(o);
        }
Example #4
0
 // Write an Ethernet header assuming that the header begins at offset 0
 public static void Write(Bytes header,
                          EthernetAddress src,
                          EthernetAddress dst,
                          ushort type)
 {
     dst.CopyOut(header.Array, header.Start);
     src.CopyOut(header.Array, header.Start + EthernetAddress.Length);
     header[12] = (byte)(type >> 8);
     header[13] = (byte)(type & 0xff);
 }
Example #5
0
        // Write an Ethernet header to "pkt", starting at position "start".
        public static int Write(byte[] !pkt,
                                int start,
                                EthernetAddress src,
                                EthernetAddress dst,
                                ushort type)
        {
            dst.CopyOut(pkt, start);
            src.CopyOut(pkt, start + EthernetAddress.Length);

            pkt[start + 12] = (byte)(type >> 8);
            pkt[start + 13] = (byte)(type & 0xff);

            return(start + 14);
        }
Example #6
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);
        }