I wrote a small application in C I want to use as a CGI-Programm with Busybox.
The program gets the IP of a client via CGI (with stdlib getenv("REMOTE_ADDR")) and does
an ARP-request to get the corresponding MAC-address.

My app works when compiled on a standard debian installation.

I get an ioctl-error on my OpenWRT.
Sure, I cross-compiled it with the SDK like that:
./staging_dir_mipsel/bin/mipsel-linux-uclibc-gcc-3.4.4  myapp.c -o index.cgi


This is the part of the code doing the arp-request:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

#define INTERFACE "br0"

int main(void) {

    char *cgiraddr;
    int s,x;
    struct arpreq a;
    struct sockaddr_in * sin;
    struct in_addr addr;

    char macaddr[18]= "\0";
    char macpart[3] = "\0";

   
    if ((s = socket(AF_INET,SOCK_DGRAM,0) == -1)) {
        printf("ERROR: Socket error");
        return -1;
    }

    cgiraddr = getenv("REMOTE_ADDR");
      
    if(cgiraddr!=NULL) {
   
        // prepare ARP-Request
        memset(&a,0,sizeof(a));
        strcpy(a.arp_dev,INTERFACE);
        sin = (struct sockaddr_in *) &(a.arp_pa);
        sin->sin_family = AF_INET;
        a.arp_flags = ATF_PUBL;

        if (! inet_aton(cgiraddr,&addr)) {
            printf("ERROR: could not convert IP address.");
            return EXIT_FAILURE;
        }
       
        // perform ARP-Request
        memcpy(&sin->sin_addr,&addr,sizeof(struct in_addr));
        if (ioctl(s,SIOCGARP,&a) < 0) {                                        // <--- I get the Error here
            printf("ERROR: ioctl error");
            return EXIT_FAILURE;
        }

        //convert MAC as formatted string
        for (x=0;x<6;x++) {
            sprintf(macpart,"%02x",a.arp_ha.sa_data[x] & 0xff);
            strncat(macaddr,macpart,2);
            strncat(macaddr,x==5 ?" ":":",1);
        }


        // Output HTML-Page
        printf("Content-Type: text/html\n");
        printf("<html><head><title>Mac-Addr</title></head>\n");
        printf("<body>\n");
        printf("MAC: %s",macaddr);
        printf("</body></html>\n");

    } else {
          return EXIT_FAILURE;
    }

return EXIT_SUCCESS;

}

Normal arp -a with the IP-Adress gives an correct answer.

(Last edited by Lace on 29 Dec 2006, 22:32)