etherspam shows how to send an Ethernet packet. It simply sends ARP Who is messages for every IP.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"                                      // Importing the mbed classes and tools.
00002 #include "Ethernet.h"
00003 
00004 using namespace mbed;
00005 
00006 __packed                                               // A __packed struct to have no gaps between the members http://en.wikipedia.org/wiki/Data_structure_alignment
00007 struct ethpkt {                                        // Ethernet layer: http://en.wikipedia.org/wiki/Ethernet#Physical_layer
00008   unsigned char   dest[6];                             //   Destination MAC
00009   unsigned char   src[6];                              //   Source MAC
00010   unsigned short  type;                                //   Payload type. ARP is 0x0806
00011                                                        // ARP layer: http://en.wikipedia.org/wiki/Address_Resolution_Protocol
00012   unsigned short  hwtype;                              //   Each data link layer protocol is assigned a number used in this field. Ethernet is 0x0001
00013   unsigned short  proto;                               //   Each protocol is assigned a number used in this field. IP is 0x0800.
00014   unsigned char   hwlen;                               //   Length in bytes of a hardware address. Ethernet addresses are 6 bytes long.
00015   unsigned char   protolen;                            //   Length in bytes of a logical address. IPv4 address are 4 bytes long.
00016   unsigned short  opcode;                              //   Specifies the operation the sender is performing: 
00017                                                        //     1 for request, 2 for reply, 3 for RARP request, and 4 for RARP reply.
00018   unsigned char   shwaddr[6];                          //   Hardware address of the sender.
00019   unsigned char   sipaddr[4];                          //   Protocol address of the sender.
00020   unsigned char   dhwaddr[6];                          //   Hardware address of the intended receiver. This field is ignored in requests.
00021   unsigned char   dipaddr[4];                          //   Protocol address of the intended receiver.
00022 };
00023 
00024 Ethernet eth;                                          // The ethernet device
00025 DigitalOut led4(LED4);                                 // A LED for showing activity
00026 
00027 unsigned short htons(unsigned short n) {               // Host short to network shor
00028   return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);      // Byte swapping
00029 }
00030 
00031 void send(const char *ipaddr) {
00032   static char data[0x600];                             // Packet buffer
00033   const unsigned char arplen = 6;                      // Hardware address length
00034   const unsigned char ethlen = 4;                      // IP address length
00035   char hwaddr[arplen];                                 // Hardware address buffer
00036   struct ethpkt *pkg   = (struct ethpkt *) &data[0];   // Force the buffer to an ethpkg
00037   unsigned char pos    = arplen;                       // Hardware/IP address position
00038 
00039   eth.address(hwaddr);                                 // Get own hardware address
00040 
00041   pkg->type            = htons(0x0806);                // Set type to ARP (0x0806)
00042   pkg->hwtype          = htons(0x0001);                // Hardware type is Ethernet (0x0001)
00043   pkg->proto           = htons(0x0800);                // Protocol is ARP Request (0x0800)
00044   pkg->hwlen           = arplen;                       // Hardware addresses are 6 Bytes long
00045   pkg->protolen        = ethlen;                       // And protocol addresses 4 Bytes
00046   pkg->opcode          = htons(0x0001);                // Send: whois XX:XX:XX:XX:XX:XX?
00047   
00048   while(pos-- > 0) {                                   // Write IP/MAC-Addresses (combined loop for all addresses)
00049     pkg->src[pos]        = hwaddr[pos];                // Set source MAC address to hwaddr on ethernet layer
00050     pkg->dest[pos]       = 0xFF;                       // Set destination MAC address to everybody (FF:FF:FF:FF:FF:FF) on ethernet layer
00051     pkg->shwaddr[pos]    = hwaddr[pos];                // Set source MAC address on ARP layer
00052     pkg->dhwaddr[pos]    = 0xFF;                       // Set destination MAC address on ARP layer
00053     if(pos < ethlen) {                                 // Check if we can copy IP addresses too.
00054       pkg->sipaddr[pos]  = 0xFF;                       // Set source ip address to 255.255.255.255
00055       pkg->dipaddr[pos]  = ipaddr[pos];                // Set destination ip address to ipaddr
00056     }
00057   }
00058 
00059   eth.write(data, 60);                                 // Write the package
00060   eth.send();                                          // Send the package
00061 }
00062                                                        // In this example we would like to make ARP requests to ask for every ip address.
00063 int main() {                                           // The programm starts here!
00064   unsigned int i = 1;                                  // The integer we use as counter and target IP address.
00065   char *c        = (char *)&i;                         // We cast the integer to an array of char c[4] to handle it as IP address.
00066   
00067   printf("Lowlevel Ethernet Spammer\n\n");             // Print out that the programm has been started.
00068 
00069   while(1) {                                           // Do forever:
00070     send(c);                                           //   Assamble and send our request. See eth_send function!
00071     i++;                                               //   Increment counter. What will increment the IP (c[4]) address as well.
00072     
00073     led4 = 1;                                          //   Show activity, by blinking with led 4:
00074     wait(0.02);                                        //   
00075     led4 = 0;                                          //     Put the led on and wait for 0.2 seconds
00076     wait(0.02);                                        //     Put the led off and wait for 0.2 seconds
00077   }
00078 }