@@ -635,9 +635,92 @@ static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *
635635 return -1 ;
636636}
637637
638- static int hyper_parse_interfaces (struct hyper_pod * pod , char * json , jsmntok_t * toks )
638+ static int hyper_parse_interface (struct hyper_interface * iface ,
639+ char * json , jsmntok_t * toks )
639640{
640641 int i = 0 , j , next_if ;
642+
643+ if (toks [i ].type != JSMN_OBJECT ) {
644+ fprintf (stdout , "network array need object\n" );
645+ return -1 ;
646+ }
647+
648+ next_if = toks [i ].size ;
649+
650+ i ++ ;
651+ for (j = 0 ; j < next_if ; j ++ , i ++ ) {
652+ if (json_token_streq (json , & toks [i ], "device" )) {
653+ iface -> device = (json_token_str (json , & toks [++ i ]));
654+ fprintf (stdout , "net device is %s\n" , iface -> device );
655+ } else if (json_token_streq (json , & toks [i ], "ipAddress" )) {
656+ iface -> ipaddr = (json_token_str (json , & toks [++ i ]));
657+ fprintf (stdout , "net ipaddress is %s\n" , iface -> ipaddr );
658+ } else if (json_token_streq (json , & toks [i ], "netMask" )) {
659+ iface -> mask = (json_token_str (json , & toks [++ i ]));
660+ fprintf (stdout , "net mask is %s\n" , iface -> mask );
661+ } else {
662+ fprintf (stderr , "get unknown section %s in interfaces\n" ,
663+ json_token_str (json , & toks [i ]));
664+ goto fail ;
665+ }
666+ }
667+
668+ return i ;
669+
670+ fail :
671+ free (iface -> device );
672+ free (iface -> ipaddr );
673+ free (iface -> mask );
674+ return -1 ;
675+ }
676+
677+ struct hyper_interface * hyper_parse_setup_interface (char * json , int length )
678+ {
679+ jsmn_parser p ;
680+ int toks_num = 10 , n ;
681+ jsmntok_t * toks = NULL ;
682+
683+ struct hyper_interface * iface = NULL ;
684+ realloc :
685+ toks = realloc (toks , toks_num * sizeof (jsmntok_t ));
686+ if (toks == NULL ) {
687+ fprintf (stderr , "allocate tokens for execcmd failed\n" );
688+ goto fail ;
689+ }
690+
691+ jsmn_init (& p );
692+ n = jsmn_parse (& p , json , length , toks , toks_num );
693+ if (n < 0 ) {
694+ fprintf (stdout , "jsmn parse failed, n is %d\n" , n );
695+ if (n == JSMN_ERROR_NOMEM ) {
696+ toks_num *= 2 ;
697+ goto realloc ;
698+ }
699+ goto out ;
700+ }
701+
702+ iface = calloc (1 , sizeof (* iface ));
703+ if (iface == NULL ) {
704+ fprintf (stderr , "allocate memory for interface failed\n" );
705+ goto out ;
706+ }
707+
708+ if (hyper_parse_interface (iface , json , toks ) < 0 ) {
709+ fprintf (stderr , "allocate memory for interface failed\n" );
710+ goto fail ;
711+ }
712+ out :
713+ free (toks );
714+ return iface ;
715+ fail :
716+ free (iface );
717+ iface = NULL ;
718+ goto out ;
719+ }
720+
721+ static int hyper_parse_interfaces (struct hyper_pod * pod , char * json , jsmntok_t * toks )
722+ {
723+ int i = 0 , j , next ;
641724 struct hyper_interface * iface ;
642725
643726 if (toks [i ].type != JSMN_ARRAY ) {
@@ -656,32 +739,11 @@ static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *
656739
657740 i ++ ;
658741 for (j = 0 ; j < pod -> i_num ; j ++ ) {
659- int i_if ;
660- iface = & pod -> iface [j ];
661-
662- if (toks [i ].type != JSMN_OBJECT ) {
663- fprintf (stdout , "network array need object\n" );
742+ next = hyper_parse_interface (& pod -> iface [j ], json , toks + i );
743+ if (next < 0 )
664744 return -1 ;
665- }
666- next_if = toks [i ].size ;
667745
668- i ++ ;
669- for (i_if = 0 ; i_if < next_if ; i_if ++ , i ++ ) {
670- if (json_token_streq (json , & toks [i ], "device" )) {
671- iface -> device = (json_token_str (json , & toks [++ i ]));
672- fprintf (stdout , "net device is %s\n" , iface -> device );
673- } else if (json_token_streq (json , & toks [i ], "ipAddress" )) {
674- iface -> ipaddr = (json_token_str (json , & toks [++ i ]));
675- fprintf (stdout , "net ipaddress is %s\n" , iface -> ipaddr );
676- } else if (json_token_streq (json , & toks [i ], "netMask" )) {
677- iface -> mask = (json_token_str (json , & toks [++ i ]));
678- fprintf (stdout , "net mask is %s\n" , iface -> mask );
679- } else {
680- fprintf (stderr , "get unknown section %s in interfaces\n" ,
681- json_token_str (json , & toks [i ]));
682- return -1 ;
683- }
684- }
746+ i += next ;
685747 }
686748
687749 return i ;
0 commit comments