forked from libnice/libnice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudotcp.c
2646 lines (2239 loc) · 78.6 KB
/
pseudotcp.c
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2010, 2014 Collabora Ltd.
* Contact: Philip Withnall
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Philip Withnall, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
/* Reproducing license from libjingle for copied code */
/*
* libjingle
* Copyright 2004--2005, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 <stdlib.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#ifndef G_OS_WIN32
# include <arpa/inet.h>
#endif
#include "pseudotcp.h"
#include "agent-priv.h"
struct _PseudoTcpSocketClass {
GObjectClass parent_class;
};
typedef struct _PseudoTcpSocketPrivate PseudoTcpSocketPrivate;
struct _PseudoTcpSocket {
GObject parent;
PseudoTcpSocketPrivate *priv;
};
G_DEFINE_TYPE (PseudoTcpSocket, pseudo_tcp_socket, G_TYPE_OBJECT);
//////////////////////////////////////////////////////////////////////
// Network Constants
//////////////////////////////////////////////////////////////////////
// Standard MTUs
const guint16 PACKET_MAXIMUMS[] = {
65535, // Theoretical maximum, Hyperchannel
32000, // Nothing
17914, // 16Mb IBM Token Ring
8166, // IEEE 802.4
//4464, // IEEE 802.5 (4Mb max)
4352, // FDDI
//2048, // Wideband Network
2002, // IEEE 802.5 (4Mb recommended)
//1536, // Expermental Ethernet Networks
//1500, // Ethernet, Point-to-Point (default)
1492, // IEEE 802.3
1006, // SLIP, ARPANET
//576, // X.25 Networks
//544, // DEC IP Portal
//512, // NETBIOS
508, // IEEE 802/Source-Rt Bridge, ARCNET
296, // Point-to-Point (low delay)
//68, // Official minimum
0, // End of list marker
};
// FIXME: This is a reasonable MTU, but we should get it from the lower layer
#define DEF_MTU 1400
#define MAX_PACKET 65532
// Note: we removed lowest level because packet overhead was larger!
#define MIN_PACKET 296
// (+ up to 40 bytes of options?)
#define IP_HEADER_SIZE 20
#define ICMP_HEADER_SIZE 8
#define UDP_HEADER_SIZE 8
// TODO: Make JINGLE_HEADER_SIZE transparent to this code?
// when relay framing is in use
#define JINGLE_HEADER_SIZE 64
//////////////////////////////////////////////////////////////////////
// Global Constants and Functions
//////////////////////////////////////////////////////////////////////
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 0 | Conversation Number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 4 | Sequence Number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 8 | Acknowledgment Number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | | |U|A|P|R|S|F| |
// 12 | Control | |R|C|S|S|Y|I| Window |
// | | |G|K|H|T|N|N| |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 16 | Timestamp sending |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 20 | Timestamp receiving |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 24 | data |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
//////////////////////////////////////////////////////////////////////
#define MAX_SEQ 0xFFFFFFFF
#define HEADER_SIZE 24
#define PACKET_OVERHEAD (HEADER_SIZE + UDP_HEADER_SIZE + \
IP_HEADER_SIZE + JINGLE_HEADER_SIZE)
// MIN_RTO = 1 second (RFC6298, Sec 2.4)
#define MIN_RTO 1000
#define DEF_RTO 1000 /* 1 seconds (RFC 6298 sect 2.1) */
#define MAX_RTO 60000 /* 60 seconds */
#define DEFAULT_ACK_DELAY 100 /* 100 milliseconds */
#define DEFAULT_NO_DELAY FALSE
#define DEFAULT_RCV_BUF_SIZE (60 * 1024)
#define DEFAULT_SND_BUF_SIZE (90 * 1024)
/* NOTE: This must fit in 8 bits. This is used on the wire. */
typedef enum {
/* Google-provided options: */
TCP_OPT_EOL = 0, /* end of list */
TCP_OPT_NOOP = 1, /* no-op */
TCP_OPT_MSS = 2, /* maximum segment size */
TCP_OPT_WND_SCALE = 3, /* window scale factor */
/* libnice extensions: */
TCP_OPT_FIN_ACK = 254, /* FIN-ACK support */
} TcpOption;
/*
#define FLAG_SYN 0x02
#define FLAG_ACK 0x10
*/
/* NOTE: This must fit in 5 bits. This is used on the wire. */
typedef enum {
FLAG_NONE = 0,
FLAG_FIN = 1 << 0,
FLAG_CTL = 1 << 1,
FLAG_RST = 1 << 2,
} TcpFlags;
#define CTL_CONNECT 0
//#define CTL_REDIRECT 1
#define CTL_EXTRA 255
#define CTRL_BOUND 0x80000000
/* Maximum segment lifetime (1 minute).
* RFC 793, §3.3 specifies 2 minutes; but Linux uses 1 minute, so let’s go with
* that. */
#define TCP_MSL (60 * 1000)
// If there are no pending clocks, wake up every 4 seconds
#define DEFAULT_TIMEOUT 4000
// If the connection is closed, once per minute
#define CLOSED_TIMEOUT (60 * 1000)
/* Timeout after reaching the TIME_WAIT state, in milliseconds.
* See: RFC 1122, §4.2.2.13.
*
* XXX: Since we can control the underlying layer’s channel ID, we can guarantee
* delayed segments won’t affect subsequent connections, so can radically
* shorten the TIME-WAIT timeout (to the extent that it basically doesn’t
* exist). It would normally be (2 * TCP_MSL). */
#define TIME_WAIT_TIMEOUT 1
//////////////////////////////////////////////////////////////////////
// Helper Functions
//////////////////////////////////////////////////////////////////////
#ifndef G_OS_WIN32
# define min(first, second) ((first) < (second) ? (first) : (second))
# define max(first, second) ((first) > (second) ? (first) : (second))
#endif
static guint32
bound(guint32 lower, guint32 middle, guint32 upper)
{
return min (max (lower, middle), upper);
}
static gboolean
time_is_between(guint32 later, guint32 middle, guint32 earlier)
{
if (earlier <= later) {
return ((earlier <= middle) && (middle <= later));
} else {
return !((later < middle) && (middle < earlier));
}
}
static gint32
time_diff(guint32 later, guint32 earlier)
{
guint32 LAST = 0xFFFFFFFF;
guint32 HALF = 0x80000000;
if (time_is_between(earlier + HALF, later, earlier)) {
if (earlier <= later) {
return (long)(later - earlier);
} else {
return (long)(later + (LAST - earlier) + 1);
}
} else {
if (later <= earlier) {
return -(long) (earlier - later);
} else {
return -(long)(earlier + (LAST - later) + 1);
}
}
}
////////////////////////////////////////////////////////
// PseudoTcpFifo works exactly like FifoBuffer in libjingle
////////////////////////////////////////////////////////
typedef struct {
guint8 *buffer;
gsize buffer_length;
gsize data_length;
gsize read_position;
} PseudoTcpFifo;
static void
pseudo_tcp_fifo_init (PseudoTcpFifo *b, gsize size)
{
b->buffer = g_slice_alloc (size);
b->buffer_length = size;
}
static void
pseudo_tcp_fifo_clear (PseudoTcpFifo *b)
{
if (b->buffer)
g_slice_free1 (b->buffer_length, b->buffer);
b->buffer = NULL;
b->buffer_length = 0;
}
static gsize
pseudo_tcp_fifo_get_buffered (PseudoTcpFifo *b)
{
return b->data_length;
}
static gboolean
pseudo_tcp_fifo_set_capacity (PseudoTcpFifo *b, gsize size)
{
if (b->data_length > size)
return FALSE;
if (size != b->data_length) {
guint8 *buffer = g_slice_alloc (size);
gsize copy = b->data_length;
gsize tail_copy = min (copy, b->buffer_length - b->read_position);
memcpy (buffer, &b->buffer[b->read_position], tail_copy);
memcpy (buffer + tail_copy, &b->buffer[0], copy - tail_copy);
g_slice_free1 (b->buffer_length, b->buffer);
b->buffer = buffer;
b->buffer_length = size;
b->read_position = 0;
}
return TRUE;
}
static void
pseudo_tcp_fifo_consume_read_data (PseudoTcpFifo *b, gsize size)
{
g_assert (size <= b->data_length);
b->read_position = (b->read_position + size) % b->buffer_length;
b->data_length -= size;
}
static void
pseudo_tcp_fifo_consume_write_buffer (PseudoTcpFifo *b, gsize size)
{
g_assert (size <= b->buffer_length - b->data_length);
b->data_length += size;
}
static gsize
pseudo_tcp_fifo_get_write_remaining (PseudoTcpFifo *b)
{
return b->buffer_length - b->data_length;
}
static gsize
pseudo_tcp_fifo_read_offset (PseudoTcpFifo *b, guint8 *buffer, gsize bytes,
gsize offset)
{
gsize available = b->data_length - offset;
gsize read_position = (b->read_position + offset) % b->buffer_length;
gsize copy = min (bytes, available);
gsize tail_copy = min(copy, b->buffer_length - read_position);
/* EOS */
if (offset >= b->data_length)
return 0;
memcpy(buffer, &b->buffer[read_position], tail_copy);
memcpy(buffer + tail_copy, &b->buffer[0], copy - tail_copy);
return copy;
}
static gsize
pseudo_tcp_fifo_write_offset (PseudoTcpFifo *b, const guint8 *buffer,
gsize bytes, gsize offset)
{
gsize available = b->buffer_length - b->data_length - offset;
gsize write_position = (b->read_position + b->data_length + offset)
% b->buffer_length;
gsize copy = min (bytes, available);
gsize tail_copy = min(copy, b->buffer_length - write_position);
if (b->data_length + offset >= b->buffer_length) {
return 0;
}
memcpy(&b->buffer[write_position], buffer, tail_copy);
memcpy(&b->buffer[0], buffer + tail_copy, copy - tail_copy);
return copy;
}
static gsize
pseudo_tcp_fifo_read (PseudoTcpFifo *b, guint8 *buffer, gsize bytes)
{
gsize copy;
copy = pseudo_tcp_fifo_read_offset (b, buffer, bytes, 0);
b->read_position = (b->read_position + copy) % b->buffer_length;
b->data_length -= copy;
return copy;
}
static gsize
pseudo_tcp_fifo_write (PseudoTcpFifo *b, const guint8 *buffer, gsize bytes)
{
gsize copy;
copy = pseudo_tcp_fifo_write_offset (b, buffer, bytes, 0);
b->data_length += copy;
return copy;
}
//////////////////////////////////////////////////////////////////////
// PseudoTcp
//////////////////////////////////////////////////////////////////////
/* Only used if FIN-ACK support is disabled. */
typedef enum {
SD_NONE,
SD_GRACEFUL,
SD_FORCEFUL
} Shutdown;
typedef enum {
sfNone,
sfDelayedAck,
sfImmediateAck,
sfFin,
sfRst,
sfDuplicateAck,
} SendFlags;
typedef struct {
guint32 conv, seq, ack;
TcpFlags flags;
guint16 wnd;
const gchar * data;
guint32 len;
guint32 tsval, tsecr;
} Segment;
typedef struct {
guint32 seq, len;
guint8 xmit;
TcpFlags flags;
} SSegment;
typedef struct {
guint32 seq, len;
} RSegment;
/**
* ClosedownSource:
* @CLOSEDOWN_LOCAL: Error detected locally, or connection forcefully closed
* locally.
* @CLOSEDOWN_REMOTE: RST segment received from the peer.
*
* Reasons for calling closedown().
*
* Since: 0.1.8
*/
typedef enum {
CLOSEDOWN_LOCAL,
CLOSEDOWN_REMOTE,
} ClosedownSource;
struct _PseudoTcpSocketPrivate {
PseudoTcpCallbacks callbacks;
Shutdown shutdown; /* only used if !support_fin_ack */
gboolean shutdown_reads;
gint error;
// TCB data
PseudoTcpState state;
guint32 conv;
gboolean bReadEnable, bWriteEnable, bOutgoing;
guint32 last_traffic;
// Incoming data
GList *rlist;
guint32 rbuf_len, rcv_nxt, rcv_wnd, lastrecv;
guint8 rwnd_scale; // Window scale factor
PseudoTcpFifo rbuf;
guint32 rcv_fin; /* sequence number of the received FIN octet, or 0 */
// Outgoing data
GQueue slist;
GQueue unsent_slist;
guint32 sbuf_len, snd_nxt, snd_wnd, lastsend;
guint32 snd_una; /* oldest unacknowledged sequence number */
guint8 swnd_scale; // Window scale factor
PseudoTcpFifo sbuf;
// Maximum segment size, estimated protocol level, largest segment sent
guint32 mss, msslevel, largest, mtu_advise;
// Retransmit timer
guint32 rto_base;
// Timestamp tracking
guint32 ts_recent, ts_lastack;
// Round-trip calculation
guint32 rx_rttvar, rx_srtt, rx_rto;
// Congestion avoidance, Fast retransmit/recovery, Delayed ACKs
guint32 ssthresh, cwnd;
guint8 dup_acks;
guint32 recover;
gboolean fast_recovery;
guint32 t_ack; /* time a delayed ack was scheduled; 0 if no acks scheduled */
guint32 last_acked_ts;
gboolean use_nagling;
guint32 ack_delay;
// This is used by unit tests to test backward compatibility of
// PseudoTcp implementations that don't support window scaling.
gboolean support_wnd_scale;
/* Current time. Typically only used for testing, when non-zero. When zero,
* the system monotonic clock is used. Units: monotonic milliseconds. */
guint32 current_time;
/* This is used by compatible implementations (with the TCP_OPT_FIN_ACK
* option) to enable correct FIN-ACK connection termination. Defaults to
* TRUE unless no compatible option is received. */
gboolean support_fin_ack;
};
#define LARGER(a,b) (((a) - (b) - 1) < (G_MAXUINT32 >> 1))
#define LARGER_OR_EQUAL(a,b) (((a) - (b)) < (G_MAXUINT32 >> 1))
#define SMALLER(a,b) LARGER ((b),(a))
#define SMALLER_OR_EQUAL(a,b) LARGER_OR_EQUAL ((b),(a))
/* properties */
enum
{
PROP_CONVERSATION = 1,
PROP_CALLBACKS,
PROP_STATE,
PROP_ACK_DELAY,
PROP_NO_DELAY,
PROP_RCV_BUF,
PROP_SND_BUF,
PROP_SUPPORT_FIN_ACK,
LAST_PROPERTY
};
static void pseudo_tcp_socket_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void pseudo_tcp_socket_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void pseudo_tcp_socket_finalize (GObject *object);
static void queue_connect_message (PseudoTcpSocket *self);
static guint32 queue (PseudoTcpSocket *self, const gchar *data,
guint32 len, TcpFlags flags);
static PseudoTcpWriteResult packet(PseudoTcpSocket *self, guint32 seq,
TcpFlags flags, guint32 offset, guint32 len, guint32 now);
static gboolean parse (PseudoTcpSocket *self,
const guint8 *_header_buf, gsize header_buf_len,
const guint8 *data_buf, gsize data_buf_len);
static gboolean process(PseudoTcpSocket *self, Segment *seg);
static int transmit(PseudoTcpSocket *self, SSegment *sseg, guint32 now);
static void attempt_send(PseudoTcpSocket *self, SendFlags sflags);
static void closedown (PseudoTcpSocket *self, guint32 err,
ClosedownSource source);
static void adjustMTU(PseudoTcpSocket *self);
static void parse_options (PseudoTcpSocket *self, const guint8 *data,
guint32 len);
static void resize_send_buffer (PseudoTcpSocket *self, guint32 new_size);
static void resize_receive_buffer (PseudoTcpSocket *self, guint32 new_size);
static void set_state (PseudoTcpSocket *self, PseudoTcpState new_state);
static void set_state_established (PseudoTcpSocket *self);
static void set_state_closed (PseudoTcpSocket *self, guint32 err);
static const gchar *pseudo_tcp_state_get_name (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_sent_fin (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_received_fin (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_received_fin_ack (PseudoTcpState state);
// The following logging is for detailed (packet-level) pseudotcp analysis only.
static PseudoTcpDebugLevel debug_level = PSEUDO_TCP_DEBUG_NONE;
#define DEBUG(level, fmt, ...) \
if (debug_level >= level) \
g_log (level == PSEUDO_TCP_DEBUG_NORMAL ? "libnice-pseudotcp" : "libnice-pseudotcp-verbose", G_LOG_LEVEL_DEBUG, "PseudoTcpSocket %p %s: " fmt, \
self, pseudo_tcp_state_get_name (self->priv->state), ## __VA_ARGS__)
void
pseudo_tcp_set_debug_level (PseudoTcpDebugLevel level)
{
debug_level = level;
}
static guint32
get_current_time (PseudoTcpSocket *socket)
{
if (G_UNLIKELY (socket->priv->current_time != 0))
return socket->priv->current_time;
return g_get_monotonic_time () / 1000;
}
void
pseudo_tcp_socket_set_time (PseudoTcpSocket *self, guint32 current_time)
{
self->priv->current_time = current_time;
}
static void
pseudo_tcp_socket_class_init (PseudoTcpSocketClass *cls)
{
GObjectClass *object_class = G_OBJECT_CLASS (cls);
object_class->get_property = pseudo_tcp_socket_get_property;
object_class->set_property = pseudo_tcp_socket_set_property;
object_class->finalize = pseudo_tcp_socket_finalize;
g_object_class_install_property (object_class, PROP_CONVERSATION,
g_param_spec_uint ("conversation", "TCP Conversation ID",
"The TCP Conversation ID",
0, G_MAXUINT32, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_CALLBACKS,
g_param_spec_pointer ("callbacks", "PseudoTcp socket callbacks",
"Structure with the callbacks to call when PseudoTcp events happen",
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_STATE,
g_param_spec_uint ("state", "PseudoTcp State",
"The current state (enum PseudoTcpState) of the PseudoTcp socket",
PSEUDO_TCP_LISTEN, PSEUDO_TCP_CLOSED, PSEUDO_TCP_LISTEN,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_ACK_DELAY,
g_param_spec_uint ("ack-delay", "ACK Delay",
"Delayed ACK timeout (in milliseconds)",
0, G_MAXUINT, DEFAULT_ACK_DELAY,
G_PARAM_READWRITE| G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_NO_DELAY,
g_param_spec_boolean ("no-delay", "No Delay",
"Disable the Nagle algorithm (like the TCP_NODELAY option)",
DEFAULT_NO_DELAY,
G_PARAM_READWRITE| G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_RCV_BUF,
g_param_spec_uint ("rcv-buf", "Receive Buffer",
"Receive Buffer size",
1, G_MAXUINT, DEFAULT_RCV_BUF_SIZE,
G_PARAM_READWRITE| G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_SND_BUF,
g_param_spec_uint ("snd-buf", "Send Buffer",
"Send Buffer size",
1, G_MAXUINT, DEFAULT_SND_BUF_SIZE,
G_PARAM_READWRITE| G_PARAM_STATIC_STRINGS));
/**
* PseudoTcpSocket:support-fin-ack:
*
* Whether to support the FIN–ACK extension to the pseudo-TCP protocol for
* this socket. The extension is only compatible with other libnice pseudo-TCP
* stacks, and not with Jingle pseudo-TCP stacks. If enabled, support is
* negotiatied on connection setup, so it is safe for a #PseudoTcpSocket with
* support enabled to be used with one with it disabled, or with a Jingle
* pseudo-TCP socket which doesn’t support it at all.
*
* Support is enabled by default.
*
* Since: 0.1.8
*/
g_object_class_install_property (object_class, PROP_SUPPORT_FIN_ACK,
g_param_spec_boolean ("support-fin-ack", "Support FIN–ACK",
"Whether to enable the optional FIN–ACK support.",
TRUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}
static void
pseudo_tcp_socket_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
PseudoTcpSocket *self = PSEUDO_TCP_SOCKET (object);
switch (property_id) {
case PROP_CONVERSATION:
g_value_set_uint (value, self->priv->conv);
break;
case PROP_CALLBACKS:
g_value_set_pointer (value, (gpointer) &self->priv->callbacks);
break;
case PROP_STATE:
g_value_set_uint (value, self->priv->state);
break;
case PROP_ACK_DELAY:
g_value_set_uint (value, self->priv->ack_delay);
break;
case PROP_NO_DELAY:
g_value_set_boolean (value, !self->priv->use_nagling);
break;
case PROP_RCV_BUF:
g_value_set_uint (value, self->priv->rbuf_len);
break;
case PROP_SND_BUF:
g_value_set_uint (value, self->priv->sbuf_len);
break;
case PROP_SUPPORT_FIN_ACK:
g_value_set_boolean (value, self->priv->support_fin_ack);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pseudo_tcp_socket_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
PseudoTcpSocket *self = PSEUDO_TCP_SOCKET (object);
switch (property_id) {
case PROP_CONVERSATION:
self->priv->conv = g_value_get_uint (value);
break;
case PROP_CALLBACKS:
{
PseudoTcpCallbacks *c = g_value_get_pointer (value);
self->priv->callbacks = *c;
}
break;
case PROP_ACK_DELAY:
self->priv->ack_delay = g_value_get_uint (value);
break;
case PROP_NO_DELAY:
self->priv->use_nagling = !g_value_get_boolean (value);
break;
case PROP_RCV_BUF:
g_return_if_fail (self->priv->state == PSEUDO_TCP_LISTEN);
resize_receive_buffer (self, g_value_get_uint (value));
break;
case PROP_SND_BUF:
g_return_if_fail (self->priv->state == PSEUDO_TCP_LISTEN);
resize_send_buffer (self, g_value_get_uint (value));
break;
case PROP_SUPPORT_FIN_ACK:
self->priv->support_fin_ack = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pseudo_tcp_socket_finalize (GObject *object)
{
PseudoTcpSocket *self = PSEUDO_TCP_SOCKET (object);
PseudoTcpSocketPrivate *priv = self->priv;
GList *i;
SSegment *sseg;
if (priv == NULL)
return;
while ((sseg = g_queue_pop_head (&priv->slist)))
g_slice_free (SSegment, sseg);
g_queue_clear (&priv->unsent_slist);
for (i = priv->rlist; i; i = i->next) {
RSegment *rseg = i->data;
g_slice_free (RSegment, rseg);
}
g_list_free (priv->rlist);
priv->rlist = NULL;
pseudo_tcp_fifo_clear (&priv->rbuf);
pseudo_tcp_fifo_clear (&priv->sbuf);
g_free (priv);
self->priv = NULL;
if (G_OBJECT_CLASS (pseudo_tcp_socket_parent_class)->finalize)
G_OBJECT_CLASS (pseudo_tcp_socket_parent_class)->finalize (object);
}
static void
pseudo_tcp_socket_init (PseudoTcpSocket *obj)
{
/* Use g_new0, and do not use g_object_set_private because the size of
* our private data is too big (150KB+) and the g_slice_allow cannot allocate
* it. So we handle the private ourselves */
PseudoTcpSocketPrivate *priv = g_new0 (PseudoTcpSocketPrivate, 1);
obj->priv = priv;
priv->shutdown = SD_NONE;
priv->error = 0;
priv->rbuf_len = DEFAULT_RCV_BUF_SIZE;
pseudo_tcp_fifo_init (&priv->rbuf, priv->rbuf_len);
priv->sbuf_len = DEFAULT_SND_BUF_SIZE;
pseudo_tcp_fifo_init (&priv->sbuf, priv->sbuf_len);
priv->state = PSEUDO_TCP_LISTEN;
priv->conv = 0;
g_queue_init (&priv->slist);
g_queue_init (&priv->unsent_slist);
priv->rcv_wnd = priv->rbuf_len;
priv->rwnd_scale = priv->swnd_scale = 0;
priv->snd_nxt = 0;
priv->snd_wnd = 1;
priv->snd_una = priv->rcv_nxt = 0;
priv->bReadEnable = TRUE;
priv->bWriteEnable = FALSE;
priv->rcv_fin = 0;
priv->t_ack = 0;
priv->msslevel = 0;
priv->largest = 0;
priv->mss = MIN_PACKET - PACKET_OVERHEAD;
priv->mtu_advise = DEF_MTU;
priv->rto_base = 0;
priv->cwnd = 2 * priv->mss;
priv->ssthresh = priv->rbuf_len;
priv->lastrecv = priv->lastsend = priv->last_traffic = 0;
priv->bOutgoing = FALSE;
priv->dup_acks = 0;
priv->recover = 0;
priv->last_acked_ts = 0;
priv->ts_recent = priv->ts_lastack = 0;
priv->rx_rto = DEF_RTO;
priv->rx_srtt = priv->rx_rttvar = 0;
priv->ack_delay = DEFAULT_ACK_DELAY;
priv->use_nagling = !DEFAULT_NO_DELAY;
priv->support_wnd_scale = TRUE;
priv->support_fin_ack = TRUE;
}
PseudoTcpSocket *pseudo_tcp_socket_new (guint32 conversation,
PseudoTcpCallbacks *callbacks)
{
return g_object_new (PSEUDO_TCP_SOCKET_TYPE,
"conversation", conversation,
"callbacks", callbacks,
NULL);
}
static void
queue_connect_message (PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
guint8 buf[8];
gsize size = 0;
buf[size++] = CTL_CONNECT;
if (priv->support_wnd_scale) {
buf[size++] = TCP_OPT_WND_SCALE;
buf[size++] = 1;
buf[size++] = priv->rwnd_scale;
}
if (priv->support_fin_ack) {
buf[size++] = TCP_OPT_FIN_ACK;
buf[size++] = 1; /* option length; zero is invalid (RFC 1122, §4.2.2.5) */
buf[size++] = 0; /* currently unused */
}
priv->snd_wnd = size;
queue (self, (char *) buf, size, FLAG_CTL);
}
static void
queue_fin_message (PseudoTcpSocket *self)
{
g_assert (self->priv->support_fin_ack);
/* FIN segments are always zero-length. */
queue (self, "", 0, FLAG_FIN);
}
static void
queue_rst_message (PseudoTcpSocket *self)
{
g_assert (self->priv->support_fin_ack);
/* RST segments are always zero-length. */
queue (self, "", 0, FLAG_RST);
}
gboolean
pseudo_tcp_socket_connect(PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
if (priv->state != PSEUDO_TCP_LISTEN) {
priv->error = EINVAL;
return FALSE;
}
set_state (self, PSEUDO_TCP_SYN_SENT);
queue_connect_message (self);
attempt_send(self, sfNone);
return TRUE;
}
void
pseudo_tcp_socket_notify_mtu(PseudoTcpSocket *self, guint16 mtu)
{
PseudoTcpSocketPrivate *priv = self->priv;
priv->mtu_advise = mtu;
if (priv->state == PSEUDO_TCP_ESTABLISHED) {
adjustMTU(self);
}
}
void
pseudo_tcp_socket_notify_clock(PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
guint32 now = get_current_time (self);
if (priv->state == PSEUDO_TCP_CLOSED)
return;
/* If in the TIME-WAIT state, any delayed segments have passed and the
* connection can be considered closed from both ends.
* FIXME: This should probably actually compare a timestamp before
* operating. */
if (priv->support_fin_ack && priv->state == PSEUDO_TCP_TIME_WAIT) {
DEBUG (PSEUDO_TCP_DEBUG_NORMAL,
"Notified clock in TIME-WAIT state; closing connection.");
set_state_closed (self, 0);
}
/* If in the LAST-ACK state, resend the FIN because it hasn’t been ACKed yet.
* FIXME: This should probably actually compare a timestamp before
* operating. */
if (priv->support_fin_ack && priv->state == PSEUDO_TCP_LAST_ACK) {
DEBUG (PSEUDO_TCP_DEBUG_NORMAL,
"Notified clock in LAST-ACK state; resending FIN segment.");
queue_fin_message (self);
attempt_send (self, sfFin);
}
// Check if it's time to retransmit a segment
if (priv->rto_base &&
(time_diff(priv->rto_base + priv->rx_rto, now) <= 0)) {
if (g_queue_get_length (&priv->slist) == 0) {
g_assert_not_reached ();
} else {
// Note: (priv->slist.front().xmit == 0)) {
// retransmit segments
guint32 nInFlight;
guint32 rto_limit;
int transmit_status;
DEBUG (PSEUDO_TCP_DEBUG_NORMAL, "timeout retransmit (rto: %u) "
"(rto_base: %u) (now: %u) (dup_acks: %u)",
priv->rx_rto, priv->rto_base, now, (guint) priv->dup_acks);
transmit_status = transmit(self, g_queue_peek_head (&priv->slist), now);
if (transmit_status != 0) {
DEBUG (PSEUDO_TCP_DEBUG_NORMAL,
"Error transmitting segment. Closing down.");
closedown (self, transmit_status, CLOSEDOWN_LOCAL);
return;
}
nInFlight = priv->snd_nxt - priv->snd_una;
priv->ssthresh = max(nInFlight / 2, 2 * priv->mss);
DEBUG (PSEUDO_TCP_DEBUG_NORMAL, "ssthresh: %u = (nInFlight: %u / 2) + "
"2 * mss: %u", priv->ssthresh, nInFlight, priv->mss);