-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
2330 lines (1991 loc) · 69.7 KB
/
loader.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
/*
* loader.c
*
* 2006-2012 Copyright (c)
* Robert Iakobashvili, <coroberti@gmail.com>
* Michael Moser, <moser.michael@gmail.com>
* 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
*
* Cooked from the CURL-project examples with thanks to the
* great CURL-project authors and contributors.
*/
// must be the first include
#include "fdsetsize.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <curl/curl.h>
#include <curl/multi.h>
// getrlimit
#include <sys/resource.h>
#include "batch.h"
#include "client.h"
#include "loader.h"
#include "conf.h"
#include "ssl_thr_lock.h"
#include "screen.h"
#include "cl_alloc.h"
static int client_tracing_function (CURL *handle,
curl_infotype type,
unsigned char *data,
size_t size,
void *userp);
static size_t do_nothing_write_func (void *ptr,
size_t size,
size_t nmemb,
void *stream);
static int create_ip_addrs (batch_context* bctx, int bctx_num);
static void* batch_function (void *batch_data);
static int initial_handles_init (struct client_context*const cdata);
static int setup_curl_handle_appl (struct client_context*const cctx,
url_context* url_ctx);
static int init_client_formed_buffer (client_context* cctx,
url_context* url,
char* buffer,
size_t buffer_len);
static int init_client_contexts (batch_context* bctx, FILE* output_file);
static void free_batch_data_allocations (struct batch_context* bctx);
static void free_url (url_context* url, int clients_max);
static int ipv6_increment(const struct in6_addr *const src,
struct in6_addr *const dest);
static int create_thr_subbatches (batch_context *bc_arr, int subbatches_num);
static int ip_addr_str_allocate_init (batch_context* bctx,
int client_index,
char** addr_str);
int stop_loading = 0;
static void sigint_handler (int signum)
{
(void) signum;
stop_loading = 1;
screen_release ();
close (STDIN_FILENO);
fprintf (stderr, "\n\n======= SIGINT Received ============.\n");
}
typedef int (*pf_user_activity) (struct client_context*const);
/*
* Batch functions for the 2 loading modes:
* hyper (epoll-based) and smooth (poll-based).
*/
static pf_user_activity ua_array[2] =
{
user_activity_hyper,
user_activity_smooth
};
static FILE *create_file (batch_context* bctx, char* fname)
{
FILE *fp = fopen(fname,"w");
if (!fp)
(void)fprintf(stderr,"%s, cannot create file \"%s\", %s\n",
bctx->batch_name,fname,strerror(errno));
return fp;
}
int
main (int argc, char *argv [])
{
batch_context bc_arr[BATCHES_MAX_NUM];
pthread_t tid[BATCHES_MAX_NUM];
int batches_num = 0;
int i = 0, error = 0;
signal (SIGPIPE, SIG_IGN);
if (parse_command_line (argc, argv) == -1)
{
fprintf (stderr,
"%s - error: failed parsing of the command line.\n", __func__);
return -1;
}
memset(bc_arr, 0, sizeof(bc_arr));
/*
Parse the configuration file.
*/
if ((batches_num = parse_config_file (config_file, bc_arr,
sizeof(bc_arr)/sizeof(*bc_arr))) <= 0)
{
fprintf (stderr, "%s - error: parse_config_file () failed.\n", __func__);
return -1;
}
/*
* De-facto the support is only for a single batch. However, we are using
* internal support for multiple batches for loading from several threads,
* using sub-batches (a subset of virtual clients).
* TODO: test env for all batches.
*/
if (test_environment (&bc_arr[0]) == -1)
{
fprintf (stderr, "%s - error: test_environment () - error.\n", __func__);
return -1;
}
/*
Add ip-addresses to the loading network interfaces
and keep them in batch-contexts.
*/
if (create_ip_addrs (bc_arr, batches_num) == -1)
{
fprintf (stderr, "%s - error: create_ip_addrs () failed. \n", __func__);
return -1;
}
else
{
fprintf (stderr,
"%s - added IP-addresses to the loading network interface.\n",
__func__);
}
signal (SIGINT, sigint_handler);
screen_init ();
if (! threads_subbatches_num)
{
fprintf (stderr, "\nRUNNING LOAD\n\n");
sleep (1);
batch_function (&bc_arr[0]);
fprintf (stderr, "Exited batch_function\n");
screen_release ();
}
else
{
fprintf (stderr, "\n%s - RUNNING LOAD, STARTING THREADS\n\n", __func__);
sleep (1);
/* Init openssl mutexes and pass two callbacks to openssl. */
if (thread_openssl_setup () == -1)
{
fprintf (stderr, "%s - error: thread_setup () - failed.\n", __func__);
return -1;
}
create_thr_subbatches (bc_arr, threads_subbatches_num);
/*
Opening threads for the batches of clients
*/
for (i = 0 ; i < threads_subbatches_num ; i++)
{
bc_arr[i].batch_id = i;
error = pthread_create (&tid[i], NULL, batch_function, &bc_arr[i]);
if (0 != error)
{
fprintf(stderr, "%s - error: Couldn't run thread number %d, errno %d\n",
__func__, i, errno);
}
else
{
bc_arr[i].thread_id = tid[i]; /* Set the thread-id */
fprintf(stderr, "%s - note: Thread %d, started normally\n", __func__, i);
}
}
/* Waiting for all running threads to terminate */
for (i = 0 ; i < threads_subbatches_num ; i++)
{
error = pthread_join (tid[i], NULL) ;
fprintf(stderr, "%s - note: Thread %d terminated normally\n", __func__, i) ;
}
thread_openssl_cleanup ();
}
return 0;
}
/****************************************************************************************
* Function name - batch_function
* Description - Runs the batch test either within the main-thread or in a separate thread.
*
* Input - *batch_data - contains loading configuration and active entities for a
* particular batch of clients.
* Return Code/Output - NULL in all cases
****************************************************************************************/
static void* batch_function (void * batch_data)
{
batch_context* bctx = (batch_context *) batch_data;
FILE* log_file = 0;
FILE* statistics_file = 0;
FILE* opstats_file = 0;
int rval = -1;
if (!bctx)
{
fprintf (stderr,
"%s - error: batch_data input is zero.\n", __func__);
return NULL;
}
if (! stderr_print_client_msg)
{
/*
Init batch logfile for the batch client output
*/
(void)sprintf (bctx-> batch_logfile, "./%s.log", bctx->batch_name);
if (!(log_file = create_file(bctx,bctx->batch_logfile)))
return NULL;
else
{
char tbuf[256];
(void)fprintf(log_file,"# %ld %s",get_tick_count(),ascii_time(tbuf));
(void)fprintf(log_file,
"# msec_offset cycle_no url_no client_no (ip) indic info\n");
}
}
/*
Init batch statistics file
*/
(void)sprintf (bctx->batch_statistics, "./%s.txt", bctx->batch_name);
if (!(bctx->statistics_file = statistics_file = create_file(bctx,
bctx->batch_statistics)))
return NULL;
else
print_statistics_header (statistics_file);
/*
Init batch operational statistics file
*/
if (bctx->dump_opstats)
{
(void)sprintf (bctx->batch_opstats, "./%s.ops", bctx->batch_name);
if (!(bctx->opstats_file = opstats_file = create_file(bctx,
bctx->batch_opstats)))
return NULL;
}
/*
Init the objects, containing client-context information.
*/
if (init_client_contexts (bctx, log_file) == -1)
{
fprintf (stderr, "%s - \"%s\" - failed to allocate or init client_contexts.\n",
__func__, bctx->batch_name);
goto cleanup;
}
/*
Init libcurl MCURL and CURL handles. Setup of the handles is delayed to
the later step, depending on urls required.
*/
if (initial_handles_init (bctx->cctx_array) == -1)
{
fprintf (stderr, "%s - \"%s\" initial_handles_init () failed.\n",
__func__, bctx->batch_name);
goto cleanup;
}
/*
Now run configuration-defined actions, like login, fetching various urls and and
sleeping in between and loggoff.
It calls user activity loading function corresponding to the used loading mode
(user_activity_smooth () or user_activity_hyper ()).
*/
rval = ua_array[loading_mode] (bctx->cctx_array);
if (rval == -1)
{
fprintf (stderr, "%s - \"%s\" -user activity failed.\n",
__func__, bctx->batch_name);
goto cleanup;
}
cleanup:
if (bctx->multiple_handle)
curl_multi_cleanup(bctx->multiple_handle);
if (log_file)
fclose (log_file);
if (statistics_file)
fclose (statistics_file);
if (opstats_file)
fclose (opstats_file);
free_batch_data_allocations (bctx);
return NULL;
}
/****************************************************************************************
* Function name - initial_handles_init
*
* Description - Libcurl initialization of curl multi-handle and the curl handles (clients),
* used in the batch
*
* Input - *ctx_array - array of clients for a particular batch/sub-batch of clients
* Return Code/Output - On Success - 0, on Error -1
****************************************************************************************/
static int initial_handles_init (client_context*const ctx_array)
{
batch_context* bctx = ctx_array->bctx;
int k = 0;
/* Init CURL multi-handle. */
if (! (bctx->multiple_handle = curl_multi_init()) )
{
fprintf (stderr,
"%s - error: curl_multi_init() failed for batch \"%s\" .\n",
__func__, bctx->batch_name) ;
return -1;
}
/* Initialize all CURL handles */
for (k = 0 ; k < bctx->client_num_max ; k++)
{
if (!(bctx->cctx_array[k].handle = curl_easy_init ()))
{
fprintf (stderr,"%s - error: curl_easy_init () failed for k=%d.\n",
__func__, k);
return -1;
}
}
return 0;
}
/*
The callback to libcurl to write all bytes to ptr.
*/
size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite (ptr, size, nmemb, stream);
return(nmemb * size);
}
/*
The callback to libcurl to skip all body bytes of the fetched urls.
*/
size_t
do_nothing_write_func (void *ptr, size_t size, size_t nmemb, void *stream)
{
(void)ptr;
(void)stream;
/*
Overwriting the default behavior to write body bytes to stdout and
just skipping the body bytes without any output.
*/
return (size*nmemb);
}
/****************************************************************************************
* Function name - setup_curl_handle
*
* Description - Inits a CURL handle, using setup_curl_handle_init () function.
*
* Input - *cctx - pointer to client context, containing CURL handle pointer;
* *url - pointer to url-context, containing all url-related information;
* Return Code/Output - On Success - 0, on Error -1
****************************************************************************************/
int setup_curl_handle (client_context*const cctx, url_context* url)
{
if (setup_curl_handle_init (cctx, url) == -1)
{
fprintf (stderr,"%s - error: failed.\n",__func__);
return -1;
}
return 0;
}
/****************************************************************************
* Function name - setup_curl_handle_init
*
* Description - Resets client context kept CURL handle and inits it locally, using
* setup_curl_handle_appl () function for the application-specific
* (HTTP/FTP) initialization.
*
* Input - *cctx- pointer to client context, containing CURL handle pointer;
* *url - pointer to url-context, containing all url-related information;
* Return Code/Output - On Success - 0, on Error -1
******************************************************************************/
int setup_curl_handle_init (client_context*const cctx, url_context* url)
{
if (!cctx || !url)
{
return -1;
}
batch_context* bctx = cctx->bctx;
CURL* handle = cctx->handle;
curl_easy_reset (handle);
/*
Choose the next URL from an url set, or complete the url template from
prior responses, or prepare to scan for new response values.
This updates the url_str with the appropriate token values, and hands the url to curl
before any other clients (possibly in other threads) can intervene.
*/
if (update_url_from_set_or_template (handle, cctx, url) < 0)
{
fprintf (stderr,"%s - error: update_url_from_set_or_template failed\n", __func__);
return -1;
}
if (bctx->ipv6)
curl_easy_setopt (handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
/* Bind the handle to a certain IP-address */
if (bctx->custom_ip_option)
{
curl_easy_setopt (handle, CURLOPT_INTERFACE,
bctx->ip_addr_array [cctx->client_index]);
}
curl_easy_setopt (handle, CURLOPT_NOSIGNAL, 1);
/* set|unset the curl proxy */
curl_easy_setopt (handle, CURLOPT_PROXY, config_proxy);
/* Set the url */
if (url->url_str && url->url_str_len)
{
/*
Note, target URL for PUT should include directory with a file
name, not just a directory.
*/
if ((url->req_type == HTTP_REQ_TYPE_GET && url->form_str) ||
(url->req_type == HTTP_REQ_TYPE_HEAD && url->form_str) ||
(url->req_type == HTTP_REQ_TYPE_DELETE && url->form_str))
{
/*
GET url with form fields. If not making searches with a search
engine, better to do it encrypted by HTTPS.
*/
if (!cctx->get_url_form_data || !cctx->get_url_form_data_len)
{
fprintf (stderr,"%s - error: get_url_form_data not allocated/initialized.\n",
__func__);
return -1;
}
strcpy (cctx->get_url_form_data, url->url_str);
if (init_client_formed_buffer (cctx,
url,
cctx->get_url_form_data + url->url_str_len -1,
cctx->get_url_form_data_len - url->url_str_len) == -1)
{
fprintf (stderr,
"%s - error: init_client_formed_buffer() failed for GET form fields.\n",
__func__);
return -1;
}
curl_easy_setopt (handle, CURLOPT_URL, cctx->get_url_form_data);
}
else
{
if (! is_template(url)) /* Handled in update_url_from_set_or_template () above. GF */
{
#if DEBUG
// curl_easy_setopt (handle, CURLOPT_URL, url->url_str);
char buf[1000];
sprintf(buf,"%s.%ld.%ld.%s",url->url_str,
cctx->cycle_num,cctx->url_curr_index,
cctx->client_name);
buf[strlen(buf)-1] = '\0'; // suppress space
curl_easy_setopt (handle, CURLOPT_URL, buf);
#else
curl_easy_setopt (handle, CURLOPT_URL, url->url_str);
#endif // DEBUG
}
}
}
else
{
fprintf (stderr,"%s - error: empty url provided.\n", __func__);
return -1;
}
// reset the url if URL_RANDOM_RANGE is used, to create a random url for caching
if ( (url->random_hrange > 0) && (url->random_hrange > url->random_lrange) ) {
randomize_url(handle, url);
}
/* Set the index to client */
if (url->url_ind >= 0)
{
cctx->url_curr_index = url->url_ind;
}
bctx->url_index = url->url_ind;
curl_easy_setopt (handle, CURLOPT_DNS_CACHE_TIMEOUT, -1);
/* Set the connection timeout */
curl_easy_setopt (handle,
CURLOPT_CONNECTTIMEOUT,
url->connect_timeout ? url->connect_timeout : connect_timeout);
/* Define the connection re-use policy. When passed 1, re-establish */
curl_easy_setopt (handle, CURLOPT_FRESH_CONNECT, url->fresh_connect);
if (url->fresh_connect)
{
curl_easy_setopt (handle, CURLOPT_FORBID_REUSE, 1);
}
/*
If DNS resolving is necesary, global DNS cache is enough,
otherwise compile libcurl with ares (cares) library support.
Attention: DNS global cache is not thread-safe, therefore use
cares for asynchronous DNS lookups.
curl_easy_setopt (handle, CURLOPT_DNS_USE_GLOBAL_CACHE, 1);
*/
curl_easy_setopt (handle, CURLOPT_VERBOSE, 1);
curl_easy_setopt (handle, CURLOPT_DEBUGFUNCTION,
client_tracing_function);
/*
This is to return cctx pointer as the void* userp to the
tracing function.
*/
curl_easy_setopt (handle, CURLOPT_DEBUGDATA, cctx);
#if 0
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, prog_cb);
curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, cctx);
if (loading_mode == LOAD_MODE_HYPER)
{
curl_easy_setopt (handle, CURLOPT_WRITEDATA, cctx);
}
#endif
if (url->log_resp_bodies || url->log_resp_headers)
{
if (response_logfiles_set (cctx, url) == -1)
{
fprintf (stderr,"%s - error: response_logfiles_set () .\n",
__func__);
return -1;
}
}
else
{
curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION,
do_nothing_write_func);
}
curl_easy_setopt (handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (handle, CURLOPT_SSL_VERIFYHOST, 0);
/* Set the private pointer to be used by the smooth-mode. */
curl_easy_setopt (handle, CURLOPT_PRIVATE, cctx);
/* Without the buffer set, we do not get any errors in tracing function. */
curl_easy_setopt (handle, CURLOPT_ERRORBUFFER, bctx->error_buffer);
/* set ignore_content_length
*/
if (url->ignore_content_length) {
curl_easy_setopt (handle, CURLOPT_IGNORE_CONTENT_LENGTH, 1);
}
#if 0
if (url->upload_file)
{
if (! url->upload_file_ptr)
{
if (! (url->upload_file_ptr = fopen (url->upload_file, "rb")))
{
fprintf (stderr,
"%s - error: failed to open() %s with errno %d.\n",
__func__, url->upload_file, errno);
return -1;
}
}
/* Enable uploading */
curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
/*
Do we want to use our own read function ? On windows - MUST.
curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_callback);
*/
/* Now specify which file to upload */
curl_easy_setopt(handle, CURLOPT_READDATA,
url->upload_file_ptr);
/* Provide the size of the upload */
curl_easy_setopt(handle, CURLOPT_INFILESIZE,
(long) url->upload_file_size);
if (url->transfer_limit_rate)
{
curl_easy_setopt(handle, CURLOPT_MAX_SEND_SPEED_LARGE,
(curl_off_t) url->transfer_limit_rate);
}
}
#endif
/* GF */
if (url->upload_file)
{
if (upload_file_stream_init (cctx, url) < 0)
return -1;
}
else
{
if (url->transfer_limit_rate)
{
curl_easy_setopt(handle, CURLOPT_MAX_RECV_SPEED_LARGE,
(curl_off_t) url->transfer_limit_rate);
}
}
if (url->body_file)
{
if (!(url->body_file_ptr = fopen(url->body_file, "rb")))
{
fprintf (stderr,
"%s - fopen() failed to open for reading filename \"%s\", errno %d.\n",
__func__, url->body_file, errno);
return -1;
}
// FIXME check returned value for fseek
fseek(url->body_file_ptr, 0, SEEK_END);
url->body_file_size = ftell(url->body_file_ptr);
fseek(url->body_file_ptr, 0, SEEK_SET); //same as rewind(f);
url->body_bytes = malloc(url->body_file_size + 1);
char* ptr = url->body_bytes;
off_t remaining = url->body_file_size;
while (remaining) {
off_t readed = fread(ptr, sizeof(char), remaining, url->body_file_ptr);
remaining -= readed;
ptr += readed;
}
fclose(url->body_file_ptr);
*ptr = 0;
}
/*
Application (url) specific setups, like HTTP-specific, FTP-specific, etc.
*/
if (setup_curl_handle_appl (cctx, url) == -1)
{
fprintf (stderr,
"%s - error: setup_curl_handle_appl () failed .\n",
__func__);
return -1;
}
return 0;
}
/****************************************************************************************
* Function name - setup_curl_handle_appl
*
* Description - Application/url-type specific setup for a single curl handle (client)
*
* Input - *cctx- pointer to client context, containing CURL handle pointer;
* *url - pointer to url-context, containing all url-related information;
* Return Code/Output - On Success - 0, on Error -1
****************************************************************************************/
int setup_curl_handle_appl (client_context*const cctx, url_context* url)
{
batch_context* bctx = cctx->bctx;
CURL* handle = cctx->handle;
cctx->is_https = (url->url_appl_type == URL_APPL_HTTPS);
if (url->url_appl_type == URL_APPL_HTTPS ||
url->url_appl_type == URL_APPL_HTTP)
{
/* ******** HTTP-SPECIFIC INITIALIZATION ************** */
/*
Follow possible HTTP-redirection from header Location of the
3xx HTTP responses, like 301, 302, 307, etc. It also updates the url,
thus no need to parse header Location. Great job done by the libcurl
people.
*/
curl_easy_setopt (handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt (handle, CURLOPT_UNRESTRICTED_AUTH, 1);
/* Enable infinitive (-1) redirection number. */
curl_easy_setopt (handle, CURLOPT_MAXREDIRS, -1);
/*
Setup the User-Agent header, configured by user. The default is MSIE-6 header.
*/
curl_easy_setopt (handle, CURLOPT_USERAGENT, bctx->user_agent);
/*
Setup the custom (HTTP) headers, if appropriate.
*/
if (url->custom_http_hdrs && url->custom_http_hdrs_num)
{
curl_easy_setopt (handle, CURLOPT_HTTPHEADER,
url->custom_http_hdrs);
}
/*
Enable cookies. This is important for various authentication schemes.
*/
if (! url->url_ind)
{
curl_easy_setopt (handle, CURLOPT_COOKIEFILE, "");
}
if (url->req_type == HTTP_REQ_TYPE_POST)
{
/*
Make POST, using post buffer, if requested.
*/
if (url->body_file)
{
curl_easy_setopt(handle, CURLOPT_POST, 1);
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, url->body_file_size);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, url->body_bytes);
}
else if (url->upload_file && url->upload_file_ptr && (!cctx->post_data || !cctx->post_data[0]))
{
curl_easy_setopt(handle, CURLOPT_POST, 1);
}
else if (cctx->post_data || url->mpart_form_post)
{
/*
Sets POST as the HTTP request method using either:
- POST-fields;
- multipart form-data as in RFC 1867;
*/
if (init_client_url_post_data (cctx, url) == -1)
{
fprintf (stderr,
"%s - error: init_client_url_post_data() failed.\n",
__func__);
return -1;
}
}
else
{
fprintf (stderr, "%s - error: post_data is NULL.\n", __func__);
return -1;
}
}
else if (url->req_type == HTTP_REQ_TYPE_PUT)
{
if (!url->upload_file || ! url->upload_file_ptr)
{
fprintf (stderr,
"%s - error: upload file is NULL or cannot be opened.\n",
__func__);
return -1;
}
else
{
// Upload is enabled earlier.
/*
HTTP PUT method.
Note, target URL for PUT should include a file
name, not only a directory
*/
curl_easy_setopt(handle, CURLOPT_PUT, 1);
}
}
else if (url->req_type == HTTP_REQ_TYPE_HEAD)
{
/*
HTTP HEAD method.
Note, no other info need to put
*/
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD");
}
else if (url->req_type == HTTP_REQ_TYPE_DELETE)
{
/*
HTTP DELETE method.
Note, no toher info need to put
*/
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "DELETE");
}
if (url->web_auth_method)
{
if (!url->web_auth_credentials)
{
if (!url->username || !url->password)
{
return -1;
}
char web_userpwd[256];
sprintf (web_userpwd, "%s:%s", url->username, url->password);
curl_easy_setopt(handle, CURLOPT_USERPWD, web_userpwd);
}
else
{
curl_easy_setopt(handle, CURLOPT_USERPWD, url->web_auth_credentials);
}
curl_easy_setopt(handle, CURLOPT_HTTPAUTH, url->web_auth_method);
}
if (url->proxy_auth_method)
{
if (!url->proxy_auth_credentials)
{
if (!url->username || !url->password)
{
return -1;
}
char proxy_userpwd[256];
sprintf (proxy_userpwd, "%s:%s", url->username, url->password);
curl_easy_setopt(handle, CURLOPT_PROXYUSERPWD, proxy_userpwd);
}
else
{
curl_easy_setopt(handle, CURLOPT_PROXYUSERPWD, url->proxy_auth_credentials);
}
curl_easy_setopt(handle, CURLOPT_PROXYAUTH, url->proxy_auth_method);
}
}
else if (url->url_appl_type == URL_APPL_FTP ||
url->url_appl_type == URL_APPL_FTPS)
{
/*********** FTP-SPECIFIC INITIALIZATION. *****************/
if (bctx->custom_ip_option && url->ftp_active)
{
curl_easy_setopt(handle,
CURLOPT_FTPPORT,
bctx->ip_addr_array [cctx->client_index]);
}
/*
Send custom FTP headers after the transfer.
*/
if (url->custom_http_hdrs && url->custom_http_hdrs_num)
{
curl_easy_setopt (handle, CURLOPT_POSTQUOTE,
url->custom_http_hdrs);
}
}
return 0;
}
/**********************************************************************
* Function name - response_logfiles_set
*
* Description - Opens a logfile for responses to be used for a certain client
* and a certain url. A separate file to be opened for headers
* and bodies. Sets the files to the logging mechanism of
* libcurl.
*
* Input - *cctx - pointer to client context
* *url - pointer to url context
* Return Code/Output - On Success - 0, on Error -1
***********************************************************************/
int response_logfiles_set (client_context* cctx, url_context* url)
{
CURL* handle = cctx->handle;
if (url->log_resp_bodies && url->dir_log)
{
// open the file
char body_file[256];
memset (body_file, 0, sizeof (body_file));
snprintf (body_file, sizeof (body_file) -1,
"%s/cl-%Zu-cycle-%ld.body",
url->dir_log,
cctx->client_index,
cctx->cycle_num
);
if (cctx->logfile_bodies)
{
fclose (cctx->logfile_bodies);
cctx->logfile_bodies = NULL;
}
if (!(cctx->logfile_bodies = fopen (body_file, "w")))
{
fprintf (stderr, "%s - error: fopen () failed with errno %d.\n",
__func__, errno);
return -1;
}
curl_easy_setopt (handle, CURLOPT_WRITEDATA, cctx->logfile_bodies);
curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, writefunction);
}
if (url->log_resp_headers && url->dir_log)
{
// open the file
char hdr_file[256];
memset (hdr_file, 0, sizeof (hdr_file));
snprintf (hdr_file, sizeof (hdr_file) -1,
"%s/cl-%Zu-cycle-%ld.hdr",
url->dir_log,
cctx->client_index,
cctx->cycle_num
);
if (cctx->logfile_headers)
{