Skip to content

Commit

Permalink
Merge pull request #2 from vpoddubchak/update-to-latest
Browse files Browse the repository at this point in the history
Update to the latest nICEr from Mozilla
  • Loading branch information
jcague authored Apr 22, 2020
2 parents c0dbdf1 + 717cd80 commit a321156
Show file tree
Hide file tree
Showing 46 changed files with 922 additions and 1,554 deletions.
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ list(APPEND nicer__c_srcs
"${NICER_SOURCE}/src/ice/ice_socket.c"
"${NICER_SOURCE}/src/net/nr_resolver.c"
"${NICER_SOURCE}/src/net/nr_socket_wrapper.c"
"${NICER_SOURCE}/src/net/nr_proxy_tunnel.c"
"${NICER_SOURCE}/src/net/nr_socket.c"
"${NICER_SOURCE}/src/net/nr_socket_local.c"
"${NICER_SOURCE}/src/net/nr_socket_multi_tcp.c"
Expand All @@ -55,8 +54,7 @@ list(APPEND nicer__c_srcs
"${NICER_SOURCE}/src/stun/stun_util.c"
"${NICER_SOURCE}/src/stun/turn_client_ctx.c"
"${NICER_SOURCE}/src/util/cb_args.c"
"${NICER_SOURCE}/src/util/ice_util.c"
"${NICER_SOURCE}/src/util/mbslen.c")
"${NICER_SOURCE}/src/util/ice_util.c")

