Add new file utils.c, Move ast_gethostbyname to utils.c
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@2931 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
parent
9bd61aa747
commit
1c3ad80905
3
Makefile
3
Makefile
|
@ -173,7 +173,8 @@ OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
|
||||||
ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
|
ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
|
||||||
cdr.o tdd.o acl.o rtp.o manager.o asterisk.o ast_expr.o \
|
cdr.o tdd.o acl.o rtp.o manager.o asterisk.o ast_expr.o \
|
||||||
dsp.o chanvars.o indications.o autoservice.o db.o privacy.o \
|
dsp.o chanvars.o indications.o autoservice.o db.o privacy.o \
|
||||||
astmm.o enum.o srv.o dns.o aescrypt.o aestab.o aeskey.o
|
astmm.o enum.o srv.o dns.o aescrypt.o aestab.o aeskey.o \
|
||||||
|
utils.o
|
||||||
ifeq (${OSARCH},Darwin)
|
ifeq (${OSARCH},Darwin)
|
||||||
OBJS+=poll.o dlfcn.o
|
OBJS+=poll.o dlfcn.o
|
||||||
ASTLINK=-Wl,-dynamic
|
ASTLINK=-Wl,-dynamic
|
||||||
|
|
1
acl.c
1
acl.c
|
@ -22,6 +22,7 @@
|
||||||
#include <asterisk/acl.h>
|
#include <asterisk/acl.h>
|
||||||
#include <asterisk/logger.h>
|
#include <asterisk/logger.h>
|
||||||
#include <asterisk/channel.h>
|
#include <asterisk/channel.h>
|
||||||
|
#include <asterisk/utils.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
|
132
asterisk.c
132
asterisk.c
|
@ -1723,135 +1723,3 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if defined(__FreeBSD__)
|
|
||||||
|
|
||||||
/* duh? ERANGE value copied from web... */
|
|
||||||
#define ERANGE 34
|
|
||||||
#undef gethostbyname
|
|
||||||
|
|
||||||
int gethostbyname_r (const char *name,
|
|
||||||
struct hostent *ret,
|
|
||||||
char *buf,
|
|
||||||
size_t buflen,
|
|
||||||
struct hostent **result,
|
|
||||||
int *h_errnop);
|
|
||||||
|
|
||||||
int gethostbyname_r (const char *name,
|
|
||||||
struct hostent *ret,
|
|
||||||
char *buf,
|
|
||||||
size_t buflen,
|
|
||||||
struct hostent **result,
|
|
||||||
int *h_errnop) {
|
|
||||||
|
|
||||||
int hsave;
|
|
||||||
struct hostent *ph;
|
|
||||||
static ast_mutex_t __mutex = AST_MUTEX_INITIALIZER;
|
|
||||||
ast_mutex_lock(&__mutex); /* begin critical area */
|
|
||||||
hsave = h_errno;
|
|
||||||
|
|
||||||
ph = gethostbyname(name);
|
|
||||||
*h_errnop = h_errno; /* copy h_errno to *h_herrnop */
|
|
||||||
if (ph == NULL) {
|
|
||||||
*result = NULL;
|
|
||||||
} else {
|
|
||||||
char **p, **q;
|
|
||||||
char *pbuf;
|
|
||||||
int nbytes=0;
|
|
||||||
int naddr=0, naliases=0;
|
|
||||||
/* determine if we have enough space in buf */
|
|
||||||
|
|
||||||
/* count how many addresses */
|
|
||||||
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
||||||
nbytes += ph->h_length; /* addresses */
|
|
||||||
nbytes += sizeof(*p); /* pointers */
|
|
||||||
naddr++;
|
|
||||||
}
|
|
||||||
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
||||||
|
|
||||||
/* count how many aliases, and total length of strings */
|
|
||||||
|
|
||||||
for (p = ph->h_aliases; *p != 0; p++) {
|
|
||||||
nbytes += (strlen(*p)+1); /* aliases */
|
|
||||||
nbytes += sizeof(*p); /* pointers */
|
|
||||||
naliases++;
|
|
||||||
}
|
|
||||||
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
|
||||||
|
|
||||||
/* here nbytes is the number of bytes required in buffer */
|
|
||||||
/* as a terminator must be there, the minimum value is ph->h_length */
|
|
||||||
if(nbytes > buflen) {
|
|
||||||
*result = NULL;
|
|
||||||
pthread_mutex_unlock(&__mutex); /* end critical area */
|
|
||||||
return ERANGE; /* not enough space in buf!! */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* There is enough space. Now we need to do a deep copy! */
|
|
||||||
/* Allocation in buffer:
|
|
||||||
from [0] to [(naddr-1) * sizeof(*p)]:
|
|
||||||
pointers to addresses
|
|
||||||
at [naddr * sizeof(*p)]:
|
|
||||||
NULL
|
|
||||||
from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
|
|
||||||
pointers to aliases
|
|
||||||
at [(naddr+naliases+1) * sizeof(*p)]:
|
|
||||||
NULL
|
|
||||||
then naddr addresses (fixed length), and naliases aliases (asciiz).
|
|
||||||
*/
|
|
||||||
|
|
||||||
*ret = *ph; /* copy whole structure (not its address!) */
|
|
||||||
|
|
||||||
/* copy addresses */
|
|
||||||
q = (char **)buf; /* pointer to pointers area (type: char **) */
|
|
||||||
ret->h_addr_list = q; /* update pointer to address list */
|
|
||||||
pbuf = buf + ((naddr+naliases+2)*sizeof(*p)); /* skip that area */
|
|
||||||
for (p = ph->h_addr_list; *p != 0; p++) {
|
|
||||||
memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
|
|
||||||
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
||||||
pbuf += ph->h_length; /* advance pbuf */
|
|
||||||
}
|
|
||||||
*q++ = NULL; /* address list terminator */
|
|
||||||
|
|
||||||
/* copy aliases */
|
|
||||||
|
|
||||||
ret->h_aliases = q; /* update pointer to aliases list */
|
|
||||||
for (p = ph->h_aliases; *p != 0; p++) {
|
|
||||||
strcpy(pbuf, *p); /* copy alias strings */
|
|
||||||
*q++ = pbuf; /* the pointer is the one inside buf... */
|
|
||||||
pbuf += strlen(*p); /* advance pbuf */
|
|
||||||
*pbuf++ = 0; /* string terminator */
|
|
||||||
}
|
|
||||||
*q++ = NULL; /* terminator */
|
|
||||||
|
|
||||||
strcpy(pbuf, ph->h_name); /* copy alias strings */
|
|
||||||
ret->h_name = pbuf;
|
|
||||||
pbuf += strlen(ph->h_name); /* advance pbuf */
|
|
||||||
*pbuf++ = 0; /* string terminator */
|
|
||||||
|
|
||||||
*result = ret; /* and let *result point to structure */
|
|
||||||
|
|
||||||
}
|
|
||||||
h_errno = hsave; /* restore h_errno */
|
|
||||||
|
|
||||||
ast_mutex_unlock(&__mutex); /* end critical area */
|
|
||||||
|
|
||||||
return (*result != NULL);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
int herrno;
|
|
||||||
struct hostent *result = NULL;
|
|
||||||
|
|
||||||
res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
|
|
||||||
|
|
||||||
if (res || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
|
||||||
return NULL;
|
|
||||||
return &hp->hp;
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <asterisk/crypto.h>
|
#include <asterisk/crypto.h>
|
||||||
#include <asterisk/acl.h>
|
#include <asterisk/acl.h>
|
||||||
#include <asterisk/manager.h>
|
#include <asterisk/manager.h>
|
||||||
|
#include <asterisk/utils.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
|
@ -72,6 +72,7 @@
|
||||||
#include <asterisk/parking.h>
|
#include <asterisk/parking.h>
|
||||||
#include <asterisk/app.h>
|
#include <asterisk/app.h>
|
||||||
#include <asterisk/musiconhold.h>
|
#include <asterisk/musiconhold.h>
|
||||||
|
#include <asterisk/utils.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
|
|
@ -179,11 +179,5 @@ static inline int ast_mutex_init(ast_mutex_t *t)
|
||||||
#endif /* DEBUG_THREADS */
|
#endif /* DEBUG_THREADS */
|
||||||
|
|
||||||
#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
|
#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__
|
||||||
struct ast_hostent {
|
|
||||||
struct hostent hp;
|
|
||||||
char buf[1024];
|
|
||||||
};
|
|
||||||
|
|
||||||
extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,9 +12,18 @@
|
||||||
#ifndef _ASTERISK_UTIL_H
|
#ifndef _ASTERISK_UTIL_H
|
||||||
#define _ASTERISK_UTIL_H
|
#define _ASTERISK_UTIL_H
|
||||||
|
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
static inline int ast_strlen_zero(const char *s)
|
static inline int ast_strlen_zero(const char *s)
|
||||||
{
|
{
|
||||||
return (*s == '\0');
|
return (*s == '\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ast_hostent {
|
||||||
|
struct hostent hp;
|
||||||
|
char buf[1024];
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
/*
|
||||||
|
* Asterisk -- A telephony toolkit for Linux.
|
||||||
|
*
|
||||||
|
* Utility functions
|
||||||
|
*
|
||||||
|
* Copyright (C) 2004, Digium
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <asterisk/lock.h>
|
||||||
|
#include <asterisk/utils.h>
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__)
|
||||||
|
|
||||||
|
/* duh? ERANGE value copied from web... */
|
||||||
|
#define ERANGE 34
|
||||||
|
#undef gethostbyname
|
||||||
|
|
||||||
|
int gethostbyname_r (const char *name, struct hostent *ret, char *buf,
|
||||||
|
size_t buflen, struct hostent **result,
|
||||||
|
int *h_errnop)
|
||||||
|
{
|
||||||
|
int hsave;
|
||||||
|
struct hostent *ph;
|
||||||
|
static ast_mutex_t __mutex = AST_MUTEX_INITIALIZER;
|
||||||
|
ast_mutex_lock(&__mutex); /* begin critical area */
|
||||||
|
hsave = h_errno;
|
||||||
|
|
||||||
|
ph = gethostbyname(name);
|
||||||
|
*h_errnop = h_errno; /* copy h_errno to *h_herrnop */
|
||||||
|
if (ph == NULL) {
|
||||||
|
*result = NULL;
|
||||||
|
} else {
|
||||||
|
char **p, **q;
|
||||||
|
char *pbuf;
|
||||||
|
int nbytes=0;
|
||||||
|
int naddr=0, naliases=0;
|
||||||
|
/* determine if we have enough space in buf */
|
||||||
|
|
||||||
|
/* count how many addresses */
|
||||||
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
||||||
|
nbytes += ph->h_length; /* addresses */
|
||||||
|
nbytes += sizeof(*p); /* pointers */
|
||||||
|
naddr++;
|
||||||
|
}
|
||||||
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
||||||
|
|
||||||
|
/* count how many aliases, and total length of strings */
|
||||||
|
for (p = ph->h_aliases; *p != 0; p++) {
|
||||||
|
nbytes += (strlen(*p)+1); /* aliases */
|
||||||
|
nbytes += sizeof(*p); /* pointers */
|
||||||
|
naliases++;
|
||||||
|
}
|
||||||
|
nbytes += sizeof(*p); /* one more for the terminating NULL */
|
||||||
|
|
||||||
|
/* here nbytes is the number of bytes required in buffer */
|
||||||
|
/* as a terminator must be there, the minimum value is ph->h_length */
|
||||||
|
if(nbytes > buflen) {
|
||||||
|
*result = NULL;
|
||||||
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
||||||
|
return ERANGE; /* not enough space in buf!! */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* There is enough space. Now we need to do a deep copy! */
|
||||||
|
/* Allocation in buffer:
|
||||||
|
from [0] to [(naddr-1) * sizeof(*p)]:
|
||||||
|
pointers to addresses
|
||||||
|
at [naddr * sizeof(*p)]:
|
||||||
|
NULL
|
||||||
|
from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] :
|
||||||
|
pointers to aliases
|
||||||
|
at [(naddr+naliases+1) * sizeof(*p)]:
|
||||||
|
NULL
|
||||||
|
then naddr addresses (fixed length), and naliases aliases (asciiz).
|
||||||
|
*/
|
||||||
|
|
||||||
|
*ret = *ph; /* copy whole structure (not its address!) */
|
||||||
|
|
||||||
|
/* copy addresses */
|
||||||
|
q = (char **)buf; /* pointer to pointers area (type: char **) */
|
||||||
|
ret->h_addr_list = q; /* update pointer to address list */
|
||||||
|
pbuf = buf + ((naddr+naliases+2)*sizeof(*p)); /* skip that area */
|
||||||
|
for (p = ph->h_addr_list; *p != 0; p++) {
|
||||||
|
memcpy(pbuf, *p, ph->h_length); /* copy address bytes */
|
||||||
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
||||||
|
pbuf += ph->h_length; /* advance pbuf */
|
||||||
|
}
|
||||||
|
*q++ = NULL; /* address list terminator */
|
||||||
|
|
||||||
|
/* copy aliases */
|
||||||
|
ret->h_aliases = q; /* update pointer to aliases list */
|
||||||
|
for (p = ph->h_aliases; *p != 0; p++) {
|
||||||
|
strcpy(pbuf, *p); /* copy alias strings */
|
||||||
|
*q++ = pbuf; /* the pointer is the one inside buf... */
|
||||||
|
pbuf += strlen(*p); /* advance pbuf */
|
||||||
|
*pbuf++ = 0; /* string terminator */
|
||||||
|
}
|
||||||
|
*q++ = NULL; /* terminator */
|
||||||
|
|
||||||
|
strcpy(pbuf, ph->h_name); /* copy alias strings */
|
||||||
|
ret->h_name = pbuf;
|
||||||
|
pbuf += strlen(ph->h_name); /* advance pbuf */
|
||||||
|
*pbuf++ = 0; /* string terminator */
|
||||||
|
|
||||||
|
*result = ret; /* and let *result point to structure */
|
||||||
|
|
||||||
|
}
|
||||||
|
h_errno = hsave; /* restore h_errno */
|
||||||
|
ast_mutex_unlock(&__mutex); /* end critical area */
|
||||||
|
|
||||||
|
return (*result != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
|
||||||
|
{
|
||||||
|
int res;
|
||||||
|
int herrno;
|
||||||
|
struct hostent *result = NULL;
|
||||||
|
|
||||||
|
res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
|
||||||
|
|
||||||
|
if (res || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
|
||||||
|
return NULL;
|
||||||
|
return &hp->hp;
|
||||||
|
}
|
Reference in New Issue