[asterisk-commits] simon.perreault: branch group/v6 r83501 - in /team/group/v6/trunk: channels/ ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Sep 21 15:05:00 CDT 2007
Author: simon.perreault
Date: Fri Sep 21 15:05:00 2007
New Revision: 83501
URL: http://svn.digium.com/view/asterisk?view=rev&rev=83501
Log:
Bind to all addresses returned by getaddrinfo().
Modified:
team/group/v6/trunk/channels/chan_sip.c
team/group/v6/trunk/include/asterisk/netsock.h
team/group/v6/trunk/main/netsock.c
Modified: team/group/v6/trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/group/v6/trunk/channels/chan_sip.c?view=diff&rev=83501&r1=83500&r2=83501
==============================================================================
--- team/group/v6/trunk/channels/chan_sip.c (original)
+++ team/group/v6/trunk/channels/chan_sip.c Fri Sep 21 15:05:00 2007
@@ -18368,23 +18368,15 @@
externexpire = time(NULL);
} else if (!strcasecmp(v->name, "bindaddr")) {
char *end;
- struct ast_vinetsock* ns = NULL;
char iabuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
if (ast_vinetsock_str2hp(iabuf, sizeof(iabuf), sbuf, sizeof(sbuf), v->value, 0, (void*)&end) || end[0] != '\0') {
ast_log(LOG_WARNING, "Invalid bindaddr entry '%s' at line %d (1)\n",
v->value, v->lineno);
- } else if (!(ns = ast_vinetsock_new(iabuf, sbuf))) {
+ } else if (ast_vinetsock_list_getaddrinfo(netsocks, iabuf, sbuf) <= 0) {
ast_log(LOG_WARNING, "Invalid bindaddr entry '%s' at line %d (2)\n",
v->value, v->lineno);
- } else {
- char hostport[NI_MAXHOST];
-
- ast_verbose(VERBOSE_PREFIX_3 "Parsed host '%s' and port '%s'\n", iabuf, sbuf);
- ast_vinetsock_get_hostport(ns, hostport, sizeof(hostport));
- ast_verbose(VERBOSE_PREFIX_3 "Initialized netsock with '%s'\n", hostport);
- ast_vinetsock_list_add(netsocks, ns);
}
} else if (!strcasecmp(v->name, "localnet")) {
struct ast_ha *na;
@@ -18597,22 +18589,15 @@
ast_vinetsock_list_set_defport(netsocks, defport);
if (ast_vinetsock_list_isempty(netsocks)) {
- struct ast_vinetsock* ns;
char sbuf[NI_MAXSERV];
snprintf(sbuf, sizeof(sbuf), "%d", defport);
if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_3 "No bindaddr specified in sip.conf. Binding to default addresses (0.0.0.0 and ::)\n");
- if ((ns = ast_vinetsock_new("::", sbuf))) {
- ast_vinetsock_list_add(netsocks, ns);
- } else {
+ if (ast_vinetsock_list_getaddrinfo(netsocks, "::", sbuf) <= 0)
ast_log(LOG_WARNING, "Unable to bind to ::\n");
- }
- if ((ns = ast_vinetsock_new("0.0.0.0", sbuf))) {
- ast_vinetsock_list_add(netsocks, ns);
- } else {
+ if (ast_vinetsock_list_getaddrinfo(netsocks, "0.0.0.0", sbuf) <= 0)
ast_log(LOG_WARNING, "Unable to bind to 0.0.0.0\n");
- }
}
ast_mutex_lock(&netlock);
ast_vinetsock_list_bind(netsocks, io, global_tos_sip, sipsock_read, NULL, 1, "SIP");
Modified: team/group/v6/trunk/include/asterisk/netsock.h
URL: http://svn.digium.com/view/asterisk/team/group/v6/trunk/include/asterisk/netsock.h?view=diff&rev=83501&r1=83500&r2=83501
==============================================================================
--- team/group/v6/trunk/include/asterisk/netsock.h (original)
+++ team/group/v6/trunk/include/asterisk/netsock.h Fri Sep 21 15:05:00 2007
@@ -75,6 +75,8 @@
void ast_vinetsock_unref(struct ast_vinetsock *ns);
/* vinetsock socket functions */
+int ast_vinetsock_list_getaddrinfo(struct ast_vinetsock_list *list,
+ const char *iabuf, const char *sbuf);
void ast_vinetsock_list_bind(struct ast_vinetsock_list *list,
struct io_context *ioc,
int tos, ast_io_cb callback, void *data,
@@ -89,7 +91,6 @@
struct ast_vinetsock *ast_vinetsock_bind_portstr(struct ast_vinetsock_list *list, struct io_context *ioc,
const char *bindinfo, char *port, int tos,
ast_io_cb callback, void *data);
-struct ast_vinetsock *ast_vinetsock_new(const char* iabuf, const char *sbuf);
int ast_vinetsock_bind(struct ast_vinetsock *ns, struct io_context *ioc,
int tos, ast_io_cb callback, void *data, int reuseaddr,
const char* module_name);
Modified: team/group/v6/trunk/main/netsock.c
URL: http://svn.digium.com/view/asterisk/team/group/v6/trunk/main/netsock.c?view=diff&rev=83501&r1=83500&r2=83501
==============================================================================
--- team/group/v6/trunk/main/netsock.c (original)
+++ team/group/v6/trunk/main/netsock.c Fri Sep 21 15:05:00 2007
@@ -71,26 +71,6 @@
#include "asterisk/srv.h"
#include "asterisk/strings.h"
-/**
- * \brief Wrapper around getaddrinfo().
- *
- * This function wraps around platform-specific deficiencies.
- */
-static int ast_vinetsock_getaddrinfo( const char *node, const char *service,
- const struct addrinfo *hints, struct addrinfo **res )
-{
- /*
- * In glibc (tested version 2.6) there is a bug with handling of empty
- * strings. Duplicate correct logic here.
- */
- if ( node && (node[0] == 0 || (node[0] == '*' && node[1] == 0)) )
- node = NULL;
- if ( service && (service[0] == 0 || (service[0] == '*' && service[1] == 0)) )
- service = NULL;
-
- return getaddrinfo( node, service, hints, res );
-}
-
/*! \brief Destroy and remove from memory a vinetsock
* \param vinetsock pointer to the vinetsock object we are destroying
* \return void
@@ -111,7 +91,6 @@
/*! \brief Destroy, remove from memory and unref in the list a vinetsock
* \param ns pointer to the vinetsock object we are freeing
* \return void
- * \note should be used in conjonction with ast_vinetsock_new
*/
void ast_vinetsock_free(struct ast_vinetsock* ns)
{
@@ -192,6 +171,59 @@
ASTOBJ_UNLOCK(iterator);
}
});
+}
+
+/*! \brief Resolve hostname and append results to list.
+ * \param list Socket list to which the results will be appended
+ * \param iabuf Address string
+ * \param sbuf Port number string
+ * \return Number of items appended to list.
+ */
+int ast_vinetsock_list_getaddrinfo(struct ast_vinetsock_list *list,
+ const char *iabuf, const char *sbuf)
+{
+ struct addrinfo hints;
+ struct addrinfo *res_addrinfo = NULL;
+ int res = 0;
+ struct ast_vinetsock *ns = 0;
+ struct addrinfo *it1;
+ struct ast_vinetsock **it2;
+ int n = 0;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_flags = AI_PASSIVE;
+
+ if ((res = getaddrinfo(iabuf, sbuf, &hints, &res_addrinfo))) {
+ ast_log(LOG_ERROR, "getaddrinfo with address %s and port %s : %s \n", iabuf, sbuf, gai_strerror(res));
+ return n;
+ }
+
+ ASTOBJ_CONTAINER_WRLOCK(list);
+ for (it2 = &list->head; *it2; it2 = &(*it2)->next[0])
+ ;
+ for (it1 = res_addrinfo; it1; it1 = it1->ai_next) {
+ if (!(ns = ast_calloc(1, sizeof(struct ast_vinetsock)))) {
+ goto cleanup;
+ }
+ ASTOBJ_INIT(ns);
+ memcpy(&ns->bindaddr, it1->ai_addr, it1->ai_addrlen);
+ ns->bindaddrlen = it1->ai_addrlen;
+ ns->sock_protocol = it1->ai_protocol;
+ ns->sock_type = it1->ai_socktype;
+ ns->sock_family = it1->ai_family;
+ *it2 = ns;
+ it2 = &ns->next[0];
+ ++n;
+ }
+
+cleanup:
+ *it2 = NULL;
+ ASTOBJ_CONTAINER_UNLOCK(list);
+ freeaddrinfo(res_addrinfo);
+
+ return n;
}
/*! \brief Iterate over the ast_vinetsock_list and bind to each ast_vinetsock
@@ -488,7 +520,7 @@
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
- if ((res = ast_vinetsock_getaddrinfo(bindinfo, port, &hints, &res_addrinfo))) {
+ if ((res = getaddrinfo(bindinfo, port, &hints, &res_addrinfo))) {
ast_log(LOG_ERROR, "getaddrinfo with address %s and port %s : %s \n", bindinfo, port, gai_strerror(res));
return NULL;
}
@@ -497,43 +529,6 @@
freeaddrinfo(res_addrinfo);
return res_vinetsock;
-}
-
-/*! \brief Create and initialize a vinetsock struct from a host and port string
- * \param iabuf Address string
- * \param sbuf Port number string
- * \return Pointer to an allocated ast_vinetsock struct
- */
-struct ast_vinetsock *ast_vinetsock_new(const char* iabuf, const char *sbuf)
-{
- struct addrinfo hints;
- struct addrinfo *res_addrinfo = NULL;
- int res = 0;
- struct ast_vinetsock *ns = 0;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_flags = AI_PASSIVE;
-
- if ((res = ast_vinetsock_getaddrinfo(iabuf, sbuf, &hints, &res_addrinfo))) {
- ast_log(LOG_ERROR, "getaddrinfo with address %s and port %s : %s \n", iabuf, sbuf, gai_strerror(res));
- return NULL;
- }
-
- if (!(ns = ast_calloc(1, sizeof(struct ast_vinetsock)))) {
- freeaddrinfo(res_addrinfo);
- return NULL;
- }
- memcpy(&ns->bindaddr, res_addrinfo->ai_addr, res_addrinfo->ai_addrlen);
- ns->bindaddrlen = res_addrinfo->ai_addrlen;
- ns->sock_protocol = res_addrinfo->ai_protocol;
- ns->sock_type = res_addrinfo->ai_socktype;
- ns->sock_family = res_addrinfo->ai_family;
- freeaddrinfo(res_addrinfo);
- ASTOBJ_INIT(ns);
-
- return ns;
}
/*! \brief Bind a vinetsock and register its callback
@@ -781,7 +776,7 @@
else
hints.ai_family = af;
hints.ai_socktype = SOCK_DGRAM;
- if(ast_vinetsock_getaddrinfo(addr, port, &hints, &res))
+ if(getaddrinfo(addr, port, &hints, &res))
return -1;
memcpy(sa, res->ai_addr, res->ai_addrlen);
@@ -990,7 +985,7 @@
if (p1 - string >= sbuflen) {
/* Length exceeded */
if (option_verbose > 1)
- ast_log(LOG_WARNING, "string '%d' >= sbuflen '%d' in %s\n", p1-string, (int)sbuflen, hostport);
+ ast_log(LOG_WARNING, "string '%d' >= sbuflen '%d' in %s\n", (int)(p1-string), (int)sbuflen, hostport);
return -1;
}
ast_copy_string(sbuf, string, p1 - string + 1);
@@ -1008,7 +1003,7 @@
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_socktype = SOCK_STREAM;
- if ((error = ast_vinetsock_getaddrinfo(hbuf, "80", &hints, &res))) {
+ if ((error = getaddrinfo(hbuf, "80", &hints, &res))) {
/* Invalid numeric address */
if (option_verbose > 1)
ast_log(LOG_WARNING, "getaddrinfo on %s: %s\n", hostport, gai_strerror(error));
@@ -1043,7 +1038,7 @@
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = stype;
hints.ai_family = AF_UNSPEC;
- error = ast_vinetsock_getaddrinfo(host, port, &hints, &res0);
+ error = getaddrinfo(host, port, &hints, &res0);
if (error) {
ast_log(LOG_WARNING, "No such host %s, port %s: %s\n",
host, port, gai_strerror(error));
@@ -1113,7 +1108,7 @@
}
*srclen = sizeof(struct sockaddr_storage);
if (getsockname(sockfd, src, srclen)) {
- getnameinfo(src, (int)srclen, iabuf, sizeof(iabuf),
+ getnameinfo(src, srclen, iabuf, sizeof(iabuf),
sbuf, sizeof(sbuf), NI_NUMERICHOST|NI_NUMERICSERV);
ast_log(LOG_WARNING, "Cannot get socket name %s port %s: %s\n", iabuf, sbuf, strerror(errno));
close(sockfd);
@@ -1147,7 +1142,7 @@
hints.ai_flags = AI_PASSIVE;
hints.ai_family = dst->sa_family;
hints.ai_socktype = SOCK_DGRAM;
- if (ast_vinetsock_getaddrinfo(NULL, port, &hints, &res)) {
+ if (getaddrinfo(NULL, port, &hints, &res)) {
return -1;
}
More information about the asterisk-commits
mailing list