1616#include "yaml.h"
1717
1818#define CONFIG_LOGGER "config"
19- #define CA_PEM_LEN 1208
2019#define DEFAULT_CA_PEM \
2120 "-----BEGIN CERTIFICATE-----\r\n" \
2221 "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\r\n" \
@@ -62,6 +61,42 @@ static int get_conf_key(char const* const key) {
6261 return 0 ;
6362}
6463
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+
65100status_t cli_core_set (ta_core_t * const core , int key , char * const value ) {
66101 if (value == NULL && (key != CACHE && key != PROXY_API && key != QUIET && key != NO_GTTA )) {
67102 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) {
71106 iota_config_t * const iota_conf = & core -> iota_conf ;
72107 ta_cache_t * const cache = & core -> cache ;
73108 iota_client_service_t * const iota_service = & core -> iota_service ;
74- FILE * file = NULL ;
75109 char * conf_file = core -> conf_file ;
76110#ifdef DB_ENABLE
77111 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) {
115149 for (char * p = strtok (value , "," ); p && idx < MAX_NODE_LIST_ELEMENTS ; p = strtok (NULL , "," ), idx ++ ) {
116150 ta_conf -> iota_host_list [idx ] = p ;
117151 }
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 );
119153 break ;
120154 case NODE_PORT_CLI :
121155 idx = 0 ;
@@ -131,15 +165,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
131165 break ;
132166
133167 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 );
143169 break ;
144170
145171 case HEALTH_TRACK_PERIOD :
@@ -257,12 +283,12 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
257283status_t ta_set_iota_client_service (iota_client_service_t * service , char const * host , uint16_t port ,
258284 char const * const ca_pem ) {
259285 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 );
263289 service -> http .port = port ;
264290 service -> http .api_version = 1 ;
265- service -> http .ca_pem = ca_pem ? ca_pem : DEFAULT_CA_PEM ;
291+ service -> http .ca_pem = ca_pem ;
266292 service -> serializer_type = SR_JSON ;
267293 init_json_serializer (& service -> serializer );
268294
0 commit comments