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

Dependencies:   mbed

Committer:
rolf
Date:
Fri Sep 04 12:25:06 2009 +0000
Revision:
0:852db76de235

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rolf 0:852db76de235 1 #include "mbed.h" // Importing the mbed classes and tools.
rolf 0:852db76de235 2 #include "Ethernet.h"
rolf 0:852db76de235 3
rolf 0:852db76de235 4 using namespace mbed;
rolf 0:852db76de235 5
rolf 0:852db76de235 6 __packed // A __packed struct to have no gaps between the members http://en.wikipedia.org/wiki/Data_structure_alignment
rolf 0:852db76de235 7 struct ethpkt { // Ethernet layer: http://en.wikipedia.org/wiki/Ethernet#Physical_layer
rolf 0:852db76de235 8 unsigned char dest[6]; // Destination MAC
rolf 0:852db76de235 9 unsigned char src[6]; // Source MAC
rolf 0:852db76de235 10 unsigned short type; // Payload type. ARP is 0x0806
rolf 0:852db76de235 11 // ARP layer: http://en.wikipedia.org/wiki/Address_Resolution_Protocol
rolf 0:852db76de235 12 unsigned short hwtype; // Each data link layer protocol is assigned a number used in this field. Ethernet is 0x0001
rolf 0:852db76de235 13 unsigned short proto; // Each protocol is assigned a number used in this field. IP is 0x0800.
rolf 0:852db76de235 14 unsigned char hwlen; // Length in bytes of a hardware address. Ethernet addresses are 6 bytes long.
rolf 0:852db76de235 15 unsigned char protolen; // Length in bytes of a logical address. IPv4 address are 4 bytes long.
rolf 0:852db76de235 16 unsigned short opcode; // Specifies the operation the sender is performing:
rolf 0:852db76de235 17 // 1 for request, 2 for reply, 3 for RARP request, and 4 for RARP reply.
rolf 0:852db76de235 18 unsigned char shwaddr[6]; // Hardware address of the sender.
rolf 0:852db76de235 19 unsigned char sipaddr[4]; // Protocol address of the sender.
rolf 0:852db76de235 20 unsigned char dhwaddr[6]; // Hardware address of the intended receiver. This field is ignored in requests.
rolf 0:852db76de235 21 unsigned char dipaddr[4]; // Protocol address of the intended receiver.
rolf 0:852db76de235 22 };
rolf 0:852db76de235 23
rolf 0:852db76de235 24 Ethernet eth; // The ethernet device
rolf 0:852db76de235 25 DigitalOut led4(LED4); // A LED for showing activity
rolf 0:852db76de235 26
rolf 0:852db76de235 27 unsigned short htons(unsigned short n) { // Host short to network shor
rolf 0:852db76de235 28 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); // Byte swapping
rolf 0:852db76de235 29 }
rolf 0:852db76de235 30
rolf 0:852db76de235 31 void send(const char *ipaddr) {
rolf 0:852db76de235 32 static char data[0x600]; // Packet buffer
rolf 0:852db76de235 33 const unsigned char arplen = 6; // Hardware address length
rolf 0:852db76de235 34 const unsigned char ethlen = 4; // IP address length
rolf 0:852db76de235 35 char hwaddr[arplen]; // Hardware address buffer
rolf 0:852db76de235 36 struct ethpkt *pkg = (struct ethpkt *) &data[0]; // Force the buffer to an ethpkg
rolf 0:852db76de235 37 unsigned char pos = arplen; // Hardware/IP address position
rolf 0:852db76de235 38
rolf 0:852db76de235 39 eth.address(hwaddr); // Get own hardware address
rolf 0:852db76de235 40
rolf 0:852db76de235 41 pkg->type = htons(0x0806); // Set type to ARP (0x0806)
rolf 0:852db76de235 42 pkg->hwtype = htons(0x0001); // Hardware type is Ethernet (0x0001)
rolf 0:852db76de235 43 pkg->proto = htons(0x0800); // Protocol is ARP Request (0x0800)
rolf 0:852db76de235 44 pkg->hwlen = arplen; // Hardware addresses are 6 Bytes long
rolf 0:852db76de235 45 pkg->protolen = ethlen; // And protocol addresses 4 Bytes
rolf 0:852db76de235 46 pkg->opcode = htons(0x0001); // Send: whois XX:XX:XX:XX:XX:XX?
rolf 0:852db76de235 47
rolf 0:852db76de235 48 while(pos-- > 0) { // Write IP/MAC-Addresses (combined loop for all addresses)
rolf 0:852db76de235 49 pkg->src[pos] = hwaddr[pos]; // Set source MAC address to hwaddr on ethernet layer
rolf 0:852db76de235 50 pkg->dest[pos] = 0xFF; // Set destination MAC address to everybody (FF:FF:FF:FF:FF:FF) on ethernet layer
rolf 0:852db76de235 51 pkg->shwaddr[pos] = hwaddr[pos]; // Set source MAC address on ARP layer
rolf 0:852db76de235 52 pkg->dhwaddr[pos] = 0xFF; // Set destination MAC address on ARP layer
rolf 0:852db76de235 53 if(pos < ethlen) { // Check if we can copy IP addresses too.
rolf 0:852db76de235 54 pkg->sipaddr[pos] = 0xFF; // Set source ip address to 255.255.255.255
rolf 0:852db76de235 55 pkg->dipaddr[pos] = ipaddr[pos]; // Set destination ip address to ipaddr
rolf 0:852db76de235 56 }
rolf 0:852db76de235 57 }
rolf 0:852db76de235 58
rolf 0:852db76de235 59 eth.write(data, 60); // Write the package
rolf 0:852db76de235 60 eth.send(); // Send the package
rolf 0:852db76de235 61 }
rolf 0:852db76de235 62 // In this example we would like to make ARP requests to ask for every ip address.
rolf 0:852db76de235 63 int main() { // The programm starts here!
rolf 0:852db76de235 64 unsigned int i = 1; // The integer we use as counter and target IP address.
rolf 0:852db76de235 65 char *c = (char *)&i; // We cast the integer to an array of char c[4] to handle it as IP address.
rolf 0:852db76de235 66
rolf 0:852db76de235 67 printf("Lowlevel Ethernet Spammer\n\n"); // Print out that the programm has been started.
rolf 0:852db76de235 68
rolf 0:852db76de235 69 while(1) { // Do forever:
rolf 0:852db76de235 70 send(c); // Assamble and send our request. See eth_send function!
rolf 0:852db76de235 71 i++; // Increment counter. What will increment the IP (c[4]) address as well.
rolf 0:852db76de235 72
rolf 0:852db76de235 73 led4 = 1; // Show activity, by blinking with led 4:
rolf 0:852db76de235 74 wait(0.02); //
rolf 0:852db76de235 75 led4 = 0; // Put the led on and wait for 0.2 seconds
rolf 0:852db76de235 76 wait(0.02); // Put the led off and wait for 0.2 seconds
rolf 0:852db76de235 77 }
rolf 0:852db76de235 78 }