On Fri, Jun 13, 2014 at 04:18:15PM +0900, Simon Horman wrote:
> This helper is similar to addr_to_str but
> tries to convert the port rather than the address
> of a struct sockaddr_storage.
> 
> This is in preparation for supporting
> an external agent check.

I may be wrong, but I'm seeing an endianness issue here, am I wrong ?
The sockaddr_storage stores in network order, so you cannot simply take
the port and print it using "%u" without first applying ntohs().

> +/* Tries to convert a sockaddr_storage port to text form. Upon success, the
> + * address family is returned so that it's easy for the caller to adapt to 
> the
> + * output format. Zero is returned if the address family is not supported. -1
> + * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
> + * supported.
> + */
> +int port_to_str(struct sockaddr_storage *addr, char *str, int size)
> +{
> +
> +     uint16_t port;
> +
> +
> +     if (size < 5)
> +             return 0;
> +     *str = '\0';
> +
> +     switch (addr->ss_family) {
> +     case AF_INET:
> +             port = ((struct sockaddr_in *)addr)->sin_port;
> +             break;
> +     case AF_INET6:
> +             port = ((struct sockaddr_in6 *)addr)->sin6_port;
> +             break;
> +     case AF_UNIX:
> +             memcpy(str, "unix", 5);
> +             return addr->ss_family;
> +     default:
> +             return 0;
> +     }
> +
> +     snprintf(str, size, "%u", port);
> +     return addr->ss_family;
> +}

Willy


Reply via email to