2006-12-31 01:02:27 +01:00
|
|
|
/* Copyright (C) 2004 MySQL AB
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
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
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2007-08-09 14:56:57 +02:00
|
|
|
#ifndef MAIN
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__linux__)
|
2004-02-27 20:30:08 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-08-09 14:56:57 +02:00
|
|
|
#endif /* FreeBSD || linux */
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
#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;
|
Bug#53445: Build with -Wall and fix warnings that it generates
Apart strict-aliasing warnings, fix the remaining warnings
generated by GCC 4.4.4 -Wall and -Wextra flags.
One major source of warnings was the in-house function my_bcmp
which (unconventionally) took pointers to unsigned characters
as the byte sequences to be compared. Since my_bcmp and bcmp
are deprecated functions whose only difference with memcmp is
the return value, every use of the function is replaced with
memcmp as the special return value wasn't actually being used
by any caller.
There were also various other warnings, mostly due to type
mismatches, missing return values, missing prototypes, dead
code (unreachable) and ignored return values.
BUILD/SETUP.sh:
Remove flags that are implied by -Wall and -Wextra.
Do not warn about unused parameters in C++.
BUILD/check-cpu:
Print only the compiler version instead of verbose banner.
Although the option is gcc specific, the check was only
being used for GCC specific checks anyway.
client/mysql.cc:
bcmp is no longer defined.
client/mysqltest.cc:
Pass a string to function expecting a format string.
Replace use of bcmp with memcmp.
cmd-line-utils/readline/Makefile.am:
Always define _GNU_SOURCE when compiling GNU readline.
Required to make certain prototypes visible.
cmd-line-utils/readline/input.c:
Condition for the code to be meaningful.
configure.in:
Remove check for bcmp.
extra/comp_err.c:
Use appropriate type.
extra/replace.c:
Replace use of bcmp with memcmp.
extra/yassl/src/crypto_wrapper.cpp:
Do not ignore the return value of fgets. Retrieve the file
position if fgets succeed -- if it fails, the function will
bail out and return a error.
extra/yassl/taocrypt/include/blowfish.hpp:
Use a single array instead of accessing positions of the sbox_
through a subscript to pbox_.
extra/yassl/taocrypt/include/runtime.hpp:
One definition of such functions is enough.
extra/yassl/taocrypt/src/aes.cpp:
Avoid potentially ambiguous conditions.
extra/yassl/taocrypt/src/algebra.cpp:
Rename arguments to avoid shadowing related warnings.
extra/yassl/taocrypt/src/blowfish.cpp:
Avoid potentially ambiguous conditions.
extra/yassl/taocrypt/src/integer.cpp:
Do not define type within a anonymous union.
Use a variable to return a value instead of
leaving the result in a register -- compiler
does not know the logic inside the asm.
extra/yassl/taocrypt/src/misc.cpp:
Define handler for pure virtual functions.
Remove unused code.
extra/yassl/taocrypt/src/twofish.cpp:
Avoid potentially ambiguous conditions.
extra/yassl/testsuite/test.hpp:
Function must have C language linkage.
include/m_string.h:
Remove check which relied on bcmp being defined -- they weren't
being used as bcmp is only visible when _BSD_SOURCE is defined.
include/my_bitmap.h:
Remove bogus helpers which were used only in a few files and
were causing warnings about dead code.
include/my_global.h:
Due to G++ bug, always silence false-positive uninitialized
variables warnings when compiling C++ code with G++.
Remove bogus helper.
libmysql/Makefile.shared:
Remove built-in implementation of bcmp.
mysql-test/lib/My/SafeProcess/safe_process.cc:
Cast pid to largest possible type for a process identifier.
mysys/mf_loadpath.c:
Leave space of the ending nul.
mysys/mf_pack.c:
Replace bcmp with memcmp.
mysys/my_bitmap.c:
Dead code removal.
mysys/my_gethwaddr.c:
Remove unused variable.
mysys/my_getopt.c:
Silence bogus uninitialized variable warning.
Do not cast away the constant qualifier.
mysys/safemalloc.c:
Cast to expected type.
mysys/thr_lock.c:
Silence bogus uninitialized variable warning.
sql/field.cc:
Replace bogus helper with a more appropriate logic which is
used throughout the code.
sql/item.cc:
Remove bogus logical condition which always evaluates to TRUE.
sql/item_create.cc:
Simplify code to avoid signedness related warnings.
sql/log_event.cc:
Replace use of bcmp with memcmp.
No need to use helpers for simple bit operations.
sql/log_event_old.cc:
Replace bmove_align with memcpy.
sql/mysqld.cc:
Move use declaration of variable to the ifdef block where it
is used. Remove now-unnecessary casts and arguments.
sql/set_var.cc:
Replace bogus helpers with simple and classic bit operations.
sql/slave.cc:
Cast to expected type and silence bogus warning.
sql/sql_class.h:
Don't use enum values as bit flags, the supposed type safety is
bogus as the combined bit flags are not a value in the enumeration.
sql/udf_example.c:
Only declare variable when necessary.
sql/unireg.h:
Replace use of bmove_align with memcpy.
storage/innobase/os/os0file.c:
Silence bogus warning.
storage/myisam/mi_open.c:
Remove bogus cast, DBUG_DUMP expects a pointer to unsigned
char.
storage/myisam/mi_page.c:
Remove bogus cast, DBUG_DUMP expects a pointer to unsigned
char.
strings/bcmp.c:
Remove built-in bcmp.
strings/ctype-ucs2.c:
Silence bogus warning.
tests/mysql_client_test.c:
Use a appropriate type as expected by simple_command().
2010-07-02 15:30:47 -03:00
|
|
|
int res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
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);
|
2010-09-09 15:51:50 +03:00
|
|
|
addr=(uchar *)LLADDR(sdl);
|
2004-02-27 20:30:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-11 22:42:39 +02:00
|
|
|
#elif defined(__WIN__)
|
2010-05-31 12:29:54 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
Workaround for BUG#32082 (Definition of VOID in my_global.h conflicts with
|
|
|
|
windows headers)
|
|
|
|
*/
|
|
|
|
#ifdef VOID
|
|
|
|
#undef VOID
|
|
|
|
#define VOID void
|
|
|
|
#endif
|
2009-09-13 19:38:06 +02:00
|
|
|
|
2009-09-11 22:42:39 +02:00
|
|
|
#include <iphlpapi.h>
|
|
|
|
|
2010-05-31 12:29:54 -03:00
|
|
|
/*
|
|
|
|
The following typedef is for dynamically loading iphlpapi.dll /
|
|
|
|
GetAdaptersAddresses. Dynamic loading is used because
|
|
|
|
GetAdaptersAddresses is not available on Windows 2000 which MySQL
|
|
|
|
still supports. Static linking would cause an unresolved export.
|
2009-09-11 22:42:39 +02:00
|
|
|
*/
|
|
|
|
typedef DWORD (WINAPI *pfnGetAdaptersAddresses)(IN ULONG Family,
|
|
|
|
IN DWORD Flags,IN PVOID Reserved,
|
2010-05-31 12:29:54 -03:00
|
|
|
OUT PIP_ADAPTER_ADDRESSES pAdapterAddresses,
|
2009-09-11 22:42:39 +02:00
|
|
|
IN OUT PULONG pOutBufLen);
|
|
|
|
|
|
|
|
/*
|
2010-05-31 12:29:54 -03:00
|
|
|
my_gethwaddr - Windows version
|
2009-09-11 22:42:39 +02:00
|
|
|
|
|
|
|
@brief Retrieve MAC address from network hardware
|
2010-05-31 12:29:54 -03:00
|
|
|
|
2009-09-11 22:42:39 +02:00
|
|
|
@param[out] to MAC address exactly six bytes
|
2010-05-31 12:29:54 -03:00
|
|
|
|
2009-09-11 22:42:39 +02:00
|
|
|
@return Operation status
|
|
|
|
@retval 0 OK
|
2010-05-31 12:29:54 -03:00
|
|
|
@retval <>0 FAILED
|
2009-09-11 22:42:39 +02:00
|
|
|
*/
|
|
|
|
my_bool my_gethwaddr(uchar *to)
|
2010-05-31 12:29:54 -03:00
|
|
|
{
|
2009-09-11 22:42:39 +02:00
|
|
|
PIP_ADAPTER_ADDRESSES pAdapterAddresses;
|
|
|
|
PIP_ADAPTER_ADDRESSES pCurrAddresses;
|
|
|
|
IP_ADAPTER_ADDRESSES adapterAddresses;
|
|
|
|
ULONG address_len;
|
2010-05-31 12:29:54 -03:00
|
|
|
my_bool return_val= 1;
|
|
|
|
static pfnGetAdaptersAddresses fnGetAdaptersAddresses=
|
|
|
|
(pfnGetAdaptersAddresses)-1;
|
2009-09-11 22:42:39 +02:00
|
|
|
|
|
|
|
if(fnGetAdaptersAddresses == (pfnGetAdaptersAddresses)-1)
|
|
|
|
{
|
|
|
|
/* Get the function from the DLL */
|
|
|
|
fnGetAdaptersAddresses= (pfnGetAdaptersAddresses)
|
|
|
|
GetProcAddress(LoadLibrary("iphlpapi.dll"),
|
|
|
|
"GetAdaptersAddresses");
|
|
|
|
}
|
|
|
|
if (!fnGetAdaptersAddresses)
|
|
|
|
return 1; /* failed to get function */
|
|
|
|
address_len= sizeof (IP_ADAPTER_ADDRESSES);
|
|
|
|
|
|
|
|
/* Get the required size for the address data. */
|
2010-05-31 12:29:54 -03:00
|
|
|
if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, &adapterAddresses, &address_len)
|
2009-09-11 22:42:39 +02:00
|
|
|
== ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
|
|
|
pAdapterAddresses= my_malloc(address_len, 0);
|
|
|
|
if (!pAdapterAddresses)
|
|
|
|
return 1; /* error, alloc failed */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pAdapterAddresses= &adapterAddresses; /* one is enough don't alloc */
|
|
|
|
|
|
|
|
/* Get the hardware info. */
|
2010-05-31 12:29:54 -03:00
|
|
|
if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, pAdapterAddresses, &address_len)
|
2009-09-11 22:42:39 +02:00
|
|
|
== NO_ERROR)
|
|
|
|
{
|
|
|
|
pCurrAddresses= pAdapterAddresses;
|
|
|
|
|
2010-05-31 12:29:54 -03:00
|
|
|
while (pCurrAddresses)
|
2009-09-11 22:42:39 +02:00
|
|
|
{
|
|
|
|
/* Look for ethernet cards. */
|
|
|
|
if (pCurrAddresses->IfType == IF_TYPE_ETHERNET_CSMACD)
|
|
|
|
{
|
|
|
|
/* check for a good address */
|
|
|
|
if (pCurrAddresses->PhysicalAddressLength < 6)
|
2010-05-31 12:29:54 -03:00
|
|
|
continue; /* bad address */
|
2009-09-11 22:42:39 +02:00
|
|
|
|
|
|
|
/* save 6 bytes of the address in the 'to' parameter */
|
|
|
|
memcpy(to, pCurrAddresses->PhysicalAddress, 6);
|
|
|
|
|
|
|
|
/* Network card found, we're done. */
|
|
|
|
return_val= 0;
|
2010-05-31 12:29:54 -03:00
|
|
|
break;
|
2009-09-11 22:42:39 +02:00
|
|
|
}
|
|
|
|
pCurrAddresses= pCurrAddresses->Next;
|
2010-05-31 12:29:54 -03:00
|
|
|
}
|
2009-09-11 22:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up memory allocation. */
|
|
|
|
if (pAdapterAddresses != &adapterAddresses)
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
2010-07-08 18:20:08 -03:00
|
|
|
my_free(pAdapterAddresses);
|
2009-09-11 22:42:39 +02:00
|
|
|
|
|
|
|
return return_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* __FreeBSD__ || __linux__ || __WIN__ */
|
2004-02-27 20:30:08 +01:00
|
|
|
/* just fail */
|
|
|
|
my_bool my_gethwaddr(uchar *to __attribute__((unused)))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-20 01:28:42 +03:00
|
|
|
#else /* MAIN */
|
2004-02-27 20:30:08 +01:00
|
|
|
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
|
|
|
|
|