diff options
author | Daniel Lezcano <dlezcano@fr.ibm.com> | 2009-10-09 11:38:39 +0200 |
---|---|---|
committer | Daniel Lezcano <dlezcano@fr.ibm.com> | 2009-10-09 11:38:39 +0200 |
commit | 4bf1968d3c0bb29ca4c068194f02975a5dfa2385 (patch) | |
tree | 85691be93d021399619143bd8e87a2349ff1d69b | |
parent | Use the configuration structure for the network (diff) | |
download | lxc-4bf1968d3c0bb29ca4c068194f02975a5dfa2385.tar.gz lxc-4bf1968d3c0bb29ca4c068194f02975a5dfa2385.tar.bz2 lxc-4bf1968d3c0bb29ca4c068194f02975a5dfa2385.zip |
Factor out some network code
We can factor out the "ip addr add"
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
-rw-r--r-- | src/lxc/conf.c | 11 | ||||
-rw-r--r-- | src/lxc/network.c | 66 | ||||
-rw-r--r-- | src/lxc/network.h | 6 |
3 files changed, 15 insertions, 68 deletions
diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 12cbf6e..1989ad8 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -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; diff --git a/src/lxc/network.c b/src/lxc/network.c index ab80acd..57f9331 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -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) { diff --git a/src/lxc/network.h b/src/lxc/network.h index 4b6ca1c..0534ec4 100644 --- a/src/lxc/network.h +++ b/src/lxc/network.h @@ -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 |