Recently i'm interested in how to access and set network interfaces in linux using a low level aproach. So i found, searching on the net the ioctl callback and i learnt how to use it. I started writing down something but today i'm having problem to change the mac address of a interface.
The code that i'm using is:
int change_mac(int handler){
srand(times(NULL));
get_hwaddr(handler,&req);
for(int i=0;i<6;i++){
req.ifr_hwaddr.sa_data[i]=(char)rand();
}
char err=ioctl(sock,SIOCSIFHWADDR,&req);
if(err<0){
printf("Problem to change the mac address with a random one\n %i",err);
exit(0);
}
}
int get_hwaddr(int handler,struct ifreq* req_a){
if(handler>=current){
return -1;
}
strncpy(req_a->ifr_name,interfaces[handler],strlen(interfaces[handler]));
if(ioctl(sock,SIOCGIFHWADDR,req_a)<0){
return -2;
}
}
To better understand my code the "interfaces" variable is a char** where i store the name of interfaces when i initializate a new interface, and in that function after store the name in that variable, i'm returning a int as handler.
Before changing the mac address, from the code i put the interface down.
Now when i call ioctl to change the mac address with the SIOCSIFHWADDR ioctl return -1.
Can anyone help me? Sorry for the english
errno
for details on what the error actually is. That might help you – e.dan