-
Notifications
You must be signed in to change notification settings - Fork 909
/
peerstatemachine.c
1314 lines (1227 loc) · 36 KB
/
peerstatemachine.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) 2012 Smile Communications, jason.penton@smilecoms.com
* Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
*
* The initial version of this code was written by Dragos Vingarzan
* (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
* Fruanhofer Institute. It was and still is maintained in a separate
* branch of the original SER. We are therefore migrating it to
* Kamailio/SR and look forward to maintaining it from here on out.
* 2011/2012 Smile Communications, Pty. Ltd.
* ported/maintained/improved by
* Jason Penton (jason(dot)penton(at)smilecoms.com and
* Richard Good (richard(dot)good(at)smilecoms.com) as part of an
* effort to add full IMS support to Kamailio/SR using a new and
* improved architecture
*
* NB: Alot of this code was originally part of OpenIMSCore,
* FhG Fokus.
* Copyright (C) 2004-2006 FhG Fokus
* Thanks for great work! This is an effort to
* break apart the various CSCF functions into logically separate
* components. We hope this will drive wider use. We also feel
* that in this way the architecture is more complete and thereby easier
* to manage in the Kamailio/SR environment
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "peerstatemachine.h"
#include "diameter_api.h"
#include "diameter_ims.h"
#include "utils.h"
#include "receiver.h"
#include "peermanager.h"
#include "config.h"
#include "worker.h"
#include "authstatemachine.h"
#include "acctstatemachine.h"
extern dp_config *config; /**< Configuration for this diameter peer */
/** Strings for the peer states */
char *dp_states[]={"Closed","Wait_Conn_Ack","Wait_I_CEA","Wait_Conn_Ack_Elect","Wait_Returns","R_Open","I_Open","Closing"};
/** Strings for the peer events */
char *dp_events[]={"Start","Stop","Timeout","Win_Election","R_Conn_CER","I_Rcv_Conn_Ack","I_Rcv_Conn_NAck",
"I_Rcv_CER","I_Rcv_CEA","R_Rcv_CER","R_Rcv_CEA","I_Rcv_Non_CEA",
"I_Rcv_DPR","I_Rcv_DPA","R_Rcv_DPR","R_Rcv_DPA",
"I_Rcv_DWR","I_Rcv_DWA","R_Rcv_DWR","R_Rcv_DWA",
"Send_Message","I_Rcv_Message","R_Rcv_Message","I_Peer_Disc","R_Peer_Disc"};
/**
* Diameter base protocol state-machine processing.
* This function get's called for every event. It updates the states and can trigger
* other events.
* @param p - the peer for which the event happened
* @param event - the event that happened
* @param msg - if a Diameter message was received this is it, or NULL if not
* @param peer_locked - if the peer lock is already aquired
* @param sock - socket that this event happened on, or NULL if unrelated
* @returns 1 on success, 0 on error. Also the peer states are updated
*/
int sm_process(peer *p,peer_event_t event,AAAMessage *msg,int peer_locked,int sock)
{
int result_code;
peer_event_t next_event;
int msg_received=0;
if (!peer_locked) lock_get(p->lock);
LM_DBG("sm_process(): Peer %.*s State %s Event %s\n",
p->fqdn.len,p->fqdn.s,dp_states[p->state],dp_events[event-101]);
switch (p->state){
case Closed:
switch (event){
case Start:
p->state = Wait_Conn_Ack;
next_event = I_Snd_Conn_Req(p);
if (next_event==I_Rcv_Conn_NAck)
sm_process(p,next_event,0,1,p->I_sock);
else{
/* wait for fd to be transmitted to the respective receiver,
* in order to get a send pipe opened */
}
break;
case R_Conn_CER:
R_Accept(p,sock);
result_code = Process_CER(p,msg);
Snd_CEA(p,msg,result_code,p->R_sock);
msg=0;
if (result_code>=2000 && result_code<3000)
p->state = R_Open;
else {
R_Disc(p);
p->state = Closed;
}
log_peer_list(L_INFO);
break;
case Stop:
/* just ignore this state */
p->state = Closed;
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case Wait_Conn_Ack:
switch(event){
case I_Rcv_Conn_Ack:
I_Snd_CER(p);
p->state = Wait_I_CEA;
break;
case I_Rcv_Conn_NAck:
Cleanup(p,p->I_sock);
p->state = Closed;
break;
case R_Conn_CER:
if (p->r_cer) AAAFreeMessage(&(p->r_cer));
R_Accept(p,sock);
result_code = Process_CER(p,msg);
if (result_code>=2000 && result_code<3000){
p->state = Wait_Conn_Ack_Elect;
p->r_cer = msg;
}
else {
p->state = Closed;
AAAFreeMessage(&msg);
R_Disc(p);
I_Disc(p);
}
break;
case Timeout:
Error(p,p->I_sock);
p->state = Closed;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case Wait_I_CEA:
switch(event){
case I_Rcv_CEA:
result_code = Process_CEA(p,msg);
if (result_code>=2000 && result_code<3000)
p->state = I_Open;
else {
Cleanup(p,p->I_sock);
p->state = Closed;
}
log_peer_list(L_INFO);
break;
case R_Conn_CER:
if (p->r_cer) AAAFreeMessage(&(p->r_cer));
R_Accept(p,sock);
result_code = Process_CER(p,msg);
if (result_code>=2000 && result_code<3000){
p->state = Wait_Returns;
if (Elect(p,msg)){
// won the election = > I_Disc(), R_Send_CEA()
LM_INFO("sm_process():Wait_I_CEA Win Elect \n");
sm_process(p,Win_Election,msg,1,sock);
} else {
// lost the election => wait for I_Recv_CEA, then R_Disc()
LM_INFO("sm_process():Wait_I_CEA Lose Elect \n");
p->r_cer = msg;
sm_process(p,I_Peer_Disc,0,1,p->I_sock);
}
}
else{
Snd_CEA(p,msg,result_code,p->R_sock);
R_Disc(p);
I_Disc(p);
p->state=Closed;
break;
}
break;
case I_Peer_Disc:
I_Disc(p);
p->state = Closed;
break;
case I_Rcv_Non_CEA:
Error(p,p->I_sock);
p->state = Closed;
break;
case Timeout:
Error(p,p->I_sock);
p->state = Closed;
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case Wait_Conn_Ack_Elect:
switch(event){
case I_Rcv_Conn_Ack:
I_Snd_CER(p);
if (p->r_cer){
p->state = Wait_Returns;
if (Elect(p,p->r_cer)){
// won the election = > I_Disc(), R_Send_CEA()
LM_INFO("sm_process():Wait_Conn_Ack_Elect Win Elect \n");
sm_process(p,Win_Election,p->r_cer,1,sock);
p->r_cer = 0;
} else {
// lost the election => wait for I_Recv_CEA, then R_Disc()
LM_INFO("sm_process():Wait_Conn_Ack_Elect Lose Elect \n");
AAAFreeMessage(&p->r_cer);
}
} else {
LM_ERR("sm_process():Wait_Conn_Ack_Elect, I_Rcv_Conn_Ack, No R-CER ! \n");
p->state = Wait_I_CEA;
}
break;
case I_Rcv_Conn_NAck:
Cleanup(p,p->I_sock);
if (p->r_cer){
result_code = Process_CER(p,p->r_cer);
Snd_CEA(p,p->r_cer,result_code,p->R_sock);
p->r_cer=0;
if (result_code>=2000 && result_code<3000)
p->state = R_Open;
else {
R_Disc(p);
p->state = Closed;
// p->state = R_Open; /* Or maybe I should disconnect it?*/
}
}else{
LM_ERR("sm_process():Wait_Conn_Ack_Elect, I_Rcv_Conn_NAck No R-CER ! \n");
}
break;
case R_Peer_Disc:
R_Disc(p);
p->state = Wait_Conn_Ack;
break;
case R_Conn_CER:
R_Reject(p,sock);
AAAFreeMessage(&msg);
p->state = Wait_Conn_Ack_Elect;
break;
case Timeout:
if (p->I_sock>=0) Error(p,p->I_sock);
if (p->R_sock>=0) Error(p,p->R_sock);
p->state = Closed;
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case Wait_Returns:
switch(event){
case Win_Election:
/* this is the Win Election -> I is dropped, R is kept */
LM_INFO("sm_process():Wait_Returns Win Elect \n");
I_Disc(p);
result_code = Process_CER(p,msg);
Snd_CEA(p,msg,result_code,p->R_sock);
if (result_code>=2000 && result_code<3000){
p->state = R_Open;
}else{
R_Disc(p);
p->state = Closed;
}
break;
case I_Peer_Disc:
I_Disc(p);
if (p->r_cer){
result_code = Process_CER(p,p->r_cer);
Snd_CEA(p,p->r_cer,result_code,p->R_sock);
p->r_cer=0;
if (result_code>=2000 && result_code<3000){
p->state = R_Open;
}else{
R_Disc(p);
p->state = Closed;
}
}else {
LM_ERR("sm_process():Wait_Returns, I_Peer_Disc No R-CER ! \n");
}
break;
case I_Rcv_CEA:
/* this is the Lost Election -> I is kept, R dropped */
LM_INFO("sm_process():Wait_Returns Lost Elect \n");
R_Disc(p);
result_code = Process_CEA(p,msg);
if (result_code>=2000 && result_code<3000)
p->state = I_Open;
else {
Cleanup(p,p->I_sock);
p->state = Closed;
}
break;
case R_Peer_Disc:
R_Disc(p);
p->state = Wait_I_CEA;
break;
case R_Conn_CER:
R_Reject(p,p->R_sock);
AAAFreeMessage(&msg);
p->state = Wait_Returns;
break;
case Timeout:
if (p->I_sock>=0) Error(p,p->I_sock);
if (p->R_sock>=0) Error(p,p->R_sock);
p->state = Closed;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case R_Open:
switch (event){
case Send_Message:
Snd_Message(p,msg);
p->state = R_Open;
break;
case R_Rcv_Message:
// delayed processing until out of the critical zone
//Rcv_Process(p,msg);
msg_received = 1;
p->state = R_Open;
break;
case R_Rcv_DWR:
result_code = Process_DWR(p,msg);
Snd_DWA(p,msg,result_code,p->R_sock);
p->state = R_Open;
break;
case R_Rcv_DWA:
Process_DWA(p,msg);
p->state = R_Open;
break;
case R_Conn_CER:
R_Reject(p,sock);
AAAFreeMessage(&msg);
p->state = R_Open;
break;
case Stop:
Snd_DPR(p);
p->state = Closing;
break;
case R_Rcv_DPR:
Snd_DPA(p,msg,AAA_SUCCESS,p->R_sock);
R_Disc(p);
p->state = Closed;
log_peer_list(L_INFO);
break;
case R_Peer_Disc:
R_Disc(p);
p->state = Closed;
log_peer_list(L_INFO);
break;
case R_Rcv_CER:
result_code = Process_CER(p,msg);
Snd_CEA(p,msg,result_code,p->R_sock);
if (result_code>=2000 && result_code<3000)
p->state = R_Open;
else {
/*R_Disc(p);p.state = Closed;*/
p->state = R_Open; /* Or maybe I should disconnect it?*/
}
break;
case R_Rcv_CEA:
result_code = Process_CEA(p,msg);
if (result_code>=2000 && result_code<3000)
p->state = R_Open;
else {
/*R_Disc(p);p.state = Closed;*/
p->state = R_Open; /* Or maybe I should disconnect it?*/
}
log_peer_list(L_INFO);
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case I_Open:
switch (event){
case Send_Message:
Snd_Message(p,msg);
p->state = I_Open;
break;
case I_Rcv_Message:
// delayed processing until out of the critical zone
//Rcv_Process(p,msg);
msg_received = 1;
p->state = I_Open;
break;
case I_Rcv_DWR:
result_code = Process_DWR(p,msg);
Snd_DWA(p,msg,result_code,p->I_sock);
p->state =I_Open;
break;
case I_Rcv_DWA:
Process_DWA(p,msg);
p->state =I_Open;
break;
case R_Conn_CER:
R_Reject(p,sock);
AAAFreeMessage(&msg);
p->state = I_Open;
break;
case Stop:
Snd_DPR(p);
p->state = Closing;
break;
case I_Rcv_DPR:
Snd_DPA(p,msg,2001,p->I_sock);
I_Disc(p);
p->state = Closed;
log_peer_list(L_INFO);
break;
case I_Peer_Disc:
I_Disc(p);
p->state = Closed;
log_peer_list(L_INFO);
break;
case I_Rcv_CER:
result_code = Process_CER(p,msg);
Snd_CEA(p,msg,result_code,p->I_sock);
if (result_code>=2000 && result_code<3000)
p->state = I_Open;
else {
/*I_Disc(p);p.state = Closed;*/
p->state = I_Open; /* Or maybe I should disconnect it?*/
}
break;
case I_Rcv_CEA:
result_code = Process_CEA(p,msg);
if (result_code>=2000 && result_code<3000)
p->state = I_Open;
else {
/*I_Disc(p);p.state = Closed;*/
p->state = I_Open; /* Or maybe I should disconnect it?*/
}
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
case Closing:
switch(event){
case I_Rcv_DPA:
I_Disc(p);
p->state = Closed;
break;
case R_Rcv_DPA:
R_Disc(p);
p->state = Closed;
break;
case Timeout:
if (p->I_sock>=0) Error(p,p->I_sock);
if (p->R_sock>=0) Error(p,p->R_sock);
p->state = Closed;
break;
case I_Peer_Disc:
I_Disc(p);
p->state = Closed;
break;
case R_Peer_Disc:
R_Disc(p);
p->state = Closed;
break;
default:
LM_ERR("sm_process(): In state %s invalid event %s\n",
dp_states[p->state],dp_events[event-101]);
goto error;
}
break;
}
if (!peer_locked) lock_release(p->lock);
if (msg_received)
Rcv_Process(p,msg);
return 1;
error:
if (!peer_locked) lock_release(p->lock);
return 0;
}
/**
* Initiator - Send Connection Request.
* Tries to connect to the remote peer's socket. If the connection is refused, the new
* state is I_Rcv_Conn_NAck, else I_Rcv_Conn_Ack.
* \note Must be called with a lock on the peer.
* @param p - peer to send to
* @returns the new state for the peer
*/
peer_event_t I_Snd_Conn_Req(peer *p)
{
LM_INFO("I_Snd_Conn_Req(): Peer %.*s \n",
p->fqdn.len,p->fqdn.s);
if (p->I_sock>0) close(p->I_sock);
p->I_sock = -1;
p->I_sock = peer_connect(p);
if (p->I_sock<0){
return I_Rcv_Conn_NAck;
}
return I_Rcv_Conn_Ack;
}
/**
* Cleans a socket by closing it and removing the reference
* \note Must be called with a lock on the peer.
* @param p - the peer
* @param sock - socket to close
*/
void Cleanup(peer *p,int sock)
{
if (sock<0) return;
close(sock);
if (p->I_sock == sock) p->I_sock = -1;
if (p->R_sock == sock) p->R_sock = -1;
}
/**
* Error action for a peer, triggers a Cleanup action.
* \note Must be called with a lock on the peer.
* @param p - the peer
* @param sock - socket to close
*/
void Error(peer *p, int sock)
{
Cleanup(p,sock);
}
/**
* Adds the Applications to a Capability Exchange message.
* \note Must be called with a lock on the peer.
* @param msg - the message to add to (request/answer)
* @param p - the peer to add applications from
*/
static inline void Snd_CE_add_applications(AAAMessage *msg,peer *p)
{
int i;
app_config *app;
char x[4];
AAA_AVP *avp1,*avp2;
AAA_AVP_LIST list;
str group;
list.head=0;list.tail=0;
for(i=0;i<config->applications_cnt;i++){
app = config->applications+i;
if (app->vendor==0){
set_4bytes(x,app->id);
AAACreateAndAddAVPToMessage(msg,
(app->type==DP_AUTHORIZATION?AVP_Auth_Application_Id:AVP_Acct_Application_Id),
AAA_AVP_FLAG_MANDATORY,0,x,4);
}else{
set_4bytes(x,app->vendor);
avp1 = AAACreateAVP(AVP_Vendor_Id,AAA_AVP_FLAG_MANDATORY,0,x,4, AVP_DUPLICATE_DATA);
AAAAddAVPToList(&list,avp1);
set_4bytes(x,app->id);
avp2 = AAACreateAVP((app->type==DP_AUTHORIZATION?AVP_Auth_Application_Id:AVP_Acct_Application_Id),
AAA_AVP_FLAG_MANDATORY,0,x,4,AVP_DUPLICATE_DATA);
AAAAddAVPToList(&list,avp2);
group = AAAGroupAVPS(list);
AAAFreeAVPList(&list);
AAACreateAndAddAVPToMessage(msg,
AVP_Vendor_Specific_Application_Id,
AAA_AVP_FLAG_MANDATORY,0,group.s,group.len);
shm_free(group.s);
}
}
for(i=0;i<config->supported_vendors_cnt;i++){
set_4bytes(x,config->supported_vendors[i]);
AAACreateAndAddAVPToMessage(msg,AVP_Supported_Vendor_Id,AAA_AVP_FLAG_MANDATORY,0,x,4);
}
}
/**
* Send a Capability Exchange Request.
* \note Must be called with a lock on the peer.
* @param p - the peer to send to
*/
void I_Snd_CER(peer *p)
{
AAAMessage *cer=0;
// AAA_AVP *avp;
unsigned long ip;
union {
struct sockaddr addr;
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr_u ;
socklen_t addrlen;
char x[18];
cer = AAANewMessage(Code_CE,0,0,0);
if (!cer) return;
cer->hopbyhopId = next_hopbyhop();
cer->endtoendId = next_endtoend();
addrlen = sizeof(addr_u);
if (getsockname(p->I_sock,&(addr_u.addr), &addrlen) == -1) {
LM_ERR("I_Snd_CER(): Error on finding local host address > %s\n",strerror(errno));
}else{
switch(addr_u.addr.sa_family){
case AF_INET:
set_2bytes(x,1);
ip = htonl(addr_u.in.sin_addr.s_addr);
set_4bytes(x+2,ip);
AAACreateAndAddAVPToMessage(cer,AVP_Host_IP_Address,AAA_AVP_FLAG_MANDATORY,0,x,6);
break;
case AF_INET6:
set_2bytes(x,2);
memcpy(x+2,addr_u.in6.sin6_addr.s6_addr,16);
AAACreateAndAddAVPToMessage(cer,AVP_Host_IP_Address,AAA_AVP_FLAG_MANDATORY,0,x,18);
break;
default:
LM_ERR("I_Snd_CER(): unknown address type with family %d\n",addr_u.addr.sa_family);
}
}
set_4bytes(x,config->vendor_id);
AAACreateAndAddAVPToMessage(cer,AVP_Vendor_Id,AAA_AVP_FLAG_MANDATORY,0,x,4);
AAACreateAndAddAVPToMessage(cer,AVP_Product_Name,AAA_AVP_FLAG_MANDATORY,0,config->product_name.s,config->product_name.len);
Snd_CE_add_applications(cer,p);
// peer_send(p,p->I_sock,cer,1);
peer_send_msg(p,cer);
}
void add_peer_application(peer *p, int id, int vendor, app_type type)
{
int i;
if (!p->applications) return;
for(i=0;i<p->applications_cnt;i++)
if (p->applications[i].id == id &&
p->applications[i].vendor == vendor &&
p->applications[i].type == type) return;
if (p->applications_cnt >= p->applications_max) {
LM_ERR("Too many applications for this peer (max %i), not adding Application %i:%i.\n", p->applications_max, id, vendor);
return;
}
p->applications[p->applications_cnt].id = id;
p->applications[p->applications_cnt].vendor = vendor;
p->applications[p->applications_cnt].type = type;
LM_DBG("Application %i of maximum %i\n", p->applications_cnt, p->applications_max);
p->applications_cnt++;
}
int count_Supported_Vendor_Id_AVPS(AAAMessage *msg)
{
AAA_AVP* avp_vendor;
int avp_vendor_cnt;
avp_vendor = AAAFindMatchingAVP(msg,0,AVP_Supported_Vendor_Id,0,0);
avp_vendor_cnt = 0;
while (avp_vendor) {
avp_vendor_cnt++;
if (!avp_vendor->next)
break;
avp_vendor = AAAFindMatchingAVP(msg,avp_vendor->next,AVP_Supported_Vendor_Id,0,0);
}
LM_DBG("Found %i Supported_Vendor AVPS\n", avp_vendor_cnt);
return avp_vendor_cnt;
}
void save_peer_applications(peer *p,AAAMessage *msg)
{
int total_cnt=0;
int supported_vendor_id_avp_cnt = 0;
AAA_AVP *avp,*avp_vendor,*avp2;
AAA_AVP_LIST group;
int id,vendor;
if (p->applications) {
shm_free(p->applications);
p->applications = 0;
p->applications_cnt = 0;
}
supported_vendor_id_avp_cnt = count_Supported_Vendor_Id_AVPS(msg);
for(avp=msg->avpList.head;avp;avp = avp->next)
switch (avp->code){
case AVP_Auth_Application_Id:
total_cnt += supported_vendor_id_avp_cnt;
break;
case AVP_Acct_Application_Id:
total_cnt += supported_vendor_id_avp_cnt;
break;
case AVP_Vendor_Specific_Application_Id:
total_cnt+=2;/* wasteful, but let's skip decoding */
break;
}
p->applications_cnt = 0;
p->applications = shm_malloc(sizeof(app_config)*total_cnt);
p->applications_max = total_cnt;
if (!p->applications){
LM_ERR("save_peer_applications(): Error allocating %ld bytes! No applications saved...\n",
(long int)(sizeof(app_config)*total_cnt));
return;
}
for(avp=msg->avpList.head;avp;avp = avp->next)
{
switch (avp->code){
case AVP_Auth_Application_Id:
id = get_4bytes(avp->data.s);
add_peer_application(p,id,0,DP_AUTHORIZATION);
avp_vendor = AAAFindMatchingAVP(msg,0,AVP_Supported_Vendor_Id,0,0);
while (avp_vendor) {
vendor = get_4bytes(avp_vendor->data.s);
LM_DBG("Found Supported Vendor for Application %i: %i\n", DP_AUTHORIZATION, vendor);
add_peer_application(p,id,vendor,DP_AUTHORIZATION);
if (!avp_vendor->next)
break;
avp_vendor = AAAFindMatchingAVP(msg,avp_vendor->next,AVP_Supported_Vendor_Id,0,AAA_FORWARD_SEARCH);
}
break;
case AVP_Acct_Application_Id:
id = get_4bytes(avp->data.s);
add_peer_application(p,id,0,DP_ACCOUNTING);
avp_vendor = AAAFindMatchingAVP(msg,0,AVP_Supported_Vendor_Id,0,0);
while (avp_vendor) {
vendor = get_4bytes(avp_vendor->data.s);
LM_DBG("Found Supported Vendor for Application %i: %i\n", DP_ACCOUNTING, vendor);
add_peer_application(p,id,vendor,DP_ACCOUNTING);
if (!avp_vendor->next)
break;
avp_vendor = AAAFindMatchingAVP(msg,avp_vendor->next,AVP_Supported_Vendor_Id,0,AAA_FORWARD_SEARCH);
}
break;
case AVP_Vendor_Specific_Application_Id:
group = AAAUngroupAVPS(avp->data);
avp_vendor = AAAFindMatchingAVPList(group,group.head,AVP_Vendor_Id,0,0);
avp2 = AAAFindMatchingAVPList(group,group.head,AVP_Auth_Application_Id,0,AAA_FORWARD_SEARCH);
if (avp_vendor&&avp2){
vendor = get_4bytes(avp_vendor->data.s);
id = get_4bytes(avp2->data.s);
add_peer_application(p,id,vendor,DP_AUTHORIZATION);
}
avp2 = AAAFindMatchingAVPList(group,group.head,AVP_Acct_Application_Id,0,AAA_FORWARD_SEARCH);
if (avp_vendor&&avp2){
vendor = get_4bytes(avp_vendor->data.s);
id = get_4bytes(avp2->data.s);
add_peer_application(p,id,vendor,DP_ACCOUNTING);
}
AAAFreeAVPList(&group);
break;
}
}
}
/**
* Process a Capability Exchange Answer.
* \note Must be called with a lock on the peer.
* @param p - the peer that the CEA was received from
* @param cea - the CEA message
* @returns the result-code from CEA or AAA_UNABLE_TO_COMPLY if no result-code found
*/
int Process_CEA(peer *p,AAAMessage *cea)
{
AAA_AVP *avp;
avp = AAAFindMatchingAVP(cea,cea->avpList.head,AVP_Result_Code,0,0);
save_peer_applications(p,cea);
AAAFreeMessage(&cea);
if (!avp) return AAA_UNABLE_TO_COMPLY;
else return get_4bytes(avp->data.s);
}
/**
* Initiator - disconnect peer.
* \note Must be called with a lock on the peer.
* @param p - the peer to disconnect
*/
void I_Disc(peer *p)
{
if (p->I_sock>=0){
close(p->I_sock);
p->I_sock = -1;
}
}
/**
* Receiver - disconnect peer.
* \note Must be called with a lock on the peer.
* @param p - the peer to disconnect
*/
void R_Disc(peer *p)
{
if (p->R_sock>=0){
close(p->R_sock);
p->R_sock = -1;
}
}
/**
* Process a Diameter Watch-dog Request.
* The last activity timer is updated in the sm_process(), on any message received.
* \note Must be called with a lock on the peer.
* @param p - the peer that the DWR was received from
* @param dwr - the DWR message
* @returns AAA_SUCCESS
*/
int Process_DWR(peer *p,AAAMessage *dwr)
{
return AAA_SUCCESS;
}
/**
* Process a Diameter Watch-dog Answer.
* The flag for waiting a DWA is reseted.
* \note Must be called with a lock on the peer.
* @param p - the peer that the DWR was received from
* @param dwa - the DWA message
*/
void Process_DWA(peer *p,AAAMessage *dwa)
{
p->waitingDWA = 0;
AAAFreeMessage(&dwa);
}
/**
* Sends a Diameter Watch-dog Request.
* The flag for waiting a DWA is set.
* \note Must be called with a lock on the peer.
* @param p - the peer that the DWR was received from
*/
void Snd_DWR(peer *p)
{
AAAMessage *dwr=0;
dwr = AAANewMessage(Code_DW,0,0,0);
if (!dwr) return;
dwr->hopbyhopId = next_hopbyhop();
dwr->endtoendId = next_endtoend();
if (p->state==I_Open)
peer_send_msg(p,dwr);
else
peer_send_msg(p,dwr);
}
/**
* Sends a Diameter Watch-dog Answer.
* \note Must be called with a lock on the peer.
* @param p - the peer that the DWR was received from
* @param dwr - the DWR message
* @param result_code - the Result-Code to attach to DWA
* @param sock - socket to send on
*/
void Snd_DWA(peer *p,AAAMessage *dwr,int result_code,int sock)
{
AAAMessage *dwa;
char x[4];
dwa = AAANewMessage(Code_DW,0,0,dwr);
if (!dwa) goto done;
set_4bytes(x,result_code);
AAACreateAndAddAVPToMessage(dwa,AVP_Result_Code,AAA_AVP_FLAG_MANDATORY,0,x,4);
peer_send_msg(p,dwa);
done:
AAAFreeMessage(&dwr);
}
/**
* Sends a Disconnect Peer Request.
* \note Must be called with a lock on the peer.
* @param p - the peer to send to
*/
void Snd_DPR(peer *p)
{
AAAMessage *dpr=0;
char x[4];
dpr = AAANewMessage(Code_DP,0,0,0);
if (!dpr) return;
dpr->hopbyhopId = next_hopbyhop();
dpr->endtoendId = next_endtoend();
set_4bytes(x,0/*busy*/);
AAACreateAndAddAVPToMessage(dpr,AVP_Disconnect_Cause,AAA_AVP_FLAG_MANDATORY,0,x,4);
if (p->state==I_Open)
peer_send_msg(p,dpr);
else
peer_send_msg(p,dpr);
}
/**
* Sends a Disconnect Peer Answer.
* \note Must be called with a lock on the peer.
* @param p - the peer to send to
* @param dpr - the DPR message
* @param result_code - the Result-Code to attach to DPA
* @param sock - socket to send on
*/
void Snd_DPA(peer *p,AAAMessage *dpr,int result_code,int sock)
{
AAAMessage *dpa;
dpa = AAANewMessage(Code_DP,0,0,dpr);
if (dpa) peer_send_msg(p,dpa);
AAAFreeMessage(&dpr);
}
/**
* Receiver - Accept a connection.
* \note Must be called with a lock on the peer.
* @param p - peer identification
* @param sock - socket to communicate through
*/
void R_Accept(peer *p,int sock)
{
p->R_sock = sock;
touch_peer(p);
}
/**
* Receiver - Reject a connection.
* \note Must be called with a lock on the peer.
* @param p - peer identification
* @param sock - socket to communicate through
*/
void R_Reject(peer *p,int sock)
{
close(sock);
}
/**
* Process a Capabilities Exchange Request.
* Checks whether there are common applications.
* \note Must be called with a lock on the peer.
* @param p - peer identification
* @param cer - the CER message
* @returns the Result-Code of the operation, AAA_SUCCESS or AAA_NO_COMMON_APPLICATION
*/
int Process_CER(peer *p,AAAMessage *cer)
{
int common_app=0;
AAA_AVP *avp,*avp_vendor,*avp2;
AAA_AVP_LIST group;
int i,id,vendor;
for(avp=cer->avpList.head;avp;avp = avp->next)
{
switch (avp->code){
case AVP_Auth_Application_Id:
id = get_4bytes(avp->data.s);
for(i=0;i<config->applications_cnt;i++)
if (id == config->applications[i].id &&
config->applications[i].vendor==0 &&
config->applications[i].type==DP_AUTHORIZATION) common_app++;
break;
case AVP_Acct_Application_Id:
id = get_4bytes(avp->data.s);
for(i=0;i<config->applications_cnt;i++)
if (id == config->applications[i].id &&
config->applications[i].vendor==0 &&
config->applications[i].type==DP_ACCOUNTING) common_app++;
break;
case AVP_Vendor_Specific_Application_Id: