[svn-commits] oej: branch group/mjoj-reorganize-dns-code-trunk r387468 - in /team/group/mjo...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Thu May 2 13:12:51 CDT 2013
Author: oej
Date: Thu May 2 13:12:50 2013
New Revision: 387468
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387468
Log:
Simplifying DNS code in chan_sip, moving functions related to DNS into dns.c from netsock2.c and chan_sip.c
Modified:
team/group/mjoj-reorganize-dns-code-trunk/channels/chan_sip.c
team/group/mjoj-reorganize-dns-code-trunk/include/asterisk/dns.h
team/group/mjoj-reorganize-dns-code-trunk/main/dns.c
team/group/mjoj-reorganize-dns-code-trunk/main/enum.c
team/group/mjoj-reorganize-dns-code-trunk/main/netsock2.c
team/group/mjoj-reorganize-dns-code-trunk/main/srv.c
Modified: team/group/mjoj-reorganize-dns-code-trunk/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/channels/chan_sip.c?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/channels/chan_sip.c (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/channels/chan_sip.c Thu May 2 13:12:50 2013
@@ -295,6 +295,7 @@
#include "sip/include/security_events.h"
#include "asterisk/sip_api.h"
#include "asterisk/app.h"
+#include "asterisk/dns.h"
/*** DOCUMENTATION
<application name="SIPDtmfMode" language="en_US">
@@ -1338,12 +1339,6 @@
static int sip_addheader(struct ast_channel *chan, const char *data);
static int sip_do_reload(enum channelreloadreason reason);
static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
- const char *name, int flag, int family);
-static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
- const char *name, int flag);
-static int ast_sockaddr_resolve_first_transport(struct ast_sockaddr *addr,
- const char *name, int flag, unsigned int transport);
/*--- Debugging
Functions for enabling debug per IP or fully, or enabling history logging for
@@ -4022,7 +4017,7 @@
(!sip_cfg.matchexternaddrlocally || !ast_apply_ha(localaddr, us)) ) {
/* if we used externhost, see if it is time to refresh the info */
if (externexpire && time(NULL) >= externexpire) {
- if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
+ if (ast_sockaddr_resolve_first_af(&externaddr, externhost, 0, get_address_family_filter(SIP_TRANSPORT_UDP))) {
ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
}
externexpire = time(NULL) + externrefresh;
@@ -6296,7 +6291,7 @@
}
}
- if (ast_sockaddr_resolve_first_transport(&dialog->sa, hostn, 0, dialog->socket.type ? dialog->socket.type : SIP_TRANSPORT_UDP)) {
+ if (ast_sockaddr_resolve_first_af(&dialog->sa, hostn, 0, get_address_family_filter(dialog->socket.type ? dialog->socket.type : SIP_TRANSPORT_UDP))) {
ast_log(LOG_WARNING, "No such host: %s\n", peername);
return -1;
}
@@ -8889,7 +8884,7 @@
}
if (via->maddr) {
- if (ast_sockaddr_resolve_first_transport(&p->sa, via->maddr, PARSE_PORT_FORBID, p->socket.type)) {
+ if (ast_sockaddr_resolve_first_af(&p->sa, via->maddr, PARSE_PORT_FORBID, get_address_family_filter(p->socket.type))) {
ast_log(LOG_WARNING, "Can't find address for maddr '%s'\n", via->maddr);
ast_log(LOG_ERROR, "error processing via header\n");
free_via(via);
@@ -10914,7 +10909,7 @@
ast_log(LOG_WARNING, "Unknown protocol '%s'.\n", proto);
return FALSE;
}
- if (ast_sockaddr_resolve_first_af(addr, host, 0, af)) {
+ if (ast_sockaddr_resolve_first_af(addr, host, 0, get_address_family_filter(af))) {
ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
return FALSE;
}
@@ -11634,7 +11629,7 @@
/*! \todo XXX If we have sip_cfg.srvlookup on, then look for NAPTR/SRV,
* otherwise, just look for A records */
- if (ast_sockaddr_resolve_first_transport(&p->sa, hostname, 0, p->socket.type)) {
+ if (ast_sockaddr_resolve_first_af(&p->sa, hostname, 0, get_address_family_filter(p->socket.type))) {
ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
return;
}
@@ -11655,7 +11650,7 @@
/*! \todo XXX If we have sip_cfg.srvlookup on, then look for
* NAPTR/SRV, otherwise, just look for A records */
- if (ast_sockaddr_resolve_first_transport(&p->sa, hostname, PARSE_PORT_FORBID, p->socket.type)) {
+ if (ast_sockaddr_resolve_first_af(&p->sa, hostname, PARSE_PORT_FORBID, get_address_family_filter(p->socket.type))) {
ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
return;
}
@@ -15930,7 +15925,7 @@
return -1;
}
- if (ast_sockaddr_resolve_first_transport(addr, hostport, 0, get_transport_str2enum(transport))) {
+ if (ast_sockaddr_resolve_first_af(addr, hostport, 0, get_address_family_filter(get_transport_str2enum(transport)))) {
ast_log(LOG_WARNING, "Invalid host name in Contact: (can't "
"resolve in DNS) : '%s'\n", hostport);
return -1;
@@ -16076,7 +16071,7 @@
ast_debug(1, "Store REGISTER's Contact header for call routing.\n");
/* XXX This could block for a long time XXX */
/*! \todo Check NAPTR/SRV if we have not got a port in the URI */
- if (ast_sockaddr_resolve_first_transport(&testsa, hostport, 0, peer->socket.type)) {
+ if (ast_sockaddr_resolve_first_af(&testsa, hostport, 0, get_address_family_filter(peer->socket.type))) {
ast_log(LOG_WARNING, "Invalid hostport '%s'\n", hostport);
ast_string_field_set(peer, fullcontact, "");
ast_string_field_set(pvt, our_contact, "");
@@ -18256,11 +18251,11 @@
return;
}
- if (maddr && ast_sockaddr_resolve_first(&p->sa, maddr, 0)) {
+ if (maddr && ast_sockaddr_resolve_first_af(&p->sa, maddr, 0, get_address_family_filter(SIP_TRANSPORT_UDP))) {
p->sa = p->recv;
}
- if (ast_sockaddr_resolve_first(&tmp, c, 0)) {
+ if (ast_sockaddr_resolve_first_af(&tmp, c, 0, get_address_family_filter(SIP_TRANSPORT_UDP))) {
ast_log(LOG_WARNING, "Could not resolve socket address for '%s'\n", c);
port = STANDARD_SIP_PORT;
} else if (!(port = ast_sockaddr_port(&tmp))) {
@@ -21747,7 +21742,7 @@
/*! \brief Enable SIP Debugging for a single IP */
static char *sip_do_debug_ip(int fd, const char *arg)
{
- if (ast_sockaddr_resolve_first_af(&debugaddr, arg, 0, 0)) {
+ if (ast_sockaddr_resolve_first_af(&debugaddr, arg, 0, get_address_family_filter(0))) {
return CLI_SHOWUSAGE;
}
@@ -32095,7 +32090,7 @@
externexpire = 0;
} else if (!strcasecmp(v->name, "externhost")) {
ast_copy_string(externhost, v->value, sizeof(externhost));
- if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
+ if (ast_sockaddr_resolve_first_af(&externaddr, externhost, 0, get_address_family_filter(SIP_TRANSPORT_UDP))) {
ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
}
externexpire = time(NULL);
@@ -33478,50 +33473,6 @@
return 0;
}
return 1;
-}
-
-/*! \brief Return the first entry from ast_sockaddr_resolve filtered by address family
- *
- * \warning Using this function probably means you have a faulty design.
- */
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
- const char* name, int flag, int family)
-{
- struct ast_sockaddr *addrs;
- int addrs_cnt;
-
- addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
- if (addrs_cnt <= 0) {
- return 1;
- }
- if (addrs_cnt > 1) {
- ast_debug(1, "Multiple addresses, using the first one only\n");
- }
-
- ast_sockaddr_copy(addr, &addrs[0]);
-
- ast_free(addrs);
- return 0;
-}
-
-/*! \brief Return the first entry from ast_sockaddr_resolve filtered by family of binddaddr
- *
- * \warning Using this function probably means you have a faulty design.
- */
-static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
- const char* name, int flag)
-{
- return ast_sockaddr_resolve_first_af(addr, name, flag, get_address_family_filter(SIP_TRANSPORT_UDP));
-}
-
-/*! \brief Return the first entry from ast_sockaddr_resolve filtered by family of binddaddr
- *
- * \warning Using this function probably means you have a faulty design.
- */
-static int ast_sockaddr_resolve_first_transport(struct ast_sockaddr *addr,
- const char* name, int flag, unsigned int transport)
-{
- return ast_sockaddr_resolve_first_af(addr, name, flag, get_address_family_filter(transport));
}
/*! \brief
Modified: team/group/mjoj-reorganize-dns-code-trunk/include/asterisk/dns.h
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/include/asterisk/dns.h?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/include/asterisk/dns.h (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/include/asterisk/dns.h Thu May 2 13:12:50 2013
@@ -36,4 +36,44 @@
int ast_search_dns(void *context, const char *dname, int class, int type,
int (*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer));
+/*!
+ * \since 1.8
+ *
+ * \brief
+ * Parses a string with an IPv4 or IPv6 address and place results into an array
+ *
+ * \details
+ * Parses a string containing a host name or an IPv4 or IPv6 address followed
+ * by an optional port (separated by a colon). The result is returned into a
+ * array of struct ast_sockaddr. Allowed formats for str are the following:
+ *
+ * hostname:port
+ * host.example.com:port
+ * a.b.c.d
+ * a.b.c.d:port
+ * a:b:c:...:d
+ * [a:b:c:...:d]
+ * [a:b:c:...:d]:port
+ *
+ * \param[out] addrs The resulting array of ast_sockaddrs
+ * \param str The string to parse
+ * \param flags If set to zero, a port MAY be present. If set to
+ * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
+ * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
+ * port MUST NOT be present.
+ *
+ * \param family Only addresses of the given family will be returned. Use 0 or
+ * AST_AF_UNSPEC to get addresses of all families.
+ *
+ * \retval 0 Failure
+ * \retval non-zero The number of elements in addrs array.
+ */
+int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family);
+
+/*! \brief Return the first entry from ast_sockaddr_resolve filtered by address family
+ *
+ * \warning Using this function probably means you have a faulty design.
+ */
+int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char *name, int flag, int family);
+
#endif /* _ASTERISK_DNS_H */
Modified: team/group/mjoj-reorganize-dns-code-trunk/main/dns.c
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/main/dns.c?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/main/dns.c (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/main/dns.c Thu May 2 13:12:50 2013
@@ -42,9 +42,9 @@
#include <resolv.h>
#include "asterisk/channel.h"
+#include "asterisk/netsock2.h"
#include "asterisk/dns.h"
#include "asterisk/endian.h"
-
#define MAX_SIZE 4096
#ifdef __PDP_ENDIAN
@@ -300,3 +300,77 @@
return ret;
}
+
+int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
+{
+ struct addrinfo hints, *res, *ai;
+ char *s, *host, *port;
+ int e, i, res_cnt;
+
+ if (!str) {
+ return 0;
+ }
+
+ s = ast_strdupa(str);
+ if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
+ return 0;
+ }
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ if ((e = getaddrinfo(host, port, &hints, &res))) {
+ ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
+ host, S_OR(port, "(null)"), gai_strerror(e));
+ return 0;
+ }
+
+ res_cnt = 0;
+ for (ai = res; ai; ai = ai->ai_next) {
+ res_cnt++;
+ }
+
+ if (res_cnt == 0) {
+ goto cleanup;
+ }
+
+ if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
+ res_cnt = 0;
+ goto cleanup;
+ }
+
+ i = 0;
+ for (ai = res; ai; ai = ai->ai_next) {
+ (*addrs)[i].len = ai->ai_addrlen;
+ memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
+ ++i;
+ }
+
+cleanup:
+ freeaddrinfo(res);
+ return res_cnt;
+}
+
+/*! \brief Return the first entry from ast_sockaddr_resolve filtered by address family
+ *
+ * \warning Using this function probably means you have a faulty design.
+ */
+int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char* name, int flag, int family)
+{
+ struct ast_sockaddr *addrs;
+ int addrs_cnt;
+
+ addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
+ if (addrs_cnt <= 0) {
+ return 1;
+ }
+ if (addrs_cnt > 1) {
+ ast_debug(1, "Multiple addresses, using the first one only\n");
+ }
+
+ ast_sockaddr_copy(addr, &addrs[0]);
+
+ ast_free(addrs);
+ return 0;
+}
Modified: team/group/mjoj-reorganize-dns-code-trunk/main/enum.c
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/main/enum.c?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/main/enum.c (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/main/enum.c Thu May 2 13:12:50 2013
@@ -74,6 +74,7 @@
#include <ctype.h>
#include <regex.h>
+#include "asterisk/netsock2.h"
#include "asterisk/enum.h"
#include "asterisk/dns.h"
#include "asterisk/channel.h"
Modified: team/group/mjoj-reorganize-dns-code-trunk/main/netsock2.c
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/main/netsock2.c?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/main/netsock2.c (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/main/netsock2.c Thu May 2 13:12:50 2013
@@ -245,58 +245,6 @@
return 1;
}
-int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
- int flags, int family)
-{
- struct addrinfo hints, *res, *ai;
- char *s, *host, *port;
- int e, i, res_cnt;
-
- if (!str) {
- return 0;
- }
-
- s = ast_strdupa(str);
- if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
- return 0;
- }
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = family;
- hints.ai_socktype = SOCK_DGRAM;
-
- if ((e = getaddrinfo(host, port, &hints, &res))) {
- ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
- host, S_OR(port, "(null)"), gai_strerror(e));
- return 0;
- }
-
- res_cnt = 0;
- for (ai = res; ai; ai = ai->ai_next) {
- res_cnt++;
- }
-
- if (res_cnt == 0) {
- goto cleanup;
- }
-
- if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
- res_cnt = 0;
- goto cleanup;
- }
-
- i = 0;
- for (ai = res; ai; ai = ai->ai_next) {
- (*addrs)[i].len = ai->ai_addrlen;
- memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
- ++i;
- }
-
-cleanup:
- freeaddrinfo(res);
- return res_cnt;
-}
-
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
{
const struct ast_sockaddr *a_tmp, *b_tmp;
Modified: team/group/mjoj-reorganize-dns-code-trunk/main/srv.c
URL: http://svnview.digium.com/svn/asterisk/team/group/mjoj-reorganize-dns-code-trunk/main/srv.c?view=diff&rev=387468&r1=387467&r2=387468
==============================================================================
--- team/group/mjoj-reorganize-dns-code-trunk/main/srv.c (original)
+++ team/group/mjoj-reorganize-dns-code-trunk/main/srv.c Thu May 2 13:12:50 2013
@@ -46,6 +46,7 @@
#endif
#include <resolv.h>
+#include "asterisk/netsock2.h"
#include "asterisk/channel.h"
#include "asterisk/srv.h"
#include "asterisk/dns.h"
More information about the svn-commits
mailing list