X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fsocket-util.c;h=7f34ea2e939ea86b0e27e482b5ba4ab477e106a1;hb=6b900de4efd368be88ef3db88b1eff8f835a7cf7;hp=1d0cede88e4830bd7975478498647bcdbe05e7f6;hpb=d017eeb9f9ebcb46c24a67fd301b3e36cd26a04e;p=sliver-openvswitch.git diff --git a/lib/socket-util.c b/lib/socket-util.c index 1d0cede88..7f34ea2e9 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1353,3 +1354,42 @@ error: *n_fdsp = 0; return EPROTO; } + +/* Calls ioctl() on an AF_INET sock, passing the specified 'command' and + * 'arg'. Returns 0 if successful, otherwise a positive errno value. */ +int +af_inet_ioctl(unsigned long int command, const void *arg) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + static int sock; + + if (ovsthread_once_start(&once)) { + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + sock = -errno; + VLOG_ERR("failed to create inet socket: %s", ovs_strerror(errno)); + } + ovsthread_once_done(&once); + } + + return (sock < 0 ? -sock + : ioctl(sock, command, arg) == -1 ? errno + : 0); +} + +int +af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd, + const char *cmd_name) +{ + int error; + + ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name); + error = af_inet_ioctl(cmd, ifr); + if (error) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); + VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name, + ovs_strerror(error)); + } + return error; +} +