@@ -42,51 +42,66 @@ void set_credentials(influxDbContext *cntxt, const char *username, const char *p
4242 safe_str_cpy (cntxt -> password , password , SETTINGS_LENGTH );
4343}
4444
45- int initDbContext (influxDbContext * cntxt , const char * hostname , const char * port , const char * database ) {
46- struct addrinfo hints , * ai = NULL ;
47-
48- cntxt -> sock_fd = -1 ;
49- memset (cntxt -> user , 0 , SETTINGS_LENGTH );
50- memset ( cntxt -> password , 0 , SETTINGS_LENGTH ) ;
51- memset ( cntxt -> ts_precision , 0 , SETTINGS_LENGTH );
45+ int createSocket (influxDbContext * cntxt )
46+ {
47+ if ( cntxt -> sock_fd >= 0 ) {
48+ // close the existing socket if it is open
49+ close (cntxt -> sock_fd );
50+ cntxt -> sock_fd = -1 ;
51+ }
5252
53- safe_str_cpy (cntxt -> hostname , hostname , SETTINGS_LENGTH );
54- safe_str_cpy (cntxt -> port , port , SETTINGS_LENGTH );
55- safe_str_cpy (cntxt -> db_name , database , SETTINGS_LENGTH );
53+ struct addrinfo hints , * ai = NULL ;
5654
5755 memset (& hints , 0 , sizeof (struct addrinfo ));
5856 hints .ai_family = AF_INET ;
5957 hints .ai_socktype = SOCK_STREAM ;
6058
61- do {
62- if (getaddrinfo (hostname , port , & hints , & ai ) != 0 ) {
63- break ;
64- }
65-
66- cntxt -> sock_fd = socket (ai -> ai_family , ai -> ai_socktype , ai -> ai_protocol );
59+ if (getaddrinfo (cntxt -> hostname , cntxt -> port , & hints , & ai ) != 0 )
60+ {
61+ return -1 ;
62+ }
6763
68- if (cntxt -> sock_fd < 0 ) {
69- break ;
70- }
64+ cntxt -> sock_fd = socket (ai -> ai_family , ai -> ai_socktype , ai -> ai_protocol );
65+ if (cntxt -> sock_fd < 0 )
66+ {
67+ freeaddrinfo (ai );
68+ return -1 ;
69+ }
7170
72- if (connect (cntxt -> sock_fd , ai -> ai_addr , ai -> ai_addrlen ) < 0 ) {
73- (void ) close (cntxt -> sock_fd );
74- cntxt -> sock_fd = -1 ;
75- }
71+ if (connect (cntxt -> sock_fd , ai -> ai_addr , ai -> ai_addrlen ) < 0 )
72+ {
73+ close (cntxt -> sock_fd );
74+ cntxt -> sock_fd = -1 ;
75+ freeaddrinfo (ai );
76+ return -1 ;
7677 }
77- while (0 );
7878
7979 freeaddrinfo (ai );
80-
8180 return cntxt -> sock_fd ;
8281}
8382
83+ int initDbContext (influxDbContext * cntxt , const char * hostname , const char * port , const char * database ) {
84+
85+ cntxt -> sock_fd = -1 ;
86+ memset (cntxt -> user , 0 , SETTINGS_LENGTH );
87+ memset (cntxt -> password , 0 , SETTINGS_LENGTH );
88+ memset (cntxt -> ts_precision , 0 , SETTINGS_LENGTH );
89+
90+ safe_str_cpy (cntxt -> hostname , hostname , SETTINGS_LENGTH );
91+ safe_str_cpy (cntxt -> port , port , SETTINGS_LENGTH );
92+ safe_str_cpy (cntxt -> db_name , database , SETTINGS_LENGTH );
93+
94+ return createSocket (cntxt );
95+ }
96+
8497int deInitDbContext (influxDbContext * cntxt ) {
8598 return close (cntxt -> sock_fd );
8699}
87100
88101
89- int sendData (influxDbContext * cntxt , char * data , size_t length ) {
102+ int sendData (influxDbContext * cntxt , char * data , size_t length )
103+ {
104+ createSocket (cntxt );
90105 char http_header [2048 ];
91106 size_t ret ;
92107 size_t header_length = 0 , sent = 0 ;
0 commit comments