Question on a wrapper function. - Unix

This is a discussion on Question on a wrapper function. - Unix ; The following wrapper is part of a 8000 line ICMP Daemon int sock_get_port(const struct sockaddr *sa, socklen_t salen) { switch (sa->sa_family) { case AF_INET: { struct sockaddr_in *sin = (struct sockaddr_in *) sa; return(sin->sin_port); } #ifdef IPV6 case AF_INET6: { ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Question on a wrapper function.

  1. Question on a wrapper function.

    The following wrapper is part of a 8000 line ICMP Daemon

    int
    sock_get_port(const struct sockaddr *sa, socklen_t salen)
    {
    switch (sa->sa_family) {
    case AF_INET: {
    struct sockaddr_in *sin = (struct sockaddr_in *) sa;

    return(sin->sin_port);
    }

    #ifdef IPV6
    case AF_INET6: {
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;

    return(sin6->sin6_port);
    }
    #endif
    }

    return(-1);
    }

    Why pass salen (ie the length) if you don't use it?

  2. Re: Question on a wrapper function.

    On Nov 24, 10:15 am, K-mart Cashier wrote:
    > The following wrapper is part of a 8000 line ICMP Daemon
    >
    > int
    > sock_get_port(const struct sockaddr *sa, socklen_t salen)
    > {
    > switch (sa->sa_family) {
    > case AF_INET: {
    > struct sockaddr_in *sin = (struct sockaddr_in *) sa;
    >
    > return(sin->sin_port);
    > }
    >
    > #ifdef IPV6
    > case AF_INET6: {
    > struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
    >
    > return(sin6->sin6_port);
    > }
    > #endif
    > }
    >
    > return(-1);
    >
    > }
    >
    > Why pass salen (ie the length) if you don't use it?


    Historical reasons, maybe, or else poor design. It might be
    educational to see what the code looked like in prior versions, if you
    have access to them.

    If salen is in fact the space available in *sa, I'd think it would be
    a good idea to check that salen >= sizeof(struct sockaddr_in) (or
    sockaddr_in6 as appropriate), just out of caution. But if the
    programmer is sure that *sa is always of the appropriate type, it
    isn't strictly necessary.

+ Reply to Thread