[asterisk-commits] jpdionne: branch group/v6-new r261614 - in /team/group/v6-new: include/asteri...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu May 6 12:15:22 CDT 2010
Author: jpdionne
Date: Thu May 6 12:15:19 2010
New Revision: 261614
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=261614
Log:
Converted necessary parts for SIP channel conversion of main/{acl,dnsmgr}.c and
include/asterisk/{acl,dnsmgr}.h to use the new version-independent API.
Modified:
team/group/v6-new/include/asterisk/acl.h
team/group/v6-new/include/asterisk/config.h
team/group/v6-new/include/asterisk/dnsmgr.h
team/group/v6-new/include/asterisk/netsock2.h
team/group/v6-new/main/acl.c
team/group/v6-new/main/config.c
team/group/v6-new/main/dnsmgr.c
team/group/v6-new/main/manager.c
team/group/v6-new/main/netsock2.c
Modified: team/group/v6-new/include/asterisk/acl.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/acl.h?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/include/asterisk/acl.h (original)
+++ team/group/v6-new/include/asterisk/acl.h Thu May 6 12:15:19 2010
@@ -29,6 +29,7 @@
#endif
#include "asterisk/network.h"
+#include "asterisk/netsock2.h"
#include "asterisk/io.h"
#define AST_SENSE_DENY 0
@@ -124,12 +125,12 @@
* of getting an entire hostent structure, you instead are given
* only the IP address inserted into a sockaddr_in structure.
*
- * \param[out] sin The IP address is written into sin->sin_addr
+ * \param[out] addr The IP address is written into sin->sin_addr
* \param value The hostname to look up
* \retval 0 Success
* \retval -1 Failure
*/
-int ast_get_ip(struct sockaddr_in *sin, const char *value);
+int ast_get_ip(struct ast_sockaddr *addr, const char *value);
/*!
* \brief Get the IP address given a hostname and optional service
@@ -141,14 +142,14 @@
* an SRV lookup will be done for "_sip._udp.example.com". If service is NULL,
* then this function acts exactly like a call to ast_get_ip.
*
- * \param[out] sin The IP address is written into sin->sin_addr
+ * \param[out] addr The IP address found
* \param value The hostname to look up
* \param service A specific service provided by the host. A NULL service results
* in an A-record lookup instead of an SRV lookup
* \retval 0 Success
* \retval -1 Failure
*/
-int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service);
+int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *value, const char *service);
/*!
* \brief Get our local IP address when contacting a remote host
@@ -164,7 +165,7 @@
* \retval -1 Failure
* \retval 0 Success
*/
-int ast_ouraddrfor(struct in_addr *them, struct in_addr *us);
+int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us);
/*!
* \brief Find an IP address associated with a specific interface
@@ -219,7 +220,7 @@
* \retval 0 Success
* \retval -1 Failure
*/
-int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr);
+int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr);
/*!
* \brief Convert a string to the appropriate COS value
Modified: team/group/v6-new/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/config.h?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/include/asterisk/config.h (original)
+++ team/group/v6-new/include/asterisk/config.h Thu May 6 12:15:19 2010
@@ -485,7 +485,7 @@
/* Returns a struct ast_sockaddr, with optional default value
* (passed by reference) and port handling (accept, ignore,
- * require, forbid). The format is 'host.name[:port]'. IPv6 address
+ * require, forbid). The format is 'ipaddress[:port]'. IPv6 address
* literals need square brackets around them if a port is specified.
*/
PARSE_ADDR = 0x000e,
@@ -512,7 +512,7 @@
PARSE_IN_RANGE = 0x0020, /* accept values inside a range */
PARSE_OUT_RANGE = 0x0040, /* accept values outside a range */
- /* Port handling, for sockaddr_in. accept/ignore/require/forbid
+ /* Port handling, for ast_sockaddr. accept/ignore/require/forbid
* port number after the hostname or address.
*/
PARSE_PORT_MASK = 0x0300, /* 0x000: accept port if present */
Modified: team/group/v6-new/include/asterisk/dnsmgr.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/dnsmgr.h?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/include/asterisk/dnsmgr.h (original)
+++ team/group/v6-new/include/asterisk/dnsmgr.h Thu May 6 12:15:19 2010
@@ -27,7 +27,7 @@
extern "C" {
#endif
-#include "asterisk/network.h"
+#include "asterisk/netsock2.h"
#include "asterisk/srv.h"
/*!
@@ -50,9 +50,8 @@
* is already known.
*
* \return a DNS manager entry
- * \version 1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number
*/
-struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service);
+struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service);
/*!
* \brief Free a DNS manager entry
@@ -78,7 +77,7 @@
* \retval non-zero failure
* \version 1.6.1 result changed from struct in_addr to struct aockaddr_in to store port number
*/
-int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service);
+int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service);
/*!
* \brief Force a refresh of a dnsmgr entry
Modified: team/group/v6-new/include/asterisk/netsock2.h
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/include/asterisk/netsock2.h?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/include/asterisk/netsock2.h (original)
+++ team/group/v6-new/include/asterisk/netsock2.h Thu May 6 12:15:19 2010
@@ -72,8 +72,8 @@
* Compares two socket addresses. Returns -1 if \a a is lexicographically
* smaller than \a b, 0 if they are equal, and 1 otherwise.
*/
-static inline int ast_sockaddr_cmp(struct ast_sockaddr *a,
- struct ast_sockaddr *b)
+static inline int ast_sockaddr_cmp(const struct ast_sockaddr *a,
+ const struct ast_sockaddr *b)
{
if (a->len < b->len)
return -1;
@@ -89,7 +89,49 @@
* This function is thread-safe. The returned string is on static
* thread-specific storage.
*/
-char *ast_sockaddr_stringify(struct ast_sockaddr *addr);
+#define AST_SOCKADDR_STR_DEFAULT 0
+#define AST_SOCKADDR_STR_ADDR 1
+#define AST_SOCKADDR_STR_PORT 2
+
+/**
+ * Convert a socket address to a string with the following formats:
+ *
+ * AST_SOCKADDR_STR_DEFAULT:
+ * a.b.c.d:xyz for IPv4
+ * [a:b:c:...:d]:xyz for IPv6.
+ * AST_SOCKADDR_STR_ADDR: address only
+ * a.b.c.d for IPv4
+ * [a:b:c:...:d] for IPv6.
+ * AST_SOCKADDR_STR_PORT: port only
+ *
+ * This function is thread-safe. The returned string is on static
+ * thread-specific storage.
+ */
+char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format);
+
+/**
+ * Wrapper around ast_sockaddr_stringify_fmt() with default format
+ */
+static inline char *ast_sockaddr_stringify(const struct ast_sockaddr *addr)
+{
+ return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
+}
+
+/**
+ * Wrapper around ast_sockaddr_stringify_fmt() to return an address only
+ */
+static inline char *ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
+{
+ return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
+}
+
+/**
+ * Wrapper around ast_sockaddr_stringify_fmt() to return a port only
+ */
+static inline char *ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
+{
+ return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_PORT);
+}
/**
* Parses a string containing an IPv4 or IPv6 address followed by an optional
@@ -114,6 +156,28 @@
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags);
/**
+ * 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 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.
+ *
+ * \return On success, returns the number of elements in addrs array. 0 on failure.
+ */
+int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags);
+
+/**
* Returns the port number of a socket address, or zero if the address is null.
*
* \warning Do not use this function unless you really know what you are doing.
@@ -154,12 +218,17 @@
/**
* Wrapper around bind(2) that uses struct ast_sockaddr.
*/
-int ast_bind(int sockfd, struct ast_sockaddr *addr);
+int ast_bind(int sockfd, const struct ast_sockaddr *addr);
/**
* Wrapper around connect(2) that uses struct ast_sockaddr.
*/
-int ast_connect(int sockfd, struct ast_sockaddr *addr);
+int ast_connect(int sockfd, const struct ast_sockaddr *addr);
+
+/**
+ * Wrapper around getsockname(2) that uses struct ast_sockaddr.
+ */
+int ast_getsockname(int sockfd, struct ast_sockaddr *addr);
/**
* Wrapper around recvfrom(2) that uses struct ast_sockaddr.
@@ -171,7 +240,7 @@
* Wrapper around sendto(2) that uses ast_sockaddr.
*/
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
- struct ast_sockaddr *dest_addr);
+ const struct ast_sockaddr *dest_addr);
/**
* These are backward compatibility functions that may be used by subsystems
Modified: team/group/v6-new/main/acl.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/acl.c?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/main/acl.c (original)
+++ team/group/v6-new/main/acl.c Thu May 6 12:15:19 2010
@@ -48,7 +48,7 @@
#include "asterisk/srv.h"
#if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
-static int get_local_address(struct in_addr *ourip)
+static int get_local_address(struct ast_sockaddr *ourip)
{
return -1;
}
@@ -112,7 +112,7 @@
}
}
-static int get_local_address(struct in_addr *ourip)
+static int get_local_address(struct ast_sockaddr *ourip)
{
int s, res = -1;
#ifdef SOLARIS
@@ -132,6 +132,8 @@
int best_score = -100;
memset(&best_addr, 0, sizeof(best_addr));
+ ast_log(LOG_WARNING, "IPv4 only function\n");
+
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
rtnerr = getifaddrs(&ifaphead);
if (rtnerr) {
@@ -207,7 +209,9 @@
#endif /* BSD_OR_LINUX */
if (res == 0 && ourip) {
- memcpy(ourip, &best_addr, sizeof(*ourip));
+ ast_sockaddr_setnull(ourip);
+ ourip->ss.ss_family = AF_INET;
+ ((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr;
}
return res;
}
@@ -372,26 +376,33 @@
return res;
}
-int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
-{
- struct hostent *hp;
- struct ast_hostent ahp;
+int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *value, const char *service)
+{
char srv[256];
char host[256];
- int tportno = ntohs(sin->sin_port);
+ struct ast_sockaddr *addrs;
+ int addrs_cnt;
+ int tportno;
+
if (service) {
snprintf(srv, sizeof(srv), "%s.%s", service, value);
if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
- sin->sin_port = htons(tportno);
value = host;
- }
- }
- if ((hp = ast_gethostbyname(value, &ahp))) {
- memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
+ ast_sockaddr_set_port(addr, tportno);
+ }
+ }
+
+ addrs_cnt = ast_sockaddr_resolve(&addrs, value, PARSE_PORT_FORBID);
+ if (addrs_cnt > 0) {
+ if (addrs_cnt > 1)
+ ast_log(LOG_WARNING, "Multiple addresses. Using the first only\n");
+ ast_sockaddr_copy(addr, &addrs[0]);
+ free(addrs);
} else {
ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
return -1;
}
+
return 0;
}
@@ -473,51 +484,45 @@
return "unknown";
}
-int ast_get_ip(struct sockaddr_in *sin, const char *value)
-{
- return ast_get_ip_or_srv(sin, value, NULL);
-}
-
-int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
+int ast_get_ip(struct ast_sockaddr *addr, const char *value)
+{
+ return ast_get_ip_or_srv(addr, value, NULL);
+}
+
+int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
{
int s;
- struct sockaddr_in sin;
- socklen_t slen;
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
ast_log(LOG_ERROR, "Cannot create socket\n");
return -1;
}
- sin.sin_family = AF_INET;
- sin.sin_port = htons(5060);
- sin.sin_addr = *them;
- if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
+
+ if (ast_connect(s, them)) {
ast_log(LOG_WARNING, "Cannot connect\n");
close(s);
return -1;
}
- slen = sizeof(sin);
- if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
+ if (ast_getsockname(s, us)) {
+
ast_log(LOG_WARNING, "Cannot get socket name\n");
close(s);
return -1;
}
close(s);
ast_debug(3, "Found IP address for this socket\n");
- *us = sin.sin_addr;
return 0;
}
-int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
+int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr)
{
char ourhost[MAXHOSTNAMELEN] = "";
- struct ast_hostent ahp;
- struct hostent *hp;
- struct in_addr saddr;
+ struct ast_sockaddr *addrs;
+ int addrs_cnt;
/* just use the bind address if it is nonzero */
- if (ntohl(bindaddr.sin_addr.s_addr)) {
- memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
+ if (!ast_sockaddr_isnull(bindaddr)) {
+ ast_sockaddr_copy(ourip, bindaddr);
ast_debug(3, "Attached to given IP address\n");
return 0;
}
@@ -525,16 +530,27 @@
if (gethostname(ourhost, sizeof(ourhost) - 1)) {
ast_log(LOG_WARNING, "Unable to get hostname\n");
} else {
- if ((hp = ast_gethostbyname(ourhost, &ahp))) {
- memcpy(ourip, hp->h_addr, sizeof(*ourip));
+ addrs_cnt = ast_sockaddr_resolve(&addrs, ourhost, PARSE_PORT_FORBID);
+ if (addrs_cnt > 0) {
+ if (addrs_cnt > 1)
+ ast_log(LOG_WARNING, "Multiple addresses. Using the first only\n");
+ ast_sockaddr_copy(ourip, &addrs[0]);
+ free(addrs);
ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
return 0;
}
}
ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
/* A.ROOT-SERVERS.NET. */
- if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip)) {
- return 0;
+ addrs_cnt = ast_sockaddr_resolve(&addrs, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID);
+ if (addrs_cnt > 0) {
+ if (addrs_cnt > 1)
+ ast_log(LOG_WARNING, "Multiple addresses. Using the first only\n");
+ if(!ast_ouraddrfor(&addrs[0], ourip)) {
+ free(addrs);
+ return 0;
+ }
+ free(addrs);
}
return get_local_address(ourip);
}
Modified: team/group/v6-new/main/config.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/config.c?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/main/config.c (original)
+++ team/group/v6-new/main/config.c Thu May 6 12:15:19 2010
@@ -2392,13 +2392,15 @@
}
case PARSE_ADDR:
{
- struct ast_sockaddr addr;
-
- if (!ast_sockaddr_parse(&addr, arg, flags & PARSE_PORT_MASK))
+ struct ast_sockaddr *addr = (struct ast_sockaddr *)p_result;
+
+ if (!ast_sockaddr_parse(&addr, arg, flags & PARSE_PORT_MASK)) {
error = 1;
+ break;
+ }
ast_debug(3, "extract addr from %s gives %s(%d)\n",
- arg, ast_sockaddr_stringify(&addr), error);
+ arg, ast_sockaddr_stringify(&addr), error);
break;
}
Modified: team/group/v6-new/main/dnsmgr.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/dnsmgr.c?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/main/dnsmgr.c (original)
+++ team/group/v6-new/main/dnsmgr.c Thu May 6 12:15:19 2010
@@ -51,9 +51,9 @@
struct ast_dnsmgr_entry {
/*! where we will store the resulting IP address and port number */
- struct sockaddr_in *result;
+ struct ast_sockaddr *result;
/*! the last result, used to check if address/port has changed */
- struct sockaddr_in last;
+ struct ast_sockaddr last;
/*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
char *service;
/*! Set to 1 if the entry changes */
@@ -85,7 +85,7 @@
.verbose = 0,
};
-struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service)
+struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
{
struct ast_dnsmgr_entry *entry;
int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
@@ -96,7 +96,7 @@
entry->result = result;
ast_mutex_init(&entry->lock);
strcpy(entry->name, name);
- memcpy(&entry->last, result, sizeof(entry->last));
+ ast_sockaddr_copy(&entry->last, result);
if (service) {
entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
strcpy(entry->service, service);
@@ -123,8 +123,10 @@
ast_free(entry);
}
-int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
-{
+int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
+{
+ struct ast_sockaddr addr;
+
if (ast_strlen_zero(name) || !result || !dnsmgr)
return -1;
@@ -133,7 +135,7 @@
/* if it's actually an IP address and not a name,
there's no need for a managed lookup */
- if (inet_aton(name, &result->sin_addr))
+ if (ast_sockaddr_parse(&addr, name, PARSE_PORT_FORBID))
return 0;
ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name);
@@ -155,25 +157,24 @@
*/
static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
{
- char iabuf[INET_ADDRSTRLEN];
- char iabuf2[INET_ADDRSTRLEN];
- struct sockaddr_in tmp;
+ struct ast_sockaddr tmp;
int changed = 0;
-
+
ast_mutex_lock(&entry->lock);
if (verbose)
ast_verb(3, "refreshing '%s'\n", entry->name);
- tmp.sin_port = entry->last.sin_port;
-
- if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, &entry->last)) {
- ast_copy_string(iabuf, ast_inet_ntoa(entry->last.sin_addr), sizeof(iabuf));
- ast_copy_string(iabuf2, ast_inet_ntoa(tmp.sin_addr), sizeof(iabuf2));
- ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s:%d to %s:%d\n",
- entry->name, iabuf, ntohs(entry->last.sin_port), iabuf2, ntohs(tmp.sin_port));
- *entry->result = tmp;
- entry->last = tmp;
- changed = entry->changed = 1;
+ if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
+ ast_sockaddr_set_port(&tmp, ast_sockaddr_port(&entry->last));
+ if(ast_sockaddr_cmp(&tmp, &entry->last)) {
+ ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
+ entry->name, ast_sockaddr_stringify(&entry->last),
+ ast_sockaddr_stringify(&tmp));
+
+ ast_sockaddr_copy(entry->result, &tmp);
+ ast_sockaddr_copy(&entry->last, &tmp);
+ changed = entry->changed = 1;
+ }
}
ast_mutex_unlock(&entry->lock);
Modified: team/group/v6-new/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/manager.c?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/main/manager.c (original)
+++ team/group/v6-new/main/manager.c Thu May 6 12:15:19 2010
@@ -1827,7 +1827,8 @@
static struct sockaddr_in *mansession_encode_sin_local(const struct mansession *s,
struct sockaddr_in *sin_local)
{
- *sin_local = s->tcptls_session->parent->local_address;
+ ast_sockaddr_to_sin(&s->tcptls_session->parent->local_address,
+ sin_local);
return sin_local;
}
@@ -4132,12 +4133,16 @@
static void *session_do(void *data)
{
struct ast_tcptls_session_instance *ser = data;
- struct mansession_session *session = build_mansession(ser->remote_address);
+ struct mansession_session *session;
struct mansession s = {
.tcptls_session = data,
};
int flags;
int res;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ session = build_mansession(ser_remote_address_tmp);
if (session == NULL) {
goto done;
@@ -4160,7 +4165,7 @@
/* these fields duplicate those in the 'ser' structure */
session->fd = s.fd = ser->fd;
session->f = s.f = ser->f;
- session->sin = ser->remote_address;
+ session->sin = ser_remote_address_tmp;
s.session = session;
AST_LIST_HEAD_INIT_NOLOCK(&session->datastores);
@@ -5431,17 +5436,35 @@
static int manager_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return generic_http_callback(ser, method, FORMAT_HTML, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = generic_http_callback(ser, method, FORMAT_HTML, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static int mxml_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return generic_http_callback(ser, method, FORMAT_XML, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = generic_http_callback(ser, method, FORMAT_XML, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static int rawman_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return generic_http_callback(ser, method, FORMAT_RAW, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = generic_http_callback(ser, method, FORMAT_RAW, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static struct ast_http_uri rawmanuri = {
@@ -5472,17 +5495,35 @@
/* Callback with Digest authentication */
static int auth_manager_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return auth_http_callback(ser, method, FORMAT_HTML, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = auth_http_callback(ser, method, FORMAT_HTML, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static int auth_mxml_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return auth_http_callback(ser, method, FORMAT_XML, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = auth_http_callback(ser, method, FORMAT_XML, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static int auth_rawman_http_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
{
- return auth_http_callback(ser, method, FORMAT_RAW, &ser->remote_address, uri, get_params, headers);
+ int retval;
+ struct sockaddr_in ser_remote_address_tmp;
+
+ ast_sockaddr_to_sin(&ser->remote_address, &ser_remote_address_tmp);
+ retval = auth_http_callback(ser, method, FORMAT_RAW, &ser_remote_address_tmp, uri, get_params, headers);
+ ser->remote_address = ast_sockaddr_from_sin(ser_remote_address_tmp);
+ return retval;
}
static struct ast_http_uri arawmanuri = {
@@ -5612,6 +5653,8 @@
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
char a1[256];
char a1_hash[256];
+ struct sockaddr_in ami_desc_local_address_tmp = { 0, };
+ struct sockaddr_in amis_desc_local_address_tmp = { 0, };
manager_enabled = 0;
@@ -5669,10 +5712,10 @@
/* default values */
ast_copy_string(global_realm, S_OR(ast_config_AST_SYSTEM_NAME, DEFAULT_REALM), sizeof(global_realm));
- memset(&ami_desc.local_address, 0, sizeof(struct sockaddr_in));
- memset(&amis_desc.local_address, 0, sizeof(amis_desc.local_address));
- amis_desc.local_address.sin_port = htons(5039);
- ami_desc.local_address.sin_port = htons(DEFAULT_MANAGER_PORT);
+ memset(&ami_desc_local_address_tmp, 0, sizeof(struct sockaddr_in));
+ memset(&amis_desc_local_address_tmp, 0, sizeof(struct sockaddr_in));
+ amis_desc_local_address_tmp.sin_port = htons(5039);
+ ami_desc_local_address_tmp.sin_port = htons(DEFAULT_MANAGER_PORT);
ami_tls_cfg.enabled = 0;
if (ami_tls_cfg.certfile) {
@@ -5704,11 +5747,12 @@
} else if (!strcasecmp(var->name, "webenabled")) {
webmanager_enabled = ast_true(val);
} else if (!strcasecmp(var->name, "port")) {
- ami_desc.local_address.sin_port = htons(atoi(val));
+ ami_desc_local_address_tmp.sin_port = htons(atoi(val));
} else if (!strcasecmp(var->name, "bindaddr")) {
- if (!inet_aton(val, &ami_desc.local_address.sin_addr)) {
+ if (!inet_aton(val, &ami_desc_local_address_tmp.sin_addr)) {
ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
- memset(&ami_desc.local_address.sin_addr, 0, sizeof(ami_desc.local_address.sin_addr));
+ memset(&ami_desc_local_address_tmp.sin_addr, 0,
+ sizeof(ami_desc_local_address_tmp.sin_addr));
}
} else if (!strcasecmp(var->name, "brokeneventsaction")) {
broken_events_action = ast_true(val);
@@ -5746,15 +5790,20 @@
}
if (manager_enabled) {
- ami_desc.local_address.sin_family = AF_INET;
+ ami_desc_local_address_tmp.sin_family = AF_INET;
}
/* if the amis address has not been set, default is the same as non secure ami */
- if (!amis_desc.local_address.sin_addr.s_addr) {
- amis_desc.local_address.sin_addr = ami_desc.local_address.sin_addr;
+ if (!amis_desc_local_address_tmp.sin_addr.s_addr) {
+ amis_desc_local_address_tmp.sin_addr =
+ ami_desc_local_address_tmp.sin_addr;
}
if (ami_tls_cfg.enabled) {
- amis_desc.local_address.sin_family = AF_INET;
- }
+ amis_desc_local_address_tmp.sin_family = AF_INET;
+ }
+ ami_desc.local_address =
+ ast_sockaddr_from_sin(ami_desc_local_address_tmp);
+ amis_desc.local_address =
+ ast_sockaddr_from_sin(amis_desc_local_address_tmp);
AST_RWLIST_WRLOCK(&users);
Modified: team/group/v6-new/main/netsock2.c
URL: http://svnview.digium.com/svn/asterisk/team/group/v6-new/main/netsock2.c?view=diff&rev=261614&r1=261613&r2=261614
==============================================================================
--- team/group/v6-new/main/netsock2.c (original)
+++ team/group/v6-new/main/netsock2.c Thu May 6 12:15:19 2010
@@ -34,10 +34,10 @@
AST_THREADSTORAGE(ast_sockaddr_stringify_buf);
-char *ast_sockaddr_stringify(struct ast_sockaddr *sa)
+char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *sa, int format)
{
char host[NI_MAXHOST];
- char port[NI_MAXSERV];
+ char port[NI_MAXSERV];
char *str;
int e;
static const size_t size = sizeof(host) - 1 + sizeof(port) - 1 + 4;
@@ -45,21 +45,85 @@
if(ast_sockaddr_isnull(sa))
return "(null)";
+ if(!(str = ast_threadstorage_get(&ast_sockaddr_stringify_buf, size)))
+ return "";
+
if ((e = getnameinfo((struct sockaddr *)&sa->ss, sa->len,
- host, sizeof(host), port, sizeof(port),
+ AST_SOCKADDR_STR_PORT ? NULL :host,
+ NI_MAXHOST,
+ AST_SOCKADDR_STR_ADDR ? NULL : port,
+ NI_MAXSERV,
NI_NUMERICHOST | NI_NUMERICSERV))) {
ast_log(LOG_ERROR, "getnameinfo(): %s\n", gai_strerror(e));
return "";
}
- if (!(str = ast_threadstorage_get(&ast_sockaddr_stringify_buf, size)))
+ switch(format) {
+ case 0:
+ snprintf(str, size, sa->ss.ss_family == AF_INET6 ?
+ "[%s]:%s" : "%s:%s", host, port);
+ break;
+ case AST_SOCKADDR_STR_ADDR:
+ strncpy(str, host, size);
+ break;
+ case AST_SOCKADDR_STR_PORT:
+ strncpy(str, port, size);
+ break;
+ default:
+ ast_log(LOG_ERROR, "Invalid format\n");
return "";
-
- snprintf(str, size, sa->ss.ss_family == AF_INET6 ? "[%s]:%s" : "%s:%s",
- host, port);
+ }
return str;
}
+
+int static _ast_sockaddr_parse(char *str, char **host, char **port, int flags)
+{
+ char *s = str;
+
+ *host = NULL;
+ *port = NULL;
+ if (*s == '[') {
+ *host = ++s;
+ for (; *s && *s != ']'; ++s)
+ ;
+ if (*s == ']') {
+ *s++ = '\0';
+ if (*s == ':')
+ *port = s + 1;
+ }
+ } else {
+ *host = s;
+ for (; *s; ++s) {
+ if (*s == ':')
+ *port = *port ? NULL : s;
+ }
+ if (*port)
+ **port++ = '\0';
+ }
+
+ switch (flags & PARSE_PORT_MASK) {
+ case PARSE_PORT_IGNORE:
+ *port = NULL;
+ break;
+ case PARSE_PORT_REQUIRE:
+ if (*port == NULL) {
+ ast_log(LOG_WARNING, "missing port in %s\n", str);
+ return 0;
+ }
+ break;
+ case PARSE_PORT_FORBID:
+ if (*port != NULL) {
+ ast_log(LOG_WARNING, "port disallowed in %s\n", str);
+ return 0;
+ }
+ break;
+ }
+
+ return 1;
+}
+
+
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
{
@@ -71,45 +135,8 @@
int e;
s = ast_strdupa(str);
-
- host = NULL;
- port = NULL;
- if (*s == '[') {
- host = ++s;
- for (; *s && *s != ']'; ++s)
- ;
- if (*s == ']') {
- *s++ = '\0';
- if (*s == ':')
- port = s + 1;
- }
- } else {
- host = s;
- for (; *s; ++s) {
- if (*s == ':')
- port = port ? NULL : s;
- }
- if (port)
- *port++ = '\0';
- }
-
- switch (flags & PARSE_PORT_MASK) {
- case PARSE_PORT_IGNORE:
- port = NULL;
- break;
- case PARSE_PORT_REQUIRE:
- if (port == NULL) {
- ast_log(LOG_WARNING, "missing port in %s\n", str);
- return 0;
- }
- break;
- case PARSE_PORT_FORBID:
- if (port != NULL) {
- ast_log(LOG_WARNING, "port disallowed in %s\n", str);
- return 0;
- }
- break;
- }
+ if(!_ast_sockaddr_parse(s, &host, &port, flags))
+ return 0;
memset(&hints, 0, sizeof(hints));
/* Hint to get only one entry from getaddrinfo */
@@ -136,6 +163,52 @@
return 1;
}
+
+int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags)
+{
+ struct addrinfo hints;
+ struct addrinfo *res;
+ struct addrinfo *ai;
+ struct ast_sockaddr *ast_res;
+ char *s;
+ char *host;
+ char *port;
+ int e;
+ int i;
+ int res_cnt;
+
+ s = ast_strdupa(str);
+ if(!_ast_sockaddr_parse(s, &host, &port, flags))
+ return 0;
+
+ memset(&hints, 0, sizeof(hints));
+ /* Hint to get only AF_INET and AF_INET6 families */
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_flags = 0;
+ /* XXX Portage to IPv6. Temporarly forces IPv4 resolution */
+ hints.ai_family = AF_INET;
+
+ if ((e = getaddrinfo(host, port, &hints, &res))) {
+ ast_log(LOG_ERROR, "getaddrinfo(): %s\n", gai_strerror(e));
+ return 0;
+ }
+
+ for(ai = res, res_cnt = 0; ai; res_cnt++, ai = ai->ai_next);
+
+ ast_res = malloc(res_cnt * sizeof(struct ast_sockaddr));
+
+ for(i = 0, ai = res; ai->ai_next; i++, ai = ai->ai_next) {
+ if(!(ai->ai_family == AF_INET || ai->ai_family == AF_INET6))
+ continue;
+ (*addrs)[i].len = ai->ai_addrlen;
+ memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
+ }
+
+ freeaddrinfo(res);
+
+ return res_cnt;
+}
+
uint16_t ast_sockaddr_port(const struct ast_sockaddr *addr)
{
@@ -180,14 +253,19 @@
return accept(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
}
-int ast_bind(int sockfd, struct ast_sockaddr *addr)
-{
- return bind(sockfd, (struct sockaddr *)&addr->ss, addr->len);
-}
-
-int ast_connect(int sockfd, struct ast_sockaddr *addr)
-{
- return connect(sockfd, (struct sockaddr *)&addr->ss, addr->len);
+int ast_bind(int sockfd, const struct ast_sockaddr *addr)
+{
+ return bind(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
+}
+
+int ast_connect(int sockfd, const struct ast_sockaddr *addr)
+{
+ return connect(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
+}
+
+int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
+{
+ return getsockname(sockfd, (struct sockaddr *)&addr->ss, addr->len);
}
ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
@@ -195,14 +273,14 @@
{
src_addr->len = sizeof(src_addr->ss);
return recvfrom(sockfd, buf, len, flags,
- (struct sockaddr *)&src_addr->ss, &src_addr->len);
+ (const struct sockaddr *)&src_addr->ss, &src_addr->len);
}
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
- struct ast_sockaddr *dest_addr)
+ const struct ast_sockaddr *dest_addr)
{
return sendto(sockfd, buf, len, flags,
- (struct sockaddr *)&dest_addr->ss, dest_addr->len);
+ (const struct sockaddr *)&dest_addr->ss, dest_addr->len);
}
int ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
More information about the asterisk-commits
mailing list