list(APPEND nicer__other_srcs
"${NICER_SOURCE}/src/crypto/nr_crypto.h"
Expand All @@ -72,7 +70,6 @@ list(APPEND nicer__other_srcs
"${NICER_SOURCE}/src/ice/ice_socket.h"
"${NICER_SOURCE}/src/net/nr_resolver.h"
"${NICER_SOURCE}/src/net/nr_socket_wrapper.h"
"${NICER_SOURCE}/src/net/nr_proxy_tunnel.h"
"${NICER_SOURCE}/src/net/nr_socket.h"
"${NICER_SOURCE}/src/net/nr_socket_local.h"
"${NICER_SOURCE}/src/net/nr_socket_multi_tcp.h"
Expand All @@ -96,8 +93,7 @@ list(APPEND nicer__other_srcs
"${NICER_SOURCE}/src/stun/stun_util.h"
"${NICER_SOURCE}/src/stun/turn_client_ctx.h"
"${NICER_SOURCE}/src/util/cb_args.h"
"${NICER_SOURCE}/src/util/ice_util.h"
"${NICER_SOURCE}/src/util/mbslen.h")
"${NICER_SOURCE}/src/util/ice_util.h")

add_library(nicer STATIC ${nicer__c_srcs} ${nicer__other_srcs})

Expand Down
10 changes: 3 additions & 7 deletions src/crypto/nr_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,24 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



static char *RCSSTRING __UNUSED__="$Id: nr_crypto.c,v 1.2 2008/04/28 17:59:01 ekr Exp $";

#include <nr_api.h>
#include "nr_crypto.h"

static int nr_ice_crypto_dummy_random_bytes(UCHAR *buf, int len)
static int nr_ice_crypto_dummy_random_bytes(UCHAR *buf, size_t len)
{
fprintf(stderr,"Need to define crypto API implementation\n");

exit(1);
}

static int nr_ice_crypto_dummy_hmac_sha1(UCHAR *key, int key_l, UCHAR *buf, int buf_l, UCHAR digest[20])
static int nr_ice_crypto_dummy_hmac_sha1(UCHAR *key, size_t key_l, UCHAR *buf, size_t buf_l, UCHAR digest[20])
{
fprintf(stderr,"Need to define crypto API implementation\n");

exit(1);
}

static int nr_ice_crypto_dummy_md5(UCHAR *buf, int buf_l, UCHAR digest[16])
static int nr_ice_crypto_dummy_md5(UCHAR *buf, size_t buf_l, UCHAR digest[16])
{
fprintf(stderr,"Need to define crypto API implementation\n");

Expand Down
6 changes: 3 additions & 3 deletions src/crypto/nr_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


typedef struct nr_ice_crypto_vtbl_ {
int (*random_bytes)(UCHAR *buf, int len);
int (*hmac_sha1)(UCHAR *key, int key_l, UCHAR *buf, int buf_l, UCHAR digest[20]);
int (*md5)(UCHAR *buf, int buf_l, UCHAR digest[16]);
int (*random_bytes)(UCHAR *buf, size_t len);
int (*hmac_sha1)(UCHAR *key, size_t key_l, UCHAR *buf, size_t buf_l, UCHAR digest[20]);
int (*md5)(UCHAR *buf, size_t buf_l, UCHAR digest[16]);
} nr_ice_crypto_vtbl;

extern nr_ice_crypto_vtbl *nr_crypto_vtbl;
Expand Down
75 changes: 52 additions & 23 deletions src/ice/ice_candidate.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



static char *RCSSTRING __UNUSED__="$Id: ice_candidate.c,v 1.2 2008/04/28 17:59:01 ekr Exp $";

#include <csi_platform.h>
#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -310,11 +306,7 @@ int nr_ice_candidate_destroy(nr_ice_candidate **candp)

cand=*candp;

if (cand->state == NR_ICE_CAND_STATE_INITIALIZING) {
/* Make sure the ICE ctx isn't still waiting around for this candidate
* to init. */
nr_ice_candidate_mark_done(cand, NR_ICE_CAND_STATE_FAILED);
}
nr_ice_candidate_stop_gathering(cand);

switch(cand->type){
case HOST:
Expand All @@ -323,14 +315,14 @@ int nr_ice_candidate_destroy(nr_ice_candidate **candp)
case RELAYED:
// record stats back to the ice ctx on destruction
if (cand->u.relayed.turn) {
nr_ice_accumulate_count(&(cand->ctx->stats.turn_401s), cand->u.relayed.turn->cnt_401s);
nr_ice_accumulate_count(&(cand->ctx->stats.turn_403s), cand->u.relayed.turn->cnt_403s);
nr_ice_accumulate_count(&(cand->ctx->stats.turn_438s), cand->u.relayed.turn->cnt_438s);
nr_accumulate_count(&(cand->ctx->stats.turn_401s), cand->u.relayed.turn->cnt_401s);
nr_accumulate_count(&(cand->ctx->stats.turn_403s), cand->u.relayed.turn->cnt_403s);
nr_accumulate_count(&(cand->ctx->stats.turn_438s), cand->u.relayed.turn->cnt_438s);

nr_turn_stun_ctx* stun_ctx;
stun_ctx = STAILQ_FIRST(&cand->u.relayed.turn->stun_ctxs);
while (stun_ctx) {
nr_ice_accumulate_count(&(cand->ctx->stats.stun_retransmits), stun_ctx->stun->retransmit_ct);
nr_accumulate_count(&(cand->ctx->stats.stun_retransmits), stun_ctx->stun->retransmit_ct);

stun_ctx = STAILQ_NEXT(stun_ctx, entry);
}
Expand All @@ -354,19 +346,33 @@ int nr_ice_candidate_destroy(nr_ice_candidate **candp)
break;
}

NR_async_timer_cancel(cand->delay_timer);
NR_async_timer_cancel(cand->ready_cb_timer);
if(cand->resolver_handle){
nr_resolver_cancel(cand->ctx->resolver,cand->resolver_handle);
}

RFREE(cand->mdns_addr);
RFREE(cand->foundation);
RFREE(cand->label);
RFREE(cand);

return(0);
}

void nr_ice_candidate_stop_gathering(nr_ice_candidate *cand)
{
if (cand->state == NR_ICE_CAND_STATE_INITIALIZING) {
/* Make sure the ICE ctx isn't still waiting around for this candidate
* to init. */
nr_ice_candidate_mark_done(cand, NR_ICE_CAND_STATE_FAILED);
}

NR_async_timer_cancel(cand->delay_timer);
cand->delay_timer=0;
NR_async_timer_cancel(cand->ready_cb_timer);
cand->ready_cb_timer=0;

if(cand->resolver_handle){
nr_resolver_cancel(cand->ctx->resolver,cand->resolver_handle);
cand->resolver_handle=0;
}
}

/* This algorithm is not super-fast, but I don't think we need a hash
table just yet and it produces a small foundation string */
static int nr_ice_get_foundation(nr_ice_ctx *ctx,nr_ice_candidate *cand)
Expand All @@ -380,7 +386,9 @@ static int nr_ice_get_foundation(nr_ice_ctx *ctx,nr_ice_candidate *cand)
while(foundation){
if(nr_transport_addr_cmp(&cand->base,&foundation->addr,NR_TRANSPORT_ADDR_CMP_MODE_ADDR))
goto next;
if(cand->type != foundation->type)
// cast necessary because there is no guarantee that enum is signed.
// foundation->type should probably match nr_ice_candidate_type
if((int)cand->type != foundation->type)
goto next;
if(cand->stun_server != foundation->stun_server)
goto next;
Expand Down Expand Up @@ -676,6 +684,11 @@ static int nr_ice_candidate_resolved_cb(void *cb_arg, nr_transport_addr *addr)
ABORT(R_NOT_FOUND);
}

if (nr_transport_addr_check_compatibility(addr, &cand->base)) {
r_log(LOG_ICE,LOG_WARNING,"ICE(%s): Skipping STUN server because of link local mis-match for candidate %s",cand->ctx->label,cand->label);
ABORT(R_NOT_FOUND);
}

/* Copy the address */
if(r=nr_transport_addr_copy(&cand->stun_server_addr,addr))
ABORT(r);
Expand Down Expand Up @@ -923,7 +936,7 @@ static void nr_ice_turn_allocated_cb(NR_SOCKET s, int how, void *cb_arg)
#endif /* USE_TURN */

/* Format the candidate attribute as per ICE S 15.1 */
int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int maxlen)
int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int maxlen, int obfuscate_srflx_addr)
{
int r,_status;
char addr[64];
Expand All @@ -934,8 +947,14 @@ int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int ma
assert(!strcmp(nr_ice_candidate_type_names[HOST], "host"));
assert(!strcmp(nr_ice_candidate_type_names[RELAYED], "relay"));

if(r=nr_transport_addr_get_addrstring(&cand->addr,addr,sizeof(addr)))
ABORT(r);
if (cand->mdns_addr) {
/* mdns_addr is NSID_LENGTH which is 39, - 2 for removing the "{" and "}"
+ 6 for ".local" for a total of 43. */
strncpy(addr, cand->mdns_addr, sizeof(addr) - 1);
} else {
if(r=nr_transport_addr_get_addrstring(&cand->addr,addr,sizeof(addr)))
ABORT(r);
}
if(r=nr_transport_addr_get_port(&cand->addr,&port))
ABORT(r);
/* https://tools.ietf.org/html/rfc6544#section-4.5 */
Expand All @@ -957,6 +976,16 @@ int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int ma
case HOST:
break;
case SERVER_REFLEXIVE:
if (obfuscate_srflx_addr) {
snprintf(attr,maxlen," raddr 0.0.0.0 rport 0");
} else {
if(r=nr_transport_addr_get_addrstring(raddr,addr,sizeof(addr)))
ABORT(r);
if(r=nr_transport_addr_get_port(raddr,&port))
ABORT(r);
snprintf(attr,maxlen," raddr %s rport %d",addr,port);
}
break;
case PEER_REFLEXIVE:
if(r=nr_transport_addr_get_addrstring(raddr,addr,sizeof(addr)))
ABORT(r);
Expand Down
6 changes: 5 additions & 1 deletion src/ice/ice_candidate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct nr_ice_candidate_ {
char *label;
char codeword[5];
int state;
int trickled;
#define NR_ICE_CAND_STATE_CREATED 1
#define NR_ICE_CAND_STATE_INITIALIZING 2
#define NR_ICE_CAND_STATE_INITIALIZED 3
Expand All @@ -64,6 +65,8 @@ struct nr_ice_candidate_ {
nr_transport_addr addr; /* The advertised address;
JDR calls this the candidate */
nr_transport_addr base; /* The base address (S 2.1)*/
int obfuscate_addrs; /* True if addresses should be obfuscated */
char *mdns_addr; /* MDNS address, if any */
char *foundation; /* Foundation for the candidate (S 4) */
UINT4 priority; /* The priority value (S 5.4 */
nr_ice_stun_server *stun_server;
Expand Down Expand Up @@ -109,7 +112,8 @@ int nr_ice_candidate_initialize(nr_ice_candidate *cand, NR_async_cb ready_cb, vo
void nr_ice_candidate_compute_codeword(nr_ice_candidate *cand);
int nr_ice_candidate_process_stun(nr_ice_candidate *cand, UCHAR *msg, int len, nr_transport_addr *faddr);
int nr_ice_candidate_destroy(nr_ice_candidate **candp);
int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int maxlen);
void nr_ice_candidate_stop_gathering(nr_ice_candidate *cand);
int nr_ice_format_candidate_attribute(nr_ice_candidate *cand, char *attr, int maxlen, int obfuscate_srflx_addr);
int nr_ice_peer_candidate_from_attribute(nr_ice_ctx *ctx,char *attr,nr_ice_media_stream *stream,nr_ice_candidate **candp);
int nr_ice_peer_peer_rflx_candidate_create(nr_ice_ctx *ctx,char *label, nr_ice_component *comp,nr_transport_addr *addr, nr_ice_candidate **candp);
int nr_ice_candidate_compute_priority(nr_ice_candidate *cand);
Expand Down
45 changes: 24 additions & 21 deletions src/ice/ice_candidate_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



static char *RCSSTRING __UNUSED__="$Id: ice_candidate_pair.c,v 1.2 2008/04/28 17:59:01 ekr Exp $";

#include <assert.h>
#include <string.h>
#include <nr_api.h>
Expand Down Expand Up @@ -154,7 +150,7 @@ int nr_ice_candidate_pair_destroy(nr_ice_cand_pair **pairp)

// record stats back to the ice ctx on destruction
if (pair->stun_client) {
nr_ice_accumulate_count(&(pair->local->ctx->stats.stun_retransmits), pair->stun_client->retransmit_ct);
nr_accumulate_count(&(pair->local->ctx->stats.stun_retransmits), pair->stun_client->retransmit_ct);
}

RFREE(pair->as_string);
Expand Down Expand Up @@ -357,6 +353,12 @@ static void nr_ice_candidate_pair_stun_cb(NR_SOCKET s, int how, void *cb_arg)
done:
_status=0;
abort:
if (_status) {
// cb doesn't return anything, but we should probably log that we aborted
// This also quiets the unused variable warnings.
r_log(LOG_ICE,LOG_DEBUG,"ICE-PEER(%s)/STREAM(%s)/CAND-PAIR(%s): STUN cb pair addr = %s abort with status: %d",
pair->pctx->label,pair->local->stream->label,pair->codeword,pair->as_string, _status);
}
return;
}

Expand Down Expand Up @@ -445,6 +447,12 @@ int nr_ice_candidate_pair_do_triggered_check(nr_ice_peer_ctx *pctx, nr_ice_cand_
} else if(pair->state==NR_ICE_PAIR_STATE_SUCCEEDED) {
r_log(LOG_ICE,LOG_DEBUG,"ICE-PEER(%s)/CAND_PAIR(%s): No new trigger check for succeeded pair",pctx->label,pair->codeword);
return(0);
} else if (pair->local->stream->obsolete) {
r_log(LOG_ICE, LOG_DEBUG,
"ICE-PEER(%s)/CAND_PAIR(%s): No new trigger check for pair with "
"obsolete stream",
pctx->label, pair->codeword);
return (0);
}

/* Do not run this logic more than once on a given pair */
Expand Down Expand Up @@ -495,7 +503,7 @@ int nr_ice_candidate_pair_do_triggered_check(nr_ice_peer_ctx *pctx, nr_ice_cand_
return(_status);
}

int nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair, int move_to_wait_state)
void nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair, int move_to_wait_state)
{
if(pair->state != NR_ICE_PAIR_STATE_FAILED){
/* If it's already running we need to terminate the stun */
Expand All @@ -508,8 +516,6 @@ int nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair, i
}
nr_ice_candidate_pair_set_state(pctx,pair,NR_ICE_PAIR_STATE_CANCELLED);
}

