Bug 1120065 - release TURN resources on PC release. r=bwc
authorNils Ohlmeier [:drno] <drno@ohlmeier.org>
Tue, 27 Jan 2015 01:18:00 +0100
changeset 226978 72073b4271d4d3a57365de5d69bcaaa0784f95c2
parent 226977 6156b3b5f1e6abfe48b89c3f0497bead255abae4
child 226979 4332055a8b22c77a60d1a1ec43e8eb3ed907bac3
push id54975
push usercbook@mozilla.com
push dateMon, 02 Feb 2015 08:02:01 +0000
treeherdermozilla-inbound@645f4595673d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbwc
bugs1120065
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1120065 - release TURN resources on PC release. r=bwc
media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.c
media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.h
--- a/media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.c
+++ b/media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.c
@@ -89,16 +89,19 @@ static int nr_turn_permission_create(nr_
 static int nr_turn_permission_find(nr_turn_client_ctx *ctx,
                                    nr_transport_addr *addr,
                                    nr_turn_permission **permp);
 static int nr_turn_permission_destroy(nr_turn_permission **permp);
 static int nr_turn_client_ensure_perm(nr_turn_client_ctx *ctx,
                                       nr_transport_addr *addr);
 static void nr_turn_client_refresh_cb(NR_SOCKET s, int how, void *arg);
 static void nr_turn_client_permissions_cb(NR_SOCKET s, int how, void *cb);
+static int nr_turn_client_send_stun_request(nr_turn_client_ctx *ctx,
+                                            nr_stun_message *req,
+                                            int flags);
 
 
 /* nr_turn_stun_ctx functions */
 static int nr_turn_stun_ctx_create(nr_turn_client_ctx *tctx, int mode,
                                    NR_async_cb success_cb,
                                    NR_async_cb error_cb,
                                    nr_turn_stun_ctx **ctxp)
 {
@@ -365,16 +368,18 @@ nr_turn_client_ctx_destroy(nr_turn_clien
     return(0);
 
   ctx=*ctxp;
   *ctxp = 0;
 
   if (ctx->label)
     r_log(NR_LOG_TURN, LOG_DEBUG, "TURN(%s): destroy", ctx->label);
 
+  nr_turn_client_deallocate(ctx);
+
   /* Cancel frees the rest of our data */
   RFREE(ctx->label);
   ctx->label = 0;
 
   nr_turn_client_cancel(ctx);
 
   RFREE(ctx->username);
   ctx->username = 0;
@@ -404,17 +409,17 @@ nr_turn_client_ctx_destroy(nr_turn_clien
 }
 
 int nr_turn_client_cancel(nr_turn_client_ctx *ctx)
 {
   nr_turn_stun_ctx *stun = 0;
 
   if (ctx->state == NR_TURN_CLIENT_STATE_CANCELLED ||
       ctx->state == NR_TURN_CLIENT_STATE_FAILED)
-    return 0;
+    return(0);
 
   if (ctx->label)
     r_log(NR_LOG_TURN, LOG_INFO, "TURN(%s): cancelling", ctx->label);
 
   /* Cancel the STUN client ctxs */
   stun = STAILQ_FIRST(&ctx->stun_ctxs);
   while (stun) {
     nr_stun_client_cancel(stun->stun);
@@ -425,16 +430,77 @@ int nr_turn_client_cancel(nr_turn_client
   NR_async_timer_cancel(ctx->connected_timer_handle);
   NR_async_timer_cancel(ctx->refresh_timer_handle);
 
   ctx->state = NR_TURN_CLIENT_STATE_CANCELLED;
 
   return(0);
 }
 
+int nr_turn_client_send_stun_request(nr_turn_client_ctx *ctx,
+                                     nr_stun_message *req,
+                                     int flags)
+{
+  int r,_status;
+
+  if ((r=nr_stun_encode_message(req)))
+    ABORT(r);
+
+  if ((r=nr_socket_sendto(ctx->sock,
+                          req->buffer, req->length, flags,
+                          &ctx->turn_server_addr))) {
+    r_log(NR_LOG_TURN, LOG_WARNING, "TURN(%s): Failed sending request",
+          ctx->label);
+    ABORT(r);
+  }
+
+  _status=0;
+abort:
+  return(_status);
+}
+
+int nr_turn_client_deallocate(nr_turn_client_ctx *ctx)
+{
+  int r,_status;
+  nr_stun_message *aloc = 0;
+  nr_stun_client_auth_params auth;
+  nr_stun_client_refresh_request_params refresh;
+
+  if (ctx->state != NR_TURN_CLIENT_STATE_ALLOCATED)
+    return(0);
+
+  r_log(NR_LOG_TURN, LOG_INFO, "TURN(%s): deallocating", ctx->label);
+
+  refresh.lifetime_secs = 0;
+
+  auth.username = ctx->username;
+  INIT_DATA(auth.password, ctx->password->data, ctx->password->len);
+
+  auth.realm = ctx->realm;
+  auth.nonce = ctx->nonce;
+
+  auth.authenticate = 1;
+
+  if ((r=nr_stun_build_refresh_request(&auth, &refresh, &aloc)))
+    ABORT(r);
+
+  // We are only sending a single request here because we are in the process of
+  // shutting everything down. Theoretically we should probably start a seperate
+  // STUN transaction which outlives the TURN context.
+  if ((r=nr_turn_client_send_stun_request(ctx, aloc, 0)))
+    ABORT(r);
+
+  ctx->state = NR_TURN_CLIENT_STATE_DEALLOCATING;
+
+  _status=0;
+abort:
+  nr_stun_message_destroy(&aloc);
+  return(_status);
+}
+
 static int nr_turn_client_failed(nr_turn_client_ctx *ctx)
 {
   if (ctx->state == NR_TURN_CLIENT_STATE_FAILED ||
       ctx->state == NR_TURN_CLIENT_STATE_CANCELLED)
     return(0);
 
   r_log(NR_LOG_TURN, LOG_WARNING, "TURN(%s) failed", ctx->label);
   nr_turn_client_cancel(ctx);
@@ -728,27 +794,19 @@ int nr_turn_client_send_indication(nr_tu
     ABORT(r);
 
   params.data.data = (UCHAR*)msg;
   params.data.len = len;
 
   if ((r=nr_stun_build_send_indication(&params, &ind)))
     ABORT(r);
 
-  if ((r=nr_stun_encode_message(ind)))
+  if ((r=nr_turn_client_send_stun_request(ctx, ind, flags)))
     ABORT(r);
 
-  if ((r=nr_socket_sendto(ctx->sock,
-                          ind->buffer, ind->length, flags,
-                          &ctx->turn_server_addr))) {
-    r_log(NR_LOG_TURN, LOG_WARNING, "TURN(%s): Failed sending send indication",
-          ctx->label);
-    ABORT(r);
-  }
-
   _status=0;
 abort:
   nr_stun_message_destroy(&ind);
   return(_status);
 }
 
 int nr_turn_client_parse_data_indication(nr_turn_client_ctx *ctx,
                                          nr_transport_addr *source_addr,
--- a/media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.h
+++ b/media/mtransport/third_party/nICEr/src/stun/turn_client_ctx.h
@@ -73,16 +73,17 @@ typedef STAILQ_HEAD(nr_turn_permission_h
    turn_client_ctx per socket/server pair. */
 typedef struct nr_turn_client_ctx_ {
   int state;
 #define NR_TURN_CLIENT_STATE_INITTED         1
 #define NR_TURN_CLIENT_STATE_ALLOCATING      2
 #define NR_TURN_CLIENT_STATE_ALLOCATED       3
 #define NR_TURN_CLIENT_STATE_FAILED          4
 #define NR_TURN_CLIENT_STATE_CANCELLED       5
+#define NR_TURN_CLIENT_STATE_DEALLOCATING    6
 
   char *label;
   nr_socket *sock;
 
   char *username;
   Data *password;
   char *nonce;
   char *realm;
@@ -113,16 +114,17 @@ int nr_turn_client_allocate(nr_turn_clie
 int nr_turn_client_get_relayed_address(nr_turn_client_ctx *ctx,
                                        nr_transport_addr *relayed_address);
 int nr_turn_client_get_mapped_address(nr_turn_client_ctx *ctx,
                                       nr_transport_addr *mapped_address);
 int nr_turn_client_process_response(nr_turn_client_ctx *ctx,
                                     UCHAR *msg, int len,
                                     nr_transport_addr *turn_server_addr);
 int nr_turn_client_cancel(nr_turn_client_ctx *ctx);
+int nr_turn_client_deallocate(nr_turn_client_ctx *ctx);
 int nr_turn_client_send_indication(nr_turn_client_ctx *ctx,
                                    const UCHAR *msg, size_t len,
                                    int flags, nr_transport_addr *remote_addr);
 int nr_turn_client_parse_data_indication(nr_turn_client_ctx *ctx,
                                          nr_transport_addr *source_addr,
                                          UCHAR *msg, size_t len,
                                          UCHAR *newmsg, size_t *newlen,
                                          size_t newsize,