diff options
author | unknown <serg@serg.mylan> | 2004-02-27 20:30:08 +0100 |
---|---|---|
committer | unknown <serg@serg.mylan> | 2004-02-27 20:30:08 +0100 |
commit | 83c04153bc02af520925a290f95b163ed047ffdb (patch) | |
tree | d0f8d063fcdfa4d4c0549be6ee8d1a3d49393175 /mysys | |
parent | 4e468f66cc48c1ede186103594fb8a4a00ce010e (diff) | |
download | mariadb-git-83c04153bc02af520925a290f95b163ed047ffdb.tar.gz |
my_gethwaddr() for linux/freebsd
UUID() function
BitKeeper/etc/ignore:
Added mysys/test_gethwaddr to the ignore list
include/my_sys.h:
my_gethwaddr()
mysys/Makefile.am:
my_gethwaddr
sql/item_create.cc:
UUID() function
sql/item_create.h:
UUID() function
sql/item_strfunc.cc:
UUID() function
sql/item_strfunc.h:
UUID() function
sql/lex.h:
UUID() function
sql/mysql_priv.h:
UUID() function
sql/mysqld.cc:
UUID() function
sql/sql_class.cc:
cleanup
Diffstat (limited to 'mysys')
-rw-r--r-- | mysys/Makefile.am | 7 | ||||
-rw-r--r-- | mysys/my_gethwaddr.c | 130 |
2 files changed, 136 insertions, 1 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am index eb6d21c360a..77b17d28ba3 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -39,7 +39,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c \ mf_format.c mf_same.c mf_dirname.c mf_fn_ext.c \ my_symlink.c my_symlink2.c \ mf_pack.c mf_unixpath.c mf_strip.c \ - mf_wcomp.c mf_wfile.c \ + mf_wcomp.c mf_wfile.c my_gethwaddr.c \ mf_qsort.c mf_qsort2.c mf_sort.c \ ptr_cmp.c mf_radix.c queues.c \ tree.c list.c hash.c array.c string.c typelib.c \ @@ -110,5 +110,10 @@ charset2html$(EXEEXT): charset2html.c $(LIBRARIES) testhash$(EXEEXT): testhash.c $(LIBRARIES) $(LINK) $(FLAGS) -DMAIN $(srcdir)/testhash.c $(LDADD) $(LIBS) +test_gethwaddr$(EXEEXT): my_gethwaddr.c $(LIBRARIES) + $(CP) $(srcdir)/my_gethwaddr.c ./test_gethwaddr.c + $(LINK) $(FLAGS) -DMAIN ./test_gethwaddr.c $(LDADD) $(LIBS) + $(RM) -f ./test_gethwaddr.c + # Don't update the files from bitkeeper %::SCCS/s.% diff --git a/mysys/my_gethwaddr.c b/mysys/my_gethwaddr.c new file mode 100644 index 00000000000..72f1cb975c4 --- /dev/null +++ b/mysys/my_gethwaddr.c @@ -0,0 +1,130 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* get hardware address for an interface */ +/* if there are many available, any non-zero one can be used */ + +#include "mysys_priv.h" +#include <m_string.h> + +#ifndef MAIN +static my_bool memcpy_and_test(uchar *to, uchar *from, uint len) +{ + uint i, res=1; + + for (i=0; i < len; i++) + if ((*to++= *from++)) + res=0; + return res; +} + +#ifdef __FreeBSD__ + +#include <net/ethernet.h> +#include <sys/sysctl.h> +#include <net/route.h> +#include <net/if.h> +#include <net/if_dl.h> + +my_bool my_gethwaddr(uchar *to) +{ + size_t len; + uchar *buf, *next, *end, *addr; + struct if_msghdr *ifm; + struct sockaddr_dl *sdl; + int i, res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0}; + + if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) + goto err; + if (!(buf = alloca(len))) + goto err; + if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) + goto err; + + end = buf + len; + + for (next = buf ; res && next < end ; next += ifm->ifm_msglen) + { + ifm = (struct if_msghdr *)next; + if (ifm->ifm_type == RTM_IFINFO) + { + sdl = (struct sockaddr_dl *)(ifm + 1); + addr=LLADDR(sdl); + res=memcpy_and_test(to, addr, ETHER_ADDR_LEN); + } + } + +err: + return res; +} + +#elif __linux__ + +#include <net/if.h> +#include <sys/ioctl.h> +#include <net/ethernet.h> + +my_bool my_gethwaddr(uchar *to) +{ + int fd, res=1; + struct ifreq ifr; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + goto err; + + bzero(&ifr, sizeof(ifr)); + strnmov(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1); + + do { + if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0) + res=memcpy_and_test(to, (uchar *)&ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); + } while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6'); + + close(fd); +err: + return res; +} + +#else +/* just fail */ +my_bool my_gethwaddr(uchar *to __attribute__((unused))) +{ + return 1; +} +#endif + +#else MAIN +int main(int argc __attribute__((unused)),char **argv) +{ + uchar mac[6]; + uint i; + MY_INIT(argv[0]); + if (my_gethwaddr(mac)) + { + printf("my_gethwaddr failed with errno %d\n", errno); + exit(1); + } + for (i=0; i < sizeof(mac); i++) + { + if (i) printf(":"); + printf("%02x", mac[i]); + } + printf("\n"); + return 0; +} +#endif + |