return(0);
}

int nr_ice_candidate_pair_select(nr_ice_cand_pair *pair)
Expand Down Expand Up @@ -543,10 +549,8 @@ int nr_ice_candidate_pair_select(nr_ice_cand_pair *pair)
return(_status);
}

int nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair, int state)
void nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair, int state)
{
int r,_status;

r_log(LOG_ICE,LOG_INFO,"ICE-PEER(%s)/CAND-PAIR(%s): setting pair to state %s: %s",
pctx->label,pair->codeword,nr_ice_cand_pair_states[state],pair->as_string);

Expand Down Expand Up @@ -578,20 +582,13 @@ int nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pai

if(pair->state==NR_ICE_PAIR_STATE_FAILED ||
pair->state==NR_ICE_PAIR_STATE_CANCELLED){
if(r=nr_ice_component_failed_pair(pair->remote->component, pair))
ABORT(r);
nr_ice_component_failed_pair(pair->remote->component, pair);
}

_status=0;
abort:
return(_status);
}

int nr_ice_candidate_pair_dump_state(nr_ice_cand_pair *pair, FILE *out)
void nr_ice_candidate_pair_dump_state(nr_ice_cand_pair *pair, int log_level)
{
/*r_log(LOG_ICE,LOG_DEBUG,"CAND-PAIR(%s): pair %s: state=%s, priority=0x%llx\n",pair->codeword,pair->as_string,nr_ice_cand_pair_states[pair->state],pair->priority);*/

return(0);
r_log(LOG_ICE,log_level,"CAND-PAIR(%s): pair %s: state=%s, priority=0x%llx\n",pair->codeword,pair->as_string,nr_ice_cand_pair_states[pair->state],pair->priority);
}


