Tuesday, March 3, 2015

How to check the IO address space on your Linux machine

Parallel port is at address 0x0378?
This is the IO address space and this information is provided under the /proc file system as:

$> cat /proc/ioports

Sample output is as follows:

Monday, March 2, 2015

Explanation and code for reading interface names and MAC addresses

Here is a useful piece of user space code that can read the interfaces and their corresponding MAC addresses.

Download complete source here.
Complete repository can be downloaded or cloned here.

Main parts of the code:

The main parts of the program are:

1. Given the name of the interface, execute an IOCTL to get the hardware address:
/* gets the MAC addr for the interface */
int get_if_mac(char *eth, char *mac) {
    struct ifreq ifr;
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1) return -1;
    strncpy(ifr.ifr_name, eth, sizeof(ifr.ifr_name));
    if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) return -1;
    memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
    return 0;
}

2. Get interface info from the system in a linkedlist ifaddr:
 if (getifaddrs(&ifaddr) == -1) {
        perror("getifaddrs");
        exit(EXIT_FAILURE);
    }
3.  Loop and call the function get_if_mac() in step 1.