@@ -683,9 +683,92 @@ static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *
683683 return -1 ;
684684}
685685
686- static int hyper_parse_interfaces (struct hyper_pod * pod , char * json , jsmntok_t * toks )
686+ static int hyper_parse_interface (struct hyper_interface * iface ,
687+ char * json , jsmntok_t * toks )
687688{
688689 int i = 0 , j , next_if ;
690+
691+ if (toks [i ].type != JSMN_OBJECT ) {
692+ fprintf (stdout , "network array need object\n" );
693+ return -1 ;
694+ }
695+
696+ next_if = toks [i ].size ;
697+
698+ i ++ ;
699+ for (j = 0 ; j < next_if ; j ++ , i ++ ) {
700+ if (json_token_streq (json , & toks [i ], "device" )) {
701+ iface -> device = (json_token_str (json , & toks [++ i ]));
702+ fprintf (stdout , "net device is %s\n" , iface -> device );
703+ } else if (json_token_streq (json , & toks [i ], "ipAddress" )) {
704+ iface -> ipaddr = (json_token_str (json , & toks [++ i ]));
705+ fprintf (stdout , "net ipaddress is %s\n" , iface -> ipaddr );
706+ } else if (json_token_streq (json , & toks [i ], "netMask" )) {
707+ iface -> mask = (json_token_str (json , & toks [++ i ]));
708+ fprintf (stdout , "net mask is %s\n" , iface -> mask );
709+ } else {
710+ fprintf (stderr , "get unknown section %s in interfaces\n" ,
711+ json_token_str (json , & toks [i ]));
712+ goto fail ;
713+ }
714+ }
715+
716+ return i ;
717+
718+ fail :
719+ free (iface -> device );
720+ free (iface -> ipaddr );
721+ free (iface -> mask );
722+ return -1 ;
723+ }
724+
725+ struct hyper_interface * hyper_parse_setup_interface (char * json , int length )
726+ {
727+ jsmn_parser p ;
728+ int toks_num = 10 , n ;
729+ jsmntok_t * toks = NULL ;
730+
731+ struct hyper_interface * iface = NULL ;
732+ realloc :
733+ toks = realloc (toks , toks_num * sizeof (jsmntok_t ));
734+ if (toks == NULL ) {
735+ fprintf (stderr , "allocate tokens for execcmd failed\n" );
736+ goto fail ;
737+ }
738+
739+ jsmn_init (& p );
740+ n = jsmn_parse (& p , json , length , toks , toks_num );
741+ if (n < 0 ) {
742+ fprintf (stdout , "jsmn parse failed, n is %d\n" , n );
743+ if (n == JSMN_ERROR_NOMEM ) {
744+ toks_num *= 2 ;
745+ goto realloc ;
746+ }
747+ goto out ;
748+ }
749+
750+ iface = calloc (1 , sizeof (* iface ));
751+ if (iface == NULL ) {
752+ fprintf (stderr , "allocate memory for interface failed\n" );
753+ goto out ;
754+ }
755+
756+ if (hyper_parse_interface (iface , json , toks ) < 0 ) {
757+ fprintf (stderr , "allocate memory for interface failed\n" );
758+ goto fail ;
759+ }
760+ out :
761+ free (toks );
762+ return iface ;
763+ fail :
764+ free (iface );
765+ iface = NULL ;
766+ goto out ;
767+ }
768+
769+ static int hyper_parse_interfaces (struct hyper_pod * pod , char * json , jsmntok_t * toks )
770+ {
771+ int i = 0 , j , next ;
689772 struct hyper_interface * iface ;
690773
691774 if (toks [i ].type != JSMN_ARRAY ) {
@@ -704,32 +787,11 @@ static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *
704787
705788 i ++ ;
706789 for (j = 0 ; j < pod -> i_num ; j ++ ) {
707- int i_if ;
708- iface = & pod -> iface [j ];
709-
710- if (toks [i ].type != JSMN_OBJECT ) {
711- fprintf (stdout , "network array need object\n" );
790+ next = hyper_parse_interface (& pod -> iface [j ], json , toks + i );
791+ if (next < 0 )
712792 return -1 ;
713- }
714- next_if = toks [i ].size ;
715793
716- i ++ ;
717- for (i_if = 0 ; i_if < next_if ; i_if ++ , i ++ ) {
718- if (json_token_streq (json , & toks [i ], "device" )) {
719- iface -> device = (json_token_str (json , & toks [++ i ]));
720- fprintf (stdout , "net device is %s\n" , iface -> device );
721- } else if (json_token_streq (json , & toks [i ], "ipAddress" )) {
722- iface -> ipaddr = (json_token_str (json , & toks [++ i ]));
723- fprintf (stdout , "net ipaddress is %s\n" , iface -> ipaddr );
724- } else if (json_token_streq (json , & toks [i ], "netMask" )) {
725- iface -> mask = (json_token_str (json , & toks [++ i ]));
726- fprintf (stdout , "net mask is %s\n" , iface -> mask );
727- } else {
728- fprintf (stderr , "get unknown section %s in interfaces\n" ,
729- json_token_str (json , & toks [i ]));
730- return -1 ;
731- }
732- }
794+ i += next ;
733795 }
734796
735797 return i ;
0 commit comments