Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAC Address printing

Tags:

c

networking

This is a code that gets some info about network the problem is when it prints the MAC address it prints it sometime normally and sometime with fff's like 00:21:84:a2:12:88 and 00:ffffff21:84:a2:12:ffffff88 varies from machine to another

here is the code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/types.h>
#    include <netinet/if_ether.h>
#define IP_QUAD(ip)  (ip)>>24,((ip)&0x00ff0000)>>16,((ip)&0x0000ff00)>>8,((ip)&0x000000ff)
#define IP_ADDR_LEN 4
struct {
    char *dev;
    int sd;
    struct ether_addr eth;
    struct in_addr ip;
    struct in_addr bcast;
    unsigned int mtu;
} local_info ;

struct ifreq eth_init(char*,struct ifreq);
struct ifreq eth_get_info(struct ifreq);
struct ifreq eth_get_bcast(struct ifreq);

int
main(int argc,char **argv){
    int sd;
    struct ifreq ifr;
    if(argc != 2){
    fprintf(stderr,"usage: <command> <devicename>\n");
    exit(1);
    }
    ifr = eth_init(argv[1],ifr);
    ifr = eth_get_info(ifr);

    printf("> Exiting...\n");
    return(0);
}

struct ifreq
eth_init(char *dev,struct ifreq ifr){
//Intitating Socket     
if((local_info.sd = socket(PF_INET,SOCK_PACKET,(ETH_P_ALL))) < 0){
        printf("> Error initating the ethernet socket..\n");
        exit(-1);
    }
//Yupeeeeeeee Descriptor open 
    printf("> Initated Ethernet socket on Descriptor (%x)\n",local_info.sd);
//Set global variables
    local_info.dev = dev;
    return ifr;
}
struct ifreq
eth_get_info(struct ifreq ifr){
    int i = ETHER_ADDR_LEN;
    char* ptr;
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
    //Getting MAC
    if(ioctl(local_info.sd,SIOCGIFHWADDR,&ifr) < 0){
            printf("> Error Getting the Local Mac address\n");
            exit(-1);
        }
    printf("> Successfully received Local MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n",
                ifr.ifr_hwaddr.sa_data[0],ifr.ifr_hwaddr.sa_data[1],ifr.ifr_hwaddr.sa_data[2]
                ,ifr.ifr_hwaddr.sa_data[3],ifr.ifr_hwaddr.sa_data[4],ifr.ifr_hwaddr.sa_data[5]);

        memcpy(&(local_info.eth),&ifr.ifr_hwaddr.sa_data,ETH_ALEN);


    // Getting IP Address
        memset(&ifr,0,sizeof(ifr));
        strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
        if( ioctl(local_info.sd,SIOCGIFADDR,&ifr) < 0){ 
                printf("> Error gettint the local IP address\n");
                exit(-1);
        }   
        printf("> Successfully received the IP Address %s\n",inet_ntoa((*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr));
        memcpy(&(local_info.ip.s_addr),&(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,IP_ADDR_LEN);

            // Get MTU
        memset(&ifr,0,sizeof(ifr));
        strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
        if ( ioctl(local_info.sd,SIOCGIFMTU,&ifr) < 0){
                printf("> Error Getting the MTU Value\n");
                exit(-1);
        }
        printf("> Recevied Successfully the MTU Value \n");
        local_info.mtu = ifr.ifr_mtu;


    return ifr;
}

struct ifreq
eth_get_bcast(struct ifreq ifr){
/* get broadcast addr for size */
        memset(&ifr,0,sizeof(ifr));
        strncpy(ifr.ifr_name, local_info.dev, sizeof (ifr.ifr_name));
        if (ioctl(local_info.sd, SIOCGIFBRDADDR, &ifr) < 0 ) { 
           printf("> Error getting the Broadcast address\n");
           exit(-1);
        }
        printf("> Received the BroadCast address: %s\n",inet_ntoa((*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr));
        memcpy(&(local_info.bcast.s_addr),
               &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
               IP_ADDR_LEN);

    return ifr;
}

the problem is the the eth_get_info function the MAC sector , the printing statement

any solutions how to fix that?

like image 214
Saad Talaat Avatar asked May 19 '11 18:05

Saad Talaat


People also ask

What is a MAC address on a printer?

A MAC or physical address is a unique number assigned to a Network Interface Card (NIC), commonly called an Ethernet card. This 'address' is created by the manufacturer. A MAC address is a 12-digit number. Each digit is a number from 0-9 or a letter from A-F.

Where is the MAC address on an HP printer?

The Mac Address is listed on a label on te back of the printer, as well you may print a Network Configuration Page from teh printer menu to locate its Mac Address.


2 Answers

It looks like a signed/unsigned problem.

Try to cast into unsigned char :

  printf("> Successfully received Local MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n",
  (unsigned char) ifr.ifr_hwaddr.sa_data[0],
  (unsigned char) ifr.ifr_hwaddr.sa_data[1],
  (unsigned char) ifr.ifr_hwaddr.sa_data[2],
  (unsigned char) ifr.ifr_hwaddr.sa_data[3],
  (unsigned char) ifr.ifr_hwaddr.sa_data[4],
  (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
like image 54
Cédric Julien Avatar answered Sep 30 '22 20:09

Cédric Julien


Although there is already an accepted answer here, there is a better solution in netinet/ether.h.

Given that Mac addresses are typically stored in u_int8_t types, as in the ether_addr struct:

You can simply do this:

printf("Mac Address: %s", ether_ntoa((struct ether_addr*)ar->sa));

in my Case ar is something that looks like this:

struct {
   u_int8_t sa[6];
}

You can easily copy this into another buffer with something like asprintf:

char *formatted_mac_address;
asprintf(formatted_mac_address, "Mac Address: %s", ether_ntoa((struct ether_addr*)ar->sa));

If you don't have a struct as I do, you can also just use the address of any u_int8_t in place of ar->sa.

Appropriate headers/etc should be pulled in, but that's going to look a lot neater than the accepted solution here.

like image 40
Lee Hambley Avatar answered Sep 30 '22 20:09

Lee Hambley