-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathscenario.cpp
3403 lines (3159 loc) · 131 KB
/
scenario.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program 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.
*
* This program 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
*
* Author : Richard GAYRAUD - 04 Nov 2003
* Olivier Jacques
* From Hewlett Packard Company.
* Shriram Natarajan
* Peter Higginson
* Venkatesh
* Lee Ballard
* Guillaume TEISSIER from FTR&D
* Wolfgang Beck
* Marc Van Diest from Belgacom
* Charles P. Wright from IBM Research
* Michael Stovenour
*/
#include <stdlib.h>
#include "config.h"
#include "sipp.hpp"
#ifdef HAVE_GSL
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_cdf.h>
#endif
/************************ Class Constructor *************************/
message::message(int index, const char *desc)
{
this->index = index;
this->desc = desc;
pause_distribution = NULL; // delete on exit
pause_variable = -1;
pause_desc = NULL; // free on exit
sessions = 0;
bShouldRecordRoutes = 0;
bShouldAuthenticate = 0;
send_scheme = NULL; // delete on exit
retrans_delay = 0;
timeout = 0;
recv_response = 0;
recv_request = NULL; // free on exit
optional = 0;
advance_state = true;
regexp_match = 0;
regexp_compile = NULL; // regfree (if not NULL) and free on exit
/* Anyway */
start_rtd = 0;
stop_rtd = 0;
repeat_rtd = 0;
lost = -1;
crlf = 0;
ignoresdp = false;
hide = 0;
display_str = NULL; // free on exit
test = -1;
condexec = -1;
condexec_inverse = false;
chance = 0;/* meaning always */
next = -1;
nextLabel = NULL; // free on exit
on_timeout = -1;
onTimeoutLabel = NULL; // free on exit
timewait = false;
/* 3pcc extended mode */
peer_dest = NULL; // free on exit
peer_src = NULL; // free on exit
/* Statistics */
nb_sent = 0;
nb_recv = 0;
nb_sent_retrans = 0;
nb_recv_retrans = 0;
nb_timeout = 0;
nb_unexp = 0;
nb_lost = 0;
counter = 0;
M_actions = NULL; // delete on exit
M_type = 0;
M_sendCmdData = NULL; // delete on exit
M_nbCmdSent = 0;
M_nbCmdRecv = 0;
content_length_flag = ContentLengthNoPresent;
/* How to match responses to this message. */
start_txn = 0;
response_txn = 0;
ack_txn = 0;
recv_response_for_cseq_method_list = NULL; // free on exit
}
message::~message()
{
delete(pause_distribution);
free(pause_desc);
delete(send_scheme);
free(recv_request);
if (regexp_compile != NULL) {
regfree(regexp_compile);
}
free(regexp_compile);
free(display_str);
free(nextLabel);
free(onTimeoutLabel);
free(peer_dest);
free(peer_src);
delete(M_actions);
delete(M_sendCmdData);
free(recv_response_for_cseq_method_list);
}
/******** Global variables which compose the scenario file **********/
scenario *main_scenario;
scenario *ooc_scenario;
scenario *aa_scenario;
scenario *display_scenario;
/* This mode setting refers to whether we open calls autonomously (MODE_CLIENT)
* or in response to requests (MODE_SERVER). */
int creationMode = MODE_CLIENT;
/* Send mode. Do we send to a fixed address or to the last one we got. */
int sendMode = MODE_CLIENT;
/* This describes what our 3PCC behavior is. */
int thirdPartyMode = MODE_3PCC_NONE;
/*************** Helper functions for various types *****************/
long get_long(const char *ptr, const char *what)
{
char *endptr;
long ret;
ret = strtol(ptr, &endptr, 0);
if (*endptr) {
ERROR("%s, \"%s\" is not a valid integer!", what, ptr);
}
return ret;
}
unsigned long long get_long_long(const char *ptr, const char *what)
{
char *endptr;
unsigned long long ret;
ret = strtoull(ptr, &endptr, 0);
if (*endptr) {
ERROR("%s, \"%s\" is not a valid integer!", what, ptr);
}
return ret;
}
/* This function returns a time in milliseconds from a string.
* The multiplier is used to convert from the default input type into
* milliseconds. For example, for seconds you should use 1000 and for
* milliseconds use 1. */
long get_time(const char *ptr, const char *what, int multiplier)
{
char *endptr;
const char *p;
long ret = 0;
double dret;
int i;
if (!isdigit(*ptr)) {
ERROR("%s, \"%s\" is not a valid time!", what, ptr);
}
for (i = 0, p = ptr; *p; p++) {
if (*p == ':') {
i++;
}
}
if (i == 1) { /* mm:ss */
ERROR("%s, \"%s\" mm:ss not implemented yet!", what, ptr);
} else if (i == 2) { /* hh:mm:ss */
ERROR("%s, \"%s\" hh:mm:ss not implemented yet!", what, ptr);
} else if (i != 0) {
ERROR("%s, \"%s\" is not a valid time!", what, ptr);
}
dret = strtod(ptr, &endptr);
if (*endptr) {
if (!strcmp(endptr, "s")) { /* Seconds */
ret = (long)(dret * 1000);
} else if (!strcmp(endptr, "ms")) { /* Milliseconds. */
ret = (long)dret;
} else if (!strcmp(endptr, "m")) { /* Minutes. */
ret = (long)(dret * 60000);
} else if (!strcmp(endptr, "h")) { /* Hours. */
ret = (long)(dret * 60 * 60 * 1000);
} else {
ERROR("%s, \"%s\" is not a valid time!", what, ptr);
}
} else {
ret = (long)(dret * multiplier);
}
return ret;
}
double get_double(const char *ptr, const char *what)
{
char *endptr;
double ret;
ret = strtod(ptr, &endptr);
if (*endptr) {
ERROR("%s, \"%s\" is not a floating point number!", what, ptr);
}
return ret;
}
/* If the value is enclosed in [brackets], it is assumed to be
* a command-line supplied keyword value (-key). */
static char* xp_get_keyword_value(const char *name)
{
const char* ptr = xp_get_value(name);
return ptr ? strdup(ptr) : NULL;
}
static char* xp_get_string(const char *name, const char *what)
{
const char *ptr;
char *unescaped;
if (!(ptr = xp_get_value(name))) {
ERROR("%s is missing the required '%s' parameter.", what, name);
}
unescaped = (char *)malloc(strlen(ptr) + 1);
if (!unescaped) {
ERROR("Out of memory!");
}
xp_unescape(ptr, unescaped);
return unescaped;
}
static double xp_get_double(const char *name, const char *what)
{
const char *ptr;
char *helptext;
double val;
if (!(ptr = xp_get_value(name))) {
ERROR("%s is missing the required '%s' parameter.", what, name);
}
helptext = (char *)malloc(100 + strlen(name) + strlen(what));
sprintf(helptext, "%s '%s' parameter", what, name);
val = get_double(ptr, helptext);
free(helptext);
return val;
}
static long xp_get_long(const char *name, const char *what)
{
const char *ptr;
char *helptext;
long val;
if (!(ptr = xp_get_value(name))) {
ERROR("%s is missing the required '%s' parameter.", what, name);
}
helptext = (char *)malloc(100 + strlen(name) + strlen(what));
sprintf(helptext, "%s '%s' parameter", what, name);
val = get_long(ptr, helptext);
free(helptext);
return val;
}
static long xp_get_long(const char *name, const char *what, long defval)
{
if (!(xp_get_value(name))) {
return defval;
}
return xp_get_long(name, what);
}
static bool xp_get_bool(const char *name, const char *what)
{
const char *ptr;
char *helptext;
bool val;
if (!(ptr = xp_get_value(name))) {
ERROR("%s is missing the required '%s' parameter.", what, name);
}
helptext = (char *)malloc(100 + strlen(name) + strlen(what));
sprintf(helptext, "%s '%s' parameter", what, name);
val = get_bool(ptr, helptext);
free(helptext);
return val;
}
static bool xp_get_bool(const char *name, const char *what, bool defval)
{
if (!(xp_get_value(name))) {
return defval;
}
return xp_get_bool(name, what);
}
int scenario::get_txn(const char *txnName, const char *what, bool start, bool isInvite, bool isAck)
{
/* Check the name's validity. */
if (txnName[0] == '\0') {
ERROR("Variable names may not be empty for %s", what);
}
if (strcspn(txnName, "$,") != strlen(txnName)) {
ERROR("Variable names may not contain $ or , for %s", what);
}
/* If this transaction has already been used, then we have nothing to do. */
str_int_map::iterator txn_it = txnMap.find(txnName);
if (txn_it != txnMap.end()) {
if (start) {
/* We need to fill in the invite field. */
transactions[txn_it->second - 1].started++;
} else if (isAck) {
transactions[txn_it->second - 1].acks++;
} else {
transactions[txn_it->second - 1].responses++;
}
return txn_it->second;
}
/* Assign this variable the next slot. */
struct txnControlInfo transaction;
transaction.name = strdup(txnName);
if (start) {
transaction.started = 1;
transaction.responses = 0;
transaction.acks = 0;
transaction.isInvite = isInvite;
} else if (isAck) {
/* Does not start or respond to this txn. */
transaction.started = 0;
transaction.responses = 0;
transaction.acks = 1;
transaction.isInvite = false;
} else {
transaction.started = 0;
transaction.responses = 1;
transaction.acks = 0;
transaction.isInvite = false;
}
transactions.push_back(transaction);
int txnNum = transactions.size();
txnMap[txnName] = txnNum;
return txnNum;
}
int scenario::find_var(const char *varName)
{
return allocVars->find(varName, false);
}
void scenario::addRtpTaskThreadID(pthread_t id)
{
threadIDs[id] = "threadID";
}
void scenario::removeRtpTaskThreadID(pthread_t id)
{
threadIDs.erase(id);
}
std::unordered_map<pthread_t, std::string>& scenario::fetchRtpTaskThreadIDs()
{
return threadIDs;
}
int scenario::get_var(const char *varName, const char *what)
{
/* Check the name's validity. */
if (varName[0] == '\0') {
ERROR("Transaction names may not be empty for %s", what);
}
if (strcspn(varName, "$,") != strlen(varName)) {
ERROR("Transaction names may not contain '$' or ',' for %s", what);
}
return allocVars->find(varName, true);
}
int scenario::xp_get_var(const char *name, const char *what)
{
const char *ptr;
if (!(ptr = xp_get_value(name))) {
ERROR("%s is missing the required '%s' variable parameter.", what, name);
}
return get_var(ptr, what);
}
static int xp_get_optional(const char *name, const char *what)
{
const char *ptr = xp_get_value(name);
if (!ptr) {
return OPTIONAL_FALSE;
}
if(!strcmp(ptr, "true")) {
return OPTIONAL_TRUE;
} else if(!strcmp(ptr, "global")) {
return OPTIONAL_GLOBAL;
} else if(!strcmp(ptr, "false")) {
return OPTIONAL_FALSE;
} else {
ERROR("Could not understand optional value for %s: %s", what, ptr);
}
return OPTIONAL_FALSE;
}
int scenario::xp_get_var(const char *name, const char *what, int defval)
{
const char *ptr;
if (!(ptr = xp_get_value(name))) {
return defval;
}
return xp_get_var(name, what);
}
bool get_bool(const char *ptr, const char *what)
{
char *endptr;
long ret;
if (!strcasecmp(ptr, "true")) {
return true;
}
if (!strcasecmp(ptr, "false")) {
return false;
}
ret = strtol(ptr, &endptr, 0);
if (*endptr) {
ERROR("%s, \"%s\" is not a valid boolean!", what, ptr);
}
return ret ? true : false;
}
/* Pretty print a time. */
int time_string(double ms, char *res, int reslen)
{
if (ms < 10000) {
/* Less then 10 seconds we represent accurately. */
if ((int)(ms + 0.9999) == (int)(ms)) {
/* We have an integer, or close enough to it. */
return snprintf(res, reslen, "%dms", (int)ms);
} else {
if (ms < 1000) {
return snprintf(res, reslen, "%.2lfms", ms);
} else {
return snprintf(res, reslen, "%.1lfms", ms);
}
}
} else if (ms < 60000) {
/* We round to 100ms for times less than a minute. */
return snprintf(res, reslen, "%.1fs", ms/1000);
} else if (ms < 60 * 60000) {
/* We round to 1s for times more than a minute. */
int s = (unsigned int)(ms / 1000);
int m = s / 60;
s %= 60;
return snprintf(res, reslen, "%d:%02d", m, s);
} else {
int s = (unsigned int)(ms / 1000);
int m = s / 60;
int h = m / 60;
s %= 60;
m %= 60;
return snprintf(res, reslen, "%d:%02d:%02d", h, m, s);
}
}
/* For backwards compatibility, we assign "true" to slot 1, false to 0, and
* allow other valid integers. */
int scenario::get_rtd(const char *ptr, bool start)
{
if(!strcmp(ptr, (char *)"false"))
return 0;
if(!strcmp(ptr, (char *)"true"))
return stats->findRtd("1", start);
return stats->findRtd(ptr, start);
}
/* Get a counter */
int scenario::get_counter(const char *ptr, const char *what)
{
/* Check the name's validity. */
if (ptr[0] == '\0') {
ERROR("Counter names names may not be empty for %s", what);
}
if (strcspn(ptr, "$,") != strlen(ptr)) {
ERROR("Counter names may not contain $ or , for %s", what);
}
return stats->findCounter(ptr, true);
}
/* Some validation functions. */
void scenario::validate_variable_usage()
{
allocVars->validate();
}
void scenario::validate_txn_usage()
{
for (unsigned int i = 0; i < transactions.size(); i++) {
if(transactions[i].started == 0) {
ERROR("Transaction %s is never started!", transactions[i].name);
} else if(transactions[i].responses == 0) {
ERROR("Transaction %s has no responses defined!", transactions[i].name);
}
if (transactions[i].isInvite && transactions[i].acks == 0) {
ERROR("Transaction %s is an INVITE transaction without an ACK!", transactions[i].name);
}
if (!transactions[i].isInvite && (transactions[i].acks > 0)) {
ERROR("Transaction %s is a non-INVITE transaction with an ACK!", transactions[i].name);
}
}
}
/* Apply the next and ontimeout labels according to our map. */
void scenario::apply_labels(msgvec v, str_int_map labels)
{
for (unsigned int i = 0; i < v.size(); i++) {
if (v[i]->nextLabel) {
str_int_map::iterator label_it = labels.find(v[i]->nextLabel);
if (label_it == labels.end()) {
ERROR("The label '%s' was not defined (index %d, next attribute)", v[i]->nextLabel, i);
}
v[i]->next = label_it->second;
}
if (v[i]->onTimeoutLabel) {
str_int_map::iterator label_it = labels.find(v[i]->onTimeoutLabel);
if (label_it == labels.end()) {
ERROR("The label '%s' was not defined (index %d, ontimeout attribute)", v[i]->onTimeoutLabel, i);
}
v[i]->on_timeout = label_it->second;
}
}
}
int get_cr_number(const char *src)
{
int res=0;
while(*src) {
if(*src == '\n') res++;
src++;
}
return res;
}
static char* clean_cdata(char *ptr, int *removed_crlf = NULL)
{
char * msg;
while((*ptr == ' ') || (*ptr == '\t') || (*ptr == '\n')) ptr++;
msg = (char *) malloc(strlen(ptr) + 3);
if(!msg) {
ERROR("Memory Overflow");
}
strcpy(msg, ptr);
ptr = msg + strlen(msg);
ptr--;
while((ptr >= msg) &&
((*ptr == ' ') ||
(*ptr == '\t') ||
(*ptr == '\n'))) {
if(*ptr == '\n' && removed_crlf) {
(*removed_crlf)++;
}
*ptr-- = 0;
}
if(ptr == msg) {
ERROR("Empty cdata in xml scenario file");
}
while ((ptr = strstr(msg, "\n "))) {
memmove(ptr + 1, ptr + 2, strlen(ptr) - 1);
}
while ((ptr = strstr(msg, " \n"))) {
memmove(ptr, ptr + 1, strlen(ptr));
}
while ((ptr = strstr(msg, "\n\t"))) {
memmove(ptr + 1, ptr + 2, strlen(ptr) - 1);
}
while ((ptr = strstr(msg, "\t\n"))) {
memmove(ptr, ptr + 1, strlen(ptr));
}
if (!strstr(msg, "\n\n")) {
strcat(msg, "\n\n");
}
return msg;
}
/********************** Scenario File analyser **********************/
void scenario::checkOptionalRecv(char *elem, unsigned int scenario_file_cursor)
{
if (last_recv_optional) {
ERROR("<recv> before <%s> sequence without a mandatory message. Please remove one 'optional=true' (element %d).", elem, scenario_file_cursor);
}
last_recv_optional = false;
}
scenario::scenario(char * filename, int deflt)
{
char * elem;
char *method_list = NULL;
unsigned int scenario_file_cursor = 0;
int L_content_length = 0 ;
char * peer;
const char* cptr;
last_recv_optional = false;
if(filename) {
if(!xp_set_xml_buffer_from_file(filename)) {
ERROR("Unable to load or parse '%s' xml scenario file", filename);
}
} else {
if(!xp_set_xml_buffer_from_string(default_scenario[deflt])) {
ERROR("Unable to load default xml scenario file");
}
}
stats = new CStat();
allocVars = new AllocVariableTable(userVariables);
hidedefault = false;
elem = xp_open_element(0);
if (!elem) {
ERROR("No element in xml scenario file");
}
if(strcmp("scenario", elem)) {
ERROR("No 'scenario' section in xml scenario file");
}
if ((cptr = xp_get_value("name"))) {
name = strdup(cptr);
} else {
name = strdup("");
}
duration = 0;
found_timewait = false;
scenario_file_cursor = 0;
while ((elem = xp_open_element(scenario_file_cursor))) {
char * ptr;
scenario_file_cursor ++;
if(!strcmp(elem, "CallLengthRepartition")) {
ptr = xp_get_string("value", "CallLengthRepartition");
stats->setRepartitionCallLength(ptr);
free(ptr);
} else if(!strcmp(elem, "ResponseTimeRepartition")) {
ptr = xp_get_string("value", "ResponseTimeRepartition");
stats->setRepartitionResponseTime(ptr);
free(ptr);
} else if(!strcmp(elem, "Global")) {
ptr = xp_get_string("variables", "Global");
char ** currentTabVarName = NULL;
int currentNbVarNames;
createStringTable(ptr, ¤tTabVarName, ¤tNbVarNames);
for (int i = 0; i < currentNbVarNames; i++) {
globalVariables->find(currentTabVarName[i], true);
}
freeStringTable(currentTabVarName, currentNbVarNames);
free(ptr);
} else if(!strcmp(elem, "User")) {
ptr = xp_get_string("variables", "User");
char ** currentTabVarName = NULL;
int currentNbVarNames;
createStringTable(ptr, ¤tTabVarName, ¤tNbVarNames);
for (int i = 0; i < currentNbVarNames; i++) {
userVariables->find(currentTabVarName[i], true);
}
freeStringTable(currentTabVarName, currentNbVarNames);
free(ptr);
} else if(!strcmp(elem, "Reference")) {
ptr = xp_get_string("variables", "Reference");
char ** currentTabVarName = NULL;
int currentNbVarNames;
createStringTable(ptr, ¤tTabVarName, ¤tNbVarNames);
for (int i = 0; i < currentNbVarNames; i++) {
int id = allocVars->find(currentTabVarName[i], false);
if (id == -1) {
ERROR("Could not reference non-existant variable '%s'", currentTabVarName[i]);
}
}
freeStringTable(currentTabVarName, currentNbVarNames);
free(ptr);
} else if(!strcmp(elem, "DefaultMessage")) {
char *id = xp_get_string("id", "DefaultMessage");
if(!(ptr = xp_get_cdata())) {
ERROR("No CDATA in 'send' section of xml scenario file");
}
char *msg = clean_cdata(ptr);
set_default_message(id, msg);
free(id);
/* XXX: This should really be per scenario. */
} else if(!strcmp(elem, "label")) {
ptr = xp_get_string("id", "label");
if (labelMap.find(ptr) != labelMap.end()) {
ERROR("The label name '%s' is used twice.", ptr);
}
labelMap[ptr] = messages.size();
free(ptr);
} else if (!strcmp(elem, "init")) {
/* We have an init section, which must be full of nops or labels. */
int nop_cursor = 0;
char *initelem;
while ((initelem = xp_open_element(nop_cursor++))) {
if (!strcmp(initelem, "nop")) {
/* We should parse this. */
message *nopmsg = new message(initmessages.size(), "scenario initialization");
initmessages.push_back(nopmsg);
nopmsg->M_type = MSG_TYPE_NOP;
getCommonAttributes(nopmsg);
} else if (!strcmp(initelem, "label")) {
/* Add an init label. */
cptr = xp_get_value("id");
if (initLabelMap.find(cptr) != initLabelMap.end()) {
ERROR("The label name '%s' is used twice.", cptr);
}
initLabelMap[cptr] = initmessages.size();
} else {
ERROR("Invalid element in an init stanza: '%s'", initelem);
}
xp_close_element();
}
} else { /** Message Case */
if (found_timewait) {
ERROR("<timewait> can only be the last message in a scenario!");
}
message *curmsg = new message(messages.size(), name ? name : "unknown scenario");
messages.push_back(curmsg);
if(!strcmp(elem, "send")) {
checkOptionalRecv(elem, scenario_file_cursor);
curmsg->M_type = MSG_TYPE_SEND;
/* Sent messages descriptions */
if(!(ptr = xp_get_cdata())) {
ERROR("No CDATA in 'send' section of xml scenario file");
}
int removed_clrf = 0;
char * msg = clean_cdata(ptr, &removed_clrf);
L_content_length = xp_get_content_length(msg);
switch (L_content_length) {
case -1 :
// the msg does not contain content-length field
break ;
case 0 :
curmsg -> content_length_flag =
message::ContentLengthValueZero; // Initialize to No present
break ;
default :
curmsg -> content_length_flag =
message::ContentLengthValueNoZero; // Initialize to No present
break ;
}
if((msg[strlen(msg) - 1] != '\n') && (removed_clrf)) {
strcat(msg, "\n");
}
char *tsrc = msg;
while(*tsrc++);
curmsg -> send_scheme = new SendingMessage(this, msg);
free(msg);
// If this is a request we are sending, then store our transaction/method matching information.
if (!curmsg->send_scheme->isResponse()) {
char *method = curmsg->send_scheme->getMethod();
bool isInvite = !strcmp(method, "INVITE");
bool isAck = !strcmp(method, "ACK");
if ((cptr = xp_get_value("start_txn"))) {
if (isAck) {
ERROR("An ACK message can not start a transaction!");
}
curmsg->start_txn = get_txn(cptr, "start transaction", true, isInvite, false);
} else if ((cptr = xp_get_value("ack_txn"))) {
if (!isAck) {
ERROR("The ack_txn attribute is valid only for ACK messages!");
}
curmsg->ack_txn = get_txn(cptr, "ack transaction", false, false, true);
} else {
int len = method_list ? strlen(method_list) : 0;
method_list = (char *)realloc(method_list, len + strlen(method) + 1);
if (!method_list) {
ERROR_NO("Out of memory allocating method_list!");
}
strcpy(method_list + len, method);
}
} else {
if (xp_get_value("start_txn")) {
ERROR("Responses can not start a transaction");
}
if (xp_get_value("ack_txn")) {
ERROR("Responses can not ACK a transaction");
}
}
if (xp_get_value("response_txn")) {
ERROR("response_txn can only be used for received messages.");
}
curmsg -> retrans_delay = xp_get_long("retrans", "retransmission timer", 0);
curmsg -> timeout = xp_get_long("timeout", "message send timeout", 0);
} else if (!strcmp(elem, "recv")) {
curmsg->M_type = MSG_TYPE_RECV;
/* Received messages descriptions */
if((cptr = xp_get_value("response"))) {
curmsg ->recv_response = get_long(cptr, "response code");
if (method_list) {
curmsg->recv_response_for_cseq_method_list = strdup(method_list);
}
if ((cptr = xp_get_value("response_txn"))) {
curmsg->response_txn = get_txn(cptr, "transaction response", false, false, false);
}
}
if ((cptr = xp_get_value("request"))) {
curmsg->recv_request = strdup(cptr);
if (xp_get_value("response_txn")) {
ERROR("response_txn can only be used for received responses.");
}
}
curmsg->optional = xp_get_optional("optional", "recv");
last_recv_optional = curmsg->optional;
curmsg->advance_state = xp_get_bool("advance_state", "recv", true);
if (!curmsg->advance_state && curmsg->optional == OPTIONAL_FALSE) {
ERROR("advance_state is allowed only for optional messages (index = %zu)", messages.size() - 1);
}
if ((cptr = xp_get_value("regexp_match"))) {
if (!strcmp(cptr, "true")) {
curmsg->regexp_match = 1;
}
}
curmsg->timeout = xp_get_long("timeout", "message timeout", 0);
/* record the route set */
/* TODO disallow optional and rrs to coexist? */
if ((cptr = xp_get_value("rrs"))) {
curmsg->bShouldRecordRoutes = get_bool(cptr, "record route set");
}
/* record the authentication credentials */
if ((cptr = xp_get_value("auth"))) {
bool temp = get_bool(cptr, "message authentication");
curmsg->bShouldAuthenticate = temp;
}
} else if(!strcmp(elem, "pause") || !strcmp(elem, "timewait")) {
checkOptionalRecv(elem, scenario_file_cursor);
curmsg->M_type = MSG_TYPE_PAUSE;
if (!strcmp(elem, "timewait")) {
curmsg->timewait = true;
found_timewait = true;
}
int var;
if ((var = xp_get_var("variable", "pause", -1)) != -1) {
curmsg->pause_variable = var;
} else {
CSample *distribution = parse_distribution(true);
bool sanity_check = xp_get_bool("sanity_check", "pause", true);
double pause_duration = distribution->cdfInv(0.99);
if (sanity_check && (pause_duration > INT_MAX)) {
char percentile[100];
char desc[100];
distribution->timeDescr(desc, sizeof(desc));
time_string(pause_duration, percentile, sizeof(percentile));
ERROR("The distribution %s has a 99th percentile of %s, which is larger than INT_MAX. You should chose different parameters.", desc, percentile);
}
curmsg->pause_distribution = distribution;
/* Update scenario duration with max duration */
duration += (int)pause_duration;
}
} else if(!strcmp(elem, "nop")) {
checkOptionalRecv(elem, scenario_file_cursor);
/* Does nothing at SIP level. This message type can be used to handle
* actions, increment counters, or for RTDs. */
curmsg->M_type = MSG_TYPE_NOP;
} else if(!strcmp(elem, "recvCmd")) {
curmsg->M_type = MSG_TYPE_RECVCMD;
curmsg->optional = xp_get_optional("optional", "recv");
last_recv_optional = curmsg->optional;
/* 3pcc extended mode */
if ((cptr = xp_get_value("src"))) {
curmsg->peer_src = strdup(cptr);
} else if (extendedTwinSippMode) {
ERROR("You must specify a 'src' for recvCmd when using extended 3pcc mode!");
}
} else if(!strcmp(elem, "sendCmd")) {
checkOptionalRecv(elem, scenario_file_cursor);
curmsg->M_type = MSG_TYPE_SENDCMD;
/* Sent messages descriptions */
/* 3pcc extended mode */
if ((cptr = xp_get_value("dest"))) {
peer = strdup(cptr);
curmsg->peer_dest = peer;
peer_map::iterator peer_it;
peer_it = peers.find(peer_map::key_type(peer));
if(peer_it == peers.end())
/* the peer (slave or master)
has not been added in the map
(first occurrence in the scenario) */
{
T_peer_infos infos = {};
infos.peer_socket = 0;
strncpy(infos.peer_host, get_peer_addr(peer), sizeof(infos.peer_host) - 1);
peers[std::string(peer)] = infos;
}
} else if (extendedTwinSippMode) {
ERROR("You must specify a 'dest' for sendCmd with extended 3pcc mode!");
}
if (!(ptr = xp_get_cdata())) {
ERROR("No CDATA in 'sendCmd' section of xml scenario file");
}
char *msg = clean_cdata(ptr);
curmsg -> M_sendCmdData = new SendingMessage(this, msg, true /* skip sanity */);
free(msg);
} else {
ERROR("Unknown element '%s' in xml scenario file", elem);
}