]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Factor out some network code
authorDaniel Lezcano <dlezcano@fr.ibm.com>
Fri, 9 Oct 2009 09:38:39 +0000 (11:38 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Fri, 9 Oct 2009 09:38:39 +0000 (11:38 +0200)
We can factor out the "ip addr add"

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/conf.c
src/lxc/network.c
src/lxc/network.h

index 12cbf6e71396b69ed109d004f45f21b0f07858dc..1989ad8fc53dcc5185d59ed127df3bd867859049 100644 (file)
@@ -787,8 +787,8 @@ static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
 
                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;
                }
        }
@@ -799,16 +799,15 @@ static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
 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 ab80acd71991eab468a2f837658e3d353836f13d..57f9331ecd621f7801481e7f8377da574196b298 100644 (file)
@@ -86,7 +86,7 @@ int lxc_device_move(int ifindex, pid_t pid)
        struct nl_handler nlh;
        struct nlmsg *nlmsg = NULL;
        struct link_req *link_req;
-       int len, err = -1;
+       int err = -1;
 
        if (netlink_open(&nlh, NETLINK_ROUTE))
                return -1;
@@ -565,14 +565,17 @@ int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr)
     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 in_addr in_addr,
         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 4b6ca1c4caecf572e29c3e16602a416656e16210..0534ec41bbac268fa1bbc8949b3abc758fbbbdfd 100644 (file)
@@ -81,11 +81,7 @@ extern int lxc_ip_forward_off(const char *name, int family);
 /*
  * 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