Unix Programming - Question on a wrapper function.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2007 > Question on a wrapper function.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Question on a wrapper function.
K-mart Cashier

2007-11-24, 1:22 pm

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?
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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com