Thread: SIOCGIFHWADDR not working.... :(

  1. #1
    Registered User
    Join Date
    Oct 2006

    SIOCGIFHWADDR not working.... :(

    int getmacaddress(char* dev, unsigned char *ipmac);
    int main()
        int mac;
        unsigned char dot_not_mac[25];
        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;
    	skfd = socket(AF_INET,SOCK_DGRAM,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;
    	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 is not printing MAC address.... please help me..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > memcpy(ipmac,ifr.ifr_hwaddr.sa_data,6);
    Because this is 6 raw bytes, not a printable string.
    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