1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 /* thread safe version of some common functions */
18 #include "mysys_priv.h"
21 /* for thread safe my_inet_ntoa */
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
33 #endif /* !defined(__WIN__) */
37 void my_inet_ntoa(struct in_addr in
, char *buf
)
40 pthread_mutex_lock(&THR_LOCK_net
);
43 pthread_mutex_unlock(&THR_LOCK_net
);
46 /* This code is not needed if my_gethostbyname_r is a macro */
47 #if !defined(my_gethostbyname_r)
50 Emulate SOLARIS style calls, not because it's better, but just to make the
51 usage of getbostbyname_r simpler.
54 #if defined(HAVE_GETHOSTBYNAME_R)
56 #if defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE)
58 struct hostent
*my_gethostbyname_r(const char *name
,
59 struct hostent
*result
, char *buffer
,
60 int buflen
, int *h_errnop
)
63 DBUG_ASSERT((size_t) buflen
>= sizeof(*result
));
64 if (gethostbyname_r(name
,result
, buffer
, (size_t) buflen
, &hp
, h_errnop
))
69 #elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT)
71 struct hostent
*my_gethostbyname_r(const char *name
,
72 struct hostent
*result
, char *buffer
,
73 int buflen
, int *h_errnop
)
75 if (gethostbyname_r(name
,result
,(struct hostent_data
*) buffer
) == -1)
85 /* gethostbyname_r with similar interface as gethostbyname() */
87 struct hostent
*my_gethostbyname_r(const char *name
,
88 struct hostent
*result
, char *buffer
,
89 int buflen
, int *h_errnop
)
92 DBUG_ASSERT(buflen
>= sizeof(struct hostent_data
));
93 hp
= gethostbyname_r(name
,result
,(struct hostent_data
*) buffer
);
97 #endif /* GLIBC2_STYLE_GETHOSTBYNAME_R */
99 #else /* !HAVE_GETHOSTBYNAME_R */
102 extern pthread_mutex_t LOCK_gethostbyname_r
;
106 No gethostbyname_r() function exists.
107 In this case we have to keep a mutex over the call to ensure that no
108 other thread is going to reuse the internal memory.
110 The user is responsible to call my_gethostbyname_r_free() when he
111 is finished with the structure.
115 my_gethostbyname_r(const char *name
,
116 struct hostent
*result
__attribute__((unused
)),
117 char *buffer
__attribute__((unused
)),
118 int buflen
__attribute__((unused
)),
122 pthread_mutex_lock(&LOCK_gethostbyname_r
);
123 hp
= gethostbyname(name
);
128 void my_gethostbyname_r_free()
130 pthread_mutex_unlock(&LOCK_gethostbyname_r
);
133 #endif /* !HAVE_GETHOSTBYNAME_R */
134 #endif /* !my_gethostbyname_r */