Expand Down Expand Up @@ -643,6 +640,12 @@ void nr_ice_candidate_pair_restart_stun_nominated_cb(NR_SOCKET s, int how, void

_status=0;
abort:
if (_status) {
// cb doesn't return anything, but we should probably log that we aborted
// This also quiets the unused variable warnings.
r_log(LOG_ICE,LOG_DEBUG,"ICE-PEER(%s)/STREAM(%s)/CAND-PAIR(%s)/COMP(%d): STUN nominated cb pair as nominated: %s abort with status: %d",
pair->pctx->label,pair->local->stream->label,pair->codeword,pair->remote->component->component_id,pair->as_string, _status);
}
return;
}

Expand Down
12 changes: 9 additions & 3 deletions src/ice/ice_candidate_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ struct nr_ice_cand_pair_ {
nr_ice_candidate *remote; /* The remote candidate */
char *foundation; /* The combined foundations */

// for RTCIceCandidatePairStats
UINT8 bytes_sent;
UINT8 bytes_recvd;
struct timeval last_sent;
struct timeval last_recvd;

nr_stun_client_ctx *stun_client; /* STUN context when acting as a client */
void *stun_client_handle;

Expand All @@ -77,9 +83,9 @@ struct nr_ice_cand_pair_ {
int nr_ice_candidate_pair_create(nr_ice_peer_ctx *pctx, nr_ice_candidate *lcand,nr_ice_candidate *rcand,nr_ice_cand_pair **pairp);
int nr_ice_candidate_pair_unfreeze(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_start(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair, int state);
int nr_ice_candidate_pair_dump_state(nr_ice_cand_pair *pair, FILE *out);
int nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair, int move_to_wait_state);
void nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair, int state);
void nr_ice_candidate_pair_dump_state(nr_ice_cand_pair *pair, int log_level);
void nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair, int move_to_wait_state);
int nr_ice_candidate_pair_select(nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_do_triggered_check(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_insert(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair);
Expand Down
Loading

0 comments on commit a321156

Please sign in to comment.