I’m trying to build a client/server application that, on the client side, establishes a TCP connection and sends a parsed JSON file through a char buffer. But first, I need to parse the JSON file, which I am doing using the Jansson library and storing into an array of structs. This is my struct:
#define DEFAULT_LENGTH 22
typedef struct {
char key[DEFAULT_LENGTH];
char value[DEFAULT_LENGTH];
} jsonLine;
For modularity/compartmentalization, I wish to dynamically allocate the array in main, and pass its address to a function, which will then build it inside there. However, it’s not working. The issue isn’t necessarily with the JSON library, since I tried the code in main and it worked perfectly.
The issue is more with passing the array and building it. This is my code:
int main(int argc, char *argv[]) {
//parse given json file into struct
if (argc < 2) {
printf("missing JSON file in cmd line arg!");
return EXIT_FAILURE;
}
jsonLine *config = (jsonLine*) malloc(11 * sizeof(DEFAULT_LENGTH)*2);
parseconfig(&config, argv);
And this is the parseconfig(): (I won’t show the whole thing, just what I think is delivering segmentation fault error)
void parseconfig (jsonLine **arr, char *input[]) {
//storing values in struct array
strcpy((**arr).key,"server_ip_addr");
strcpy((**arr).value,json_string_value(json_object_get(root, "server_ip_addr")));
(*arr)++;
strcpy((**arr).key,"UDP_src_port");
strcpy((**arr).value,json_string_value(json_object_get(root, "UDP_src_port")));
(*arr)++;
strcpy((**arr).key,"UDP_dest_port");
strcpy((**arr).value,json_string_value(json_object_get(root, "UDP_dest_port")));
(*arr)++;
strcpy((**arr).key,"TCP_dest_port_headSYN");
strcpy((**arr).value,json_string_value(json_object_get(root, "TCP_dest_port_headSYN")));
(*arr)++;
strcpy((**arr).key,"TCP_dest_port_tailSYN");
strcpy((**arr).value,json_string_value(json_object_get(root, "TCP_dest_port_tailSYN")));
(*arr)++;
strcpy((**arr).key,"TCP_port_preProb");
strcpy((**arr).value,json_string_value(json_object_get(root, "TCP_port_preProb")));
(*arr)++;
strcpy((**arr).key,"TCP_port_postProb");
strcpy((**arr).value,json_string_value(json_object_get(root, "TCP_port_postProb")));
(*arr)++;
strcpy((**arr).key,"UDP_packet_size");
strcpy((**arr).value,json_string_value(json_object_get(root, "UDP_packet_size")));
(*arr)++;
strcpy((**arr).key,"inter_time");
strcpy((**arr).value,json_string_value(json_object_get(root, "inter_time")));
(*arr)++;
strcpy((**arr).key,"UDP_train_size");
strcpy((**arr).value,json_string_value(json_object_get(root, "UDP_train_size")));
(*arr)++;
strcpy((**arr).key,"UDP_TTL");
strcpy((**arr).value,json_string_value(json_object_get(root, "UDP_TTL")));
json_decref(root);
To be honest, I have no idea what the problem is, and might just transfer the code to the main function for simplicity.