~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/my_gethostbyname.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2002, 2004 MySQL AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; version 2
 
6
   of the License.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
/* Thread safe version of gethostbyname_r() */
 
19
 
 
20
#include "mysys_priv.h"
 
21
#if !defined(__WIN__)
 
22
#include <netdb.h>
 
23
#endif
 
24
#include <my_net.h>
 
25
 
 
26
/* This file is not needed if my_gethostbyname_r is a macro */
 
27
#if !defined(my_gethostbyname_r)
 
28
 
 
29
/*
 
30
  Emulate SOLARIS style calls, not because it's better, but just to make the
 
31
  usage of getbostbyname_r simpler.
 
32
*/
 
33
 
 
34
#if defined(HAVE_GETHOSTBYNAME_R)
 
35
 
 
36
#if defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE)
 
37
 
 
38
struct hostent *my_gethostbyname_r(const char *name,
 
39
                                   struct hostent *result, char *buffer,
 
40
                                   int buflen, int *h_errnop)
 
41
{
 
42
  struct hostent *hp;
 
43
  DBUG_ASSERT((size_t) buflen >= sizeof(*result));
 
44
  if (gethostbyname_r(name,result, buffer, (size_t) buflen, &hp, h_errnop))
 
45
    return 0;
 
46
  return hp;
 
47
}
 
48
 
 
49
#elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT)
 
50
 
 
51
struct hostent *my_gethostbyname_r(const char *name,
 
52
                                   struct hostent *result, char *buffer,
 
53
                                   int buflen, int *h_errnop)
 
54
{
 
55
  if (gethostbyname_r(name,result,(struct hostent_data *) buffer) == -1)
 
56
  {
 
57
    *h_errnop= errno;
 
58
    return 0;
 
59
  }
 
60
  return result;
 
61
}
 
62
 
 
63
#else
 
64
 
 
65
/* gethostbyname_r with similar interface as gethostbyname() */
 
66
 
 
67
struct hostent *my_gethostbyname_r(const char *name,
 
68
                                   struct hostent *result, char *buffer,
 
69
                                   int buflen, int *h_errnop)
 
70
{
 
71
  struct hostent *hp;
 
72
  DBUG_ASSERT(buflen >= sizeof(struct hostent_data));
 
73
  hp= gethostbyname_r(name,result,(struct hostent_data *) buffer);
 
74
  *h_errnop= errno;
 
75
  return hp;
 
76
}
 
77
#endif /* GLIBC2_STYLE_GETHOSTBYNAME_R */
 
78
 
 
79
#else /* !HAVE_GETHOSTBYNAME_R */
 
80
 
 
81
#ifdef THREAD
 
82
extern pthread_mutex_t LOCK_gethostbyname_r;
 
83
#endif
 
84
 
 
85
/*
 
86
  No gethostbyname_r() function exists.
 
87
  In this case we have to keep a mutex over the call to ensure that no
 
88
  other thread is going to reuse the internal memory.
 
89
 
 
90
  The user is responsible to call my_gethostbyname_r_free() when he
 
91
  is finished with the structure.
 
92
*/
 
93
 
 
94
struct hostent *my_gethostbyname_r(const char *name,
 
95
                                   struct hostent *result, char *buffer,
 
96
                                   int buflen, int *h_errnop)
 
97
{
 
98
  struct hostent *hp;
 
99
  pthread_mutex_lock(&LOCK_gethostbyname_r);
 
100
  hp= gethostbyname(name);
 
101
  *h_errnop= h_errno;
 
102
  return hp;
 
103
}
 
104
 
 
105
void my_gethostbyname_r_free()
 
106
{
 
107
  pthread_mutex_unlock(&LOCK_gethostbyname_r);  
 
108
}
 
109
 
 
110
#endif /* !HAVE_GETHOSTBYNAME_R */
 
111
#endif /* !my_gethostbyname_r */