summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nICEr/src/stun/nr_socket_buffered_stun.c
blob: 4b5b92fb75db9a354d46586c71cc21c313540b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
Copyright (c) 2007, Adobe Systems, Incorporated
Copyright (c) 2013, Mozilla
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <nr_api.h>

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <assert.h>
#include <inttypes.h>

#include "p_buf.h"
#include "nr_socket.h"
#include "stun.h"
#include "nr_socket_buffered_stun.h"

#define NR_MAX_FRAME_SIZE 0xFFFF

typedef struct nr_frame_header_ {
  UINT2 frame_length;
  char  data[0];
} nr_frame_header;

typedef struct nr_socket_buffered_stun_ {
  nr_socket *inner;
  nr_transport_addr remote_addr;
  int connected;

  /* Read state */
  int read_state;
#define NR_ICE_SOCKET_READ_NONE   0
#define NR_ICE_SOCKET_READ_HDR    1
#define NR_ICE_SOCKET_READ_FAILED 2
  UCHAR *buffer;
  size_t buffer_size;
  size_t bytes_needed;
  size_t bytes_read;
  NR_async_cb readable_cb;
  void *readable_cb_arg;

  /* Write state */
  nr_p_buf_ctx *p_bufs;
  nr_p_buf_head pending_writes;
  size_t pending;
  size_t max_pending;
  nr_framing_type framing_type;
} nr_socket_buffered_stun;

static int nr_socket_buffered_stun_destroy(void **objp);
static int nr_socket_buffered_stun_sendto(void *obj,const void *msg, size_t len,
  int flags, const nr_transport_addr *to);
static int nr_socket_buffered_stun_recvfrom(void *obj,void * restrict buf,
  size_t maxlen, size_t *len, int flags, nr_transport_addr *from);
static int nr_socket_buffered_stun_getfd(void *obj, NR_SOCKET *fd);
static int nr_socket_buffered_stun_getaddr(void *obj, nr_transport_addr *addrp);
static int nr_socket_buffered_stun_close(void *obj);
static int nr_socket_buffered_stun_connect(void *sock, const nr_transport_addr *addr);
static int nr_socket_buffered_stun_write(void *obj,const void *msg, size_t len, size_t *written);
static void nr_socket_buffered_stun_writable_cb(NR_SOCKET s, int how, void *arg);
static int nr_socket_buffered_stun_listen(void *obj, int backlog);
static int nr_socket_buffered_stun_accept(void *obj, nr_transport_addr *addrp, nr_socket **sockp);

static nr_socket_vtbl nr_socket_buffered_stun_vtbl={
  2,
  nr_socket_buffered_stun_destroy,
  nr_socket_buffered_stun_sendto,
  nr_socket_buffered_stun_recvfrom,
  nr_socket_buffered_stun_getfd,
  nr_socket_buffered_stun_getaddr,
  nr_socket_buffered_stun_connect,
  0,
  0,
  nr_socket_buffered_stun_close,
  nr_socket_buffered_stun_listen,
  nr_socket_buffered_stun_accept
};

void nr_socket_buffered_stun_set_readable_cb(nr_socket *sock,
  NR_async_cb readable_cb, void *readable_cb_arg)
{
  nr_socket_buffered_stun *buf_sock = (nr_socket_buffered_stun *)sock->obj;

  buf_sock->readable_cb = readable_cb;
  buf_sock->readable_cb_arg = readable_cb_arg;
}

