From: Dongsheng Yang Date: Tue, 16 Sep 2014 05:09:59 +0000 (+0800) Subject: network: introduce a interface named lxc_netdev_isup(). X-Git-Tag: lxc-1.1.0.alpha3~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=efa1cf45a23c450b4566705f2fe46fc22a0a6482;p=thirdparty%2Flxc.git network: introduce a interface named lxc_netdev_isup(). When we need to know some info about a netdev, such as is_up or not, we need to read the flag for the netdev. This patch introduce a interface function named lxc_netdev_isup() to check is a netdev up or down. And introduce a network private function named netdev_get_flag() to get flag for netdev by netlink. Changelog: 10/15/2015: Return failure if name==NULL to avoid later strlen fun Signed-off-by: Dongsheng Yang Acked-by: Serge E. Hallyn --- diff --git a/src/lxc/network.c b/src/lxc/network.c index 4c577b17d..941cb35e4 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -305,6 +305,86 @@ out: return err; } +int netdev_get_flag(const char* name, int *flag) +{ + struct nl_handler nlh; + struct nlmsg *nlmsg = NULL, *answer = NULL; + struct link_req *link_req; + int index, len, err; + struct ifinfomsg *ifi; + + if (!name) + return -EINVAL; + + err = netlink_open(&nlh, NETLINK_ROUTE); + if (err) + return err; + + err = -EINVAL; + len = strlen(name); + if (len == 1 || len >= IFNAMSIZ) + goto out; + + err = -ENOMEM; + nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); + if (!nlmsg) + goto out; + + answer = nlmsg_alloc(NLMSG_GOOD_SIZE); + if (!answer) + goto out; + + err = -EINVAL; + index = if_nametoindex(name); + if (!index) + goto out; + + link_req = (struct link_req *)nlmsg; + link_req->ifinfomsg.ifi_family = AF_UNSPEC; + link_req->ifinfomsg.ifi_index = index; + nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); + nlmsg->nlmsghdr.nlmsg_flags = NLM_F_REQUEST; + nlmsg->nlmsghdr.nlmsg_type = RTM_GETLINK; + + err = netlink_transaction(&nlh, nlmsg, answer); + if (err) + goto out; + + ifi = NLMSG_DATA(answer); + + *flag = ifi->ifi_flags; +out: + netlink_close(&nlh); + nlmsg_free(nlmsg); + nlmsg_free(answer); + return err; +} + +/* + * \brief Check a interface is up or not. + * + * \param name: name for the interface. + * + * \return int. + * 0 means interface is down. + * 1 means interface is up. + * Others means error happened, and ret-value is the error number. + */ +int lxc_netdev_isup(const char* name) +{ + int flag; + int err; + + err = netdev_get_flag(name, &flag); + if (err) + goto out; + if (flag & IFF_UP) + return 1; + return 0; +out: + return err; +} + int netdev_get_mtu(int ifindex) { struct nl_handler nlh; diff --git a/src/lxc/network.h b/src/lxc/network.h index 768489df0..cd42c8f59 100644 --- a/src/lxc/network.h +++ b/src/lxc/network.h @@ -51,6 +51,8 @@ extern int netdev_set_flag(const char *name, int flag); /* * Set the device network up or down */ + +extern int lxc_netdev_isup(const char *name); extern int lxc_netdev_up(const char *name); extern int lxc_netdev_down(const char *name);