What I'm missing and I do not really know how to do it is list which network cards are active, get the names (example: eth0, eth1), I have a program that already gets the mac and occupies the validation of the eth0 card , but my question is how to list all the others and know if they are active or not, I put the program that I have made, is formed by:
Lic2.h
#ifndef _LIC2_H_
#define _LIC2_H_
#define LIC_OK 0
#define LIC_NO_ADAPT_NAME_PROVIDED -1
#define LIC_INVALID_ADAPTER -2
#ifdef __cplusplus
extern "C" {
#endif
int licGetMacAddress(char * adapter, unsigned char mAddress[6]);
#ifdef __cplusplus
}
#endif
#endif /* LIC2_H_ */
Lic2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "Lic2.h"
int licGetMacAddress(char * adapter, unsigned char mAddress[6]) {
int x, i;
int fd;
struct ifreq ifr;
char direcMac[20];
/* Initialices output variable */
memset(mAddress, 0, 6);
/* Validates adapter name */
if (!adapter)
return LIC_NO_ADAPT_NAME_PROVIDED;
if (!strlen(adapter))
return LIC_NO_ADAPT_NAME_PROVIDED;
/* Gets adapter's macaddress */
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , adapter , IFNAMSIZ-1);
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
memcpy(mAddress, ifr.ifr_hwaddr.sa_data, 6);
/* Verifies if adapter doesn't exist */
x = 0;
for (i = 0; i < 6; i++)
if (!mAddress[i]) x++;
if (x == 6)
return LIC_INVALID_ADAPTER;
return LIC_OK;
}
and the "Main" so call it
example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Lic2.h"
int main () {
int status, i;
unsigned char mAddress[6];
status=licGetMacAddress("eth0", mAddress);
if (status!=LIC_OK) {
printf("Error al obtener el Mac Address: %d \n", status);
} else {
printf("Mac Address: ");
for (i=0; i<6; i++)
printf("%02X", mAddress[i]);
printf("\n");
}
return status;
}