16
16
#include "yaml.h"
17
17
18
18
#define CONFIG_LOGGER "config"
19
- #define CA_PEM_LEN 1208
20
19
#define DEFAULT_CA_PEM \
21
20
"-----BEGIN CERTIFICATE-----\r\n" \
22
21
"MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\r\n" \
@@ -62,6 +61,42 @@ static int get_conf_key(char const* const key) {
62
61
return 0 ;
63
62
}
64
63
64
+ #define CA_BUFFER_SIZE 2048
65
+ static char * read_ca_pem (char * ca_pem_path ) {
66
+ FILE * file = NULL ;
67
+ if ((file = fopen (ca_pem_path , "r" )) == NULL ) {
68
+ ta_log_error ("%s\n" , ta_error_to_string (SC_CONF_FOPEN_ERROR ));
69
+ goto done ;
70
+ }
71
+
72
+ char * ca_pem = (char * )malloc (sizeof (char ) * (CA_BUFFER_SIZE ));
73
+ if (!ca_pem ) {
74
+ ta_log_error ("%s\n" , ta_error_to_string (SC_OOM ));
75
+ goto done ;
76
+ }
77
+
78
+ char c ;
79
+ int i = 0 , buffer_nums = 1 ;
80
+ while ((c = fgetc (file )) != EOF ) {
81
+ ca_pem [i ++ ] = c ;
82
+ if (!(i < CA_BUFFER_SIZE )) {
83
+ ca_pem = realloc (ca_pem , sizeof (char ) * buffer_nums * CA_BUFFER_SIZE );
84
+ buffer_nums ++ ;
85
+ }
86
+ }
87
+ ca_pem [i ] = '\0' ;
88
+
89
+ if (feof (file )) {
90
+ ta_log_debug ("Read the End Of File.\n" );
91
+ } else {
92
+ ta_log_error ("Read file error\n" );
93
+ }
94
+
95
+ done :
96
+ fclose (file );
97
+ return ca_pem ;
98
+ }
99
+
65
100
status_t cli_core_set (ta_core_t * const core , int key , char * const value ) {
66
101
if (value == NULL && (key != CACHE && key != PROXY_API && key != QUIET && key != NO_GTTA )) {
67
102
ta_log_error ("%s\n" , "SC_NULL" );
@@ -71,7 +106,6 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
71
106
iota_config_t * const iota_conf = & core -> iota_conf ;
72
107
ta_cache_t * const cache = & core -> cache ;
73
108
iota_client_service_t * const iota_service = & core -> iota_service ;
74
- FILE * file = NULL ;
75
109
char * conf_file = core -> conf_file ;
76
110
#ifdef DB_ENABLE
77
111
db_client_service_t * const db_service = & core -> db_service ;
@@ -115,7 +149,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
115
149
for (char * p = strtok (value , "," ); p && idx < MAX_NODE_LIST_ELEMENTS ; p = strtok (NULL , "," ), idx ++ ) {
116
150
ta_conf -> iota_host_list [idx ] = p ;
117
151
}
118
- strncpy (iota_service -> http .host , ta_conf -> iota_host_list [0 ], HOST_MAX_LEN );
152
+ strncpy (iota_service -> http .host , ta_conf -> iota_host_list [0 ], HOST_MAX_LEN - 1 );
119
153
break ;
120
154
case NODE_PORT_CLI :
121
155
idx = 0 ;
@@ -131,15 +165,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
131
165
break ;
132
166
133
167
case CA_PEM :
134
- if ((file = fopen (value , "r" )) == NULL ) {
135
- /* The specified configuration file does not exist */
136
- ta_log_error ("%s\n" , ta_error_to_string (SC_CONF_FOPEN_ERROR ));
137
- return SC_CONF_FOPEN_ERROR ;
138
- }
139
- char * temp_ca_pem = (char * )malloc (sizeof (char ) * (CA_PEM_LEN + 1 ));
140
- fread (temp_ca_pem , CA_PEM_LEN , 1 , file );
141
- iota_service -> http .ca_pem = temp_ca_pem ;
142
- fclose (file );
168
+ iota_service -> http .ca_pem = read_ca_pem (value );
143
169
break ;
144
170
145
171
case HEALTH_TRACK_PERIOD :
@@ -257,12 +283,12 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
257
283
status_t ta_set_iota_client_service (iota_client_service_t * service , char const * host , uint16_t port ,
258
284
char const * const ca_pem ) {
259
285
strncpy (service -> http .path , "/" , CONTENT_TYPE_MAX_LEN );
260
- strncpy (service -> http .content_type , "application/json" , CONTENT_TYPE_MAX_LEN );
261
- strncpy (service -> http .accept , "application/json" , CONTENT_TYPE_MAX_LEN );
262
- strncpy (service -> http .host , host , HOST_MAX_LEN );
286
+ strncpy (service -> http .content_type , "application/json" , CONTENT_TYPE_MAX_LEN - 1 );
287
+ strncpy (service -> http .accept , "application/json" , CONTENT_TYPE_MAX_LEN - 1 );
288
+ strncpy (service -> http .host , host , HOST_MAX_LEN - 1 );
263
289
service -> http .port = port ;
264
290
service -> http .api_version = 1 ;
265
- service -> http .ca_pem = ca_pem ? ca_pem : DEFAULT_CA_PEM ;
291
+ service -> http .ca_pem = ca_pem ;
266
292
service -> serializer_type = SR_JSON ;
267
293
init_json_serializer (& service -> serializer );
268
294
0 commit comments