@@ -668,6 +668,26 @@ struct nlmsghdr* Netlink::netlink_ip_alloc() {
668668 return nlmsg;
669669}
670670
671+ struct nlmsghdr * Netlink::netlink_ip_dealloc () {
672+ size_t len = NLMSG_ALIGN (SIZE_ALIGN) + NLMSG_ALIGN (sizeof (struct nlmsghdr *));
673+ struct nlmsghdr *nlmsg = (struct nlmsghdr *) malloc (len);
674+ memset (nlmsg, 0 , len);
675+
676+ struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;
677+ uni->ifaddrmsg .ifa_family = AF_INET;
678+ uni->ifaddrmsg .ifa_scope = 0 ;
679+
680+ nlmsg->nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg ));
681+ nlmsg->nlmsg_type = RTM_DELADDR;
682+ // NLM_F_REQUEST Must be set on all request messages
683+ // NLM_F_ACK Request for an acknowledgment on success
684+ // NLM_F_CREATE Create object if it doesn't already exist
685+ // NLM_F_EXCL Don't replace if the object already exists
686+ nlmsg->nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK|NLM_F_CREATE|NLM_F_EXCL;
687+
688+ return nlmsg;
689+ }
690+
671691struct nlmsghdr * Netlink::netlink_ipv6_alloc () {
672692 size_t len = NLMSG_ALIGN (SIZE_ALIGN) + NLMSG_ALIGN (sizeof (struct nlmsghdr *));
673693 struct nlmsghdr *nlmsg = (struct nlmsghdr *) malloc (len);
@@ -845,6 +865,42 @@ void Netlink::set_iface_ip(const std::string &iface, const std::string &ip, int
845865 netlink_nl_send (nlmsg);
846866}
847867
868+ void Netlink::unset_iface_ip (const std::string &iface, const std::string &ip, int prefix) {
869+ struct nlmsghdr *nlmsg = netlink_ip_dealloc ();
870+ struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;
871+ struct rtattr *rta;
872+ struct in_addr ia;
873+
874+ int index = get_iface_index (iface);
875+ if (index == -1 ) {
876+ logger->error (" unset_iface_ip: iface {0} does not exist" , iface);
877+ throw std::runtime_error (" unset_iface_ip: iface does not exist" );
878+ }
879+
880+ uni->ifaddrmsg .ifa_index = index;
881+ uni->ifaddrmsg .ifa_prefixlen = prefix;
882+
883+ if (inet_pton (AF_INET, ip.c_str (), &ia) <= 0 ) {
884+ free (nlmsg);
885+ logger->error (" unset_iface_ip: Error in inet_pton" );
886+ throw std::runtime_error (" unset_iface_ip: Error in inet_pton" );
887+ }
888+
889+ rta = NLMSG_TAIL (nlmsg);
890+ rta->rta_type = IFA_LOCAL;
891+ rta->rta_len = RTA_LENGTH (sizeof (struct in_addr ));
892+ memcpy (RTA_DATA (rta), &ia, sizeof (struct in_addr ));
893+ nlmsg->nlmsg_len = NLMSG_ALIGN (nlmsg->nlmsg_len ) + RTA_ALIGN (rta->rta_len );
894+
895+ rta = NLMSG_TAIL (nlmsg);
896+ rta->rta_type = IFA_ADDRESS;
897+ rta->rta_len = RTA_LENGTH (sizeof (struct in_addr ));
898+ memcpy (RTA_DATA (rta), &ia, sizeof (struct in_addr ));
899+ nlmsg->nlmsg_len = NLMSG_ALIGN (nlmsg->nlmsg_len ) + RTA_ALIGN (rta->rta_len );
900+
901+ netlink_nl_send (nlmsg);
902+ }
903+
848904void Netlink::set_iface_ipv6 (const std::string &iface, const std::string &ipv6) {
849905 struct nlmsghdr *nlmsg = netlink_ipv6_alloc ();
850906 struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;
0 commit comments