-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparse_conf.c
4789 lines (4053 loc) · 134 KB
/
parse_conf.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
/*
* parse_conf.c
*
* 2006-2007 Copyright (c)
* Robert Iakobashvili, <[email protected]>
* All rights reserved.*
*
* 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
*/
// must be first include
#include "fdsetsize.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "conf.h"
#include "batch.h"
#include "client.h"
#include "cl_alloc.h"
#include "url.h"
extern char * strcasestr(const char *, const char *);
#define EXPLORER_USERAGENT_STR "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
#define BATCH_MAX_CLIENTS_NUM 4096
#define NON_APPLICABLE_STR ""
#define NON_APPLICABLE_STR_2 "N/A"
#define REQ_GET "GET"
#define REQ_POST "POST"
#define REQ_PUT "PUT"
#define REQ_HEAD "HEAD"
#define REQ_DELETE "DELETE"
#define FT_UNIQUE_USERS_AND_PASSWORDS "UNIQUE_USERS_AND_PASSWORDS"
#define FT_UNIQUE_USERS_SAME_PASSWORD "UNIQUE_USERS_SAME_PASSWORD"
#define FT_SINGLE_USER "SINGLE_USER"
#define FT_RECORDS_FROM_FILE "RECORDS_FROM_FILE"
#define FT_AS_IS "AS_IS"
#define AUTH_BASIC "BASIC"
#define AUTH_DIGEST "DIGEST"
#define AUTH_GSS_NEGOTIATE "GSS_NEGOTIATE"
#define AUTH_NTLM "NTLM"
#define AUTH_ANY "ANY"
static int random_seed = -1;
static char random_state[256];
static unsigned char
resp_status_errors_tbl_default[URL_RESPONSE_STATUS_ERRORS_TABLE_SIZE];
/*
value - supposed to be a null-terminated string.
*/
typedef int (*fparser) (batch_context*const bctx, char*const value);
/*
Used to map a tag to its value parser function.
*/
typedef struct tag_parser_pair
{
char* tag; /* string name of the param */
fparser parser;
} tag_parser_pair;
/*
* Declarations of tag parsing functions.
*/
/*
* GENERAL section tag parsers.
*/
static int batch_name_parser (batch_context*const bctx, char*const value);
static int clients_num_max_parser (batch_context*const bctx, char*const value);
static int clients_num_start_parser (batch_context*const bctx, char*const value);
static int clients_rampup_inc_parser (batch_context*const bctx, char*const value);
static int interface_parser (batch_context*const bctx, char*const value);
static int netmask_parser (batch_context*const bctx, char*const value);
static int ip_addr_min_parser (batch_context*const bctx, char*const value);
static int ip_addr_max_parser (batch_context*const bctx, char*const value);
static int ip_shared_num_parser (batch_context*const bctx, char*const value);
static int cycles_num_parser (batch_context*const bctx, char*const value);
static int run_time_parser (batch_context*const bctx, char*const value);
static int user_agent_parser (batch_context*const bctx, char*const value);
static int urls_num_parser (batch_context*const bctx, char*const value);
static int dump_opstats_parser (batch_context*const bctx, char*const value);
static int req_rate_parser (batch_context*const bctx, char*const value);
/*
* URL section tag parsers.
*/
static int url_parser (batch_context*const bctx, char*const value);
static int url_short_name_parser (batch_context*const bctx, char*const value);
static int url_use_current_parser (batch_context*const bctx, char*const value);
static int url_dont_cycle_parser (batch_context*const bctx, char*const value);
static int header_parser (batch_context*const bctx, char*const value);
static int request_type_parser (batch_context*const bctx, char*const value);
static int username_parser (batch_context*const bctx, char*const value);
static int password_parser (batch_context*const bctx, char*const value);
static int form_usage_type_parser (batch_context*const bctx, char*const value);
static int form_string_parser (batch_context*const bctx, char*const value);
static int form_records_file_parser (batch_context*const bctx, char*const value);
static int upload_file_parser (batch_context*const bctx, char*const value);
static int multipart_form_data_parser (batch_context*const bctx, char*const value);
static int web_auth_method_parser (batch_context*const bctx, char*const value);
static int web_auth_credentials_parser (batch_context*const bctx, char*const value);
static int proxy_auth_method_parser (batch_context*const bctx, char*const value);
static int proxy_auth_credentials_parser (batch_context*const bctx, char*const value);
static int fresh_connect_parser (batch_context*const bctx, char*const value);
static int timer_tcp_conn_setup_parser (batch_context*const bctx, char*const value);
static int timer_url_completion_parser (batch_context*const bctx, char*const value);
static int timer_after_url_sleep_parser (batch_context*const bctx, char*const value);
static int ftp_active_parser (batch_context*const bctx, char*const value);
static int log_resp_headers_parser (batch_context*const bctx, char*const value);
static int log_resp_bodies_parser (batch_context*const bctx, char*const value);
static int response_status_errors_parser (batch_context*const bctx, char*const value);
static int transfer_limit_rate_parser (batch_context*const bctx, char*const value);
static int fetch_probability_parser (batch_context*const bctx, char*const value);
static int fetch_probability_once_parser (batch_context*const bctx, char*const value);
static int form_records_random_parser (batch_context*const bctx, char*const value);
static int form_records_file_max_num_parser(batch_context*const bctx, char*const value);
/* GF url-set parsers. */
static int url_template_parser(batch_context* const bctx, char* const value);
static int url_token_parser(batch_context* const bctx, char* const value);
static int url_token_file_parser(batch_context* const bctx, char* const value);
static int response_token_parser(batch_context* const bctx, char* const value);
static int form_records_cycle_parser(batch_context* const bctx, char* const value);
static int random_seed_parser (batch_context*const bctx, char*const value);
static int ignore_content_length (batch_context*const bctx, char*const value);
static int url_random_range (batch_context*const bctx, char*const value);
static int url_random_token (batch_context*const bctx, char*const value);
/*
* The mapping between tag strings and parsing functions.
*/
static const tag_parser_pair tp_map [] =
{
/*------------------------ GENERAL SECTION ------------------------------ */
{"BATCH_NAME", batch_name_parser},
{"CLIENTS_NUM_MAX", clients_num_max_parser},
{"CLIENTS_NUM_START", clients_num_start_parser},
{"CLIENTS_RAMPUP_INC", clients_rampup_inc_parser},
{"INTERFACE", interface_parser},
{"NETMASK", netmask_parser},
{"IP_ADDR_MIN", ip_addr_min_parser},
{"IP_ADDR_MAX", ip_addr_max_parser},
{"IP_SHARED_NUM", ip_shared_num_parser},
{"CYCLES_NUM", cycles_num_parser},
{"RUN_TIME", run_time_parser},
{"USER_AGENT", user_agent_parser},
{"URLS_NUM", urls_num_parser},
{"DUMP_OPSTATS", dump_opstats_parser},
{"REQ_RATE", req_rate_parser},
/*------------------------ URL SECTION -------------------------------- */
{"URL", url_parser},
{"URL_SHORT_NAME", url_short_name_parser},
{"URL_USE_CURRENT", url_use_current_parser},
{"URL_DONT_CYCLE", url_dont_cycle_parser},
{"HEADER", header_parser},
{"REQUEST_TYPE", request_type_parser},
{"USERNAME", username_parser},
{"PASSWORD", password_parser},
{"FORM_USAGE_TYPE", form_usage_type_parser},
{"FORM_STRING", form_string_parser},
{"FORM_RECORDS_FILE", form_records_file_parser},
{"UPLOAD_FILE", upload_file_parser},
{"MULTIPART_FORM_DATA", multipart_form_data_parser},
{"WEB_AUTH_METHOD", web_auth_method_parser},
{"WEB_AUTH_CREDENTIALS", web_auth_credentials_parser},
{"PROXY_AUTH_METHOD", proxy_auth_method_parser},
{"PROXY_AUTH_CREDENTIALS", proxy_auth_credentials_parser},
{"FRESH_CONNECT", fresh_connect_parser},
{"TIMER_TCP_CONN_SETUP", timer_tcp_conn_setup_parser},
{"TIMER_URL_COMPLETION", timer_url_completion_parser},
{"TIMER_AFTER_URL_SLEEP", timer_after_url_sleep_parser},
{"FTP_ACTIVE", ftp_active_parser},
{"LOG_RESP_HEADERS", log_resp_headers_parser},
{"LOG_RESP_BODIES", log_resp_bodies_parser},
{"RESPONSE_STATUS_ERRORS", response_status_errors_parser},
{"TRANSFER_LIMIT_RATE", transfer_limit_rate_parser},
{"FETCH_PROBABILITY", fetch_probability_parser},
{"FETCH_PROBABILITY_ONCE", fetch_probability_once_parser},
{"FORM_RECORDS_RANDOM", form_records_random_parser},
{"FORM_RECORDS_FILE_MAX_NUM", form_records_file_max_num_parser},
/* GF */
{"URL_TEMPLATE", url_template_parser},
{"URL_TOKEN", url_token_parser},
{"URL_TOKEN_FILE", url_token_file_parser},
{"RESPONSE_TOKEN", response_token_parser},
{"FORM_RECORDS_CYCLE", form_records_cycle_parser},
{"RANDOM_SEED", random_seed_parser},
{"IGNORE_CONTENT_LENGTH", ignore_content_length},
{"URL_RANDOM_RANGE", url_random_range},
{"URL_RANDOM_TOKEN", url_random_token},
{NULL, 0}
};
static int validate_batch (batch_context*const bctx);
static int validate_batch_general (batch_context*const bctx);
static int validate_batch_url (batch_context*const bctx);
static int post_validate_init (batch_context*const bctx);
static int load_form_records_file (batch_context*const bctx, url_context* url);
static int load_form_record_string (char*const input,
size_t input_length,
form_records_cdata* form_record,
size_t record_num,
char** separator);
static int add_param_to_batch (char*const input,
size_t input_length,
batch_context*const bctx,
int*const batch_num);
static int pre_parser (char** ptr, size_t* len);
static url_appl_type url_schema_classification (const char* const url);
static char* skip_non_ws (char*ptr, size_t*const len);
static char* eat_ws (char*ptr, size_t*const len);
static int is_ws (char*const ptr);
static int is_non_ws (char*const ptr);
static int find_first_cycling_url (batch_context* bctx);
static int find_last_cycling_url (batch_context* bctx);
static int netmask_to_cidr (char *dotted_ipv4);
static int print_correct_form_usagetype (form_usagetype ftype, char* value);
static int parse_timer_range (char* input,
size_t input_len,
long* first_val,
long* second_val);
static int upload_file_streams_alloc(batch_context* batch);
/****************************************************************************************
* Function name - find_tag_parser
*
* Description - Makes a look-up of a tag value parser function for an input tag-string
*
* Input - *tag - pointer to the tag string, coming from the configuration file
* Return Code/Output - On success - parser function, on failure - NULL
****************************************************************************************/
static fparser find_tag_parser (const char* tag)
{
size_t index;
for (index = 0; tp_map[index].tag; index++)
{
if (!strcmp (tp_map[index].tag, tag))
return tp_map[index].parser;
}
return NULL;
}
/****************************************************************************************
* Function name - add_param_to_batch
*
* Description - Takes configuration file string of the form TAG = value and extacts
* loading batch configuration parameters from it.
*
* Input - *str_buff - pointer to the configuration file string of the form TAG = value
* str_len - length of the <str_buff> string
* *bctx_array - array of the batch contexts
* Input/Output batch_num - index of the batch to fill and advance, when required.
* Still supporting multiple batches in one batch file.
*
* Return Code/Output - On success - 0, on failure - (-1)
****************************************************************************************/
static int add_param_to_batch (char*const str_buff,
size_t str_len,
batch_context*const bctx_array,
int*const batch_num)
{
if (!str_buff || !str_len || !bctx_array)
return -1;
/*We are not eating LWS, as it supposed to be done before... */
char* equal = NULL;
if ( ! (equal = strchr (str_buff, '=')))
{
fprintf (stderr,
"%s - error: input string \"%s\" is short of '=' sign.\n",
__func__, str_buff) ;
return -1;
}
else
{
*equal = '\0'; /* The idea from Igor Potulnitsky */
}
long string_length = (long) str_len;
long val_len = 0;
if ((val_len = string_length - (long)(equal - str_buff) - 1) < 0)
{
*equal = '=' ;
fprintf(stderr, "%s - error: in \"%s\" a valid name should follow '='.\n",
__func__, str_buff);
return -1;
}
/* remove TWS */
str_len = strlen (str_buff) + 1;
char* str_end = skip_non_ws (str_buff, &str_len);
if (str_end)
*str_end = '\0';
/* Lookup for value parsing function for the input tag */
fparser parser = 0;
if (! (parser = find_tag_parser (str_buff)))
{
fprintf (stderr, "%s - error: unknown tag %s.\n"
"\nATTENTION: If the tag not misspelled, read README.Migration file.\n\n",
__func__, str_buff);
return -1;
}
/* Removing LWS, TWS and comments from the value */
size_t value_len = (size_t) val_len;
char* value = equal + 1;
if (pre_parser (&value, &value_len) == -1)
{
fprintf (stderr,"%s - error: pre_parser () failed for tag %s and value \"%s\".\n",
__func__, str_buff, equal + 1);
return -1;
}
if (!strlen (value))
{
fprintf (stderr,"%s - warning: tag %s has an empty value string.\n",
__func__, str_buff);
return 0;
}
/* Remove quotes from the value */
if (*value == '"')
{
value++, value_len--;
if (value_len < 2)
{
return 0;
}
else
{
if (*(value +value_len-2) == '"')
{
*(value +value_len-2) = '\0';
value_len--;
}
}
}
if (strstr (str_buff, tp_map[0].tag))
{
/* On string "BATCH_NAME" - next batch and move the number */
++(*batch_num);
}
if ((*parser) (&bctx_array[*batch_num], value) == -1)
{
fprintf (stderr,"%s - parser failed for tag %s and value %s.\n",
__func__, str_buff, equal + 1);
return -1;
}
return 0;
}
/****************************************************************************************
* Function name - load_form_record_string
*
* Description - Parses string with credentials <user>SP<password>, allocates at virtual
* client memory and places the credentials to the client post buffer.
*
* Input - *input - pointer to the credentials file string
* input_len - length of the <input> string
*
* Input/Output *form_record - pointer to the form_records_cdata array
* record_num - index of the record ?
* *separator - the separating symbol initialized by the first string and
* further used.
* Return Code/Output - On success - 0, on failure - (-1)
****************************************************************************************/
static int load_form_record_string (char*const input,
size_t input_len,
form_records_cdata* form_record,
size_t record_num,
char** separator)
{
static const char* separators_supported [] =
{
",",
":",
";",
" ",
/*"@", we need @ for email addresses */
"/",
0
};
char* sp = NULL;
int i;
if (!input || !input_len)
{
fprintf (stderr, "%s - error: wrong input\n", __func__);
return -1;
}
/*
Figure out the separator used by the first string analyses
*/
if (! record_num)
{
for (i = 0; separators_supported [i]; i++)
{
if ((sp = strchr (input, *separators_supported [i])))
{
*separator = (char *) separators_supported [i]; /* Remember the separator */
break;
}
}
if (!separators_supported [i])
{
fprintf (stderr,
"%s - failed to locate in the first string \"%s\" \n"
"any supported separator.\nThe supported separators are:\n",
__func__, input);
for (i = 0; separators_supported [i]; i++)
{
fprintf (stderr,"\"%s\"\n", separators_supported [i]);
}
return -1;
}
}
char * token = 0, *strtokp = 0;
size_t token_count = 0;
for (token = strtok_r (input, *separator, &strtokp);
token != 0;
token = strtok_r (0, *separator, &strtokp))
{
size_t token_len = strlen (token);
if (! token_len)
{
fprintf (stderr, "%s - warning: token is empty. \n", __func__);
}
else if (token_len >= FORM_RECORDS_TOKEN_MAX_LEN)
{
fprintf (stderr, "%s - error: token is above the allowed "
"FORM_RECORDS_TOKEN_MAX_LEN (%d). \n",
__func__, FORM_RECORDS_TOKEN_MAX_LEN);
}
else
{
if (! (form_record->form_tokens[token_count] =
calloc (token_len +1, sizeof (char))))
{
fprintf (stderr, "%s - error: calloc() failed with errno %d\n",
__func__, errno);
return -1;
}
else
{
strcpy (form_record->form_tokens[token_count], token);
}
}
if (++token_count >= FORM_RECORDS_MAX_TOKENS_NUM)
{
fprintf (stderr, "%s - warning: tokens number is above"
" FORM_RECORDS_MAX_TOKENS_NUM (%d). \n",
__func__, FORM_RECORDS_MAX_TOKENS_NUM);
break;
}
}
return 0;
}
/****************************************************************************************
* Function name - pre_parser
*
* Description - Prepares value token from the configuration file to parsing. Removes LWS,
* cuts off comments, removes TWS or after quotes closing, removes quotes.
*
* Input/Output - **ptr - second pointer to value string
* *len - pointer to the length of the value string
* Return Code/Output - On success - 0, on failure - (-1)
****************************************************************************************/
static int pre_parser (char** ptr, size_t* len)
{
char* value_start = NULL;
char* quotes_closing = NULL;
char* value_end = NULL;
/* remove LWS */
if ( ! (value_start = eat_ws (*ptr, len)))
{
fprintf (stderr, "%s - error: only LWS found in the value \"%s\".\n",
__func__, value_start);
return -1;
}
/* Cut-off the comments in value string, starting from '#' */
char* comments = NULL;
if ((comments = strchr (value_start, '#')))
{
*comments = '\0'; /* The idea from Igor Potulnitsky */
if (! (*len = strlen (value_start)))
{
fprintf (stderr, "%s - error: value \"%s\" has only comments.\n",
__func__, value_start);
return -1;
}
}
/* Everything after quotes closing or TWS */
if (*value_start == '"')
{
/* Enable usage of quotted strings with wight spaces inside, line User-Agent strings. */
if (*(value_start + 1))
{
if ((quotes_closing = strchr (value_start + 1, '"')))
value_end = quotes_closing + 1;
}
else
{
value_end = value_start;
}
}
/* If not quotted strings, thus, cut the value on the first white space */
if (!value_end)
value_end = skip_non_ws (value_start, len);
if (value_end)
{
*value_end = '\0';
}
*ptr = value_start;
*len = strlen (value_start) + 1;
return 0;
}
/*******************************************************************************
* Function name - parse_timer_range
*
* Description - Parses potential timer ranges with values looking either as
* "1000" or "1000-2000"
*
* Input- *input - pointer to value string
* input_len - length of the value string, pointed by <input>
* Input/Output - *first_val - used to return the first long value
* *second_val - used to return the second long value, which is optional
*
* Return Code/Output - On success - 0, on failure - (-1)
*********************************************************************************/
static int parse_timer_range (char* input,
size_t input_len,
long* first_val,
long* second_val)
{
if (!input || !input_len || !first_val || !second_val)
{
fprintf (stderr, "%s - error: wrong input\n", __func__);
return -1;
}
const char separator = '-';
char* second = 0;
char* sep = 0;
sep = strchr (input, separator);
if (sep)
{
*sep = '\0';
if ((sep - input < (int)input_len) && (*(sep + 1)))
{
second = sep + 1;
}
else
{
*sep = separator;
fprintf (stderr, "%s - error: wrong input %s. "
"Separator %c exists, but no value after the separator.\n",
__func__, input, separator);
return -1 ;
}
}
*first_val = atol (input);
if (*first_val < 0)
{
fprintf (stderr, "%s - error: wrong input %s. "
"Only non-negative values are allowed.\n",
__func__, input);
return -1;
}
if (sep)
{
*second_val = atol (second);
if (sep && *second_val < 0)
{
fprintf (stderr, "%s - error: wrong input %s. "
"Only non-negative values are allowed.\n",
__func__, second);
return -1;
}
if (sep && *first_val >= *second_val)
{
fprintf (stderr, "%s - error: wrong input. "
"First value (%ld) should be less then the second (%ld).\n"
"Switch the order.\n", __func__, *first_val, *second_val);
return -1 ;
}
}
return 0;
}
/******************************************************************************
* Function name - eat_ws
*
* Description - Eats leading white space. Returns pointer to the start of
* the non-white-space or NULL. Returns via len a new length.
*
* Input - *ptr - pointer to the url context
* Input/Output- *len - pointer to a lenght
* Return Code/Output - Returns pointer to the start of the non-white-space or NULL
*******************************************************************************/
char* eat_ws (char* ptr, size_t*const len)
{
if (!ptr || !*len)
return NULL;
while (*len && is_ws (ptr))
++ptr, --(*len);
return *len ? ptr : NULL;
}
/******************************************************************************
* Function name - skip_non_ws
*
* Description - Skips non-white space. Returns pointer to the start of
* the white-space or NULL. Returns via len a new length.
*
* Input - *ptr - pointer to the url context
* Input/Output- *len - pointer to a lenght
* Return Code/Output - Returns pointer to the start of the white-space or NULL
*******************************************************************************/
static char* skip_non_ws (char*ptr, size_t*const len)
{
if (!ptr || !*len)
return NULL;
while (*len && is_non_ws (ptr))
++ptr, --(*len);
return *len ? ptr : NULL;
}
/******************************************************************************
* Function name - is_ws
*
* Description - Determines, whether a char pointer points to a white space
* Input - *ptr - pointer to the url context
* Return Code/Output - If white space - 1, else 0
*******************************************************************************/
static int is_ws (char*const ptr)
{
return (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n') ? 1 : 0;
}
/******************************************************************************
* Function name - is_non_ws
*
* Description - Determines, whether a char pointer points to a non-white space
* Input - *ptr - pointer to the url context
* Return Code/Output - If non-white space - 1, else 0
*******************************************************************************/
static int is_non_ws (char*const ptr)
{
return ! is_ws (ptr);
}
/*
**
** TAG PARSERS IMPLEMENTATION
**
*/
static int batch_name_parser (batch_context*const bctx, char*const value)
{
strncpy (bctx->batch_name, value, BATCH_NAME_SIZE);
return 0;
}
static int clients_num_max_parser (batch_context*const bctx, char*const value)
{
bctx->client_num_max = 0;
bctx->client_num_max = atoi (value);
/* fprintf (stderr, "\nclients number is %d\n", bctx->client_num_max); */
if (bctx->client_num_max < 1)
{
fprintf (stderr, "%s - error: clients number (%d) is out of the range\n",
__func__, bctx->client_num_max);
return -1;
}
return 0;
}
static int clients_num_start_parser (batch_context*const bctx, char*const value)
{
bctx->client_num_start = 0;
bctx->client_num_start = atoi (value);
/* fprintf (stderr, "\nclients number is %d\n", bctx->client_num_start); */
if (bctx->client_num_start < 0)
{
fprintf (stderr, "%s - error: clients starting number (%d) is out of the range\n",
__func__, bctx->client_num_start);
return -1;
}
return 0;
}
static int interface_parser (batch_context*const bctx, char*const value)
{
strncpy (bctx->net_interface, value, sizeof (bctx->net_interface) -1);
return 0;
}
static int netmask_parser (batch_context*const bctx, char*const value)
{
/* CIDR number of non-masked first bits -16, 24, etc */
if (! strchr (value, '.') && !strchr (value, ':'))
{
/* CIDR number of non-masked first bits -16, 24, etc */
bctx->cidr_netmask = atoi (value);
}
else
{
bctx->cidr_netmask = netmask_to_cidr (value);
}
if (bctx->cidr_netmask < 1 || bctx->cidr_netmask > 128)
{
fprintf (stderr,
"%s - error: network mask (%d) is out of range. Expecting from 1 to 128.\n",
__func__, bctx->cidr_netmask);
return -1;
}
return 0;
}
static int ip_addr_min_parser (batch_context*const bctx, char*const value)
{
struct in_addr inv4;
memset (&inv4, 0, sizeof (struct in_addr));
bctx->ipv6 = strchr (value, ':') ? 1 : 0;
if (inet_pton (bctx->ipv6 ? AF_INET6 : AF_INET,
value,
bctx->ipv6 ? (void *)&bctx->ipv6_addr_min : (void *)&inv4) == -1)
{
fprintf (stderr,
"%s - error: inet_pton () failed for ip_addr_min %s\n",
__func__, value);
return -1;
}
if (!bctx->ipv6)
{
bctx->ip_addr_min = ntohl (inv4.s_addr);
}
return 0;
}
static int ip_addr_max_parser (batch_context*const bctx, char*const value)
{
struct in_addr inv4;
memset (&inv4, 0, sizeof (struct in_addr));
bctx->ipv6 = strchr (value, ':') ? 1 : 0;
if (inet_pton (bctx->ipv6 ? AF_INET6 : AF_INET,
value,
bctx->ipv6 ? (void *)&bctx->ipv6_addr_max : (void *)&inv4) == -1)
{
fprintf (stderr,
"%s - error: inet_pton () failed for ip_addr_max %s\n",
__func__, value);
return -1;
}
if (!bctx->ipv6)
{
bctx->ip_addr_max = ntohl (inv4.s_addr);
}
return 0;
}
static int ip_shared_num_parser (batch_context*const bctx, char*const value)
{
bctx->ip_shared_num = atol (value);
if (bctx->ip_shared_num <= 0)
{
fprintf (stderr,
"%s - error: a positive number is expected as the value"
"for tag IP_SHARED_NUM\n", __func__);
return -1;
}
return 0;
}
static int cycles_num_parser (batch_context*const bctx, char*const value)
{
bctx->cycles_num = atol (value);
if (bctx->cycles_num < 0)
{
bctx->cycles_num = LONG_MAX - 1;
}
return 0;
}
static int run_time_parser (batch_context*const bctx, char*const value)
{
char *token = 0, *strtokp = 0;
const char *delim = ":";
long dhms[4];
int ct = 0;
const int max_ct = sizeof(dhms)/sizeof(*dhms);
char value_buf[100];
// preserve value from destruction by strtok
(void)strncpy(value_buf,value,sizeof(value_buf));
(void)memset((char *)dhms,0,sizeof(dhms));
for (token = strtok_r(value_buf,delim,&strtokp);
token != 0; token = strtok_r(0,delim,&strtokp), ct++)
{
if (ct >= max_ct)
{
(void)fprintf(stderr,
"%s - error: a value in the form [[[D:]H:]M:]S is expected"
" for tag RUN_TIME\n", __func__);
return -1;
}
int i = 0;
while (++i < max_ct)
dhms[i - 1] = dhms[i];
dhms[max_ct - 1] = atol(token);
}
long run_time = (60 * (60 * (24 * dhms[0] + dhms[1]) + dhms[2]) + dhms[3])*
1000; // msecs
if (run_time < 0)
{
run_time = LONG_MAX - 1;
}
bctx->run_time = (unsigned long)run_time;
return 0;
}
static int clients_rampup_inc_parser (batch_context*const bctx, char*const value)
{
bctx->clients_rampup_inc = atol (value);
if (bctx->clients_rampup_inc < 0)
{
fprintf (stderr,
"%s - error: clients_rampup_inc (%s) should be a zero or positive number\n",
__func__, value);
return -1;
}
return 0;
}
static int user_agent_parser (batch_context*const bctx, char*const value)
{
if (strlen (value) <= 0)
{
fprintf(stderr, "%s - warning: empty USER_AGENT "
"\"%s\", taking the defaults\n", __func__, value);
return 0;
}
strncpy (bctx->user_agent, value, sizeof(bctx->user_agent) - 1);
return 0;
}
static int urls_num_parser (batch_context*const bctx, char*const value)
{
bctx->urls_num = atoi (value);
if (bctx->urls_num < 1)
{
fprintf (stderr,
"%s - error: urls_num (%s) should be one or more.\n",
__func__, value);
return -1;
}
/* Preparing the staff to load URLs and handles */
if (! (bctx->url_ctx_array =
(url_context *) cl_calloc (bctx->urls_num, sizeof (url_context))))
{
fprintf (stderr,
"%s - error: failed to allocate URL-context array for %d urls\n",
__func__, bctx->urls_num);
return -1;
}
bctx->url_index = -1; /* Starting from the 0 position in the arrays */
return 0;
}
static int dump_opstats_parser (batch_context*const bctx, char*const value)
{
if (value[0] == 'Y' || value[0] == 'y' ||
value[0] == 'N' || value[0] == 'n')
bctx->dump_opstats = (value[0] == 'Y' || value[0] == 'y');
else
{
fprintf (stderr,
"%s - error: DUMP_OPSTATS value (%s) must start with Y|y|N|n.\n",
__func__, value);
return -1;
}
return 0;
}
static int req_rate_parser (batch_context*const bctx, char*const value)
{
bctx->req_rate = atol (value);
if (bctx->req_rate < 0)
{