Thread: SIOCGIFHWADDR not working.... :(

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    SIOCGIFHWADDR not working.... :(

    Code:
    int getmacaddress(char* dev, unsigned char *ipmac);
    
    int main()
    {
        int mac;
        unsigned char dot_not_mac[25];
        mac=getmacaddress("eth0",dot_not_mac);
        printf(" the MAC address is %s\n",dot_not_mac);
        return 0;
    }
    
    int getmacaddress(char* dev,unsigned char *ipmac)
    {
    	int skfd=0;
    	struct ifreq ifr;
    
    	strcpy(ifr.ifr_name,dev);
    	skfd = socket(AF_INET,SOCK_DGRAM,0);
    	if(skfd<0)
    	{
    		DEBUG1("Can't open socket!\n");
    		return -1;
    	}
    	if(ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
    	{
    		DEBUG1("Can't read local mac address!\n");
    		return -1;
    	}
    
    	memcpy(ipmac,ifr.ifr_hwaddr.sa_data,6);
          	close(skfd);
    	return 0;
    }
    the output of the code is as below....
    the MAC address is

    can anyone tell me what is wrong with this code... this code is not working...it is not printing MAC address.... please help me..
    thanks..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > memcpy(ipmac,ifr.ifr_hwaddr.sa_data,6);
    Because this is 6 raw bytes, not a printable string.
    Code:
    unsigned char *p = ifr.ifr_hwaddr.sa_data;
    sprintf( ipmac, "&#37;02x:%02x:%02x:%02x:%02x:%02x",
             p[0], p[1], p[2], p[3], p[4], p[5] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM