/
kz_amqp.c
3329 lines (2798 loc) · 88.8 KB
/
kz_amqp.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
/*
* $Id$
*
* Kazoo module interface
*
* Copyright (C) 2010-2014 2600Hz
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2014-08 first version (2600hz)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <amqp.h>
#include <amqp_framing.h>
#include <amqp_tcp_socket.h>
#include <amqp_ssl_socket.h>
#include <json.h>
#include <uuid/uuid.h>
#include "../../core/mem/mem.h"
#include "../../core/timer_proc.h"
#include "../../core/sr_module.h"
#include "../../core/pvar.h"
#include "../../core/mod_fix.h"
#include "../../core/lvalue.h"
#include "../tm/tm_load.h"
#include "../../core/route.h"
#include "../../core/receive.h"
#include "../../core/action.h"
#include "../../core/script_cb.h"
#include "kz_amqp.h"
#include "kz_json.h"
#include "kz_hash.h"
#define RET_AMQP_ERROR 2
struct tm_binds tmb;
kz_amqp_bindings_ptr kz_bindings = NULL;
int bindings_count = 0;
static unsigned long rpl_query_routing_key_count = 0;
typedef struct json_object *json_obj_ptr;
extern pv_spec_t kz_query_result_spec;
extern int *kz_worker_pipes;
extern int kz_cmd_pipe;
extern struct timeval kz_amqp_tv;
extern struct timeval kz_qtimeout_tv;
extern struct timeval kz_timer_tv;
extern str kz_amqps_ca_cert;
extern str kz_amqps_cert;
extern str kz_amqps_key;
extern int kz_amqps_verify_peer;
extern int kz_amqps_verify_hostname;
extern pv_spec_t kz_query_timeout_spec;
const amqp_bytes_t kz_amqp_empty_bytes = { 0, NULL };
const amqp_table_t kz_amqp_empty_table = { 0, NULL };
kz_amqp_zones_ptr kz_zones = NULL;
kz_amqp_zone_ptr kz_primary_zone = NULL;
amqp_exchange_declare_ok_t * AMQP_CALL kz_amqp_exchange_declare(amqp_connection_state_t state, amqp_channel_t channel, kz_amqp_exchange_ptr exchange, amqp_table_t arguments)
{
LM_DBG("declare exchange %.*s , %.*s\n",
(int)exchange->name.len, (char*)exchange->name.bytes,
(int)exchange->type.len, (char*)exchange->type.bytes);
#if AMQP_VERSION_MAJOR == 0 && AMQP_VERSION_MINOR < 6
return amqp_exchange_declare(state, channel, exchange->name, exchange->type,
exchange->passive, exchange->durable,
arguments);
#else
return amqp_exchange_declare(state, channel, exchange->name, exchange->type,
exchange->passive, exchange->durable,
exchange->auto_delete, exchange->internal,
arguments);
#endif
}
amqp_queue_declare_ok_t * AMQP_CALL kz_amqp_queue_declare(amqp_connection_state_t state, amqp_channel_t channel, kz_amqp_queue_ptr queue, amqp_table_t arguments)
{
return amqp_queue_declare(state, channel, queue->name, queue->passive, queue->durable, queue->exclusive, queue->auto_delete, arguments);
}
amqp_queue_bind_ok_t * AMQP_CALL kz_amqp_queue_bind(amqp_connection_state_t state, amqp_channel_t channel, kz_amqp_exchange_ptr exchange, kz_amqp_queue_ptr queue, kz_amqp_routings_ptr queue_bindings, amqp_table_t arguments)
{
amqp_queue_bind_ok_t *ret = amqp_queue_bind(state, channel, queue->name, exchange->name, queue_bindings->routing, arguments);
if(ret >= 0 && queue_bindings->next) {
return kz_amqp_queue_bind(state, channel, exchange, queue, queue_bindings->next, arguments);
} else {
return ret;
}
}
int set_non_blocking(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (flags < 0)
return flags;
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0)
return -1;
return 0;
}
static inline str* kz_str_dup(str* src)
{
char *dst_char = (char*)shm_malloc(sizeof(str)+src->len+1);
if (!dst_char) {
LM_ERR("error allocating shared memory for str");
return NULL;
}
str* dst = (str*)dst_char;
dst->s = dst_char+sizeof(str);
memcpy(dst->s, src->s, src->len);
dst->len = src->len;
dst->s[dst->len] = '\0';
return dst;
}
static inline str* kz_str_dup_from_char(char* src)
{
int len = strlen(src);
char *dst_char = (char*)shm_malloc(sizeof(str)+len+1);
if (!dst_char) {
LM_ERR("error allocating shared memory for str");
return NULL;
}
str* dst = (str*)dst_char;
dst->s = dst_char+sizeof(str);
memcpy(dst->s, src, len);
dst->len = len;
dst->s[dst->len] = '\0';
return dst;
}
static char *kz_amqp_str_dup(str *src)
{
char *res;
if (!src || !src->s)
return NULL;
if (!(res = (char *) shm_malloc(src->len + 1)))
return NULL;
strncpy(res, src->s, src->len);
res[src->len] = 0;
return res;
}
static char *kz_amqp_string_dup(char *src)
{
char *res;
int sz;
if (!src )
return NULL;
sz = strlen(src);
if (!(res = (char *) shm_malloc(sz + 1)))
return NULL;
strncpy(res, src, sz);
res[sz] = 0;
return res;
}
static char *kz_local_amqp_str_dup(str *src)
{
char *res;
if (!src || !src->s)
return NULL;
if (!(res = (char *) pkg_malloc(src->len + 1)))
return NULL;
strncpy(res, src->s, src->len);
res[src->len] = 0;
return res;
}
static inline str* kz_local_str_dup(str* src)
{
char *dst_char = (char*)pkg_malloc(sizeof(str)+src->len+1);
if (!dst_char) {
LM_ERR("error allocating shared memory for str");
return NULL;
}
str* dst = (str*)dst_char;
dst->s = dst_char+sizeof(str);
memcpy(dst->s, src->s, src->len);
dst->len = src->len;
dst->s[dst->len] = '\0';
return dst;
}
char *kz_amqp_bytes_dup(amqp_bytes_t bytes)
{
char *res;
int sz;
if (!bytes.bytes )
return NULL;
sz = bytes.len;
if (!(res = (char *) shm_malloc(sz + 1)))
return NULL;
strncpy(res, bytes.bytes, sz);
res[sz] = 0;
return res;
}
static inline str* kz_str_from_amqp_bytes(amqp_bytes_t src)
{
char *dst_char = (char*)shm_malloc(sizeof(str)+src.len+1);
if (!dst_char) {
LM_ERR("error allocating shared memory for str");
return NULL;
}
str* dst = (str*)dst_char;
dst->s = dst_char+sizeof(str);
memcpy(dst->s, src.bytes, src.len);
dst->len = src.len;
dst->s[dst->len] = '\0';
return dst;
}
char *kz_local_amqp_bytes_dup(amqp_bytes_t bytes)
{
char *res;
int sz;
if (!bytes.bytes )
return NULL;
sz = bytes.len;
if (!(res = (char *) pkg_malloc(sz + 1)))
return NULL;
strncpy(res, bytes.bytes, sz);
res[sz] = 0;
return res;
}
void kz_amqp_bytes_free(amqp_bytes_t bytes)
{
if(bytes.bytes)
shm_free(bytes.bytes);
}
amqp_bytes_t kz_amqp_bytes_malloc_dup(amqp_bytes_t src)
{
amqp_bytes_t result = {0, 0};
result.len = src.len;
result.bytes = shm_malloc(src.len+1);
if (result.bytes != NULL) {
memcpy(result.bytes, src.bytes, src.len);
((char*)result.bytes)[result.len] = '\0';
}
return result;
}
void kz_local_amqp_bytes_free(amqp_bytes_t bytes)
{
if(bytes.bytes)
pkg_free(bytes.bytes);
}
amqp_bytes_t kz_local_amqp_bytes_malloc_dup(amqp_bytes_t src)
{
amqp_bytes_t result = {0, 0};
result.len = src.len;
result.bytes = pkg_malloc(src.len+1);
if (result.bytes != NULL) {
memcpy(result.bytes, src.bytes, src.len);
((char*)result.bytes)[result.len] = '\0';
}
return result;
}
amqp_bytes_t kz_local_amqp_bytes_dup_from_string(char *src)
{
return kz_local_amqp_bytes_malloc_dup(amqp_cstring_bytes(src));
}
amqp_bytes_t kz_amqp_bytes_dup_from_string(char *src)
{
return kz_amqp_bytes_malloc_dup(amqp_cstring_bytes(src));
}
amqp_bytes_t kz_amqp_bytes_dup_from_str(str *src)
{
return kz_amqp_bytes_malloc_dup(amqp_cstring_bytes(src->s));
}
void kz_amqp_free_pipe_cmd(kz_amqp_cmd_ptr cmd)
{
if(cmd == NULL)
return;
if (cmd->exchange)
shm_free(cmd->exchange);
if (cmd->exchange_type)
shm_free(cmd->exchange_type);
if (cmd->queue)
shm_free(cmd->queue);
if (cmd->routing_key)
shm_free(cmd->routing_key);
if (cmd->reply_routing_key)
shm_free(cmd->reply_routing_key);
if (cmd->payload)
shm_free(cmd->payload);
if (cmd->return_payload)
shm_free(cmd->return_payload);
if (cmd->message_id)
shm_free(cmd->message_id);
if (cmd->cb_route)
shm_free(cmd->cb_route);
if (cmd->err_route)
shm_free(cmd->err_route);
lock_release(&cmd->lock);
lock_destroy(&cmd->lock);
shm_free(cmd);
}
void kz_amqp_free_consumer_delivery(kz_amqp_consumer_delivery_ptr ptr)
{
if(ptr == NULL)
return;
if(ptr->payload)
shm_free(ptr->payload);
if(ptr->event_key)
shm_free(ptr->event_key);
if(ptr->event_subkey)
shm_free(ptr->event_subkey);
if(ptr->message_id)
shm_free(ptr->message_id);
if(ptr->routing_key)
shm_free(ptr->routing_key);
if(ptr->cmd)
kz_amqp_free_pipe_cmd(ptr->cmd);
shm_free(ptr);
}
void kz_amqp_free_bind(kz_amqp_bind_ptr bind)
{
if(bind == NULL)
return;
if(bind->exchange)
kz_amqp_exchange_free(bind->exchange);
if(bind->exchange_bindings)
kz_amqp_exchange_bindings_free(bind->exchange_bindings);
if(bind->queue)
kz_amqp_queue_free(bind->queue);
if(bind->queue_bindings)
kz_amqp_routing_free(bind->queue_bindings);
if(bind->event_key.bytes)
kz_amqp_bytes_free(bind->event_key);
if(bind->event_subkey.bytes)
kz_amqp_bytes_free(bind->event_subkey);
if(bind->consistent_worker_key)
shm_free(bind->consistent_worker_key);
shm_free(bind);
}
void kz_amqp_free_connection(kz_amqp_connection_ptr conn)
{
if(!conn)
return;
if(conn->url)
shm_free(conn->url);
shm_free(conn);
}
kz_amqp_cmd_ptr kz_amqp_alloc_pipe_cmd()
{
kz_amqp_cmd_ptr cmd = (kz_amqp_cmd_ptr)shm_malloc(sizeof(kz_amqp_cmd));
if(cmd == NULL) {
LM_ERR("failed to allocate kz_amqp_cmd in process %d\n", getpid());
return NULL;
}
memset(cmd, 0, sizeof(kz_amqp_cmd));
if(lock_init(&cmd->lock)==NULL) {
LM_ERR("cannot init the lock for pipe command in process %d\n", getpid());
lock_dealloc(&cmd->lock);
kz_amqp_free_pipe_cmd(cmd);
return NULL;
}
lock_get(&cmd->lock);
return cmd;
}
kz_amqp_bind_ptr kz_amqp_bind_alloc(kz_amqp_exchange_ptr exchange, kz_amqp_exchange_binding_ptr exchange_bindings, kz_amqp_queue_ptr queue, kz_amqp_routings_ptr queue_bindings, str* event_key, str* event_subkey )
{
kz_amqp_bind_ptr bind = NULL;
bind = (kz_amqp_bind_ptr)shm_malloc(sizeof(kz_amqp_bind));
if(bind == NULL) {
LM_ERR("error allocation memory for bind alloc\n");
goto error;
}
memset(bind, 0, sizeof(kz_amqp_bind));
bind->exchange = exchange;
bind->queue = queue;
bind->exchange_bindings = exchange_bindings;
bind->queue_bindings = queue_bindings;
if(event_key != NULL) {
bind->event_key = kz_amqp_bytes_dup_from_str(event_key);
if (bind->event_key.bytes == NULL) {
LM_ERR("Out of memory allocating for routing key\n");
goto error;
}
}
if(event_subkey != NULL) {
bind->event_subkey = kz_amqp_bytes_dup_from_str(event_subkey);
if (bind->event_subkey.bytes == NULL) {
LM_ERR("Out of memory allocating for routing key\n");
goto error;
}
}
return bind;
error:
kz_amqp_free_bind(bind);
return NULL;
}
kz_amqp_zone_ptr kz_amqp_get_primary_zone() {
if(kz_primary_zone == NULL) {
kz_primary_zone = (kz_amqp_zone_ptr) shm_malloc(sizeof(kz_amqp_zone));
memset(kz_primary_zone, 0, sizeof(kz_amqp_zone));
kz_primary_zone->zone = shm_malloc(dbk_primary_zone_name.len+1);
strcpy(kz_primary_zone->zone, dbk_primary_zone_name.s);
kz_primary_zone->zone[dbk_primary_zone_name.len] = '\0';
kz_primary_zone->servers = (kz_amqp_servers_ptr) shm_malloc(sizeof(kz_amqp_servers));
memset(kz_primary_zone->servers, 0, sizeof(kz_amqp_servers));
}
return kz_primary_zone;
}
kz_amqp_zone_ptr kz_amqp_get_zones() {
if(kz_zones == NULL) {
kz_zones = (kz_amqp_zones_ptr) shm_malloc(sizeof(kz_amqp_zones));
kz_zones->head = kz_zones->tail = kz_amqp_get_primary_zone();
}
return kz_zones->head;
}
kz_amqp_zone_ptr kz_amqp_get_zone(char* zone) {
kz_amqp_zone_ptr ret = NULL;
for(ret = kz_amqp_get_zones(); ret != NULL; ret = ret->next)
if(!strcmp(ret->zone, zone))
return ret;
return NULL;
}
kz_amqp_zone_ptr kz_amqp_add_zone(char* zone) {
kz_amqp_zone_ptr zone_ptr = (kz_amqp_zone_ptr) shm_malloc(sizeof(kz_amqp_zone));
memset(zone_ptr, 0, sizeof(kz_amqp_zone));
zone_ptr->zone = shm_malloc(strlen(zone)+1);
strcpy(zone_ptr->zone, zone);
zone_ptr->zone[strlen(zone)] = '\0';
zone_ptr->servers = (kz_amqp_servers_ptr) shm_malloc(sizeof(kz_amqp_servers));
memset(zone_ptr->servers, 0, sizeof(kz_amqp_servers));
kz_zones->tail->next = zone_ptr;
kz_zones->tail = zone_ptr;
return zone_ptr;
}
kz_amqp_queue_ptr kz_amqp_targeted_queue(char *name)
{
str queue = str_init(name);
return kz_amqp_queue_new(&queue);
}
int kz_amqp_bind_init_targeted_channel(kz_amqp_server_ptr server, int idx )
{
kz_amqp_bind_ptr bind = NULL;
str rpl_exch = str_init("targeted");
str rpl_exch_type = str_init("direct");
int ret = -1;
char serverid[512];
bind = (kz_amqp_bind_ptr)shm_malloc(sizeof(kz_amqp_bind));
if(bind == NULL) {
LM_ERR("error allocation memory for reply\n");
goto error;
}
memset(bind, 0, sizeof(kz_amqp_bind));
bind->exchange = kz_amqp_exchange_new(&rpl_exch, &rpl_exch_type);
if(bind->exchange == NULL) {
LM_ERR("error allocation memory for reply\n");
goto error;
}
sprintf(serverid, "kamailio@%.*s-<%d-%d>", dbk_node_hostname.len, dbk_node_hostname.s, server->id, idx);
bind->queue = kz_amqp_targeted_queue(serverid);
if(bind->queue == NULL) {
LM_ERR("error allocation memory for reply\n");
goto error;
}
sprintf(serverid, "kamailio@%.*s-<%d>-targeted-%d", dbk_node_hostname.len, dbk_node_hostname.s, server->id, idx);
bind->queue_bindings = kz_amqp_routing_new(serverid);
if (bind->queue_bindings == NULL) {
LM_ERR("Out of memory allocating for exchange/routing_key\n");
goto error;
}
server->channels[idx].targeted = bind;
return 0;
error:
kz_amqp_free_bind(bind);
return ret;
}
int kz_tm_bind()
{
load_tm_f load_tm;
if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0)))
{
LOG(L_ERR, "cannot import load_tm\n");
return 0;
}
if (load_tm( &tmb )==-1)
return 0;
return 1;
}
int kz_amqp_init() {
int i;
kz_amqp_zone_ptr g;
kz_amqp_server_ptr s;
if(!kz_hash_init())
return 0;
if(!kz_tm_bind())
return 0;
if(kz_bindings == NULL) {
kz_bindings = (kz_amqp_bindings_ptr) shm_malloc(sizeof(kz_amqp_bindings));
memset(kz_bindings, 0, sizeof(kz_amqp_bindings));
}
for (g = kz_amqp_get_zones(); g != NULL; g = g->next) {
for (s = g->servers->head; s != NULL; s = s->next) {
if(s->channels == NULL) {
s->channels = shm_malloc(dbk_channels * sizeof(kz_amqp_channel));
memset(s->channels, 0, dbk_channels * sizeof(kz_amqp_channel));
for(i=0; i < dbk_channels; i++) {
s->channels[i].channel = i+1;
if(lock_init(&s->channels[i].lock)==NULL) {
LM_ERR("could not initialize locks for channels\n");
return 0;
}
if(kz_amqp_bind_init_targeted_channel(s, i)) {
LM_ERR("could not initialize targeted channels\n");
return 0;
}
}
}
}
}
return 1;
}
void kz_amqp_destroy_connection(kz_amqp_connection_ptr s)
{
kz_amqp_free_connection(s);
}
void kz_amqp_destroy_connection_ptr(kz_amqp_conn_ptr s)
{
}
void kz_amqp_destroy_channels(kz_amqp_server_ptr server_ptr)
{
int i;
if(server_ptr->channels == NULL) {
return;
}
for(i=0; i < dbk_channels; i++) {
if(server_ptr->channels[i].targeted != NULL) {
kz_amqp_free_bind(server_ptr->channels[i].targeted);
}
}
shm_free(server_ptr->channels);
server_ptr->channels = NULL;
}
kz_amqp_server_ptr kz_amqp_destroy_server(kz_amqp_server_ptr server_ptr)
{
kz_amqp_server_ptr next = server_ptr->next;
kz_amqp_destroy_connection(server_ptr->connection);
kz_amqp_destroy_channels(server_ptr);
if (server_ptr->producer) { shm_free(server_ptr->producer); }
shm_free(server_ptr);
return next;
}
kz_amqp_zone_ptr kz_amqp_destroy_zone(kz_amqp_zone_ptr zone_ptr)
{
kz_amqp_zone_ptr next = zone_ptr->next;
kz_amqp_server_ptr server_ptr = zone_ptr->servers->head;
while(server_ptr) {
server_ptr = kz_amqp_destroy_server(server_ptr);
}
shm_free(zone_ptr->zone);
shm_free(zone_ptr->servers);
shm_free(zone_ptr);
return next;
}
void kz_amqp_destroy_zones()
{
kz_amqp_zone_ptr g = kz_amqp_get_zones();
while(g) {
g = kz_amqp_destroy_zone(g);
}
shm_free(kz_zones);
kz_zones = NULL;
kz_primary_zone = NULL;
}
void kz_amqp_destroy() {
kz_amqp_destroy_zones();
if(kz_bindings != NULL) {
kz_amqp_binding_ptr binding = kz_bindings->head;
while(binding != NULL) {
kz_amqp_binding_ptr free = binding;
binding = binding->next;
if(free->bind != NULL) {
kz_amqp_free_bind(free->bind);
}
shm_free(free);
}
shm_free(kz_bindings);
}
kz_hash_destroy();
}
#define KZ_URL_MAX_SIZE 100
static char* KZ_URL_ROOT = "/";
int kz_amqp_add_connection(modparam_t type, void* val)
{
kz_amqp_zone_ptr zone_ptr = NULL;
char* url = (char*) val;
int len = strlen(url);
if(len > KZ_URL_MAX_SIZE) {
LM_ERR("connection url exceeds max size %d\n", KZ_URL_MAX_SIZE);
return -1;
}
if(!strncmp(url, "zone=", 5)) {
char* ptr = strchr(url, ';');
char* zone_str_ptr = url+(5*sizeof(char));
if(ptr == NULL) {
LM_ERR("missing ';' at the end of zone name '%s'\n", url);
return -1;
}
*ptr = '\0';
if(strlen(zone_str_ptr) == 0) {
LM_ERR("invalid zone name '%s'\n", url);
return -1;
}
zone_ptr = kz_amqp_get_zone(zone_str_ptr);
if(zone_ptr == NULL) {
zone_ptr = kz_amqp_add_zone(zone_str_ptr);
if(zone_ptr == NULL) {
LM_ERR("unable to add zone %s\n", zone_str_ptr);
return -1;
}
}
url = ++ptr;
} else {
zone_ptr = kz_amqp_get_zones();
}
kz_amqp_connection_ptr newConn = shm_malloc(sizeof(kz_amqp_connection));
memset(newConn, 0, sizeof(kz_amqp_connection));
newConn->url = shm_malloc( (KZ_URL_MAX_SIZE + 1) * sizeof(char) );
memset(newConn->url, 0, (KZ_URL_MAX_SIZE + 1) * sizeof(char));
// maintain compatibility
if (!strncmp(url, "kazoo://", 8)) {
sprintf(newConn->url, "amqp://%s", (char*)(url+(8*sizeof(char))) );
} else {
strcpy(newConn->url, url);
newConn->url[len] = '\0';
}
if(amqp_parse_url(newConn->url, &newConn->info) == AMQP_STATUS_BAD_URL) {
LM_ERR("ERROR PARSING URL \"%s\"\n", newConn->url);
goto error;
}
if(newConn->info.vhost == NULL) {
newConn->info.vhost = KZ_URL_ROOT;
#if AMQP_VERSION_MAJOR == 0 && AMQP_VERSION_MINOR < 6
} else if(newConn->info.vhost[0] == '/' && strlen(newConn->info.vhost) == 1) { // bug in amqp_parse_url ?
newConn->info.vhost++;
#endif
}
kz_amqp_server_ptr server_ptr = (kz_amqp_server_ptr)shm_malloc(sizeof(kz_amqp_server));
server_ptr->connection = newConn;
server_ptr->id = ++kz_server_counter;
server_ptr->zone = zone_ptr;
if(zone_ptr->servers->tail) {
zone_ptr->servers->tail->next = server_ptr;
zone_ptr->servers->tail = server_ptr;
} else {
zone_ptr->servers->head = server_ptr;
zone_ptr->servers->tail = server_ptr;
}
return 0;
error:
kz_amqp_free_connection(newConn);
return -1;
}
void kz_amqp_connection_close(kz_amqp_conn_ptr rmq) {
LM_DBG("Close rmq connection\n");
if (!rmq)
return;
if(rmq->heartbeat)
kz_amqp_timer_destroy(&rmq->heartbeat);
kz_amqp_fire_connection_event("closed", rmq->server->connection->info.host, rmq->server->zone->zone);
if (rmq->conn) {
LM_DBG("close connection: %d rmq(%p)->conn(%p)\n", getpid(), (void *)rmq, rmq->conn);
kz_amqp_error("closing connection", amqp_connection_close(rmq->conn, AMQP_REPLY_SUCCESS));
if (amqp_destroy_connection(rmq->conn) < 0) {
LM_ERR("cannot destroy connection\n");
}
rmq->conn = NULL;
rmq->socket = NULL;
rmq->channel_count = 0;
}
rmq->state = KZ_AMQP_CONNECTION_CLOSED;
}
void kz_amqp_channel_close(kz_amqp_conn_ptr rmq, amqp_channel_t channel) {
LM_DBG("Close rmq channel\n");
if (!rmq)
return;
LM_DBG("close channel: %d rmq(%p)->channel(%d)\n", getpid(), (void *)rmq, channel);
kz_amqp_error("closing channel", amqp_channel_close(rmq->conn, channel, AMQP_REPLY_SUCCESS));
}
int kz_ssl_initialized = 0;
int kz_amqp_connection_open_ssl(kz_amqp_conn_ptr rmq) {
if(!kz_ssl_initialized) {
kz_ssl_initialized = 1;
amqp_set_initialize_ssl_library(1);
}
if (!(rmq->conn = amqp_new_connection())) {
LM_ERR("Failed to create new AMQP connection\n");
goto error;
}
rmq->socket = amqp_ssl_socket_new(rmq->conn);
if (!rmq->socket) {
LM_ERR("Failed to create SSL socket to AMQP broker\n");
goto nosocket;
}
if (kz_amqps_ca_cert.s) {
if (amqp_ssl_socket_set_cacert(rmq->socket, kz_amqps_ca_cert.s)) {
LM_ERR("Failed to set CA certificate for amqps connection\n");
goto nosocket;
}
}
if (kz_amqps_cert.s && kz_amqps_key.s) {
if (amqp_ssl_socket_set_key(rmq->socket, kz_amqps_cert.s, kz_amqps_key.s)) {
LM_ERR("Failed to set client key/certificate for amqps connection\n");
goto nosocket;
}
}
#if AMQP_VERSION_MAJOR == 0 && AMQP_VERSION_MINOR < 8
amqp_ssl_socket_set_verify(rmq->socket, kz_amqps_verify_peer | kz_amqps_verify_hostname);
#else
amqp_ssl_socket_set_verify_peer(rmq->socket, kz_amqps_verify_peer);
amqp_ssl_socket_set_verify_hostname(rmq->socket, kz_amqps_verify_hostname);
#endif
if (amqp_socket_open(rmq->socket, rmq->server->connection->info.host, rmq->server->connection->info.port)) {
LM_ERR("Failed to open SSL socket to AMQP broker : %s : %i\n",
rmq->server->connection->info.host, rmq->server->connection->info.port);
goto nosocket;
}
if (kz_amqp_error("Logging in", amqp_login(rmq->conn,
rmq->server->connection->info.vhost,
0,
131072,
dbk_use_hearbeats,
AMQP_SASL_METHOD_PLAIN,
rmq->server->connection->info.user,
rmq->server->connection->info.password))) {
LM_ERR("Login to AMQP broker failed!\n");
goto error;
}
rmq->state = KZ_AMQP_CONNECTION_OPEN;
return 0;
nosocket:
if (amqp_destroy_connection(rmq->conn) < 0) {
LM_ERR("cannot destroy connection\n");
}
rmq->conn = NULL;
return -1;
error:
kz_amqp_connection_close(rmq);
return -1;
}
int kz_amqp_connection_open(kz_amqp_conn_ptr rmq) {
rmq->state = KZ_AMQP_CONNECTION_CLOSED;
rmq->channel_count = rmq->channel_counter = 0;
if(rmq->server->connection->info.ssl)
return kz_amqp_connection_open_ssl(rmq);
rmq->channel_count = rmq->channel_counter = 0;
if (!(rmq->conn = amqp_new_connection())) {
LM_DBG("Failed to create new AMQP connection\n");
goto error;
}
rmq->socket = amqp_tcp_socket_new(rmq->conn);
if (!rmq->socket) {
LM_DBG("Failed to create TCP socket to AMQP broker\n");
goto nosocket;
}
if (amqp_socket_open(rmq->socket, rmq->server->connection->info.host, rmq->server->connection->info.port)) {
LM_DBG("Failed to open TCP socket to AMQP broker\n");
goto nosocket;
}
if (kz_amqp_error("Logging in", amqp_login(rmq->conn,
rmq->server->connection->info.vhost,
0,
131072,
dbk_use_hearbeats,
AMQP_SASL_METHOD_PLAIN,
rmq->server->connection->info.user,
rmq->server->connection->info.password))) {
LM_ERR("Login to AMQP broker failed!\n");
goto error;
}
rmq->state = KZ_AMQP_CONNECTION_OPEN;
return 0;
nosocket:
if (amqp_destroy_connection(rmq->conn) < 0) {
LM_ERR("cannot destroy connection\n");
}
rmq->conn = NULL;
return -1;
error:
kz_amqp_connection_close(rmq);
return -1;
}
int kz_amqp_channel_open(kz_amqp_conn_ptr rmq, amqp_channel_t channel) {
if(rmq == NULL) {
LM_DBG("rmq == NULL \n");
return -1;
}
amqp_channel_open(rmq->conn, channel);
if (kz_amqp_error("Opening channel", amqp_get_rpc_reply(rmq->conn))) {
LM_ERR("Failed to open channel AMQP %d!\n", channel);
return -1;
}
return 0;
}
int kz_amqp_consume_error(kz_amqp_conn_ptr ptr)
{
amqp_connection_state_t conn = ptr->conn;
amqp_frame_t frame;
int ret = 0;
amqp_rpc_reply_t reply;
if (AMQP_STATUS_OK != amqp_simple_wait_frame_noblock(conn, &frame, &kz_amqp_tv)) {
// should i ignore this or close the connection?
LM_ERR("ERROR ON SIMPLE_WAIT_FRAME\n");
return ret;
}
if (AMQP_FRAME_METHOD == frame.frame_type) {
switch (frame.payload.method.id) {
case AMQP_BASIC_ACK_METHOD:
/* if we've turned publisher confirms on, and we've published a message
* here is a message being confirmed
*/
ret = 1;
break;
case AMQP_BASIC_RETURN_METHOD:
/* if a published message couldn't be routed and the mandatory flag was set
* this is what would be returned. The message then needs to be read.
*/
{
ret = 1;
amqp_message_t message;
reply = amqp_read_message(conn, frame.channel, &message, 0);
if (AMQP_RESPONSE_NORMAL != reply.reply_type) {
LM_ERR("AMQP_BASIC_RETURN_METHOD read_message\n");
break;
}
LM_DBG("Received this message : %.*s\n", (int) message.body.len, (char*)message.body.bytes);
amqp_destroy_message(&message);
}
break;
case AMQP_CHANNEL_CLOSE_METHOD:
/* a channel.close method happens when a channel exception occurs, this
* can happen by publishing to an exchange that doesn't exist for example
*
* In this case you would need to open another channel redeclare any queues
* that were declared auto-delete, and restart any consumers that were attached
* to the previous channel
*/
LM_ERR("AMQP_CHANNEL_CLOSE_METHOD\n");
if(frame.channel > 0)
ptr->server->channels[frame.channel-1].state = KZ_AMQP_CHANNEL_CLOSED;
break;
case AMQP_CONNECTION_CLOSE_METHOD:
/* a connection.close method happens when a connection exception occurs,
* this can happen by trying to use a channel that isn't open for example.