46807fef |
/* |
086eab5c |
* Copyright (C) 2007-2009 Sourcefire, Inc.
*
* Authors: Nigel Horne |
46807fef |
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
46807fef |
*
*/
|
80ee1490 |
#ifdef _MSC_VER
#include <windows.h>
#include <winsock.h>
#endif
|
46807fef |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h> |
80ee1490 |
#ifdef HAVE_STRING_H |
65d503e6 |
#include <string.h> |
80ee1490 |
#endif
|
ace24e1f |
#ifdef HAVE_SYS_TYPES_H |
46807fef |
#include <sys/types.h> |
80ee1490 |
#endif
|
ace24e1f |
#ifndef C_WINDOWS |
46807fef |
#include <netdb.h> |
80ee1490 |
#endif |
46807fef |
|
e091cb38 |
#ifdef CL_NOTHREADS
#undef CL_THREAD_SAFE
#endif
|
46807fef |
#ifdef CL_THREAD_SAFE
#include <pthread.h>
#endif
|
fc83da82 |
#include "network.h"
|
46807fef |
/*
* TODO: gethostbyname_r is non-standard so different operating
* systems do it in different ways. Need more examples
* Perhaps we could use res_search()?
*
* Returns 0 for success
*/
int r_gethostbyname(const char *hostname, struct hostent *hp, char *buf, size_t len)
{
struct hostent *hp2;
int ret = -1; |
7959343d |
#if !defined(HAVE_GETHOSTBYNAME_R_6) && !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_3) |
808c3684 |
#ifdef CL_THREAD_SAFE
static pthread_mutex_t hostent_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif |
7959343d |
#endif |
46807fef |
if((hostname == NULL) || (hp == NULL))
return -1; |
a609276c |
memset(hp, 0, sizeof(struct hostent));
#if defined(HAVE_GETHOSTBYNAME_R_6)
/* e.g. Linux */ |
46807fef |
if(gethostbyname_r(hostname, hp, buf, len, &hp2, &ret) < 0)
return ret;
#elif defined(HAVE_GETHOSTBYNAME_R_5)
/* e.g. BSD, Solaris, Cygwin */
if(gethostbyname_r(hostname, hp, buf, len, &ret) == NULL)
return ret;
#elif defined(HAVE_GETHOSTBYNAME_R_3)
/* e.g. HP/UX, AIX */
if(gethostbyname_r(hostname, &hp, (struct hostent_data *)buf) < 0)
return h_errno;
#else |
e25b94f9 |
/* Single thread the code e.g. VS2005 */ |
46807fef |
#ifdef CL_THREAD_SAFE
pthread_mutex_lock(&hostent_mutex);
#endif
if((hp2 = gethostbyname(hostname)) == NULL) {
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&hostent_mutex);
#endif
return h_errno;
}
memcpy(hp, hp2, sizeof(struct hostent));
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&hostent_mutex);
#endif
|
a7282c2f |
#endif |
29fdbe1b |
return 0; |
46807fef |
} |