-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathconnection.c
1923 lines (1687 loc) · 79.9 KB
/
connection.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdlib.h>
#include <string.h>
#include "azure_c_shared_utility/optimize_size.h"
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/xio.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/tickcounter.h"
#include "azure_uamqp_c/connection.h"
#include "azure_uamqp_c/frame_codec.h"
#include "azure_uamqp_c/amqp_frame_codec.h"
#include "azure_uamqp_c/amqp_definitions.h"
#include "azure_uamqp_c/amqpvalue_to_string.h"
/* Requirements satisfied by the virtue of implementing the ISO:*/
/* Codes_SRS_CONNECTION_01_088: [Any data appearing beyond the protocol header MUST match the version indicated by the protocol header.] */
/* Codes_SRS_CONNECTION_01_015: [Implementations SHOULD NOT expect to be able to reuse open TCP sockets after close performatives have been exchanged.] */
/* Codes_SRS_CONNECTION_01_087: [The protocol header consists of the upper case ASCII letters "AMQP" followed by a protocol id of zero, followed by three unsigned bytes representing the major, minor, and revision of the protocol version (currently 1 (MAJOR), 0 (MINOR), 0 (REVISION)). In total this is an 8-octet sequence] */
static const unsigned char amqp_header[] = { 'A', 'M', 'Q', 'P', 0, 1, 0, 0 };
typedef enum RECEIVE_FRAME_STATE_TAG
{
RECEIVE_FRAME_STATE_FRAME_SIZE,
RECEIVE_FRAME_STATE_FRAME_DATA
} RECEIVE_FRAME_STATE;
typedef struct ENDPOINT_INSTANCE_TAG
{
uint16_t incoming_channel;
uint16_t outgoing_channel;
ON_ENDPOINT_FRAME_RECEIVED on_endpoint_frame_received;
ON_CONNECTION_STATE_CHANGED on_connection_state_changed;
void* callback_context;
CONNECTION_HANDLE connection;
} ENDPOINT_INSTANCE;
typedef struct CONNECTION_INSTANCE_TAG
{
XIO_HANDLE io;
size_t header_bytes_received;
CONNECTION_STATE connection_state;
FRAME_CODEC_HANDLE frame_codec;
AMQP_FRAME_CODEC_HANDLE amqp_frame_codec;
ENDPOINT_INSTANCE** endpoints;
uint32_t endpoint_count;
char* host_name;
char* container_id;
TICK_COUNTER_HANDLE tick_counter;
uint32_t remote_max_frame_size;
ON_SEND_COMPLETE on_send_complete;
void* on_send_complete_callback_context;
ON_NEW_ENDPOINT on_new_endpoint;
void* on_new_endpoint_callback_context;
ON_CONNECTION_STATE_CHANGED on_connection_state_changed;
void* on_connection_state_changed_callback_context;
ON_IO_ERROR on_io_error;
void* on_io_error_callback_context;
/* options */
uint32_t max_frame_size;
uint16_t channel_max;
milliseconds idle_timeout;
milliseconds remote_idle_timeout;
milliseconds remote_idle_timeout_send_frame_millisecond;
double idle_timeout_empty_frame_send_ratio;
tickcounter_ms_t last_frame_received_time;
tickcounter_ms_t last_frame_sent_time;
unsigned int is_underlying_io_open : 1;
unsigned int idle_timeout_specified : 1;
unsigned int is_remote_frame_received : 1;
unsigned int is_trace_on : 1;
} CONNECTION_INSTANCE;
/* Codes_SRS_CONNECTION_01_258: [on_connection_state_changed shall be invoked whenever the connection state changes.]*/
static void connection_set_state(CONNECTION_HANDLE connection, CONNECTION_STATE connection_state)
{
uint64_t i;
CONNECTION_STATE previous_state = connection->connection_state;
connection->connection_state = connection_state;
/* Codes_SRS_CONNECTION_22_001: [If a connection state changed occurs and a callback is registered the callback shall be called.] */
if (connection->on_connection_state_changed)
{
connection->on_connection_state_changed(connection->on_connection_state_changed_callback_context, connection_state, previous_state);
}
/* Codes_SRS_CONNECTION_01_260: [Each endpoint's on_connection_state_changed shall be called.] */
for (i = 0; i < connection->endpoint_count; i++)
{
/* Codes_SRS_CONNECTION_01_259: [The callback_context passed in connection_create_endpoint.] */
connection->endpoints[i]->on_connection_state_changed(connection->endpoints[i]->callback_context, connection_state, previous_state);
}
}
// This callback usage needs to be either verified and commented or integrated into
// the state machine.
static void unchecked_on_send_complete(void* context, IO_SEND_RESULT send_result)
{
(void)context;
(void)send_result;
}
static int send_header(CONNECTION_HANDLE connection)
{
int result;
/* Codes_SRS_CONNECTION_01_093: [_ When the client opens a new socket connection to a server, it MUST send a protocol header with the client's preferred protocol version.] */
/* Codes_SRS_CONNECTION_01_104: [Sending the protocol header shall be done by using xio_send.] */
if (xio_send(connection->io, amqp_header, sizeof(amqp_header), unchecked_on_send_complete, NULL) != 0)
{
/* Codes_SRS_CONNECTION_01_106: [When sending the protocol header fails, the connection shall be immediately closed.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
/* Codes_SRS_CONNECTION_01_057: [END In this state it is illegal for either endpoint to write anything more onto the connection. The connection can be safely closed and discarded.] */
connection_set_state(connection, CONNECTION_STATE_END);
/* Codes_SRS_CONNECTION_01_105: [When xio_send fails, connection_dowork shall return a non-zero value.] */
result = __FAILURE__;
}
else
{
if (connection->is_trace_on == 1)
{
LOG(AZ_LOG_TRACE, LOG_LINE, "-> Header (AMQP 0.1.0.0)");
}
/* Codes_SRS_CONNECTION_01_041: [HDR SENT In this state the connection header has been sent to the peer but no connection header has been received.] */
connection_set_state(connection, CONNECTION_STATE_HDR_SENT);
result = 0;
}
return result;
}
static const char* get_frame_type_as_string(AMQP_VALUE descriptor)
{
const char* result;
if (is_open_type_by_descriptor(descriptor))
{
result = "[OPEN]";
}
else if (is_begin_type_by_descriptor(descriptor))
{
result = "[BEGIN]";
}
else if (is_attach_type_by_descriptor(descriptor))
{
result = "[ATTACH]";
}
else if (is_flow_type_by_descriptor(descriptor))
{
result = "[FLOW]";
}
else if (is_disposition_type_by_descriptor(descriptor))
{
result = "[DISPOSITION]";
}
else if (is_transfer_type_by_descriptor(descriptor))
{
result = "[TRANSFER]";
}
else if (is_detach_type_by_descriptor(descriptor))
{
result = "[DETACH]";
}
else if (is_end_type_by_descriptor(descriptor))
{
result = "[END]";
}
else if (is_close_type_by_descriptor(descriptor))
{
result = "[CLOSE]";
}
else
{
result = "[Unknown]";
}
return result;
}
static void log_incoming_frame(AMQP_VALUE performative)
{
#ifdef NO_LOGGING
UNUSED(performative);
#else
AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative);
if (descriptor == NULL)
{
LogError("Error getting performative descriptor");
}
else
{
char* performative_as_string;
LOG(AZ_LOG_TRACE, 0, "<- ");
LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor));
performative_as_string = NULL;
LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative)));
if (performative_as_string != NULL)
{
free(performative_as_string);
}
}
#endif
}
static void log_outgoing_frame(AMQP_VALUE performative)
{
#ifdef NO_LOGGING
UNUSED(performative);
#else
AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative);
if (descriptor == NULL)
{
LogError("Error getting performative descriptor");
}
else
{
char* performative_as_string;
LOG(AZ_LOG_TRACE, 0, "-> ");
LOG(AZ_LOG_TRACE, 0, (char*)get_frame_type_as_string(descriptor));
performative_as_string = NULL;
LOG(AZ_LOG_TRACE, LOG_LINE, (performative_as_string = amqpvalue_to_string(performative)));
if (performative_as_string != NULL)
{
free(performative_as_string);
}
}
#endif
}
static void on_bytes_encoded(void* context, const unsigned char* bytes, size_t length, bool encode_complete)
{
CONNECTION_HANDLE connection = (CONNECTION_HANDLE)context;
if (xio_send(connection->io, bytes, length, encode_complete ? connection->on_send_complete : NULL, connection->on_send_complete_callback_context) != 0)
{
LogError("Cannot send encoded bytes");
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
}
}
static int send_open_frame(CONNECTION_HANDLE connection)
{
int result;
/* Codes_SRS_CONNECTION_01_151: [The connection max_frame_size setting shall be passed down to the frame_codec when the Open frame is sent.] */
if (frame_codec_set_max_frame_size(connection->frame_codec, connection->max_frame_size) != 0)
{
LogError("Cannot set max frame size");
/* Codes_SRS_CONNECTION_01_207: [If frame_codec_set_max_frame_size fails the connection shall be closed and the state set to END.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
/* Codes_SRS_CONNECTION_01_134: [The container id field shall be filled with the container id specified in connection_create.] */
OPEN_HANDLE open_performative = open_create(connection->container_id);
if (open_performative == NULL)
{
LogError("Cannot create OPEN performative");
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
/* Codes_SRS_CONNECTION_01_137: [The max_frame_size connection setting shall be set in the open frame by using open_set_max_frame_size.] */
if (open_set_max_frame_size(open_performative, connection->max_frame_size) != 0)
{
LogError("Cannot set max frame size");
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
/* Codes_SRS_CONNECTION_01_139: [The channel_max connection setting shall be set in the open frame by using open_set_channel_max.] */
else if (open_set_channel_max(open_performative, connection->channel_max) != 0)
{
LogError("Cannot set max channel");
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
/* Codes_SRS_CONNECTION_01_142: [If no idle_timeout value has been specified, no value shall be stamped in the open frame (no call to open_set_idle_time_out shall be made).] */
else if ((connection->idle_timeout_specified) &&
/* Codes_SRS_CONNECTION_01_141: [If idle_timeout has been specified by a call to connection_set_idle_timeout, then that value shall be stamped in the open frame.] */
(open_set_idle_time_out(open_performative, connection->idle_timeout) != 0))
{
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
/* Codes_SRS_CONNECTION_01_136: [If no hostname value has been specified, no value shall be stamped in the open frame (no call to open_set_hostname shall be made).] */
else if ((connection->host_name != NULL) &&
/* Codes_SRS_CONNECTION_01_135: [If hostname has been specified by a call to connection_set_hostname, then that value shall be stamped in the open frame.] */
(open_set_hostname(open_performative, connection->host_name) != 0))
{
LogError("Cannot set hostname");
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
AMQP_VALUE open_performative_value = amqpvalue_create_open(open_performative);
if (open_performative_value == NULL)
{
LogError("Cannot create OPEN AMQP value");
/* Codes_SRS_CONNECTION_01_208: [If the open frame cannot be constructed, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
/* Codes_SRS_CONNECTION_01_002: [Each AMQP connection begins with an exchange of capabilities and limitations, including the maximum frame size.] */
/* Codes_SRS_CONNECTION_01_004: [After establishing or accepting a TCP connection and sending the protocol header, each peer MUST send an open frame before sending any other frames.] */
/* Codes_SRS_CONNECTION_01_005: [The open frame describes the capabilities and limits of that peer.] */
/* Codes_SRS_CONNECTION_01_205: [Sending the AMQP OPEN frame shall be done by calling amqp_frame_codec_begin_encode_frame with channel number 0, the actual performative payload and 0 as payload_size.] */
/* Codes_SRS_CONNECTION_01_006: [The open frame can only be sent on channel 0.] */
connection->on_send_complete = NULL;
connection->on_send_complete_callback_context = NULL;
if (amqp_frame_codec_encode_frame(connection->amqp_frame_codec, 0, open_performative_value, NULL, 0, on_bytes_encoded, connection) != 0)
{
LogError("amqp_frame_codec_encode_frame failed");
/* Codes_SRS_CONNECTION_01_206: [If sending the frame fails, the connection shall be closed and state set to END.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
if (connection->is_trace_on == 1)
{
log_outgoing_frame(open_performative_value);
}
/* Codes_SRS_CONNECTION_01_046: [OPEN SENT In this state the connection headers have been exchanged. An open frame has been sent to the peer but no open frame has yet been received.] */
connection_set_state(connection, CONNECTION_STATE_OPEN_SENT);
result = 0;
}
amqpvalue_destroy(open_performative_value);
}
}
open_destroy(open_performative);
}
}
return result;
}
static int send_close_frame(CONNECTION_HANDLE connection, ERROR_HANDLE error_handle)
{
int result;
CLOSE_HANDLE close_performative;
/* Codes_SRS_CONNECTION_01_217: [The CLOSE frame shall be constructed by using close_create.] */
close_performative = close_create();
if (close_performative == NULL)
{
LogError("Cannot create close performative");
result = __FAILURE__;
}
else
{
if ((error_handle != NULL) &&
/* Codes_SRS_CONNECTION_01_238: [If set, this field indicates that the connection is being closed due to an error condition.] */
(close_set_error(close_performative, error_handle) != 0))
{
LogError("Cannot set error on CLOSE");
result = __FAILURE__;
}
else
{
AMQP_VALUE close_performative_value = amqpvalue_create_close(close_performative);
if (close_performative_value == NULL)
{
LogError("Cannot create AMQP CLOSE performative value");
result = __FAILURE__;
}
else
{
/* Codes_SRS_CONNECTION_01_215: [Sending the AMQP CLOSE frame shall be done by calling amqp_frame_codec_begin_encode_frame with channel number 0, the actual performative payload and 0 as payload_size.] */
/* Codes_SRS_CONNECTION_01_013: [However, implementations SHOULD send it on channel 0] */
connection->on_send_complete = NULL;
connection->on_send_complete_callback_context = NULL;
if (amqp_frame_codec_encode_frame(connection->amqp_frame_codec, 0, close_performative_value, NULL, 0, on_bytes_encoded, connection) != 0)
{
LogError("amqp_frame_codec_encode_frame failed");
result = __FAILURE__;
}
else
{
if (connection->is_trace_on == 1)
{
log_outgoing_frame(close_performative_value);
}
result = 0;
}
amqpvalue_destroy(close_performative_value);
}
}
close_destroy(close_performative);
}
return result;
}
static void close_connection_with_error(CONNECTION_HANDLE connection, const char* condition_value, const char* description)
{
ERROR_HANDLE error_handle = error_create(condition_value);
if (error_handle == NULL)
{
/* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
}
else
{
/* Codes_SRS_CONNECTION_01_219: [The error description shall be set to an implementation defined string.] */
if (error_set_description(error_handle, description) != 0)
{
LogError("Cannot set error description on CLOSE frame");
/* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
}
else if (send_close_frame(connection, error_handle) != 0)
{
LogError("Cannot send CLOSE frame");
/* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
}
else
{
/* Codes_SRS_CONNECTION_01_213: [When passing the bytes to frame_codec fails, a CLOSE frame shall be sent and the state shall be set to DISCARDING.] */
/* Codes_SRS_CONNECTION_01_055: [DISCARDING The DISCARDING state is a variant of the CLOSE SENT state where the close is triggered by an error.] */
/* Codes_SRS_CONNECTION_01_010: [After writing this frame the peer SHOULD continue to read from the connection until it receives the partner's close frame ] */
connection_set_state(connection, CONNECTION_STATE_DISCARDING);
}
error_destroy(error_handle);
}
}
static ENDPOINT_INSTANCE* find_session_endpoint_by_outgoing_channel(CONNECTION_HANDLE connection, uint16_t outgoing_channel)
{
uint32_t i;
ENDPOINT_INSTANCE* result;
for (i = 0; i < connection->endpoint_count; i++)
{
if (connection->endpoints[i]->outgoing_channel == outgoing_channel)
{
break;
}
}
if (i == connection->endpoint_count)
{
LogError("Cannot find session endpoint for channel %u", (unsigned int)outgoing_channel);
result = NULL;
}
else
{
result = connection->endpoints[i];
}
return result;
}
static ENDPOINT_INSTANCE* find_session_endpoint_by_incoming_channel(CONNECTION_HANDLE connection, uint16_t incoming_channel)
{
uint32_t i;
ENDPOINT_INSTANCE* result;
for (i = 0; i < connection->endpoint_count; i++)
{
if (connection->endpoints[i]->incoming_channel == incoming_channel)
{
break;
}
}
if (i == connection->endpoint_count)
{
LogError("Cannot find session endpoint for channel %u", (unsigned int)incoming_channel);
result = NULL;
}
else
{
result = connection->endpoints[i];
}
return result;
}
static int connection_byte_received(CONNECTION_HANDLE connection, unsigned char b)
{
int result;
switch (connection->connection_state)
{
default:
LogError("Unknown connection state: %d", (int)connection->connection_state);
result = __FAILURE__;
break;
/* Codes_SRS_CONNECTION_01_039: [START In this state a connection exists, but nothing has been sent or received. This is the state an implementation would be in immediately after performing a socket connect or socket accept.] */
case CONNECTION_STATE_START:
/* Codes_SRS_CONNECTION_01_041: [HDR SENT In this state the connection header has been sent to the peer but no connection header has been received.] */
case CONNECTION_STATE_HDR_SENT:
if (b != amqp_header[connection->header_bytes_received])
{
/* Codes_SRS_CONNECTION_01_089: [If the incoming and outgoing protocol headers do not match, both peers MUST close their outgoing stream] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
result = __FAILURE__;
}
else
{
connection->header_bytes_received++;
if (connection->header_bytes_received == sizeof(amqp_header))
{
if (connection->is_trace_on == 1)
{
LOG(AZ_LOG_TRACE, LOG_LINE, "<- Header (AMQP 0.1.0.0)");
}
connection_set_state(connection, CONNECTION_STATE_HDR_EXCH);
if (send_open_frame(connection) != 0)
{
LogError("Cannot send open frame");
connection_set_state(connection, CONNECTION_STATE_END);
}
}
result = 0;
}
break;
/* Codes_SRS_CONNECTION_01_040: [HDR RCVD In this state the connection header has been received from the peer but a connection header has not been sent.] */
case CONNECTION_STATE_HDR_RCVD:
/* Codes_SRS_CONNECTION_01_042: [HDR EXCH In this state the connection header has been sent to the peer and a connection header has been received from the peer.] */
/* we should not really get into this state, but just in case, we would treat that in the same way as HDR_RCVD */
case CONNECTION_STATE_HDR_EXCH:
/* Codes_SRS_CONNECTION_01_045: [OPEN RCVD In this state the connection headers have been exchanged. An open frame has been received from the peer but an open frame has not been sent.] */
case CONNECTION_STATE_OPEN_RCVD:
/* Codes_SRS_CONNECTION_01_046: [OPEN SENT In this state the connection headers have been exchanged. An open frame has been sent to the peer but no open frame has yet been received.] */
case CONNECTION_STATE_OPEN_SENT:
/* Codes_SRS_CONNECTION_01_048: [OPENED In this state the connection header and the open frame have been both sent and received.] */
case CONNECTION_STATE_OPENED:
/* Codes_SRS_CONNECTION_01_212: [After the initial handshake has been done all bytes received from the io instance shall be passed to the frame_codec for decoding by calling frame_codec_receive_bytes.] */
if (frame_codec_receive_bytes(connection->frame_codec, &b, 1) != 0)
{
LogError("Cannot process received bytes");
/* Codes_SRS_CONNECTION_01_218: [The error amqp:internal-error shall be set in the error.condition field of the CLOSE frame.] */
/* Codes_SRS_CONNECTION_01_219: [The error description shall be set to an implementation defined string.] */
close_connection_with_error(connection, "amqp:internal-error", "connection_byte_received::frame_codec_receive_bytes failed");
result = __FAILURE__;
}
else
{
result = 0;
}
break;
}
return result;
}
static void connection_on_bytes_received(void* context, const unsigned char* buffer, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
{
if (connection_byte_received((CONNECTION_HANDLE)context, buffer[i]) != 0)
{
LogError("Cannot process received bytes");
break;
}
}
}
static void connection_on_io_open_complete(void* context, IO_OPEN_RESULT io_open_result)
{
CONNECTION_HANDLE connection = (CONNECTION_HANDLE)context;
if (io_open_result == IO_OPEN_OK)
{
/* Codes_SRS_CONNECTION_01_084: [The connection_instance state machine implementing the protocol requirements shall be run as part of connection_dowork.] */
switch (connection->connection_state)
{
default:
LogError("Unknown connection state: %d", (int)connection->connection_state);
break;
case CONNECTION_STATE_START:
/* Codes_SRS_CONNECTION_01_086: [Prior to sending any frames on a connection_instance, each peer MUST start by sending a protocol header that indicates the protocol version used on the connection_instance.] */
/* Codes_SRS_CONNECTION_01_091: [The AMQP peer which acted in the role of the TCP client (i.e. the peer that actively opened the connection_instance) MUST immediately send its outgoing protocol header on establishment of the TCP connection_instance.] */
if (send_header(connection) != 0)
{
LogError("Cannot send header");
}
break;
case CONNECTION_STATE_HDR_SENT:
case CONNECTION_STATE_OPEN_SENT:
case CONNECTION_STATE_OPENED:
break;
case CONNECTION_STATE_HDR_EXCH:
/* Codes_SRS_CONNECTION_01_002: [Each AMQP connection_instance begins with an exchange of capabilities and limitations, including the maximum frame size.] */
/* Codes_SRS_CONNECTION_01_004: [After establishing or accepting a TCP connection_instance and sending the protocol header, each peer MUST send an open frame before sending any other frames.] */
/* Codes_SRS_CONNECTION_01_005: [The open frame describes the capabilities and limits of that peer.] */
if (send_open_frame(connection) != 0)
{
LogError("Cannot send OPEN frame");
connection_set_state(connection, CONNECTION_STATE_END);
}
break;
case CONNECTION_STATE_OPEN_RCVD:
break;
}
}
else
{
connection_set_state(connection, CONNECTION_STATE_END);
}
}
static void connection_on_io_error(void* context)
{
CONNECTION_HANDLE connection = (CONNECTION_HANDLE)context;
/* Codes_SRS_CONNECTION_22_005: [If the io notifies the connection instance of an IO_STATE_ERROR state and an io error callback is registered, the connection shall call the registered callback.] */
if (connection->on_io_error)
{
connection->on_io_error(connection->on_io_error_callback_context);
}
if (connection->connection_state != CONNECTION_STATE_END)
{
/* Codes_SRS_CONNECTION_01_202: [If the io notifies the connection instance of an IO_STATE_ERROR state the connection shall be closed and the state set to END.] */
connection_set_state(connection, CONNECTION_STATE_ERROR);
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
}
}
static void on_empty_amqp_frame_received(void* context, uint16_t channel)
{
CONNECTION_HANDLE connection = (CONNECTION_HANDLE)context;
/* It does not matter on which channel we received the frame */
(void)channel;
if (connection->is_trace_on == 1)
{
LOG(AZ_LOG_TRACE, LOG_LINE, "<- Empty frame");
}
if (tickcounter_get_current_ms(connection->tick_counter, &connection->last_frame_received_time) != 0)
{
LogError("Cannot get tickcounter value");
}
}
static void on_amqp_frame_received(void* context, uint16_t channel, AMQP_VALUE performative, const unsigned char* payload_bytes, uint32_t payload_size)
{
CONNECTION_HANDLE connection = (CONNECTION_HANDLE)context;
(void)channel;
if (tickcounter_get_current_ms(connection->tick_counter, &connection->last_frame_received_time) != 0)
{
LogError("Cannot get tickcounter value");
close_connection_with_error(connection, "amqp:internal-error", "cannot get current tick count");
}
else
{
if (connection->is_underlying_io_open)
{
switch (connection->connection_state)
{
default:
if (performative == NULL)
{
/* Codes_SRS_CONNECTION_01_223: [If the on_endpoint_frame_received is called with a NULL performative then the connection shall be closed with the error condition amqp:internal-error and an implementation defined error description.] */
close_connection_with_error(connection, "amqp:internal-error", "connection_endpoint_frame_received::NULL performative");
LogError("connection_endpoint_frame_received::NULL performative");
}
else
{
AMQP_VALUE descriptor = amqpvalue_get_inplace_descriptor(performative);
uint64_t performative_ulong;
if (connection->is_trace_on == 1)
{
log_incoming_frame(performative);
}
if (is_open_type_by_descriptor(descriptor))
{
if (channel != 0)
{
/* Codes_SRS_CONNECTION_01_006: [The open frame can only be sent on channel 0.] */
/* Codes_SRS_CONNECTION_01_222: [If an Open frame is received in a manner violating the ISO specification, the connection shall be closed with condition amqp:not-allowed and description being an implementation defined string.] */
close_connection_with_error(connection, "amqp:not-allowed", "OPEN frame received on a channel that is not 0");
LogError("OPEN frame received on a channel that is not 0");
}
if (connection->connection_state == CONNECTION_STATE_OPENED)
{
/* Codes_SRS_CONNECTION_01_239: [If an Open frame is received in the Opened state the connection shall be closed with condition amqp:illegal-state and description being an implementation defined string.] */
close_connection_with_error(connection, "amqp:illegal-state", "OPEN frame received in the OPENED state");
LogError("OPEN frame received in the OPENED state");
}
else if ((connection->connection_state == CONNECTION_STATE_OPEN_SENT) ||
(connection->connection_state == CONNECTION_STATE_HDR_EXCH))
{
OPEN_HANDLE open_handle;
if (amqpvalue_get_open(performative, &open_handle) != 0)
{
/* Codes_SRS_CONNECTION_01_143: [If any of the values in the received open frame are invalid then the connection shall be closed.] */
/* Codes_SRS_CONNECTION_01_220: [The error amqp:invalid-field shall be set in the error.condition field of the CLOSE frame.] */
close_connection_with_error(connection, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing OPEN frame");
LogError("connection_endpoint_frame_received::failed parsing OPEN frame");
}
else
{
if (open_get_idle_time_out(open_handle, &connection->remote_idle_timeout) == 0)
{
/* since we obtained the remote_idle_timeout, compute at what millisecond we should send the empty frame */
connection->remote_idle_timeout_send_frame_millisecond = (milliseconds)(connection->idle_timeout_empty_frame_send_ratio * connection->remote_idle_timeout);
}
if ((open_get_max_frame_size(open_handle, &connection->remote_max_frame_size) != 0) ||
/* Codes_SRS_CONNECTION_01_167: [Both peers MUST accept frames of up to 512 (MIN-MAX-FRAME-SIZE) octets.] */
(connection->remote_max_frame_size < 512))
{
/* Codes_SRS_CONNECTION_01_143: [If any of the values in the received open frame are invalid then the connection shall be closed.] */
/* Codes_SRS_CONNECTION_01_220: [The error amqp:invalid-field shall be set in the error.condition field of the CLOSE frame.] */
close_connection_with_error(connection, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing OPEN frame");
LogError("connection_endpoint_frame_received::failed parsing OPEN frame");
}
else
{
if (connection->connection_state == CONNECTION_STATE_OPEN_SENT)
{
connection_set_state(connection, CONNECTION_STATE_OPENED);
}
else
{
if (send_open_frame(connection) != 0)
{
connection_set_state(connection, CONNECTION_STATE_END);
}
else
{
connection_set_state(connection, CONNECTION_STATE_OPENED);
}
}
}
open_destroy(open_handle);
}
}
else
{
/* do nothing for now ... */
}
}
else if (is_close_type_by_descriptor(descriptor))
{
/* Codes_SRS_CONNECTION_01_012: [A close frame MAY be received on any channel up to the maximum channel number negotiated in open.] */
/* Codes_SRS_CONNECTION_01_242: [The connection module shall accept CLOSE frames even if they have extra payload bytes besides the Close performative.] */
/* Codes_SRS_CONNECTION_01_225: [HDR_RCVD HDR OPEN] */
if ((connection->connection_state == CONNECTION_STATE_HDR_RCVD) ||
/* Codes_SRS_CONNECTION_01_227: [HDR_EXCH OPEN OPEN] */
(connection->connection_state == CONNECTION_STATE_HDR_EXCH) ||
/* Codes_SRS_CONNECTION_01_228: [OPEN_RCVD OPEN *] */
(connection->connection_state == CONNECTION_STATE_OPEN_RCVD) ||
/* Codes_SRS_CONNECTION_01_235: [CLOSE_SENT - * TCP Close for Write] */
(connection->connection_state == CONNECTION_STATE_CLOSE_SENT) ||
/* Codes_SRS_CONNECTION_01_236: [DISCARDING - * TCP Close for Write] */
(connection->connection_state == CONNECTION_STATE_DISCARDING))
{
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
}
else
{
CLOSE_HANDLE close_handle;
/* Codes_SRS_CONNECTION_01_012: [A close frame MAY be received on any channel up to the maximum channel number negotiated in open.] */
if (channel > connection->channel_max)
{
close_connection_with_error(connection, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing CLOSE frame");
LogError("connection_endpoint_frame_received::failed parsing CLOSE frame");
}
else
{
if (amqpvalue_get_close(performative, &close_handle) != 0)
{
close_connection_with_error(connection, "amqp:invalid-field", "connection_endpoint_frame_received::failed parsing CLOSE frame");
LogError("connection_endpoint_frame_received::failed parsing CLOSE frame");
}
else
{
close_destroy(close_handle);
connection_set_state(connection, CONNECTION_STATE_CLOSE_RCVD);
if (send_close_frame(connection, NULL) != 0)
{
LogError("Cannot send CLOSE frame");
}
/* Codes_SRS_CONNECTION_01_214: [If the close frame cannot be constructed or sent, the connection shall be closed and set to the END state.] */
if (xio_close(connection->io, NULL, NULL) != 0)
{
LogError("xio_close failed");
}
connection_set_state(connection, CONNECTION_STATE_END);
}
}
}
}
else
{
amqpvalue_get_ulong(descriptor, &performative_ulong);
switch (performative_ulong)
{
default:
LogError("Bad performative: %02x", performative);
break;
case AMQP_BEGIN:
{
BEGIN_HANDLE begin;
if (amqpvalue_get_begin(performative, &begin) != 0)
{
LogError("Cannot get begin performative");
}
else
{
uint16_t remote_channel;
ENDPOINT_HANDLE new_endpoint = NULL;
bool remote_begin = false;
if (begin_get_remote_channel(begin, &remote_channel) != 0)
{
remote_begin = true;
if (connection->on_new_endpoint != NULL)
{
new_endpoint = connection_create_endpoint(connection);
if (!connection->on_new_endpoint(connection->on_new_endpoint_callback_context, new_endpoint))
{
connection_destroy_endpoint(new_endpoint);
new_endpoint = NULL;
}
}
}
if (!remote_begin)
{
ENDPOINT_INSTANCE* session_endpoint = find_session_endpoint_by_outgoing_channel(connection, remote_channel);
if (session_endpoint == NULL)
{
LogError("Cannot create session endpoint");
}
else
{
session_endpoint->incoming_channel = channel;
session_endpoint->on_endpoint_frame_received(session_endpoint->callback_context, performative, payload_size, payload_bytes);
}
}
else
{
if (new_endpoint != NULL)
{
new_endpoint->incoming_channel = channel;
new_endpoint->on_endpoint_frame_received(new_endpoint->callback_context, performative, payload_size, payload_bytes);
}
}
begin_destroy(begin);
}
break;
}
case AMQP_FLOW:
case AMQP_TRANSFER:
case AMQP_DISPOSITION:
case AMQP_END: