aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-04-22 22:14:04 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-04-22 22:14:04 +0000
commit3b4ae75c7bafc7ac062cb71c472e22f0f25ffa28 (patch)
treebb4d6cb13ae1da64d060a169d3e006da218183fd
parent26f45c0f03c41c3e3fd7577df2a996b4b15d0999 (diff)
When we receive a full frame that is supposed to contain our call number,
ensure that it has the correct one. (closes issue #10078) (AST-2008-006) git-svn-id: http://svn.digium.com/svn/asterisk/tags/1.4.19.1@114557 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r--channels/chan_iax2.c52
1 files changed, 34 insertions, 18 deletions
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index b6d683204..0b3e77fb0 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -1239,13 +1239,13 @@ static struct iax_frame *iaxfrdup2(struct iax_frame *fr)
#define NEW_ALLOW 1
#define NEW_FORCE 2
-static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, struct chan_iax2_pvt *cur)
+static int match(struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, struct chan_iax2_pvt *cur, int full_frame)
{
if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
(cur->addr.sin_port == sin->sin_port)) {
/* This is the main host */
- if ((cur->peercallno == callno) ||
- ((dcallno == cur->callno) && !cur->peercallno)) {
+ if ( (cur->peercallno == 0 || cur->peercallno == callno) &&
+ (full_frame ? dcallno == cur->callno : 1) ) {
/* That's us. Be sure we keep track of the peer call number */
return 1;
}
@@ -1336,7 +1336,7 @@ static int make_trunk(unsigned short callno, int locked)
/*!
* \note Calling this function while holding another pvt lock can cause a deadlock.
*/
-static int find_callno(unsigned short callno, unsigned short dcallno, struct sockaddr_in *sin, int new, int sockfd)
+static int find_callno(unsigned short callno, unsigned short dcallno, struct sockaddr_in *sin, int new, int sockfd, int full_frame)
{
int res = 0;
int x;
@@ -1349,7 +1349,7 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
ast_mutex_lock(&iaxsl[x]);
if (iaxs[x]) {
/* Look for an exact match */
- if (match(sin, callno, dcallno, iaxs[x])) {
+ if (match(sin, callno, dcallno, iaxs[x], full_frame)) {
res = x;
}
}
@@ -1359,7 +1359,7 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
ast_mutex_lock(&iaxsl[x]);
if (iaxs[x]) {
/* Look for an exact match */
- if (match(sin, callno, dcallno, iaxs[x])) {
+ if (match(sin, callno, dcallno, iaxs[x], full_frame)) {
res = x;
}
}
@@ -1367,6 +1367,8 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
}
}
if ((res < 1) && (new >= NEW_ALLOW)) {
+ int start, found = 0;
+
/* It may seem odd that we look through the peer list for a name for
* this *incoming* call. Well, it is weird. However, users don't
* have an IP address/port number that we can match against. So,
@@ -1375,15 +1377,29 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
* correct, but it will be changed if needed after authentication. */
if (!iax2_getpeername(*sin, host, sizeof(host)))
snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
- gettimeofday(&now, NULL);
- for (x=1;x<TRUNK_CALL_START;x++) {
+
+ now = ast_tvnow();
+ start = 1 + (ast_random() % (TRUNK_CALL_START - 1));
+ for (x = start; 1; x++) {
+ if (x == TRUNK_CALL_START) {
+ x = 0;
+ continue;
+ }
+
/* Find first unused call number that hasn't been used in a while */
ast_mutex_lock(&iaxsl[x]);
- if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) break;
+ if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
+ found = 1;
+ break;
+ }
ast_mutex_unlock(&iaxsl[x]);
+
+ if (x == start - 1) {
+ break;
+ }
}
/* We've still got lock held if we found a spot */
- if (x >= TRUNK_CALL_START) {
+ if (x == start - 1 && !found) {
ast_log(LOG_WARNING, "No more space\n");
return 0;
}
@@ -6848,7 +6864,7 @@ static int socket_process(struct iax2_thread *thread)
}
/* This is a video frame, get call number */
- fr->callno = find_callno(ntohs(vh->callno) & ~0x8000, dcallno, &sin, new, fd);
+ fr->callno = find_callno(ntohs(vh->callno) & ~0x8000, dcallno, &sin, new, fd, 0);
minivid = 1;
} else if ((meta->zeros == 0) && !(ntohs(meta->metacmd) & 0x8000)) {
unsigned char metatype;
@@ -6906,7 +6922,7 @@ static int socket_process(struct iax2_thread *thread)
/* Stop if we don't have enough data */
if (len > res)
break;
- fr->callno = find_callno(callno & ~IAX_FLAG_FULL, 0, &sin, NEW_PREVENT, fd);
+ fr->callno = find_callno(callno & ~IAX_FLAG_FULL, 0, &sin, NEW_PREVENT, fd, 0);
if (fr->callno) {
ast_mutex_lock(&iaxsl[fr->callno]);
/* If it's a valid call, deliver the contents. If not, we
@@ -6993,7 +7009,7 @@ static int socket_process(struct iax2_thread *thread)
}
if (!fr->callno)
- fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &sin, new, fd);
+ fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &sin, new, fd, ntohs(mh->callno) & IAX_FLAG_FULL);
if (fr->callno > 0)
ast_mutex_lock(&iaxsl[fr->callno]);
@@ -8481,7 +8497,7 @@ static int iax2_do_register(struct iax2_registry *reg)
if (!reg->callno) {
if (option_debug)
ast_log(LOG_DEBUG, "Allocate call number\n");
- reg->callno = find_callno(0, 0, &reg->addr, NEW_FORCE, defaultsockfd);
+ reg->callno = find_callno(0, 0, &reg->addr, NEW_FORCE, defaultsockfd, 0);
if (reg->callno < 1) {
ast_log(LOG_WARNING, "Unable to create call for registration\n");
return -1;
@@ -8541,7 +8557,7 @@ static int iax2_provision(struct sockaddr_in *end, int sockfd, char *dest, const
memset(&ied, 0, sizeof(ied));
iax_ie_append_raw(&ied, IAX_IE_PROVISIONING, provdata.buf, provdata.pos);
- callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd);
+ callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
if (!callno)
return -1;
@@ -8683,7 +8699,7 @@ static int iax2_poke_peer(struct iax2_peer *peer, int heldcall)
}
if (heldcall)
ast_mutex_unlock(&iaxsl[heldcall]);
- peer->callno = find_callno(0, 0, &peer->addr, NEW_FORCE, peer->sockfd);
+ peer->callno = find_callno(0, 0, &peer->addr, NEW_FORCE, peer->sockfd, 0);
if (heldcall)
ast_mutex_lock(&iaxsl[heldcall]);
if (peer->callno < 1) {
@@ -8763,7 +8779,7 @@ static struct ast_channel *iax2_request(const char *type, int format, void *data
if (pds.port)
sin.sin_port = htons(atoi(pds.port));
- callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd);
+ callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
if (callno < 1) {
ast_log(LOG_WARNING, "Unable to create call\n");
*cause = AST_CAUSE_CONGESTION;
@@ -10079,7 +10095,7 @@ static int cache_get_callno_locked(const char *data)
ast_log(LOG_DEBUG, "peer: %s, username: %s, password: %s, context: %s\n",
pds.peer, pds.username, pds.password, pds.context);
- callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd);
+ callno = find_callno(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
if (callno < 1) {
ast_log(LOG_WARNING, "Unable to create call\n");
return -1;