2
* Copyright (C) 2007-2009 Sourcefire, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29
#include "clamav-config.h"
38
#ifdef HAVE_SYS_TYPES_H
39
#include <sys/types.h>
57
* TODO: gethostbyname_r is non-standard so different operating
58
* systems do it in different ways. Need more examples
59
* Perhaps we could use res_search()?
61
* Returns 0 for success
63
int r_gethostbyname(const char *hostname, struct hostent *hp, char *buf, size_t len)
67
#if !defined(HAVE_GETHOSTBYNAME_R_6) && !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_3)
69
static pthread_mutex_t hostent_mutex = PTHREAD_MUTEX_INITIALIZER;
73
if((hostname == NULL) || (hp == NULL))
75
memset(hp, 0, sizeof(struct hostent));
76
#if defined(HAVE_GETHOSTBYNAME_R_6)
78
if(gethostbyname_r(hostname, hp, buf, len, &hp2, &ret) < 0)
80
#elif defined(HAVE_GETHOSTBYNAME_R_5)
81
/* e.g. BSD, Solaris, Cygwin */
82
if(gethostbyname_r(hostname, hp, buf, len, &ret) == NULL)
84
#elif defined(HAVE_GETHOSTBYNAME_R_3)
86
if(gethostbyname_r(hostname, &hp, (struct hostent_data *)buf) < 0)
89
/* Single thread the code e.g. VS2005 */
91
pthread_mutex_lock(&hostent_mutex);
93
if((hp2 = gethostbyname(hostname)) == NULL) {
95
pthread_mutex_unlock(&hostent_mutex);
99
memcpy(hp, hp2, sizeof(struct hostent));
100
#ifdef CL_THREAD_SAFE
101
pthread_mutex_unlock(&hostent_mutex);