We can factor out the "ip addr add"

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
---
 src/lxc/conf.c    |   11 ++++-----
 src/lxc/network.c |   64 ++++++------------------------------------------------
 src/lxc/network.h |    6 -----
 3 files changed, 14 insertions(+), 67 deletions(-)

Index: lxc/src/lxc/conf.c
===================================================================
--- lxc.orig/src/lxc/conf.c
+++ lxc/src/lxc/conf.c
@@ -786,8 +786,8 @@ static int setup_ipv4_addr(struct lxc_li
 
 		inetdev = iterator->elem;
 
-		if (lxc_ip_addr_add(ifindex, inetdev->addr,
-				    inetdev->prefix, inetdev->bcast)) {
+		if (lxc_ip_addr_add(AF_INET, ifindex,
+				    &inetdev->addr, inetdev->prefix)) {
 			return -1;
 		}
 	}
@@ -798,16 +798,15 @@ static int setup_ipv4_addr(struct lxc_li
 static int setup_ipv6_addr(struct lxc_list *ip, int ifindex)
 {
 	struct lxc_list *iterator;
-	struct lxc_inet6dev *inet6dev;
+	struct lxc_inetdev *inet6dev;
 
 	lxc_list_for_each(iterator, ip) {
 
 		inet6dev = iterator->elem;
 
-		if (lxc_ip6_addr_add(ifindex, inet6dev->addr,
-				     inet6dev->prefix, inet6dev->bcast)) {
+		if (lxc_ip_addr_add(AF_INET6, ifindex,
+				    & inet6dev->addr, inet6dev->prefix))
 			return -1;
-		}
 	}
 
 	return 0;
Index: lxc/src/lxc/network.c
===================================================================
--- lxc.orig/src/lxc/network.c
+++ lxc/src/lxc/network.c
@@ -565,14 +565,17 @@ int lxc_convert_mac(char *macaddr, struc
     return 0;
 }
 
-int lxc_ip_addr_add(int ifindex, struct in_addr in_addr,
-		    int prefix,  struct in_addr in_bcast)
+int lxc_ip_addr_add(int family, int ifindex, void *addr, int prefix)
 {
 	struct nl_handler nlh;
 	struct nlmsg *nlmsg = NULL, *answer = NULL;
 	struct ip_req *ip_req;
+	int addrlen;
 	int err = -1;
 
+	addrlen = family == AF_INET ? sizeof(struct in_addr) :
+		sizeof(struct in6_addr);
+
 	if (netlink_open(&nlh, NETLINK_ROUTE))
 		return -1;
 
@@ -591,13 +594,13 @@ int lxc_ip_addr_add(int ifindex, struct 
         ip_req->nlmsg.nlmsghdr.nlmsg_type = RTM_NEWADDR;
 	ip_req->ifa.ifa_prefixlen = prefix;
         ip_req->ifa.ifa_index = ifindex;
-        ip_req->ifa.ifa_family = AF_INET;
+        ip_req->ifa.ifa_family = family;
 	ip_req->ifa.ifa_scope = 0;
 	
-	if (nla_put_buffer(nlmsg, IFA_LOCAL, &in_addr, sizeof(in_addr)))
+	if (nla_put_buffer(nlmsg, IFA_LOCAL, addr, addrlen))
 		goto out;
 
-	if (nla_put_buffer(nlmsg, IFA_ADDRESS, &in_addr, sizeof(in_addr)))
+	if (nla_put_buffer(nlmsg, IFA_ADDRESS, addr, addrlen))
 		goto out;
 
 /* 	if (in_bcast.s_addr != INADDR_ANY) */
@@ -616,57 +619,6 @@ out:
 	return err;
 }
 
-int lxc_ip6_addr_add(int ifindex, struct in6_addr in6_addr,
-		     int prefix,  struct in6_addr in6_bcast)
-{
-	struct nl_handler nlh;
-	struct nlmsg *nlmsg = NULL, *answer = NULL;
-	struct ip_req *ip_req;
-	int err = -1;
-
-	if (netlink_open(&nlh, NETLINK_ROUTE))
-		return -1;
-
-	nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
-	if (!nlmsg)
-		goto out;
-
-	answer = nlmsg_alloc(NLMSG_GOOD_SIZE);
-	if (!answer)
-		goto out;
-
-	ip_req = (struct ip_req *)nlmsg;
-        ip_req->nlmsg.nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
-        ip_req->nlmsg.nlmsghdr.nlmsg_flags =
-		NLM_F_ACK|NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL;
-        ip_req->nlmsg.nlmsghdr.nlmsg_type = RTM_NEWADDR;
-	ip_req->ifa.ifa_prefixlen = prefix;
-        ip_req->ifa.ifa_index = ifindex;
-        ip_req->ifa.ifa_family = AF_INET6;
-	ip_req->ifa.ifa_scope = 0;
-	
-	if (nla_put_buffer(nlmsg, IFA_LOCAL, &in6_addr, sizeof(in6_addr)))
-		goto out;
-
-	if (nla_put_buffer(nlmsg, IFA_ADDRESS, &in6_addr, sizeof(in6_addr)))
-		goto out;
-
-/* 	if (in6_bcast.s6_addr != in6addr_any.s6_addr) */
-/* 		if (nla_put_buffer(nlmsg, IFA_BROADCAST, &in6_bcast, */
-/* 				   sizeof(in6_bcast))) */
-/* 			goto out; */
-
-	if (netlink_transaction(&nlh, nlmsg, answer))
-		goto out;
-
-	err = 0;
-out:
-	netlink_close(&nlh);
-	nlmsg_free(answer);
-	nlmsg_free(nlmsg);
-	return err;
-}
-
 static int bridge_add_del_interface(const char *bridge, 
 				    const char *ifname, int detach)
 {
Index: lxc/src/lxc/network.h
===================================================================
--- lxc.orig/src/lxc/network.h
+++ lxc/src/lxc/network.h
@@ -81,11 +81,7 @@ extern int lxc_ip_forward_off(const char
 /*
  * Set ip address
  */
-extern int lxc_ip_addr_add(int ifindex, struct in_addr addr,
-			   int prefix, struct in_addr bcast);
-
-extern int lxc_ip6_addr_add(int ifindex, struct in6_addr addr,
-			    int prefix, struct in6_addr bcast);
+extern int lxc_ip_addr_add(int family, int ifindex, void *addr, int prefix);
 
 /*
  * Attach an interface to the bridge



