@@ -1188,14 +1188,47 @@ STATUS getIceConfigLws(PSignalingClient pSignalingClient, UINT64 time)
1188
1188
CHK ((SERVICE_CALL_RESULT ) ATOMIC_LOAD (& pSignalingClient -> result ) == SERVICE_CALL_RESULT_OK && resultLen != 0 && pResponseStr != NULL ,
1189
1189
STATUS_SIGNALING_LWS_CALL_FAILED );
1190
1190
1191
- // Parse the response
1191
+ CHK_STATUS (
1192
+ parseIceConfigResponse (pResponseStr , resultLen , MAX_ICE_CONFIG_COUNT , pSignalingClient -> iceConfigs , & pSignalingClient -> iceConfigCount ));
1193
+
1194
+ // Perform some validation on the ice configuration
1195
+ CHK_STATUS (validateIceConfiguration (pSignalingClient ));
1196
+
1197
+ CleanUp :
1198
+
1199
+ if (STATUS_FAILED (retStatus )) {
1200
+ DLOGE ("Call Failed with Status: 0x%08x" , retStatus );
1201
+ }
1202
+
1203
+ freeLwsCallInfo (& pLwsCallInfo );
1204
+
1205
+ LEAVES ();
1206
+ return retStatus ;
1207
+ }
1208
+
1209
+ STATUS parseIceConfigResponse (PCHAR pResponseStr , UINT32 responseLen , UINT8 maxIceConfigs , PIceConfigInfo pIceConfigs , PUINT32 pIceConfigCount )
1210
+ {
1211
+ ENTERS ();
1212
+ STATUS retStatus = STATUS_SUCCESS ;
1213
+ jsmn_parser parser ;
1214
+ jsmntok_t tokens [MAX_JSON_TOKEN_COUNT ];
1215
+ jsmntok_t * pToken ;
1216
+ UINT32 i , configCount = 0 , strLen ; // Note: strLen excludes the NULL terminator
1217
+ INT32 j , tokenCount ;
1218
+ UINT64 ttl ;
1219
+ BOOL jsonInIceServerList = FALSE;
1220
+
1221
+ CHK (pIceConfigs != NULL && pIceConfigCount != NULL && pResponseStr != NULL , STATUS_NULL_ARG );
1222
+ CHK (maxIceConfigs > 0 , STATUS_INVALID_ARG );
1223
+ CHK (!IS_EMPTY_STRING (pResponseStr ), STATUS_INVALID_API_CALL_RETURN_JSON );
1224
+
1192
1225
jsmn_init (& parser );
1193
- tokenCount = jsmn_parse (& parser , pResponseStr , resultLen , tokens , SIZEOF (tokens ) / SIZEOF (jsmntok_t ));
1226
+ tokenCount = jsmn_parse (& parser , pResponseStr , responseLen , tokens , SIZEOF (tokens ) / SIZEOF (jsmntok_t ));
1194
1227
CHK (tokenCount > 1 , STATUS_INVALID_API_CALL_RETURN_JSON );
1195
1228
CHK (tokens [0 ].type == JSMN_OBJECT , STATUS_INVALID_API_CALL_RETURN_JSON );
1196
1229
1197
- MEMSET (& pSignalingClient -> iceConfigs , 0x00 , MAX_ICE_CONFIG_COUNT * SIZEOF (IceConfigInfo ));
1198
- pSignalingClient -> iceConfigCount = 0 ;
1230
+ MEMSET (pIceConfigs , 0x00 , maxIceConfigs * SIZEOF (IceConfigInfo ));
1231
+ * pIceConfigCount = 0 ;
1199
1232
1200
1233
// Loop through the tokens and extract the ice configuration
1201
1234
for (i = 0 ; i < tokenCount ; i ++ ) {
@@ -1204,58 +1237,64 @@ STATUS getIceConfigLws(PSignalingClient pSignalingClient, UINT64 time)
1204
1237
jsonInIceServerList = TRUE;
1205
1238
1206
1239
CHK (tokens [i + 1 ].type == JSMN_ARRAY , STATUS_INVALID_API_CALL_RETURN_JSON );
1207
- CHK (tokens [i + 1 ].size <= MAX_ICE_CONFIG_COUNT , STATUS_SIGNALING_MAX_ICE_CONFIG_COUNT );
1240
+ if (tokens [i + 1 ].size > maxIceConfigs ) {
1241
+ DLOGW ("Received more ice configs (%d) than supported (%d). Will ignore the rest." , tokens [i + 1 ].size , maxIceConfigs );
1242
+ }
1208
1243
}
1209
1244
} else {
1210
1245
pToken = & tokens [i ];
1211
- if (pToken -> type == JSMN_OBJECT ) {
1246
+ if (pToken -> type == JSMN_UNDEFINED ) {
1247
+ DLOGW ("Encountered unexpected item in the JSON! %*.s" , pResponseStr , responseLen );
1248
+ // Skip this token and continue parsing
1249
+ // Don't return error, just move to next token
1250
+ if (i + 1 < tokenCount && tokens [i + 1 ].type != JSMN_OBJECT ) {
1251
+ i ++ ; // Skip the value associated with this field
1252
+ }
1253
+ continue ;
1254
+ } else if (pToken -> type == JSMN_OBJECT ) {
1255
+ if (configCount + 1 > maxIceConfigs ) {
1256
+ // That's all we have room to parse
1257
+ break ;
1258
+ }
1212
1259
configCount ++ ;
1213
1260
} else if (compareJsonString (pResponseStr , pToken , JSMN_STRING , (PCHAR ) "Username" )) {
1214
1261
strLen = (UINT32 ) (pToken [1 ].end - pToken [1 ].start );
1215
1262
CHK (strLen <= MAX_ICE_CONFIG_USER_NAME_LEN , STATUS_INVALID_API_CALL_RETURN_JSON );
1216
- STRNCPY (pSignalingClient -> iceConfigs [configCount - 1 ].userName , pResponseStr + pToken [1 ].start , strLen );
1217
- pSignalingClient -> iceConfigs [configCount - 1 ].userName [MAX_ICE_CONFIG_USER_NAME_LEN ] = '\0' ;
1263
+ SNPRINTF (pIceConfigs [configCount - 1 ].userName , MAX_ICE_CONFIG_USER_NAME_BUFFER_LEN , "%.*s" , strLen , pResponseStr + pToken [1 ].start );
1218
1264
i ++ ;
1219
1265
} else if (compareJsonString (pResponseStr , pToken , JSMN_STRING , (PCHAR ) "Password" )) {
1220
1266
strLen = (UINT32 ) (pToken [1 ].end - pToken [1 ].start );
1221
1267
CHK (strLen <= MAX_ICE_CONFIG_CREDENTIAL_LEN , STATUS_INVALID_API_CALL_RETURN_JSON );
1222
- STRNCPY (pSignalingClient -> iceConfigs [configCount - 1 ].password , pResponseStr + pToken [1 ].start , strLen );
1223
- pSignalingClient -> iceConfigs [configCount - 1 ].userName [MAX_ICE_CONFIG_CREDENTIAL_LEN ] = '\0' ;
1268
+ SNPRINTF (pIceConfigs [configCount - 1 ].password , MAX_ICE_CONFIG_CREDENTIAL_BUFFER_LEN , "%.*s" , strLen , pResponseStr + pToken [1 ].start );
1224
1269
i ++ ;
1225
1270
} else if (compareJsonString (pResponseStr , pToken , JSMN_STRING , (PCHAR ) "Ttl" )) {
1226
- CHK_STATUS (STRTOUI64 (pResponseStr + pToken [1 ].start , pResponseStr + pToken [1 ].end , 10 , & ttl ));
1227
-
1228
- // NOTE: Ttl value is in seconds
1229
- pSignalingClient -> iceConfigs [configCount - 1 ].ttl = ttl * HUNDREDS_OF_NANOS_IN_A_SECOND ;
1271
+ retStatus = STRTOUI64 ((PCHAR ) pResponseStr + pToken [1 ].start , (PCHAR ) pResponseStr + pToken [1 ].end , 10 , & ttl );
1272
+ if (STATUS_FAILED (retStatus )) {
1273
+ strLen = (UINT32 ) (pToken [1 ].end - pToken [1 ].start );
1274
+ DLOGE ("Unable to convert TTL: %.*s to a number" , strLen , (PCHAR ) pResponseStr + pToken [1 ].start );
1275
+ retStatus = STATUS_INVALID_API_CALL_RETURN_JSON ;
1276
+ CHK (FALSE, retStatus );
1277
+ }
1278
+ pIceConfigs [configCount - 1 ].ttl = ttl * HUNDREDS_OF_NANOS_IN_A_SECOND ;
1230
1279
i ++ ;
1231
1280
} else if (compareJsonString (pResponseStr , pToken , JSMN_STRING , (PCHAR ) "Uris" )) {
1232
- // Expect an array of elements
1233
1281
CHK (pToken [1 ].type == JSMN_ARRAY , STATUS_INVALID_API_CALL_RETURN_JSON );
1234
1282
CHK (pToken [1 ].size <= MAX_ICE_CONFIG_URI_COUNT , STATUS_SIGNALING_MAX_ICE_URI_COUNT );
1235
1283
for (j = 0 ; j < pToken [1 ].size ; j ++ ) {
1236
1284
strLen = (UINT32 ) (pToken [j + 2 ].end - pToken [j + 2 ].start );
1237
1285
CHK (strLen <= MAX_ICE_CONFIG_URI_LEN , STATUS_SIGNALING_MAX_ICE_URI_LEN );
1238
- STRNCPY (pSignalingClient -> iceConfigs [configCount - 1 ].uris [j ], pResponseStr + pToken [j + 2 ].start , strLen );
1239
- pSignalingClient -> iceConfigs [configCount - 1 ].uris [j ][MAX_ICE_CONFIG_URI_LEN ] = '\0' ;
1240
- pSignalingClient -> iceConfigs [configCount - 1 ].uriCount ++ ;
1286
+ SNPRINTF (pIceConfigs [configCount - 1 ].uris [j ], MAX_ICE_CONFIG_URI_BUFFER_LEN , "%.*s" , strLen , pResponseStr + pToken [j + 2 ].start );
1287
+ pIceConfigs [configCount - 1 ].uriCount ++ ;
1241
1288
}
1242
-
1243
1289
i += pToken [1 ].size + 1 ;
1244
1290
}
1245
1291
}
1246
1292
}
1247
1293
1248
- // Perform some validation on the ice configuration
1249
- pSignalingClient -> iceConfigCount = configCount ;
1250
- CHK_STATUS (validateIceConfiguration (pSignalingClient ));
1294
+ * pIceConfigCount = configCount ;
1251
1295
1252
1296
CleanUp :
1253
-
1254
- if (STATUS_FAILED (retStatus )) {
1255
- DLOGE ("Call Failed with Status: 0x%08x" , retStatus );
1256
- }
1257
-
1258
- freeLwsCallInfo (& pLwsCallInfo );
1297
+ CHK_LOG_ERR (retStatus );
1259
1298
1260
1299
LEAVES ();
1261
1300
return retStatus ;
0 commit comments