mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_net.c
bloba82ee2dc478d81556cd95e5ba803a2361c11e092
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"
19 #include <m_string.h>
21 /* for thread safe my_inet_ntoa */
22 #if !defined(__WIN__)
23 #include <netdb.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 #include <sys/socket.h>
26 #endif
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
32 #endif
33 #endif /* !defined(__WIN__) */
34 #include "my_net.h"
37 void my_inet_ntoa(struct in_addr in, char *buf)
39 char *ptr;
40 pthread_mutex_lock(&THR_LOCK_net);
41 ptr=inet_ntoa(in);
42 strmov(buf,ptr);
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)
62 struct hostent *hp;
63 DBUG_ASSERT((size_t) buflen >= sizeof(*result));
64 if (gethostbyname_r(name,result, buffer, (size_t) buflen, &hp, h_errnop))
65 return 0;
66 return hp;
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)
77 *h_errnop= errno;
78 return 0;
80 return result;
83 #else
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)
91 struct hostent *hp;
92 DBUG_ASSERT(buflen >= sizeof(struct hostent_data));
93 hp= gethostbyname_r(name,result,(struct hostent_data *) buffer);
94 *h_errnop= errno;
95 return hp;
97 #endif /* GLIBC2_STYLE_GETHOSTBYNAME_R */
99 #else /* !HAVE_GETHOSTBYNAME_R */
101 #ifdef THREAD
102 extern pthread_mutex_t LOCK_gethostbyname_r;
103 #endif
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.
114 struct hostent *
115 my_gethostbyname_r(const char *name,
116 struct hostent *result __attribute__((unused)),
117 char *buffer __attribute__((unused)),
118 int buflen __attribute__((unused)),
119 int *h_errnop)
121 struct hostent *hp;
122 pthread_mutex_lock(&LOCK_gethostbyname_r);
123 hp= gethostbyname(name);
124 *h_errnop= h_errno;
125 return hp;
128 void my_gethostbyname_r_free()
130 pthread_mutex_unlock(&LOCK_gethostbyname_r);
133 #endif /* !HAVE_GETHOSTBYNAME_R */
134 #endif /* !my_gethostbyname_r */