+
+\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
+}