+
+#ifndef _WIN32
+/* 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) {
+ int error = sock_errno();
+ VLOG_ERR("failed to create inet socket: %s", sock_strerror(error));
+ sock = -error;
+ }
+ 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;
+}
+#endif
+\f
+/* sockaddr_storage helpers. */
+
+/* Returns the IPv4 or IPv6 port in 'ss'. */
+uint16_t
+ss_get_port(const struct sockaddr_storage *ss)
+{
+ if (ss->ss_family == AF_INET) {
+ const struct sockaddr_in *sin
+ = ALIGNED_CAST(const struct sockaddr_in *, ss);
+ return ntohs(sin->sin_port);
+ } else if (ss->ss_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6
+ = ALIGNED_CAST(const struct sockaddr_in6 *, ss);
+ return ntohs(sin6->sin6_port);
+ } else {
+ OVS_NOT_REACHED();
+ }
+}
+
+/* Formats the IPv4 or IPv6 address in 'ss' into the 'bufsize' bytes in 'buf'.
+ * If 'ss' is an IPv6 address, puts square brackets around the address.
+ * 'bufsize' should be at least SS_NTOP_BUFSIZE.
+ *
+ * Returns 'buf'. */
+char *
+ss_format_address(const struct sockaddr_storage *ss,
+ char *buf, size_t bufsize)
+{
+ ovs_assert(bufsize >= SS_NTOP_BUFSIZE);
+ if (ss->ss_family == AF_INET) {
+ const struct sockaddr_in *sin
+ = ALIGNED_CAST(const struct sockaddr_in *, ss);
+
+ snprintf(buf, bufsize, IP_FMT, IP_ARGS(sin->sin_addr.s_addr));
+ } else if (ss->ss_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6
+ = ALIGNED_CAST(const struct sockaddr_in6 *, ss);
+
+ buf[0] = '[';
+ inet_ntop(AF_INET6, sin6->sin6_addr.s6_addr, buf + 1, bufsize - 1);
+ strcpy(strchr(buf, '\0'), "]");
+ } else {
+ OVS_NOT_REACHED();
+ }
+
+ return buf;
+}
+
+size_t
+ss_length(const struct sockaddr_storage *ss)
+{
+ switch (ss->ss_family) {
+ case AF_INET:
+ return sizeof(struct sockaddr_in);
+
+ case AF_INET6:
+ return sizeof(struct sockaddr_in6);
+
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+/* For Windows socket calls, 'errno' is not set. One has to call
+ * WSAGetLastError() to get the error number and then pass it to
+ * this function to get the correct error string.
+ *
+ * ovs_strerror() calls strerror_r() and would not get the correct error
+ * string for Windows sockets, but is good for POSIX. */
+const char *
+sock_strerror(int error)
+{
+#ifdef _WIN32
+ return ovs_format_message(error);
+#else
+ return ovs_strerror(error);
+#endif
+}