forked from steve-o/openpgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv_unittest.c
1785 lines (1614 loc) · 49.4 KB
/
recv_unittest.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
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
*
* unit tests for transport recv api
*
* Copyright (c) 2009 Miru Limited.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#ifndef _WIN32
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h> /* _GNU_SOURCE for in6_pktinfo */
# include <arpa/inet.h>
#else
# include <ws2tcpip.h>
# include <mswsock.h>
#endif
#include <glib.h>
#include <check.h>
/* mock state */
#define TEST_NETWORK ""
#define TEST_DPORT 7500
#define TEST_SPORT 1000
#define TEST_XPORT 1001
#define TEST_SRC_ADDR "127.0.0.1"
#define TEST_END_ADDR "127.0.0.2"
#define TEST_GROUP_ADDR "239.192.0.1"
#define TEST_PEER_ADDR "127.0.0.6"
#define TEST_DLR_ADDR "127.0.0.9"
#define TEST_MAX_TPDU 1500
#define TEST_TXW_SQNS 32
#define TEST_RXW_SQNS 32
#define TEST_HOPS 16
#define TEST_SPM_AMBIENT ( pgm_secs(30) )
#define TEST_SPM_HEARTBEAT_INIT { pgm_msecs(100), pgm_msecs(100), pgm_msecs(100), pgm_msecs(100), pgm_msecs(1300), pgm_secs(7), pgm_secs(16), pgm_secs(25), pgm_secs(30) }
#define TEST_PEER_EXPIRY ( pgm_secs(300) )
#define TEST_SPMR_EXPIRY ( pgm_msecs(250) )
#define TEST_NAK_BO_IVL ( pgm_msecs(50) )
#define TEST_NAK_RPT_IVL ( pgm_secs(2) )
#define TEST_NAK_RDATA_IVL ( pgm_secs(2) )
#define TEST_NAK_DATA_RETRIES 5
#define TEST_NAK_NCF_RETRIES 2
struct mock_recvmsg_t {
#ifndef _WIN32
struct msghdr* mr_msg;
#else
WSAMSG* mr_msg;
#endif
ssize_t mr_retval;
int mr_errno;
};
struct pgm_peer_t;
GList* mock_recvmsg_list = NULL;
static int mock_pgm_type = -1;
static gboolean mock_reset_on_spmr = FALSE;
static gboolean mock_data_on_spmr = FALSE;
static struct pgm_peer_t* mock_peer = NULL;
GList* mock_data_list = NULL;
unsigned mock_pgm_loss_rate = 0;
#ifndef _WIN32
static ssize_t mock_recvmsg (int, struct msghdr*, int);
#else
static int mock_recvfrom (SOCKET, char*, int, int, struct sockaddr*, int*);
#endif
#define pgm_parse_raw mock_pgm_parse_raw
#define pgm_parse_udp_encap mock_pgm_parse_udp_encap
#define pgm_verify_spm mock_pgm_verify_spm
#define pgm_verify_nak mock_pgm_verify_nak
#define pgm_verify_ncf mock_pgm_verify_ncf
#define pgm_select_info mock_pgm_select_info
#define pgm_poll_info mock_pgm_poll_info
#define pgm_set_reset_error mock_pgm_set_reset_error
#define pgm_flush_peers_pending mock_pgm_flush_peers_pending
#define pgm_peer_has_pending mock_pgm_peer_has_pending
#define pgm_peer_set_pending mock_pgm_peer_set_pending
#define pgm_txw_retransmit_is_empty mock_pgm_txw_retransmit_is_empty
#define pgm_rxw_create mock_pgm_rxw_create
#define pgm_rxw_readv mock_pgm_rxw_readv
#define pgm_new_peer mock_pgm_new_peer
#define pgm_on_data mock_pgm_on_data
#define pgm_on_spm mock_pgm_on_spm
#define pgm_on_ack mock_pgm_on_ack
#define pgm_on_nak mock_pgm_on_nak
#define pgm_on_deferred_nak mock_pgm_on_deferred_nak
#define pgm_on_peer_nak mock_pgm_on_peer_nak
#define pgm_on_nnak mock_pgm_on_nnak
#define pgm_on_ncf mock_pgm_on_ncf
#define pgm_on_spmr mock_pgm_on_spmr
#define pgm_sendto mock_pgm_sendto
#define pgm_timer_prepare mock_pgm_timer_prepare
#define pgm_timer_check mock_pgm_timer_check
#define pgm_timer_expiration mock_pgm_timer_expiration
#define pgm_timer_dispatch mock_pgm_timer_dispatch
#define pgm_time_now mock_pgm_time_now
#define pgm_time_update_now mock_pgm_time_update_now
#define recvmsg mock_recvmsg
#define recvfrom mock_recvfrom
#define pgm_WSARecvMsg mock_pgm_WSARecvMsg
#define pgm_loss_rate mock_pgm_loss_rate
#define RECV_DEBUG
#include "recv.c"
pgm_rxw_t* mock_pgm_rxw_create (const pgm_tsi_t*, const uint16_t, const unsigned, const unsigned, const ssize_t, const uint32_t);
static pgm_time_t _mock_pgm_time_update_now (void);
pgm_time_update_func mock_pgm_time_update_now = _mock_pgm_time_update_now;
static
void
mock_setup (void)
{
if (!g_thread_supported ()) g_thread_init (NULL);
}
/* cleanup test state */
static
void
mock_teardown (void)
{
mock_recvmsg_list = NULL;
mock_pgm_type = -1;
mock_reset_on_spmr = FALSE;
mock_data_on_spmr = FALSE;
mock_peer = NULL;
mock_data_list = NULL;
mock_pgm_loss_rate = 0;
}
static
struct pgm_sock_t*
generate_sock (void)
{
const pgm_tsi_t tsi = { { 1, 2, 3, 4, 5, 6 }, g_htons((guint16)TEST_SPORT) };
struct pgm_sock_t* sock = g_new0 (struct pgm_sock_t, 1);
sock->window = g_new0 (pgm_txw_t, 1);
memcpy (&sock->tsi, &tsi, sizeof(pgm_tsi_t));
sock->is_nonblocking = TRUE;
sock->is_bound = TRUE;
sock->is_destroyed = FALSE;
sock->is_reset = FALSE;
sock->rx_buffer = pgm_alloc_skb (TEST_MAX_TPDU);
sock->max_tpdu = TEST_MAX_TPDU;
sock->rxw_sqns = TEST_RXW_SQNS;
sock->dport = g_htons((guint16)TEST_DPORT);
sock->can_send_data = TRUE;
sock->can_send_nak = TRUE;
sock->can_recv_data = TRUE;
sock->peers_hashtable = pgm_hashtable_new (pgm_tsi_hash, pgm_tsi_equal);
pgm_rand_create (&sock->rand_);
sock->nak_bo_ivl = 100*1000;
pgm_notify_init (&sock->pending_notify);
pgm_notify_init (&sock->rdata_notify);
pgm_mutex_init (&sock->receiver_mutex);
pgm_rwlock_init (&sock->lock);
pgm_rwlock_init (&sock->peers_lock);
return sock;
}
static
struct pgm_sk_buff_t*
generate_packet (void)
{
struct pgm_sk_buff_t* skb;
skb = pgm_alloc_skb (TEST_MAX_TPDU);
skb->data = skb->head;
skb->len = sizeof(struct pgm_ip) + sizeof(struct pgm_header);
skb->tail = (guint8*)skb->data + skb->len;
/* add IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_hl = sizeof(struct pgm_ip) / 4;
iphdr->ip_v = 4;
iphdr->ip_tos = 0;
iphdr->ip_id = 0;
iphdr->ip_off = 0;
iphdr->ip_ttl = 16;
iphdr->ip_p = IPPROTO_PGM;
iphdr->ip_sum = 0;
iphdr->ip_src.s_addr = inet_addr (TEST_SRC_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_GROUP_ADDR);
/* add PGM header */
struct pgm_header* pgmhdr = (gpointer)(iphdr + 1);
pgmhdr->pgm_sport = g_htons ((guint16)TEST_XPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_options = 0;
pgmhdr->pgm_gsi[0] = 1;
pgmhdr->pgm_gsi[1] = 2;
pgmhdr->pgm_gsi[2] = 3;
pgmhdr->pgm_gsi[3] = 4;
pgmhdr->pgm_gsi[4] = 5;
pgmhdr->pgm_gsi[5] = 6;
pgmhdr->pgm_tsdu_length = 0;
pgmhdr->pgm_checksum = 0;
skb->pgm_header = pgmhdr;
return skb;
}
static
void
generate_odata (
const char* source,
const guint source_len,
const guint32 data_sqn,
const guint32 data_trail,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_data) + source_len);
/* add ODATA header */
struct pgm_data* datahdr = (gpointer)(skb->pgm_header + 1);
datahdr->data_sqn = g_htonl (data_sqn);
datahdr->data_trail = g_htonl (data_trail);
/* add payload */
gpointer data = (gpointer)(datahdr + 1);
memcpy (data, source, source_len);
/* finalize PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_type = PGM_ODATA;
pgmhdr->pgm_tsdu_length = g_htons (source_len);
/* finalize IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_spm (
const guint32 spm_sqn,
const guint32 spm_trail,
const guint32 spm_lead,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_spm));
/* add SPM header */
struct pgm_spm* spm = (gpointer)(skb->pgm_header + 1);
spm->spm_sqn = g_htonl (spm_sqn);
spm->spm_trail = g_htonl (spm_trail);
spm->spm_lead = g_htonl (spm_lead);
/* finalize PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_type = PGM_SPM;
/* finalize IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_nak (
const guint32 nak_sqn,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_spm));
/* update IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_src.s_addr = inet_addr (TEST_END_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_SRC_ADDR);
/* update PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_sport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_SPORT);
/* add NAK header */
struct pgm_nak* nak = (gpointer)(skb->pgm_header + 1);
nak->nak_sqn = g_htonl (nak_sqn);
/* finalize PGM header */
pgmhdr->pgm_type = PGM_NAK;
/* finalize IP header */
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_peer_nak (
const guint32 nak_sqn,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_spm));
/* update IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_src.s_addr = inet_addr (TEST_PEER_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_GROUP_ADDR);
/* update PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_sport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_SPORT);
/* add NAK header */
struct pgm_nak* nak = (gpointer)(skb->pgm_header + 1);
nak->nak_sqn = g_htonl (nak_sqn);
/* finalize PGM header */
pgmhdr->pgm_type = PGM_NAK;
/* finalize IP header */
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_nnak (
const guint32 nak_sqn,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_nak));
/* update IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_src.s_addr = inet_addr (TEST_DLR_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_SRC_ADDR);
/* update PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_sport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_SPORT);
/* add NNAK header */
struct pgm_nak* nak = (gpointer)(skb->pgm_header + 1);
nak->nak_sqn = g_htonl (nak_sqn);
/* finalize PGM header */
pgmhdr->pgm_type = PGM_NNAK;
/* finalize IP header */
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_ncf (
const guint32 nak_sqn,
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
pgm_skb_put (skb, sizeof(struct pgm_nak));
/* add NAK header */
struct pgm_nak* nak = (gpointer)(skb->pgm_header + 1);
nak->nak_sqn = g_htonl (nak_sqn);
/* finalize PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_type = PGM_NCF;
/* finalize IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_spmr (
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
/* update IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_src.s_addr = inet_addr (TEST_END_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_SRC_ADDR);
/* update PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_sport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_SPORT);
/* finalize PGM header */
pgmhdr->pgm_type = PGM_SPMR;
/* finalize IP header */
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_peer_spmr (
gpointer* packet,
gsize* len
)
{
struct pgm_sk_buff_t* skb = generate_packet ();
/* update IP header */
struct pgm_ip* iphdr = skb->data;
iphdr->ip_src.s_addr = inet_addr (TEST_PEER_ADDR);
iphdr->ip_dst.s_addr = inet_addr (TEST_GROUP_ADDR);
/* update PGM header */
struct pgm_header* pgmhdr = skb->pgm_header;
pgmhdr->pgm_sport = g_htons ((guint16)TEST_DPORT);
pgmhdr->pgm_dport = g_htons ((guint16)TEST_SPORT);
/* finalize PGM header */
pgmhdr->pgm_type = PGM_SPMR;
/* finalize IP header */
iphdr->ip_len = g_htons (skb->len);
*packet = skb->head;
*len = skb->len;
}
static
void
generate_msghdr (
const gpointer packet,
const gsize packet_len
)
{
struct pgm_ip* iphdr = packet;
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = iphdr->ip_src.s_addr
};
struct pgm_iovec iov = {
.iov_base = packet,
.iov_len = packet_len
};
struct pgm_cmsghdr* packet_cmsg = g_malloc0 (sizeof(struct pgm_cmsghdr) + sizeof(struct in_pktinfo));
packet_cmsg->cmsg_len = sizeof(*packet_cmsg) + sizeof(struct in_pktinfo);
packet_cmsg->cmsg_level = IPPROTO_IP;
packet_cmsg->cmsg_type = IP_PKTINFO;
struct in_pktinfo packet_info = {
.ipi_ifindex = 2,
#if !defined(_WIN32) && !defined(__CYGWIN__)
.ipi_spec_dst = iphdr->ip_src.s_addr, /* local address */
#endif
.ipi_addr = iphdr->ip_dst.s_addr /* destination address */
};
memcpy ((char*)(packet_cmsg + 1), &packet_info, sizeof(struct in_pktinfo));
#ifndef _WIN32
struct msghdr packet_msg = {
.msg_name = g_memdup (&addr, sizeof(addr)), /* source address */
.msg_namelen = sizeof(addr),
.msg_iov = g_memdup (&iov, sizeof(iov)),
.msg_iovlen = 1,
.msg_control = &packet_cmsg,
.msg_controllen = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo),
.msg_flags = 0
};
#else
WSAMSG packet_msg = {
.name = (LPSOCKADDR)g_memdup (&addr, sizeof(addr)),
.namelen = sizeof(addr),
.lpBuffers = (LPWSABUF)g_memdup (&iov, sizeof(iov)),
.dwBufferCount = 1,
.dwFlags = 0
};
packet_msg.Control.buf = (char*)&packet_cmsg;
packet_msg.Control.len = sizeof(struct pgm_cmsghdr) + sizeof(struct in_pktinfo);
#endif
struct mock_recvmsg_t* mr = g_malloc (sizeof(struct mock_recvmsg_t));
mr->mr_msg = g_memdup (&packet_msg, sizeof(packet_msg));
mr->mr_errno = 0;
mr->mr_retval = packet_len;
mock_recvmsg_list = g_list_append (mock_recvmsg_list, mr);
}
static
void
push_block_event (void)
{
/* block */
struct mock_recvmsg_t* mr = g_malloc (sizeof(struct mock_recvmsg_t));
mr->mr_msg = NULL;
mr->mr_errno = PGM_SOCK_EAGAIN;
mr->mr_retval = SOCKET_ERROR;
mock_recvmsg_list = g_list_append (mock_recvmsg_list, mr);
}
/** packet module */
bool
mock_pgm_parse_raw (
struct pgm_sk_buff_t* const skb,
struct sockaddr* const dst,
pgm_error_t** error
)
{
const struct pgm_ip* ip = (struct pgm_ip*)skb->data;
struct sockaddr_in* sin = (struct sockaddr_in*)dst;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = ip->ip_dst.s_addr;
const gsize ip_header_length = ip->ip_hl * 4;
skb->pgm_header = (gpointer)( (guint8*)skb->data + ip_header_length );
skb->data = skb->pgm_header;
skb->len -= ip_header_length;
memcpy (&skb->tsi.gsi, skb->pgm_header->pgm_gsi, sizeof(pgm_gsi_t));
skb->tsi.sport = skb->pgm_header->pgm_sport;
return TRUE;
}
bool
mock_pgm_parse_udp_encap (
struct pgm_sk_buff_t* const skb,
pgm_error_t** error
)
{
skb->pgm_header = skb->data;
memcpy (&skb->tsi.gsi, skb->pgm_header->pgm_gsi, sizeof(pgm_gsi_t));
skb->tsi.sport = skb->pgm_header->pgm_sport;
return TRUE;
}
bool
mock_pgm_verify_spm (
const struct pgm_sk_buff_t* const skb
)
{
return TRUE;
}
bool
mock_pgm_verify_nak (
const struct pgm_sk_buff_t* const skb
)
{
return TRUE;
}
bool
mock_pgm_verify_ncf (
const struct pgm_sk_buff_t* const skb
)
{
return TRUE;
}
/** socket module */
#ifdef HAVE_POLL
int
mock_pgm_poll_info (
pgm_sock_t* const sock,
struct pollfd* fds,
int* n_fds,
short events
)
{
}
#else
int
mock_pgm_select_info (
pgm_sock_t* const sock,
fd_set*const readfds,
fd_set*const writefds,
int*const n_fds
)
{
}
#endif
pgm_peer_t*
mock__pgm_peer_ref (
pgm_peer_t* peer
)
{
pgm_atomic_inc32 (&peer->ref_count);
return peer;
}
PGM_GNUC_INTERNAL
pgm_peer_t*
mock_pgm_new_peer (
pgm_sock_t* const sock,
const pgm_tsi_t* const tsi,
const struct sockaddr* const src_addr,
const socklen_t src_addr_len,
const struct sockaddr* const dst_addr,
const socklen_t dst_addr_len,
const pgm_time_t now
)
{
pgm_peer_t* peer = g_malloc0 (sizeof(pgm_peer_t));
peer->expiry = now + sock->peer_expiry;
memcpy (&peer->tsi, tsi, sizeof(pgm_tsi_t));
memcpy (&peer->group_nla, dst_addr, dst_addr_len);
memcpy (&peer->local_nla, src_addr, src_addr_len);
((struct sockaddr_in*)&peer->local_nla)->sin_port = g_htons (sock->udp_encap_ucast_port);
((struct sockaddr_in*)&peer->nla)->sin_port = g_htons (sock->udp_encap_ucast_port);
peer->window = mock_pgm_rxw_create (&peer->tsi,
sock->max_tpdu,
sock->rxw_sqns,
sock->rxw_secs,
sock->rxw_max_rte,
sock->ack_c_p);
peer->spmr_expiry = now + sock->spmr_expiry;
gpointer entry = mock__pgm_peer_ref(peer);
pgm_hashtable_insert (sock->peers_hashtable, &peer->tsi, entry);
peer->peers_link.next = sock->peers_list;
peer->peers_link.data = peer;
if (sock->peers_list)
sock->peers_list->prev = &peer->peers_link;
sock->peers_list = &peer->peers_link;
return peer;
}
PGM_GNUC_INTERNAL
void
mock_pgm_set_reset_error (
pgm_sock_t* const sock,
pgm_peer_t* const source,
struct pgm_msgv_t* const msgv
)
{
}
PGM_GNUC_INTERNAL
int
mock_pgm_flush_peers_pending (
pgm_sock_t* const sock,
struct pgm_msgv_t** pmsg,
const struct pgm_msgv_t* const msg_end,
size_t* const bytes_read,
unsigned* const data_read
)
{
if (mock_data_list) {
size_t len = 0;
unsigned count = 0;
while (mock_data_list && *pmsg <= msg_end) {
struct pgm_msgv_t* mock_msgv = mock_data_list->data;
(*pmsg)->msgv_len = mock_msgv->msgv_len;
for (unsigned i = 0; i < mock_msgv->msgv_len; i++) {
(*pmsg)->msgv_skb[i] = mock_msgv->msgv_skb[i];
len += mock_msgv->msgv_skb[i]->len;
}
count++;
(*pmsg)++;
mock_data_list = g_list_delete_link (mock_data_list, mock_data_list);
}
*bytes_read = len;
*data_read = count;
if (*pmsg > msg_end)
return -PGM_SOCK_ENOBUFS;
}
return 0;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_peer_has_pending (
pgm_peer_t* const peer
)
{
return FALSE;
}
PGM_GNUC_INTERNAL
void
mock_pgm_peer_set_pending (
pgm_sock_t* const sock,
pgm_peer_t* const peer
)
{
g_assert (NULL != sock);
g_assert (NULL != peer);
if (peer->pending_link.data) return;
peer->pending_link.data = peer;
peer->pending_link.next = sock->peers_pending;
sock->peers_pending = &peer->pending_link;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_data (
pgm_sock_t* const sock,
pgm_peer_t* const sender,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_data (sock:%p sender:%p skb:%p)",
(gpointer)sock, (gpointer)sender, (gpointer)skb);
mock_pgm_type = PGM_ODATA;
((pgm_rxw_t*)sender->window)->has_event = 1;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_ack (
pgm_sock_t* const sock,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_ack (sock:%p skb:%p)",
(gpointer)sock, (gpointer)skb);
mock_pgm_type = PGM_ACK;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_deferred_nak (
pgm_sock_t* const sock
)
{
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_nak (
pgm_sock_t* const sock,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_nak (sock:%p skb:%p)",
(gpointer)sock, (gpointer)skb);
mock_pgm_type = PGM_NAK;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_peer_nak (
pgm_sock_t* const sock,
pgm_peer_t* const sender,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_peer_nak (sock:%p sender:%p skb:%p)",
(gpointer)sock, (gpointer)sender, (gpointer)skb);
mock_pgm_type = PGM_NAK;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_ncf (
pgm_sock_t* const sock,
pgm_peer_t* const sender,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_ncf (sock:%p sender:%p skb:%p)",
(gpointer)sock, (gpointer)sender, (gpointer)skb);
mock_pgm_type = PGM_NCF;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_nnak (
pgm_sock_t* const sock,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_nnak (sock:%p skb:%p)",
(gpointer)sock, (gpointer)skb);
mock_pgm_type = PGM_NNAK;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_spm (
pgm_sock_t* const sock,
pgm_peer_t* const sender,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_spm (sock:%p sender:%p skb:%p)",
(gpointer)sock, (gpointer)sender, (gpointer)skb);
mock_pgm_type = PGM_SPM;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_on_spmr (
pgm_sock_t* const sock,
pgm_peer_t* const peer,
struct pgm_sk_buff_t* const skb
)
{
g_debug ("mock_pgm_on_spmr (sock:%p peer:%p skb:%p)",
(gpointer)sock, (gpointer)peer, (gpointer)skb);
mock_pgm_type = PGM_SPMR;
if (mock_reset_on_spmr) {
sock->is_reset = TRUE;
mock_pgm_peer_set_pending (sock, mock_peer);
}
if (mock_data_on_spmr) {
mock_pgm_peer_set_pending (sock, mock_peer);
}
return TRUE;
}
/** transmit window */
PGM_GNUC_INTERNAL
bool
mock_pgm_txw_retransmit_is_empty (
const pgm_txw_t*const window
)
{
return TRUE;
}
/** receive window */
pgm_rxw_t*
mock_pgm_rxw_create (
const pgm_tsi_t* tsi,
const uint16_t tpdu_size,
const unsigned sqns,
const unsigned secs,
const ssize_t max_rte,
const uint32_t ack_c_p
)
{
return g_new0 (pgm_rxw_t, 1);
}
ssize_t
mock_pgm_rxw_readv (
pgm_rxw_t* const window,
struct pgm_msgv_t** pmsg,
const unsigned pmsglen
)
{
return -1;
}
/** net module */
PGM_GNUC_INTERNAL
ssize_t
mock_pgm_sendto (
pgm_sock_t* sock,
bool use_rate_limit,
bool use_router_alert,
const void* buf,
size_t len,
const struct sockaddr* to,
socklen_t tolen
)
{
return len;
}
/** timer module */
PGM_GNUC_INTERNAL
bool
mock_pgm_timer_prepare (
pgm_sock_t* const sock
)
{
return FALSE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_timer_check (
pgm_sock_t* const sock
)
{
return FALSE;
}
PGM_GNUC_INTERNAL
pgm_time_t
mock_pgm_timer_expiration (
pgm_sock_t* const sock
)
{
return 100L;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_timer_dispatch (
pgm_sock_t* const sock
)
{
return TRUE;
}
/** time module */
static pgm_time_t mock_pgm_time_now = 0x1;
static
pgm_time_t
_mock_pgm_time_update_now (void)
{
return mock_pgm_time_now;
}
/** libc */
#ifndef _WIN32
static
ssize_t
mock_recvmsg (
int s,
struct msghdr* msg,
int flags
)
{
g_assert (NULL != msg);
g_assert (NULL != mock_recvmsg_list);
g_debug ("mock_recvmsg (s:%d msg:%p flags:%d)",
s, (gpointer)msg, flags);
struct mock_recvmsg_t* mr = mock_recvmsg_list->data;
struct msghdr* mock_msg = mr->mr_msg;
ssize_t mock_retval = mr->mr_retval;
int mock_errno = mr->mr_errno;
mock_recvmsg_list = g_list_delete_link (mock_recvmsg_list, mock_recvmsg_list);
if (mock_msg) {
g_assert_cmpuint (mock_msg->msg_namelen, <=, msg->msg_namelen);
g_assert_cmpuint (mock_msg->msg_iovlen, <=, msg->msg_iovlen);
g_assert_cmpuint (mock_msg->msg_controllen, <=, msg->msg_controllen);
if (mock_msg->msg_namelen)
memcpy (msg->msg_name, mock_msg->msg_name, mock_msg->msg_namelen);
if (mock_msg->msg_iovlen) {
for (unsigned i = 0; i < mock_msg->msg_iovlen; i++) {