4141#include "rest_client.h"
4242#include "rest_methods.h"
4343#include "rest_cb.h"
44+ #include "rest_sockets.h"
4445
4546#define REST_CORRELATION_COOKIE "RESTCORR"
4647
@@ -70,10 +71,12 @@ extern int _async_resume_retr_itv;
7071#define MAX_HOST_LENGTH 128
7172
7273/* file descriptor limits */
73- #define WORD_SIZE_BITS 64
74- #define WORD_SIZE_BYTES 16
75- #define BYTE_LEN 8
76- extern struct rlimit lim ;
74+ // #define WORD_SIZE_BITS 64
75+ // #define WORD_SIZE_BYTES 16
76+ // #define BYTE_LEN 8
77+ // extern struct rlimit lim;
78+
79+ static CURLSH * curl_share ;
7780
7881#define REST_TRACE_API_MODULE "proto_hep"
7982extern int rest_proto_id ;
@@ -459,168 +462,6 @@ static int init_transfer(CURL *handle, char *url, unsigned long timeout_s)
459462 return -1 ;
460463}
461464
462- typedef struct _file_descriptors {
463- unsigned char * tracked_socks ;
464- int max_fd_index ;
465- } file_descriptors ;
466-
467- static CURLSH * curl_share = NULL ;
468- static file_descriptors fds ;
469- size_t aligned_bitset_len ;
470- int total_chunks ;
471-
472- int init_process_limits (void ) {
473- aligned_bitset_len = (((lim .rlim_cur + WORD_SIZE_BITS - 1 ) / WORD_SIZE_BITS ) * WORD_SIZE_BITS ) / WORD_SIZE_BITS ;
474-
475- fds .tracked_socks = (unsigned char * ) pkg_malloc (aligned_bitset_len );
476- fds .max_fd_index = 0 ;
477- total_chunks = aligned_bitset_len / sizeof (uint64_t );
478-
479- if (fds .tracked_socks == NULL ) {
480- return -1 ;
481- }
482-
483- return 0 ;
484- }
485-
486- static inline int get_max_fd (file_descriptors * fds ) {
487- uint64_t * socket_bitmask ;
488- uint64_t sockets ;
489- int start_index , bitmask_index ;
490-
491- bitmask_index = fds -> max_fd_index - 1 ;
492-
493- if (bitmask_index < 0 ) {
494- return -2 ;
495- }
496-
497- socket_bitmask = (uint64_t * ) fds -> tracked_socks ;
498- sockets = socket_bitmask [bitmask_index ];
499-
500- if (!sockets ) {
501- fds -> max_fd_index -- ;
502- return -1 ;
503- }
504-
505- start_index = (fds -> max_fd_index * (WORD_SIZE_BITS )) - 1 ;
506-
507- return start_index - __builtin_clzll (sockets );
508- }
509-
510- /*
511- * Adds the socket to a bitmask and calculates index of that socket in the bitmask
512- * eg. s = 63 then index is 1 and if s = 65 then index is 2, in increments of 64 bits
513- * The index is the max index to check when actioning on the sockets
514- */
515- static inline void add_sock (file_descriptors * fds , int s ) {
516- int sock_index = (s >> 6 ) + 1 ;
517-
518- if (sock_index > fds -> max_fd_index ) {
519- fds -> max_fd_index = sock_index ;
520- }
521-
522- fds -> tracked_socks [s / BYTE_LEN ] |= (1 << (s % BYTE_LEN ));
523- }
524-
525- /*
526- * Removes the socket from the bitmask and then checks the max bitmask for any setbits
527- * If no bits are set then the max index is decremented regardless if the socket removed was in that chunk
528- */
529- static inline void remove_sock (file_descriptors * fds , int s ) {
530- uint64_t sockets ;
531- uint64_t * socket_bitmask ;
532- int bitmask_index ;
533-
534- fds -> tracked_socks [s / BYTE_LEN ] &= ~(1 << (s % BYTE_LEN ));
535-
536- bitmask_index = fds -> max_fd_index - 1 ;
537- socket_bitmask = (uint64_t * ) fds -> tracked_socks ;
538-
539- if (bitmask_index > 0 ) {
540- memcpy (& sockets , socket_bitmask + fds -> max_fd_index - 1 , sizeof (sockets ));
541-
542- if (!sockets ) {
543- fds -> max_fd_index -- ;
544- }
545- }
546- }
547-
548- static int socket_action_cb (CURL * e , curl_socket_t s , int event , void * cbp , void * sockp )
549- {
550- LM_DBG ("called for socket %d status %d\n" , s , event );
551- file_descriptors * fds = (file_descriptors * ) cbp ;
552-
553- if (event != CURL_POLL_REMOVE ) {
554- add_sock (fds , s );
555- } else if (event == CURL_POLL_REMOVE ) {
556- remove_sock (fds , s );
557- }
558-
559- return 0 ;
560- }
561-
562- static int timer_cb (CURLM * multi_handle , long timeout_ms , void * cbp )
563- {
564- LM_DBG ("multi_handle timer called %d\n" , timeout_ms );
565- long * p = (long * ) cbp ;
566-
567- * p = timeout_ms ;
568-
569- return 0 ;
570- }
571-
572- static int start_multi_socket (CURLM * multi_handle ) {
573- CURLMcode mrc ;
574- int running ;
575-
576- memset (fds .tracked_socks , 0 , aligned_bitset_len );
577- fds .max_fd_index = 0 ;
578- mrc = curl_multi_socket_action (multi_handle , CURL_SOCKET_TIMEOUT , 0 , & running );
579-
580- if (mrc != CURLM_OK ) {
581- LM_ERR ("curl_multi_socket_action: %s\n" , curl_multi_strerror (mrc ));
582- return -1 ;
583- }
584-
585- return running ;
586- }
587-
588- static int run_multi_socket (CURLM * multi_handle ) {
589- CURLMcode mrc ;
590- int running ;
591- uint64_t sockets ;
592- uint64_t * socket_bitmask ;
593-
594- socket_bitmask = (uint64_t * ) fds .tracked_socks ;
595-
596- if (fds .max_fd_index > 0 ) {
597- memcpy (& sockets , socket_bitmask + fds .max_fd_index - 1 , sizeof (sockets ));
598-
599- if (!sockets ) {
600- fds .max_fd_index -- ;
601- }
602- }
603-
604- for (int i = 0 ; i < fds .max_fd_index ; i ++ ) {
605- memcpy (& sockets , socket_bitmask + i , sizeof (sockets ));
606-
607- while (sockets ) {
608- int curl_s = (i * WORD_SIZE_BITS ) + __builtin_ctzll (sockets );
609- LM_DBG ("Action on socket %d\n" , curl_s );
610-
611- mrc = curl_multi_socket_action (multi_handle , curl_s , 0 , & running );
612- if (mrc != CURLM_OK ) {
613- LM_ERR ("curl_multi_socket_action: %s\n" , curl_multi_strerror (mrc ));
614- return -1 ;
615- }
616-
617- sockets &= sockets - 1 ;
618- }
619- }
620-
621- return running ;
622- }
623-
624465int connect_only (preconnect_urls * precon_urls , int total_cons ) {
625466 CURLcode rc ;
626467 CURLMcode mrc ;
@@ -680,13 +521,14 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
680521
681522 busy_wait = connect_poll_interval ;
682523
524+ if (setsocket_callback (multi_handle ) != 0 ) {
525+ goto cleanup ;
526+ }
527+
683528 // TODO Need to expose these settings for connect and async req
684529 w_curl_multi_setopt (multi_handle , CURLMOPT_MAX_HOST_CONNECTIONS , (long ) num_of_connections );
685530 w_curl_multi_setopt (multi_handle , CURLMOPT_MAXCONNECTS , (long ) num_of_connections );
686531
687- w_curl_multi_setopt (multi_handle , CURLMOPT_SOCKETFUNCTION , socket_action_cb );
688- w_curl_multi_setopt (multi_handle , CURLMOPT_SOCKETDATA , & fds );
689-
690532 w_curl_multi_setopt (multi_handle , CURLMOPT_TIMERFUNCTION , timer_cb );
691533 w_curl_multi_setopt (multi_handle , CURLMOPT_TIMERDATA , & timer );
692534
@@ -1192,6 +1034,10 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
11921034 multi_handle = multi_list -> multi_handle ;
11931035 curl_multi_add_handle (multi_handle , handle );
11941036
1037+ if (setsocket_callback (multi_handle ) != 0 ) {
1038+ goto cleanup ;
1039+ }
1040+
11951041 w_curl_multi_setopt (multi_handle , CURLMOPT_MAX_HOST_CONNECTIONS , (long ) max_host_connection );
11961042 w_curl_multi_setopt (multi_handle , CURLMOPT_MAXCONNECTS , (long ) max_async_transfers );
11971043
@@ -1203,9 +1049,6 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
12031049 timeout = connect_timeout ;
12041050 busy_wait = connect_poll_interval ;
12051051
1206- w_curl_multi_setopt (multi_handle , CURLMOPT_SOCKETFUNCTION , socket_action_cb );
1207- w_curl_multi_setopt (multi_handle , CURLMOPT_SOCKETDATA , & fds );
1208-
12091052 w_curl_multi_setopt (multi_handle , CURLMOPT_TIMERFUNCTION , timer_cb );
12101053 w_curl_multi_setopt (multi_handle , CURLMOPT_TIMERDATA , & timer );
12111054
@@ -1236,7 +1079,7 @@ int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
12361079 async_parm -> handle = handle ;
12371080 async_parm -> multi_list = multi_list ;
12381081 header_list = NULL ;
1239- * out_fd = get_max_fd (& fds ); // Running only one socket at a time so it's always the max
1082+ * out_fd = get_max_fd (); // Running only one socket at a time so it's always the max
12401083 return RCL_OK ;
12411084
12421085error :
0 commit comments