request mac addresses from ip

Hi,

i try to request mac addresses from all clients in my subnet with the function “IptoMAC”. If i put this function in a loop from 1 to 254, it crashes in any case between x.x.x.1-40 (wireshark doesn’t display any further arp-packets from arduino) and it is also skipping some ips. This function is very unstable and i don’t understand why.

sorry for my horrible code, i’m still learning :wink:

/*
  arp.pde:
 sending arp packets
 */

#include <SPI.h>         // needed for Ethernet library communication with the W5100 (Arduino ver>0018)
#include <Ethernet.h>
#include <utility/w5100.h>

//------------------ethernet-data----------------------
byte mac[] = {
  0xA0,0x00,0x00,0x00,0x00,0x00};
byte ip[] = {
  192,168,2,15};
//-----------------------------------------------------
SOCKET s; // our socket that will be opened in RAW mode
byte rbuf[1500+14]; // receive buffer 
byte sbuf[1500+14]; // send buffer 
int rbuflen; // length of data to receive
int sbuflen; // length of data to send
int mac_address_register[254][7];
byte TargetIP[4] = {
  192,168,2,0  };

byte array_compare(byte *array1,byte *array2,byte len);
byte array_cpy(byte *array, byte *add_array,byte *start_byte,byte end_byte);
byte IptoMAC(byte *IPtoRequest, byte *mac_array);

void setup() {
  //Serial.begin(9600);
  //Ethernet.begin(mac, ip);
  // initialize the w5100 chip and open a RAW socket 
  W5100.init();
  W5100.writeSnMR(s, SnMR::MACRAW);
  W5100.execCmdSn(s, Sock_OPEN);
  delay(2000);
}

void loop() {

  for (int i=1;i<255;i++)
  {
    //for (int j=0;j<2;j++)
    //{	
    TargetIP[3] = i;
    byte return_mac[7];
    return_mac[6] = IptoMAC(TargetIP,return_mac);
    //if the return value is 1, then the mac-address was written properly to "return_mac"

    /*
    Serial.println(return_mac[6]);
    /*
      if (return_mac[6] == 1) 
     {
     for(int k=0;i<7;k++)
     {
     mac_address_register[i][k] = return_mac[k];
     }
     break;
     }
     
    }*/
  }
  delay(1000);
}


byte IptoMAC(byte *IPtoRequest, byte *mac_array) {
  byte DestAddress[] = {
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF          }; //6
  byte *SourceAddress = mac; //6
  byte Type[] = {
    0x08,0x06            }; //2
  byte HardwareType[] = {
    0x00,0x01          }; //2
  byte ProtocolType[] = {
    0x08,0x00          }; //2
  byte HardwareSize[] = {
    0x06          }; //1
  byte ProtocolSize[] = {
    0x04          }; //1
  byte Opcode[] = {
    0x00,0x01          }; //2
  byte *SenderMAC = mac; //6
  byte *SenderIP = ip; //4
  byte TargetMAC[] = {
    0x00,0x00,0x00,0x00,0x00,0x00          }; //6
  byte *TargetIP = IPtoRequest; //4

  byte packet_len = 0;
  byte packet[42];
  array_cpy(packet,DestAddress,&packet_len,6);
  array_cpy(packet,SourceAddress,&packet_len,6);
  array_cpy(packet,Type,&packet_len,2);
  array_cpy(packet,HardwareType,&packet_len,2);
  array_cpy(packet,ProtocolType,&packet_len,2);
  array_cpy(packet,HardwareSize,&packet_len,1);
  array_cpy(packet,ProtocolSize,&packet_len,1);
  array_cpy(packet,Opcode,&packet_len,2);
  array_cpy(packet,SenderMAC,&packet_len,6);
  array_cpy(packet,SenderIP,&packet_len,4);
  array_cpy(packet,TargetMAC,&packet_len,6);
  array_cpy(packet,TargetIP,&packet_len,4);

  W5100.send_data_processing(s, (uint8_t *)packet, 42);
  W5100.execCmdSn(s, Sock_SEND_MAC);
  byte ReplyIP[4] = {
    0,0,0,0        };
  unsigned long timer = millis();

  do
  {
    rbuflen = W5100.getRXReceivedSize(s);

    if (rbuflen>0) {

      // receive packet
      W5100.recv_data_processing(s, rbuf, rbuflen);
      W5100.execCmdSn(s, Sock_RECV);

      if ((rbuf[14] == 0x08) && (rbuf[15] == 0x06) && (rbuf[22] == 0x00) && (rbuf[23] == 0x02))
      {
        for (int i=0; i<4; i++)
        {
          ReplyIP[i] = rbuf[30+i];
        }
      }
    }
  }
  while ((array_compare(ReplyIP,IPtoRequest,4)!= 1) && ((millis() - timer)< 500));

  if (array_compare(ReplyIP,IPtoRequest,4) == 1)
  {

    for (int i=0; i<6; i++)
    {
      mac_array[i] = rbuf[i+24];
    }
    return 1;
  }
  return 0;
}

byte array_compare(byte *array1,byte *array2,byte len) {

  for(byte i = 0; i <len;i++)
  {
    if (array1[i] != array2[i]) return 0;
  }
  return 1;
}

byte array_cpy(byte *array, byte *add_array,byte *start_byte,byte end_byte) {
  memcpy(&array[*start_byte],add_array,end_byte * sizeof(byte));
  *start_byte+= end_byte;
}
byte rbuf[1500+14]; // receive buffer 
byte sbuf[1500+14]; // send buffer

3028 bytes of SRAM used for these two arrays.

A Duemilanove or UNO has 2048 bytes of SRAM. What Arduino are you using?

I'm using the arduino ethernet board. thank you, ram exhaustion seemed to be the problem.