int nr_socket_buffered_set_connected_to(nr_socket *sock, nr_transport_addr *remote_addr)
{
  nr_socket_buffered_stun *buf_sock = (nr_socket_buffered_stun *)sock->obj;
  int r, _status;

  if ((r=nr_transport_addr_copy(&buf_sock->remote_addr, remote_addr)))
    ABORT(r);

  buf_sock->connected = 1;

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

int nr_socket_buffered_stun_create(nr_socket *inner, int max_pending,
  nr_framing_type framing_type, nr_socket **sockp)
{
  int r, _status;
  nr_socket_buffered_stun *sock = 0;
  size_t frame_size;

  if (!(sock = RCALLOC(sizeof(nr_socket_buffered_stun))))
    ABORT(R_NO_MEMORY);

  sock->inner = inner;
  sock->framing_type = framing_type;

  if ((r=nr_ip4_port_to_transport_addr(INADDR_ANY, 0, IPPROTO_TCP, &sock->remote_addr)))
    ABORT(r);

  switch (framing_type) {
    case ICE_TCP_FRAMING:
      frame_size = sizeof(nr_frame_header);
      sock->buffer_size = sizeof(nr_frame_header) + NR_MAX_FRAME_SIZE;
      sock->bytes_needed = sizeof(nr_frame_header);
      break;
    case TURN_TCP_FRAMING:
      frame_size = 0;
      sock->buffer_size = NR_STUN_MAX_MESSAGE_SIZE;
      sock->bytes_needed = sizeof(nr_stun_message_header);
      break;
    default:
      assert(0);
      ABORT(R_BAD_ARGS);
  }

  /* TODO(ekr@rtfm.com): Check this */
  if (!(sock->buffer = RMALLOC(sock->buffer_size)))
    ABORT(R_NO_MEMORY);

  sock->read_state = NR_ICE_SOCKET_READ_NONE;
  sock->connected = 0;

  STAILQ_INIT(&sock->pending_writes);
  if ((r=nr_p_buf_ctx_create(sock->buffer_size, &sock->p_bufs)))
    ABORT(r);
  sock->max_pending = max_pending + frame_size;

  if ((r=nr_socket_create_int(sock, &nr_socket_buffered_stun_vtbl, sockp)))
    ABORT(r);

  _status=0;
abort:
  if (_status && sock) {
    void *sock_v = sock;
    sock->inner = 0;  /* Give up ownership so we don't destroy */
    nr_socket_buffered_stun_destroy(&sock_v);
  }
  return(_status);
}

/* Note: This destroys the inner socket */
int nr_socket_buffered_stun_destroy(void **objp)
{
  nr_socket_buffered_stun *sock;
  NR_SOCKET fd;

  if (!objp || !*objp)
    return 0;

  sock = (nr_socket_buffered_stun *)*objp;
  *objp = 0;

  /* Free the buffer if needed */
  RFREE(sock->buffer);

  /* Cancel waiting on the socket */
  if (sock->inner && !nr_socket_getfd(sock->inner, &fd)) {
    NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);
    NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_READ);
  }

  nr_p_buf_free_chain(sock->p_bufs, &sock->pending_writes);
  nr_p_buf_ctx_destroy(&sock->p_bufs);
  nr_socket_destroy(&sock->inner);
  RFREE(sock);

  return 0;
}

static int nr_socket_buffered_stun_sendto(void *obj,const void *msg, size_t len,
  int flags, const nr_transport_addr *to)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;
  int r, _status;
  size_t written;
  nr_frame_header *frame = NULL;

  /* Check that we are writing to the connected address if
     connected */
  if (!nr_transport_addr_is_wildcard(&sock->remote_addr)) {
    if (nr_transport_addr_cmp(&sock->remote_addr, to, NR_TRANSPORT_ADDR_CMP_MODE_ALL)) {
      r_log(LOG_GENERIC, LOG_ERR, "Sendto on connected socket doesn't match");
      ABORT(R_BAD_DATA);
    }
  }

  if (sock->framing_type == ICE_TCP_FRAMING) {

    assert(len <= NR_MAX_FRAME_SIZE);
    if (len > NR_MAX_FRAME_SIZE)
      ABORT(R_FAILED);

    if (!(frame = RMALLOC(len + sizeof(nr_frame_header))))
      ABORT(R_NO_MEMORY);

    frame->frame_length = htons(len);
    memcpy(frame->data, msg, len);
    len += sizeof(nr_frame_header);
    msg = frame;
  }

  if ((r=nr_socket_buffered_stun_write(obj, msg, len, &written)))
    ABORT(r);

  if (len != written)
    ABORT(R_IO_ERROR);

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

static void nr_socket_buffered_stun_failed(nr_socket_buffered_stun *sock)
  {
    NR_SOCKET fd;

    sock->read_state = NR_ICE_SOCKET_READ_FAILED;

    /* Cancel waiting on the socket */
    if (sock->inner && !nr_socket_getfd(sock->inner, &fd)) {
      NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);
      NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_READ);
    }
  }

