-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_json.c
135 lines (106 loc) · 5.43 KB
/
parse_json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <errno.h>
#include "parse_json.h"
void read_json(struct json *json_s, char *config_file, char *data) {
FILE *fptr = NULL;
fptr = fopen(config_file, "r");
char *buff;
size_t buffer_size = 100;
size_t line_len;
while (true) {
buff = (char *) malloc(buffer_size * sizeof(char));
if(buff == NULL) {
continue;
}
line_len = getline(&buff, &buffer_size, fptr);
if (feof(fptr)) {
free(buff);
break;
}
load_json_s(json_s, buff, data);
printf("line: %s\n", buff);
free(buff);
}
fclose(fptr);
}
void load_json_s(struct json * json_s, char *line, char *data) {
//check the line we get for the keywords in the config file
if (strstr(line, "server ip address") != NULL) {
json_s->serverIp = strdup(line); //allocate mem for the server ip address
json_s->serverIp = json_s->serverIp + strcspn(json_s->serverIp, ":") + 3; //pointer magic
*(json_s->serverIp + strcspn(json_s->serverIp, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->serverIp);
} else if (strstr(line, "src port number udp") != NULL) {
json_s->srcPortUdp = strdup(line); //allocate mem for the server ip address
json_s->srcPortUdp = json_s->srcPortUdp + strcspn(json_s->srcPortUdp, ":") + 3; //pointer magic
*(json_s->srcPortUdp + strcspn(json_s->srcPortUdp, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->srcPortUdp);
} else if (strstr(line, "dst port number udp") != NULL) {
json_s->dstPortUdp = strdup(line); //allocate mem for the server ip address
json_s->dstPortUdp = json_s->dstPortUdp + strcspn(json_s->dstPortUdp, ":") + 3; //pointer magic
*(json_s->dstPortUdp + strcspn(json_s->dstPortUdp, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->dstPortUdp);
} else if (strstr(line, "dst port number tcp head") != NULL) {
json_s->dstPortTcpHead = strdup(line); //allocate mem for the server ip address
json_s->dstPortTcpHead = json_s->dstPortTcpHead + strcspn(json_s->dstPortTcpHead, ":") + 3; //pointer magic
*(json_s->dstPortTcpHead + strcspn(json_s->dstPortTcpHead, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->dstPortTcpHead);
} else if (strstr(line, "dst port number tcp tail") != NULL) {
json_s->dstPortTcpTail = strdup(line); //allocate mem for the server ip address
json_s->dstPortTcpTail = json_s->dstPortTcpTail + strcspn(json_s->dstPortTcpTail, ":") + 3; //pointer magic
*(json_s->dstPortTcpTail + strcspn(json_s->dstPortTcpTail, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->dstPortTcpTail);
} else if (strstr(line, "tcp port number") != NULL) {
json_s->tcpPort = strdup(line); //allocate mem for the server ip address
json_s->tcpPort = json_s->tcpPort + strcspn(json_s->tcpPort, ":") + 3; //pointer magic
*(json_s->tcpPort + strcspn(json_s->tcpPort, "\"")) = '\0'; //more pointer magic
sprintf(data, "%s %s", data, json_s->tcpPort);
} else if (strstr(line, "payload size") != NULL) {
char *tmp = strdup(line); //allocate mem for the server ip address
tmp = tmp + strcspn(tmp, ":") + 3; //pointer magic
*(tmp + strcspn(tmp, "\"")) = '\0'; //more pointer magic
json_s->payloadSize = atoi(tmp);
if (json_s->payloadSize <= 0) {
json_s->payloadSize = 1000;
}
sprintf(data, "%s %d", data, json_s->payloadSize);
} else if (strstr(line, "inter-measurement time") != NULL) {
char *tmp = strdup(line); //allocate mem for the server ip address
tmp = tmp + strcspn(tmp, ":") + 3; //pointer magic
*(tmp + strcspn(tmp, "\"")) = '\0'; //more pointer magic
json_s->msrTime = atoi(tmp);
if (json_s->msrTime <= 0) {
json_s->msrTime = 15;
}
sprintf(data, "%s %d", data, json_s->msrTime);
} else if (strstr(line, "udp num packets") != NULL) {
char *tmp = strdup(line); //allocate mem for the server ip address
tmp = tmp + strcspn(tmp, ":") + 3; //pointer magic
*(tmp + strcspn(tmp, "\"")) = '\0'; //more pointer magic
json_s->numPackets = atoi(tmp);
if (json_s->numPackets <= 0) {
json_s->numPackets = 6000;
}
sprintf(data, "%s %d", data, json_s->numPackets);
} else if (strstr(line, "ttl") != NULL) {
char *tmp = strdup(line); //allocate mem for the server ip address
tmp = tmp + strcspn(tmp, ":") + 3; //pointer magic
*(tmp + strcspn(tmp, "\"")) = '\0'; //more pointer magic
json_s->ttl = atoi(tmp);
if (json_s->ttl <= 0) {
json_s->ttl = 255;
}
sprintf(data, "%s %d", data, json_s->ttl);
}
}
void print_json_s(struct json *json_s) {
printf("Server IP: %s\n", json_s->serverIp);
printf("Source Port UDP: %s\n", json_s->srcPortUdp);
printf("Dest Port UDP: %s\n", json_s->dstPortUdp);
printf("Dest Port TCP Head: %s\n", json_s->dstPortTcpHead);
printf("Dest Port TCP Tail: %s\n", json_s->dstPortTcpTail);
printf("TCP Port: %s\n", json_s->tcpPort);
printf("Payload Size: %d\n", json_s->payloadSize);
printf("Measure Time: %d\n", json_s->msrTime);
printf("Number of Packets: %d\n", json_s->numPackets);
printf("TTL: %d\n", json_s->ttl);
}