[Asterisk-cvs] asterisk/channels chan_iax2.c,1.317,1.318
kpfleming at lists.digium.com
kpfleming at lists.digium.com
Mon Jul 11 22:11:27 CDT 2005
Update of /usr/cvsroot/asterisk/channels
In directory mongoose.digium.com:/tmp/cvs-serv12962/channels
Modified Files:
chan_iax2.c
Log Message:
allow explicit source address/port selection for peers (bug #4257, with minor mods)
Index: chan_iax2.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_iax2.c,v
retrieving revision 1.317
retrieving revision 1.318
diff -u -d -r1.317 -r1.318
--- chan_iax2.c 10 Jul 2005 23:49:57 -0000 1.317
+++ chan_iax2.c 12 Jul 2005 02:19:41 -0000 1.318
@@ -7827,6 +7827,92 @@
}
+/*--- check_src_ip: Check if address can be used as packet source.
+ returns:
+ 0 address available
+ 1 address unavailable
+-1 error
+*/
+static int check_srcaddr(struct sockaddr *sa, socklen_t salen)
+{
+ int sd;
+ int res;
+
+ sd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sd < 0) {
+ ast_log(LOG_ERROR, "Socket: %s\n", strerror(errno));
+ return -1;
+ }
+
+ res = bind(sd, sa, salen);
+ if (res < 0) {
+ ast_log(LOG_DEBUG, "Can't bind: %s\n", strerror(errno));
+ close(sd);
+ return 1;
+ }
+
+ close(sd);
+ return 0;
+}
+
+/*--- peer_set_srcaddr: Parse the "sourceaddress" value,
+ lookup in netsock list and set peer's sockfd. Defaults to defaultsockfd if
+ not found. */
+static int peer_set_srcaddr(struct iax2_peer *peer, const char *srcaddr)
+{
+ struct sockaddr_in sin;
+ int nonlocal = 1;
+ int port = IAX_DEFAULT_PORTNO;
+ int sockfd = defaultsockfd;
+ char *tmp;
+ char *addr;
+ char *portstr;
+
+ tmp = ast_strdupa(srcaddr);
+ if (!tmp) {
+ ast_log(LOG_WARNING, "Out of memory!\n");
+ return -1;
+ }
+
+ addr = strsep(&tmp, ":");
+ portstr = tmp;
+
+ if (portstr) {
+ port = atoi(portstr);
+ if (port < 1)
+ port = IAX_DEFAULT_PORTNO;
+ }
+
+ if (!ast_get_ip(&sin, tmp)) {
+ struct ast_netsock *sock;
+ int res;
+
+ sin.sin_port = 0;
+ res = check_srcaddr((struct sockaddr *) &sin, sizeof(sin));
+ if (res == 0) {
+ /* ip address valid. */
+ sin.sin_port = htons(port);
+ sock = ast_netsock_find(&netsock, &sin);
+ if (sock) {
+ sockfd = ast_netsock_sockfd(sock);
+ nonlocal = 0;
+ }
+ }
+ }
+
+ peer->sockfd = sockfd;
+
+ if (nonlocal) {
+ ast_log(LOG_WARNING, "Non-local or unbound address specified (%s) in sourceaddress for '%s', reverting to default\n",
+ srcaddr, peer->name);
+ return -1;
+ } else {
+ ast_log(LOG_DEBUG, "Using sourceaddress %s for '%s'\n", srcaddr, peer->name);
+ return 0;
+ }
+}
+
+
/*--- build_peer: Create peer structure based on configuration */
static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, int temponly)
{
@@ -7943,6 +8029,8 @@
free(peer);
return NULL;
}
+ } else if (!strcasecmp(v->name, "sourceaddress")) {
+ peer_set_srcaddr(peer, v->value);
} else if (!strcasecmp(v->name, "permit") ||
!strcasecmp(v->name, "deny")) {
peer->ha = ast_append_ha(v->name, v->value, peer->ha);
More information about the svn-commits
mailing list