static int nr_socket_buffered_stun_recvfrom(void *obj,void * restrict buf,
  size_t maxlen, size_t *len, int flags, nr_transport_addr *from)
{
  int r, _status;
  size_t bytes_read;
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;
  nr_frame_header *frame = (nr_frame_header *)sock->buffer;
  size_t skip_hdr_size = (sock->framing_type == ICE_TCP_FRAMING) ? sizeof(nr_frame_header) : 0;

  if (sock->read_state == NR_ICE_SOCKET_READ_FAILED) {
    ABORT(R_FAILED);
  }

  while (sock->bytes_needed) {
    /* Read all the expected bytes */
    assert(sock->bytes_needed <= sock->buffer_size - sock->bytes_read);

    if(r=nr_socket_read(sock->inner,
                        sock->buffer + sock->bytes_read,
                        sock->bytes_needed, &bytes_read, 0))
      ABORT(r);

    assert(bytes_read <= sock->bytes_needed);
    sock->bytes_needed -= bytes_read;
    sock->bytes_read += bytes_read;

    /* Unfinished */
    if (sock->bytes_needed)
      ABORT(R_WOULDBLOCK);

    /* No more bytes expected */
    if (sock->read_state == NR_ICE_SOCKET_READ_NONE) {
      size_t remaining_length;
      if (sock->framing_type == ICE_TCP_FRAMING) {
        if (sock->bytes_read < sizeof(nr_frame_header))
          ABORT(R_BAD_DATA);
        remaining_length = ntohs(frame->frame_length);
      } else {
        int tmp_length;

        /* Parse the header */
        if (r = nr_stun_message_length(sock->buffer, sock->bytes_read, &tmp_length))
          ABORT(r);
        assert(tmp_length >= 0);
        if (tmp_length < 0)
          ABORT(R_BAD_DATA);
        remaining_length = tmp_length;

      }
      /* Check to see if we have enough room */
      if ((sock->buffer_size - sock->bytes_read) < remaining_length)
        ABORT(R_BAD_DATA);

      sock->read_state = NR_ICE_SOCKET_READ_HDR;
      /* Set ourselves up to read the rest of the data */
      sock->bytes_needed = remaining_length;
    }
  }

  assert(skip_hdr_size <= sock->bytes_read);
  if (skip_hdr_size > sock->bytes_read)
    ABORT(R_BAD_DATA);
  sock->bytes_read -= skip_hdr_size;

  if (maxlen < sock->bytes_read)
    ABORT(R_BAD_ARGS);

  *len = sock->bytes_read;
  memcpy(buf, sock->buffer + skip_hdr_size, sock->bytes_read);

  sock->bytes_read = 0;
  sock->read_state = NR_ICE_SOCKET_READ_NONE;
  sock->bytes_needed = (sock->framing_type == ICE_TCP_FRAMING) ? sizeof(nr_frame_header) : sizeof(nr_stun_message_header);

  if ((r = nr_transport_addr_copy(from, &sock->remote_addr))) ABORT(r);

  _status=0;
abort:
  if (_status && (_status != R_WOULDBLOCK)) {
    nr_socket_buffered_stun_failed(sock);
  }

  return(_status);
}

static int nr_socket_buffered_stun_getfd(void *obj, NR_SOCKET *fd)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;

  return nr_socket_getfd(sock->inner, fd);
}

static int nr_socket_buffered_stun_getaddr(void *obj, nr_transport_addr *addrp)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;

  return nr_socket_getaddr(sock->inner, addrp);
}

static int nr_socket_buffered_stun_close(void *obj)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;
  NR_SOCKET fd;

  /* Cancel waiting on the socket */
  if (sock->inner && !nr_socket_getfd(sock->inner, &fd)) {
    NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);
  }

  return nr_socket_close(sock->inner);
}

