@@ -423,6 +423,64 @@ static int hyper_set_interface_attr(struct rtnl_handle *rth,
423423 return 0 ;
424424}
425425
426+ static int hyper_set_interface_ipaddrs (struct rtnl_handle * rth ,
427+ int ifindex ,
428+ struct list_head * ipaddresses ){
429+ uint8_t data [4 ];
430+ unsigned mask ;
431+ struct {
432+ struct nlmsghdr n ;
433+ struct ifaddrmsg ifa ;
434+ char buf [256 ];
435+ } req ;
436+ struct hyper_ipaddress * ip ;
437+
438+ memset (& req , 0 , sizeof (req ));
439+ req .n .nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg ));
440+ req .ifa .ifa_family = AF_INET ;
441+ req .ifa .ifa_index = ifindex ;
442+ req .ifa .ifa_scope = 0 ;
443+
444+ list_for_each_entry (ip , ipaddresses , list ) {
445+ if (ip -> addr [0 ]== '\0' ){
446+ continue ;
447+ } else if (ip -> addr [0 ]== '-' ) {
448+ //start with '-' means delete an existing address
449+ req .n .nlmsg_flags = NLM_F_REQUEST ;
450+ req .n .nlmsg_type = RTM_DELADDR ;;
451+ if (get_addr_ipv4 ((uint8_t * )& data , & ip -> addr [1 ]) <= 0 ) {
452+ fprintf (stderr , "get addr failed\n" );
453+ return -1 ;
454+ }
455+ } else {
456+ req .n .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL ;
457+ req .n .nlmsg_type = RTM_NEWADDR ;
458+ if (get_addr_ipv4 ((uint8_t * )& data , ip -> addr ) <= 0 ) {
459+ fprintf (stderr , "get addr failed\n" );
460+ return -1 ;
461+ }
462+ }
463+
464+ if (addattr_l (& req .n , sizeof (req ), IFA_LOCAL , & data , 4 )) {
465+ fprintf (stderr , "setup attr failed\n" );
466+ return -1 ;
467+ }
468+
469+ if (get_netmask (& mask , ip -> mask ) < 0 ) {
470+ fprintf (stderr , "get netamsk failed\n" );
471+ return -1 ;
472+ }
473+
474+ req .ifa .ifa_prefixlen = mask ;
475+ fprintf (stdout , "interface get netamsk %d %s\n" , req .ifa .ifa_prefixlen , ip -> mask );
476+ if (rtnl_talk (rth , & req .n , 0 , 0 , NULL ) < 0 ) {
477+ perror ("rtnl_talk failed" );
478+ return -1 ;
479+ }
480+ }
481+ return 0 ;
482+ }
483+
426484static int hyper_set_interface_name (struct rtnl_handle * rth ,
427485 int ifindex ,
428486 char * new_device_name )
@@ -452,56 +510,22 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
452510 struct hyper_interface * iface ,
453511 struct hyper_pod * pod )
454512{
455- uint8_t data [4 ];
456- unsigned mask ;
457- struct {
458- struct nlmsghdr n ;
459- struct ifaddrmsg ifa ;
460- char buf [256 ];
461- } req ;
462513 int ifindex ;
463- struct hyper_ipaddress * ip ;
464-
465- if (!iface -> device || list_empty (& iface -> ipaddresses )) {
466- fprintf (stderr , "interface information incorrect\n" );
514+ if (!iface -> device ) {
515+ fprintf (stderr , "device name can't be empty\n" );
467516 return -1 ;
468517 }
469518
470- memset (& req , 0 , sizeof (req ));
471- req .n .nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg ));
472- req .n .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL ;
473- req .n .nlmsg_type = RTM_NEWADDR ;
474- req .ifa .ifa_family = AF_INET ;
475-
476519 ifindex = hyper_get_ifindex (iface -> device , pod );
477520 if (ifindex < 0 ) {
478521 fprintf (stderr , "failed to get the ifindix of %s\n" , iface -> device );
479522 return -1 ;
480523 }
481524
482- req .ifa .ifa_index = ifindex ;
483- req .ifa .ifa_scope = 0 ;
484-
485- list_for_each_entry (ip , & iface -> ipaddresses , list ) {
486- if (get_addr_ipv4 ((uint8_t * )& data , ip -> addr ) <= 0 ) {
487- fprintf (stderr , "get addr failed\n" );
488- return -1 ;
489- }
490-
491- if (addattr_l (& req .n , sizeof (req ), IFA_LOCAL , & data , 4 )) {
492- fprintf (stderr , "setup attr failed\n" );
493- return -1 ;
494- }
495-
496- if (get_netmask (& mask , ip -> mask ) < 0 ) {
497- fprintf (stderr , "get netamsk failed\n" );
498- return -1 ;
499- }
500-
501- req .ifa .ifa_prefixlen = mask ;
502- fprintf (stdout , "interface get netamsk %d %s\n" , req .ifa .ifa_prefixlen , ip -> mask );
503- if (rtnl_talk (rth , & req .n , 0 , 0 , NULL ) < 0 ) {
504- perror ("rtnl_talk failed" );
525+ if (!list_empty (& iface -> ipaddresses )) {
526+ if (hyper_set_interface_ipaddrs (rth , ifindex , & iface -> ipaddresses ) < 0 ) {
527+ fprintf (stderr , "set ip addresses failed for interface %s\n" ,
528+ iface -> device );
505529 return -1 ;
506530 }
507531 }
@@ -603,7 +627,6 @@ int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod)
603627 if (netlink_open (& rth ) < 0 )
604628 return -1 ;
605629
606-
607630 iface = hyper_parse_setup_interface (json , length );
608631 if (iface == NULL ) {
609632 fprintf (stderr , "parse interface failed\n" );
@@ -657,6 +680,7 @@ static int hyper_remove_nic(char *device)
657680 close (fd );
658681 return 0 ;
659682}
683+
660684int hyper_cmd_delete_interface (char * json , int length )
661685{
662686 int ret = -1 ;
0 commit comments