From 0427b4baf452d12c0a7a8a25111b9a472701625c Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 6 Mar 2013 13:05:56 -0500 Subject: [PATCH] handle "use-candidate" properly --- daemon/call.c | 17 ++++++++++++++--- daemon/sdp.c | 21 ++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index 623186401..ed6ceb44d 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -213,7 +213,7 @@ static int stream_packet(struct streamrelay *r, str *s, struct sockaddr_in6 *fsi struct streamrelay *p, *p2; struct peer *pe, *pe2; struct callstream *cs; - int ret, update = 0; + int ret, update = 0, stun_ret = 0; struct sockaddr_in sin; struct sockaddr_in6 sin6; struct msghdr mh; @@ -236,8 +236,13 @@ static int stream_packet(struct streamrelay *r, str *s, struct sockaddr_in6 *fsi smart_ntop_port(addr, fsin, sizeof(addr)); if (r->stun && is_stun(s)) { - if (!stun(s, r, fsin)) + stun_ret = stun(s, r, fsin); + if (!stun_ret) return 0; + if (stun_ret == 1) /* use candidate */ + goto peerinfo2; + else /* not an stun packet */ + stun_ret = 0; } if (p->fd.fd == -1) { @@ -271,6 +276,7 @@ peerinfo: pe->codec = "unknown"; } +peerinfo2: p2 = &p->up->rtps[p->idx ^ 1]; p->peer.ip46 = fsin->sin6_addr; p->peer.port = ntohs(fsin->sin6_port); @@ -284,7 +290,8 @@ peerinfo: forward: if (is_addr_unspecified(&r->peer_advertised.ip46) - || !r->peer_advertised.port || !r->fd.fd_family) + || !r->peer_advertised.port || !r->fd.fd_family + || stun_ret) goto drop; ZERO(mh); @@ -1647,6 +1654,8 @@ static str *streams_print(GQueue *s, int num, enum call_opmode opmode, const cha goto out; t = s->head->data; + mutex_lock(&t->lock); + if (format == SAF_TCP) call_stream_address_gstring(o, &t->peers[off], format); @@ -1661,6 +1670,8 @@ static str *streams_print(GQueue *s, int num, enum call_opmode opmode, const cha g_string_append_printf(o, " %c", (af == AF_INET) ? '4' : '6'); } + mutex_unlock(&t->lock); + out: g_string_append(o, "\n"); diff --git a/daemon/sdp.c b/daemon/sdp.c index d16492944..80970b48c 100644 --- a/daemon/sdp.c +++ b/daemon/sdp.c @@ -767,7 +767,9 @@ static int insert_ice_address(struct sdp_chopper *chop, struct streamrelay *sr) char buf[64]; int len; + mutex_lock(&sr->up->up->lock); call_stream_address(buf, sr->up, SAF_ICE, &len); + mutex_unlock(&sr->up->up->lock); chopper_append_dup(chop, buf, len); chopper_append_printf(chop, " %hu", sr->fd.localport); @@ -778,7 +780,9 @@ static int insert_ice_address_alt(struct sdp_chopper *chop, struct streamrelay * char buf[64]; int len; + mutex_lock(&sr->up->up->lock); call_stream_address_alt(buf, sr->up, SAF_ICE, &len); + mutex_unlock(&sr->up->up->lock); chopper_append_dup(chop, buf, len); chopper_append_printf(chop, " %hu", sr->fd.localport); @@ -794,7 +798,9 @@ static int replace_network_address(struct sdp_chopper *chop, struct network_addr if (copy_up_to(chop, &address->address_type)) return -1; + mutex_lock(&sr->up->up->lock); call_stream_address(buf, sr->up, SAF_NG, &len); + mutex_unlock(&sr->up->up->lock); chopper_append_dup(chop, buf, len); if (skip_over(chop, &address->address)) @@ -1081,19 +1087,28 @@ int sdp_replace(struct sdp_chopper *chop, GQueue *sessions, struct call *call, } if (do_ice) { - /* XXX locking here? */ + mutex_lock(&rtp->up->up->lock); if (!rtp->up->ice_ufrag.s) { create_random_string(call, &rtp->up->ice_ufrag, 8); create_random_string(call, &rtp->up->ice_pwd, 28); } + rtp->stun = 1; + mutex_unlock(&rtp->up->up->lock); + + mutex_lock(&rtcp->up->up->lock); + if (rtp->up != rtcp->up && !rtcp->up->ice_ufrag.s) { + /* safe to read */ + rtcp->up->ice_ufrag = rtp->up->ice_ufrag; + rtcp->up->ice_pwd = rtp->up->ice_pwd; + } + rtcp->stun = 1; + mutex_unlock(&rtcp->up->up->lock); chopper_append_c(chop, "a=ice-ufrag:"); chopper_append_str(chop, &rtp->up->ice_ufrag); chopper_append_c(chop, "\r\na=ice-pwd:"); chopper_append_str(chop, &rtp->up->ice_pwd); chopper_append_c(chop, "\r\n"); - rtp->stun = 1; - rtcp->stun = 1; } if (!flags->ice_remove) {