static int nr_socket_buffered_stun_listen(void *obj, int backlog)
{
  int r, _status;
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;

  if (!sock->inner)
    ABORT(R_FAILED);

  if ((r=nr_socket_listen(sock->inner, backlog)))
    ABORT(r);

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


static int nr_socket_buffered_stun_accept(void *obj, nr_transport_addr *addrp, nr_socket **sockp)
{
  nr_socket_buffered_stun *bsock = (nr_socket_buffered_stun *)obj;

  return nr_socket_accept(bsock->inner, addrp, sockp);
}

static void nr_socket_buffered_stun_connected_cb(NR_SOCKET s, int how, void *arg)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)arg;
  int r, _status;
  NR_SOCKET fd;

  assert(!sock->connected);

  sock->connected = 1;

  if ((r=nr_socket_getfd(sock->inner, &fd)))
    ABORT(r);
  NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);

  // once connected arm for read
  if (sock->readable_cb) {
    NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_READ, sock->readable_cb, sock->readable_cb_arg);
  }

  if (sock->pending) {
    r_log(LOG_GENERIC, LOG_INFO, "Invoking writable_cb on connected (%u)", (uint32_t) sock->pending);
    nr_socket_buffered_stun_writable_cb(s, how, arg);
  }

  _status=0;
abort:
  if (_status) {
    r_log(LOG_GENERIC, LOG_ERR, "Failure in nr_socket_buffered_stun_connected_cb: %d", _status);

  }
}

static int nr_socket_buffered_stun_connect(void *obj, const nr_transport_addr *addr)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;
  int r, _status;

  if ((r=nr_transport_addr_copy(&sock->remote_addr, addr)))
    ABORT(r);

  if ((r=nr_socket_connect(sock->inner, addr))) {
    if (r == R_WOULDBLOCK) {
      NR_SOCKET fd;

      if ((r=nr_socket_getfd(sock->inner, &fd)))
        ABORT(r);

      NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_WRITE, nr_socket_buffered_stun_connected_cb, sock);
      ABORT(R_WOULDBLOCK);
    }
    ABORT(r);
  } else {
    r_log(LOG_GENERIC, LOG_INFO, "Connected without blocking");
    sock->connected = 1;
  }

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

static int nr_socket_buffered_stun_arm_writable_cb(nr_socket_buffered_stun *sock)
{
  int r, _status;
  NR_SOCKET fd;

  if ((r=nr_socket_getfd(sock->inner, &fd)))
    ABORT(r);

  NR_ASYNC_WAIT(fd, NR_ASYNC_WAIT_WRITE, nr_socket_buffered_stun_writable_cb, sock);

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

static int nr_socket_buffered_stun_write(void *obj,const void *msg, size_t len, size_t *written)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)obj;
  int already_armed = 0;
  int r,_status;
  size_t written2 = 0;
  size_t original_len = len;

  /* Buffers are close to full, report error. Do this now so we never
     get partial writes */
  if ((sock->pending + len) > sock->max_pending) {
    r_log(LOG_GENERIC, LOG_INFO, "Write buffer for %s full (%u + %u > %u) - re-arming @%p",
          sock->remote_addr.as_string, (uint32_t)sock->pending, (uint32_t)len, (uint32_t)sock->max_pending,
          &(sock->pending));
    ABORT(R_WOULDBLOCK);
  }


  if (sock->connected && !sock->pending) {
    r = nr_socket_write(sock->inner, msg, len, &written2, 0);
    if (r) {
      if (r != R_WOULDBLOCK) {
        r_log(LOG_GENERIC, LOG_ERR, "Write error for %s - %d",
              sock->remote_addr.as_string, r);
        ABORT(r);
      }
      r_log(LOG_GENERIC, LOG_INFO, "Write of %" PRIu64 " blocked for %s",
            (uint64_t) len, sock->remote_addr.as_string);

      written2=0;
    }
  } else {
    already_armed = 1;
  }

  /* Buffer what's left */
  len -= written2;

  if (len) {
    if ((r=nr_p_buf_write_to_chain(sock->p_bufs, &sock->pending_writes,
                                   ((UCHAR *)msg) + written2, len))) {
      r_log(LOG_GENERIC, LOG_ERR, "Write_to_chain error for %s - %d",
            sock->remote_addr.as_string, r);

      ABORT(r);
    }

    sock->pending += len;
  }

  if (sock->pending) {
    if (!already_armed) {
      if ((r=nr_socket_buffered_stun_arm_writable_cb(sock)))
        ABORT(r);
    }
    r_log(LOG_GENERIC, LOG_INFO, "Write buffer not empty for %s  %u - %s armed (@%p),%s connected",
          sock->remote_addr.as_string, (uint32_t)sock->pending,
          already_armed ? "already" : "", &sock->pending,
          sock->connected ? "" : " not");
  }

  *written = original_len;

  _status=0;
