diff options
author | Mike Fulbright <msf@redhat.com> | 2003-07-07 21:14:04 +0000 |
---|---|---|
committer | Mike Fulbright <msf@redhat.com> | 2003-07-07 21:14:04 +0000 |
commit | 26cd7f6a9bda813c83c7f8dd79c50f4ceb54a61d (patch) | |
tree | ae2e96d07b38914e368992935ff20d34ea6fdfbe /isys | |
parent | 4394fd0521de56b6021fddf07faf34cac9a15910 (diff) | |
download | anaconda-26cd7f6a9bda813c83c7f8dd79c50f4ceb54a61d.tar.gz anaconda-26cd7f6a9bda813c83c7f8dd79c50f4ceb54a61d.tar.xz anaconda-26cd7f6a9bda813c83c7f8dd79c50f4ceb54a61d.zip |
routine to get mac addr of nic
Diffstat (limited to 'isys')
-rw-r--r-- | isys/getmacaddr.c | 58 | ||||
-rw-r--r-- | isys/getmacaddr.h | 5 |
2 files changed, 63 insertions, 0 deletions
diff --git a/isys/getmacaddr.c b/isys/getmacaddr.c new file mode 100644 index 000000000..6a419dc74 --- /dev/null +++ b/isys/getmacaddr.c @@ -0,0 +1,58 @@ +/* + * getmacaddr.c - get mac address for ethernet interface + * + * Copyright 2003 Red Hat, Inc. + * + * Michael Fulbright <msf@redhat.com> + * + * This software may be freely redistributed under the terms of the GNU + * general public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> +#include <net/if.h> + +/* returns NULL or allocated string */ +char *getMacAddr(char *ifname) { + int i; + int sock; + char *rcstr; + struct ifreq ifr; + + if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) + return NULL; + + /* Setup our control structures. */ + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, ifname); + + if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) + return NULL; + + rcstr = (char *) malloc(24); + *rcstr = '\0'; + for (i=0; i<6; i++) { + char tmp[4]; + sprintf(tmp, "%02X",(unsigned char)ifr.ifr_hwaddr.sa_data[i]); + strcat(rcstr,tmp); + if (i != 5) + strcat(rcstr, ":"); + } + + return rcstr; +} + +#ifdef TESTING +int main() { + + printf("%s\n", getMacAddr("eth0")); +} +#endif diff --git a/isys/getmacaddr.h b/isys/getmacaddr.h new file mode 100644 index 000000000..96a483abd --- /dev/null +++ b/isys/getmacaddr.h @@ -0,0 +1,5 @@ +#ifndef __GET_MAC_ADDR_H__ +#define __GET_MAC_ADDR_H__ +char *getMacAddr(char *ifname); +#endif + |