| fjblurt@yahoo.com 2007-11-25, 1:37 am |
| On Nov 24, 10:15 am, K-mart Cashier <cdal...@gmail.com> 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.
|