/* * getmac.c * Retrieves the MAC address for a given network interface on Solaris * platforms using two methods: arp and dlpi. Please note that dlpi * requires root to retrieve an address. * Compile * gcc getmac.c -o getmac -lsocket -ldlpi * Run * ./getmac * Ex: ./getmac e1000g0 * * Written by Paul Johnson * paulie@pauliesworld.org */ #include #include #include #include #include #include #include #include #include static char* get_physical_address_arp(char* nicname) { int i, j, sock, nicount; char octet[3]; char* finaladdress; struct arpreq arpreq; struct ifreq nicnumber[24]; struct ifconf ifconf; if ((sock=socket(AF_INET,SOCK_DGRAM,0)) > -1) { ifconf.ifc_buf = (caddr_t)nicnumber; ifconf.ifc_len = sizeof(nicnumber); } else { return("Could not create socket."); } if (!ioctl(sock,SIOCGIFCONF,(char*)&ifconf)) { nicount = ifconf.ifc_len/(sizeof(struct ifreq)); for (i=0; i<=nicount; i++) { if (!strcmp(nicnumber[i].ifr_name,nicname)) { break; } if (i == (nicount-1)) { close(sock); return("No such interface."); } } } else { close(sock); return("ioctl error."); } ((struct sockaddr_in*)&arpreq.arp_pa)->sin_addr.s_addr= ((struct sockaddr_in*)&nicnumber[i].ifr_addr)->sin_addr.s_addr; if (!(ioctl(sock,SIOCGARP,(char*)&arpreq))) { finaladdress = (char*)malloc(48); for (j=0; j<=5; j++) { snprintf(octet,sizeof(octet),"%02x", (unsigned char)arpreq.arp_ha.sa_data[j]); strcat (finaladdress,octet); } close (sock); return (finaladdress); } else { return ("Could not determine MAC address."); } } static char* get_physical_address_dlpi(char* nicname) { int i, physaddrlen=DLPI_PHYSADDR_MAX; char octet[3]; char physaddr[DLPI_PHYSADDR_MAX]; char* finaladdress; dlpi_handle_t dh; if (dlpi_open(nicname, &dh, 0) != DLPI_SUCCESS) { return("dlpi failure, are you root?"); } if (dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, &physaddrlen) != DLPI_SUCCESS) { dlpi_close(dh); return("Could not retrieve physical address."); } finaladdress = (char*)malloc(48); for (i=0; i\n\n"); return 0; } physaddress_arp = get_physical_address_arp(argv[1]); physaddress_dlpi = get_physical_address_dlpi(argv[1]); printf("arp:\t%s\ndlpi:\t%s\n",physaddress_arp,physaddress_dlpi); return(0); }