abort:
  return _status;
}

static void nr_socket_buffered_stun_writable_cb(NR_SOCKET s, int how, void *arg)
{
  nr_socket_buffered_stun *sock = (nr_socket_buffered_stun *)arg;
  int r,_status;
  nr_p_buf *n1, *n2;

  if (sock->read_state == NR_ICE_SOCKET_READ_FAILED) {
    ABORT(R_FAILED);
  }

  /* Try to flush */
  STAILQ_FOREACH_SAFE(n1, &sock->pending_writes, entry, n2) {
    size_t written = 0;

    if ((r=nr_socket_write(sock->inner, n1->data + n1->r_offset,
                           n1->length - n1->r_offset,
                           &written, 0))) {

      r_log(LOG_GENERIC, LOG_ERR, "Write error for %s - %d",
            sock->remote_addr.as_string, r);
      ABORT(r);
    }

    n1->r_offset += written;
    assert(sock->pending >= written);
    sock->pending -= written;

    if (n1->r_offset < n1->length) {
      /* We wrote something, but not everything */
      r_log(LOG_GENERIC, LOG_INFO, "Write in callback didn't write all (remaining %u of %u) for %s",
            n1->length - n1->r_offset, n1->length,
            sock->remote_addr.as_string);
      ABORT(R_WOULDBLOCK);
    }

    /* We are done with this p_buf */
    STAILQ_REMOVE_HEAD(&sock->pending_writes, entry);
    nr_p_buf_free(sock->p_bufs, n1);
  }

  assert(!sock->pending);
  _status=0;
abort:
  r_log(LOG_GENERIC, LOG_INFO, "Writable_cb %s (%u (%p) pending)",
        sock->remote_addr.as_string, (uint32_t)sock->pending, &(sock->pending));
  if (_status && _status != R_WOULDBLOCK) {
    r_log(LOG_GENERIC, LOG_ERR, "Failure in writable_cb: %d", _status);
    nr_socket_buffered_stun_failed(sock);
    /* Report this failure up; the only way to do this is a readable callback.
     * Once the user tries to read (using nr_socket_buffered_stun_recvfrom), it
     * will notice that there has been a failure. */
    if (sock->readable_cb) {
      sock->readable_cb(s, NR_ASYNC_WAIT_READ, sock->readable_cb_arg);
    }
  } else if (sock->pending) {
    nr_socket_buffered_stun_arm_writable_cb(sock);
  }
}

int nr_socket_buffered_stun_reset(nr_socket* sock_arg, nr_socket* new_inner) {
  int r, _status;
  NR_SOCKET fd;

  nr_socket_buffered_stun* sock = (nr_socket_buffered_stun*)sock_arg->obj;

  if (sock->inner && !nr_socket_getfd(sock->inner, &fd)) {
    r_log(LOG_GENERIC, LOG_DEBUG, "In %s, canceling wait on old socket", __FUNCTION__);
    NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_WRITE);
    NR_ASYNC_CANCEL(fd, NR_ASYNC_WAIT_READ);
  }

  nr_socket_destroy(&sock->inner);
  sock->inner = new_inner;

  sock->read_state = NR_ICE_SOCKET_READ_NONE;
  sock->connected = 0;

  sock->bytes_read = 0;
  sock->bytes_needed = (sock->framing_type == ICE_TCP_FRAMING)
                           ? sizeof(nr_frame_header)
                           : sizeof(nr_stun_message_header);
  sock->pending = 0;

  nr_p_buf_free_chain(sock->p_bufs, &sock->pending_writes);
  nr_p_buf_ctx_destroy(&sock->p_bufs);

  STAILQ_INIT(&sock->pending_writes);

  if ((r = nr_p_buf_ctx_create(sock->buffer_size, &sock->p_bufs))) {
    ABORT(r);
  }

  if ((r = nr_ip4_port_to_transport_addr(INADDR_ANY, 0, IPPROTO_TCP,
                                         &sock->remote_addr))) {
    ABORT(r);
  }

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