MCPcopy Create free account
hub / github.com/esnet/iperf / JSON_read

Function JSON_read

src/iperf_api.c:3020–3078  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3018/*************************************************************/
3019
3020static cJSON *
3021JSON_read(int fd, int max_size)
3022{
3023 uint32_t hsize, nsize;
3024 size_t strsize;
3025 char *str;
3026 cJSON *json = NULL;
3027 int rc;
3028 char msg_buf[WARN_STR_LEN * 2];
3029
3030 /*
3031 * Read a four-byte integer, which is the length of the JSON to follow.
3032 * Then read the JSON into a buffer and parse it. Return a parsed JSON
3033 * structure, NULL if there was an error.
3034 */
3035 rc = Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp);
3036 if (rc == sizeof(nsize)) {
3037 hsize = ntohl(nsize);
3038 if (hsize > 0 && (max_size == 0 || hsize <= max_size)) {
3039 /* Allocate a buffer to hold the JSON */
3040 strsize = hsize + 1; /* +1 for trailing NULL */
3041 if (strsize) {
3042 str = (char *) calloc(sizeof(char), strsize);
3043 if (str != NULL) {
3044 rc = Nread(fd, str, hsize, Ptcp);
3045 if (rc >= 0) {
3046 /*
3047 * We should be reading in the number of bytes corresponding to the
3048 * length in that 4-byte integer. If we don't the socket might have
3049 * prematurely closed. Only do the JSON parsing if we got the
3050 * correct number of bytes.
3051 */
3052 if (rc == hsize) {
3053 json = cJSON_Parse(str);
3054 }
3055 else {
3056 snprintf(msg_buf, sizeof(msg_buf), "JSON size of data read does not correspond to offered length - expected %d bytes but received %d; errno=%d", hsize, rc, errno);
3057 warning(msg_buf);
3058 }
3059 }
3060 else {
3061 snprintf(msg_buf, sizeof(msg_buf), "JSON data read failed; errno=%d", errno);
3062 warning(msg_buf);
3063 }
3064 free(str);
3065 }
3066 }
3067 }
3068 else {
3069 snprintf(msg_buf, sizeof(msg_buf), "JSON data length overflow - %d bytes JSON size is not allowed", hsize);
3070 warning(msg_buf);
3071 }
3072 }
3073 else {
3074 snprintf(msg_buf, sizeof(msg_buf), "Failed to read JSON data size - read returned %d; errno=%d", rc, errno);
3075 warning(msg_buf);
3076 }
3077 return json;

Callers 2

get_parametersFunction · 0.85
get_resultsFunction · 0.85

Calls 3

NreadFunction · 0.85
cJSON_ParseFunction · 0.85
warningFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…