From 46807fef4ad651d29fecd081fd827b1c6341145e Mon Sep 17 00:00:00 2001 From: Tomasz Kojm Date: Sat, 20 Aug 2005 22:40:59 +0000 Subject: [PATCH] new files git-svn: trunk@1700 --- clamav-devel/ChangeLog | 4 ++ clamav-devel/shared/network.c | 88 +++++++++++++++++++++++++++++++++++ clamav-devel/shared/network.h | 28 +++++++++++ 3 files changed, 120 insertions(+) create mode 100644 clamav-devel/shared/network.c create mode 100644 clamav-devel/shared/network.h diff --git a/clamav-devel/ChangeLog b/clamav-devel/ChangeLog index af990de33..29d65ed51 100644 --- a/clamav-devel/ChangeLog +++ b/clamav-devel/ChangeLog @@ -1,3 +1,7 @@ +Sun Aug 21 00:32:10 CEST 2005 (tk) +---------------------------------- + * shared/network.[ch]: new files (include r_gethostbyname by NJH) + Wed Aug 17 15:53:33 CEST 2005 (acab) ------------------------------------ * libclamav/spin.c: fixed bitmap shifting diff --git a/clamav-devel/shared/network.c b/clamav-devel/shared/network.c new file mode 100644 index 000000000..2cd8e0f8a --- /dev/null +++ b/clamav-devel/shared/network.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2005 Nigel Horne + * + * 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 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#if HAVE_CONFIG_H +#include "clamav-config.h" +#endif + +#include +#include +#include + +#ifdef CL_THREAD_SAFE +#include +#endif + +/* + * 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) +{ +#if defined(HAVE_GETHOSTBYNAME_R_6) + /* e.g. Linux */ + struct hostent *hp2; + int ret = -1; + + if((hostname == NULL) || (hp == NULL)) + return -1; + if(gethostbyname_r(hostname, hp, buf, len, &hp2, &ret) < 0) + return ret; +#elif defined(HAVE_GETHOSTBYNAME_R_5) + /* e.g. BSD, Solaris, Cygwin */ + int ret = -1; + + if((hostname == NULL) || (hp == NULL)) + return -1; + if(gethostbyname_r(hostname, hp, buf, len, &ret) == NULL) + return ret; +#elif defined(HAVE_GETHOSTBYNAME_R_3) + /* e.g. HP/UX, AIX */ + if((hostname == NULL) || (hp == NULL)) + return -1; + if(gethostbyname_r(hostname, &hp, (struct hostent_data *)buf) < 0) + return h_errno; +#else + /* Single thread the code */ + struct hostent *hp2; +#ifdef CL_THREAD_SAFE + static pthread_mutex_t hostent_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + + if((hostname == NULL) || (hp == NULL)) + return -1; +#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 + + return 0; +} diff --git a/clamav-devel/shared/network.h b/clamav-devel/shared/network.h new file mode 100644 index 000000000..d4953fd22 --- /dev/null +++ b/clamav-devel/shared/network.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2005 Nigel Horne + * + * 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 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef __NETWORK_H +#define __NETWORK_H + +#include +#include + +int r_gethostbyname(const char *hostname, struct hostent *hp, char *buf, size_t len); + +#endif