00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include <ctype.h>
00013
#include <string.h>
00014
#include <asterisk/lock.h>
00015
#include <asterisk/utils.h>
00016
00017
#if defined(__FreeBSD__)
00018
00019
00020
#define ERANGE 34
00021
#undef gethostbyname
00022
00023
AST_MUTEX_DEFINE_STATIC(__mutex);
00024
00025
int gethostbyname_r (
const char *name,
struct hostent *ret,
char *buf,
00026 size_t buflen,
struct hostent **result,
00027
int *h_errnop)
00028 {
00029
int hsave;
00030
struct hostent *ph;
00031
ast_mutex_lock(&__mutex);
00032 hsave = h_errno;
00033
00034 ph =
gethostbyname(name);
00035 *h_errnop = h_errno;
00036
if (ph == NULL) {
00037 *result = NULL;
00038 }
else {
00039
char **p, **q;
00040
char *pbuf;
00041
int nbytes=0;
00042
int naddr=0, naliases=0;
00043
00044
00045
00046
for (p = ph->h_addr_list; *p != 0; p++) {
00047 nbytes += ph->h_length;
00048 nbytes +=
sizeof(*p);
00049 naddr++;
00050 }
00051 nbytes +=
sizeof(*p);
00052
00053
00054
for (p = ph->h_aliases; *p != 0; p++) {
00055 nbytes += (strlen(*p)+1);
00056 nbytes +=
sizeof(*p);
00057 naliases++;
00058 }
00059 nbytes +=
sizeof(*p);
00060
00061
00062
00063
if(nbytes > buflen) {
00064 *result = NULL;
00065
ast_mutex_unlock(&__mutex);
00066
return ERANGE;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 *ret = *ph;
00083
00084
00085 q = (
char **)buf;
00086 ret->h_addr_list = q;
00087 pbuf = buf + ((naddr+naliases+2)*
sizeof(*p));
00088
for (p = ph->h_addr_list; *p != 0; p++) {
00089 memcpy(pbuf, *p, ph->h_length);
00090 *q++ = pbuf;
00091 pbuf += ph->h_length;
00092 }
00093 *q++ = NULL;
00094
00095
00096 ret->h_aliases = q;
00097
for (p = ph->h_aliases; *p != 0; p++) {
00098 strcpy(pbuf, *p);
00099 *q++ = pbuf;
00100 pbuf += strlen(*p);
00101 *pbuf++ = 0;
00102 }
00103 *q++ = NULL;
00104
00105 strcpy(pbuf, ph->h_name);
00106 ret->h_name = pbuf;
00107 pbuf += strlen(ph->h_name);
00108 *pbuf++ = 0;
00109
00110 *result = ret;
00111
00112 }
00113 h_errno = hsave;
00114
ast_mutex_unlock(&__mutex);
00115
00116
return (*result == NULL);
00117 }
00118
00119
00120
#endif
00121
00122 struct hostent *
ast_gethostbyname(
const char *host,
struct ast_hostent *hp)
00123 {
00124
int res;
00125
int herrno;
00126
const char *
s;
00127
struct hostent *result = NULL;
00128
00129
00130
00131
00132
s = host;
00133
while(
s && *
s) {
00134
if (!isdigit(*
s))
00135
break;
00136
s++;
00137 }
00138
if (!
s || !*
s)
00139
return NULL;
00140 res = gethostbyname_r(host, &hp->
hp, hp->
buf,
sizeof(hp->
buf), &result, &herrno);
00141
00142
if (res || !result || !hp->
hp.h_addr_list || !hp->
hp.h_addr_list[0])
00143
return NULL;
00144
return &hp->
hp;
00145 }