summaryrefslogtreecommitdiff
path: root/mysys/my_net.c
blob: 820abf323866e60ceaa6af4c226bd6d98120fc70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright (C) 2000 MySQL 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; version 2 of the License.

   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 */

/* thread safe version of some common functions */

#include "mysys_priv.h"
#include <m_string.h>

/* for thread safe my_inet_ntoa */
#if !defined(__WIN__)
#include <netdb.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#endif /* !defined(__WIN__) */
#include "my_net.h"


void my_inet_ntoa(struct in_addr in, char *buf)
{
  char *ptr;
  pthread_mutex_lock(&THR_LOCK_net);
  ptr=inet_ntoa(in);
  strmov(buf,ptr);
  pthread_mutex_unlock(&THR_LOCK_net);
}

/* This code is not needed if my_gethostbyname_r is a macro */
#if !defined(my_gethostbyname_r)

/*
  Emulate SOLARIS style calls, not because it's better, but just to make the
  usage of getbostbyname_r simpler.
*/

#if defined(HAVE_GETHOSTBYNAME_R)

#if defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE)

struct hostent *my_gethostbyname_r(const char *name,
				   struct hostent *result, char *buffer,
				   int buflen, int *h_errnop)
{
  struct hostent *hp;
  DBUG_ASSERT((size_t) buflen >= sizeof(*result));
  if (gethostbyname_r(name,result, buffer, (size_t) buflen, &hp, h_errnop))
    return 0;
  return hp;
}

#elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT)

struct hostent *my_gethostbyname_r(const char *name,
				   struct hostent *result, char *buffer,
				   int buflen, int *h_errnop)
{
  if (gethostbyname_r(name,result,(struct hostent_data *) buffer) == -1)
  {
    *h_errnop= errno;
    return 0;
  }
  return result;
}

#else

/* gethostbyname_r with similar interface as gethostbyname() */

struct hostent *my_gethostbyname_r(const char *name,
				   struct hostent *result, char *buffer,
				   int buflen, int *h_errnop)
{
  struct hostent *hp;
  DBUG_ASSERT(buflen >= sizeof(struct hostent_data));
  hp= gethostbyname_r(name,result,(struct hostent_data *) buffer);
  *h_errnop= errno;
  return hp;
}
#endif /* GLIBC2_STYLE_GETHOSTBYNAME_R */

#else /* !HAVE_GETHOSTBYNAME_R */

#ifdef THREAD
extern pthread_mutex_t LOCK_gethostbyname_r;
#endif

/*
  No gethostbyname_r() function exists.
  In this case we have to keep a mutex over the call to ensure that no
  other thread is going to reuse the internal memory.

  The user is responsible to call my_gethostbyname_r_free() when he
  is finished with the structure.
*/

struct hostent *
my_gethostbyname_r(const char *name,
                   struct hostent *result __attribute__((unused)), 
                   char *buffer __attribute__((unused)),
                   int buflen __attribute__((unused)), 
                   int *h_errnop)
{
  struct hostent *hp;
  pthread_mutex_lock(&LOCK_gethostbyname_r);
  hp= gethostbyname(name);
  *h_errnop= h_errno;
  return hp;
}

void my_gethostbyname_r_free()
{
  pthread_mutex_unlock(&LOCK_gethostbyname_r);  
}

#endif /* !HAVE_GETHOSTBYNAME_R */
#endif /* !my_gethostbyname_r */