Article View: news.software.nntp
Article #35278Re: nnrpd daemon (-D) mode fixes
From: Andrew Gierth
Date: Wed, 10 Feb 1999 00:00
Date: Wed, 10 Feb 1999 00:00
73 lines
4098 bytes
4098 bytes
>>>>> "bill" == bill davidsen <davidsen@tmr.com> writes:
bill> When a procedure prototype requires a struct of one type, and
bill> you provide a struct of another type which mayor may not be the
bill> same size on every system, using a cast is not "correct." What
bill> is correct is to declare the struct of the proper type in the
bill> first place.
Not in this particular case. (Don't blame me, blame the guys who
designed the socket API in the first place, but remember that this all
happened before useful things like "void" and function prototypes were
invented.)
struct sockaddr is a "generic" socket address, which is useful only
for passing around pointers to when you don't know what address family
you are dealing with. It's not intended that you ever actually allocate
structures of this type.
struct sockaddr_in, struct sockaddr_in6, struct sockaddr_un, etc., are
socket address structures for specific families. When you know what
address family you are dealing with, you allocate a structure of the
appropriate type. All these types are designed such that the family
(and length, if present) members are compatible with those of the
generic structure, but they may not be the same size as the generic
struct (sockaddr_in usually is, but sockaddr_in6 and sockaddr_un are
larger). To cope with the variation in size, functions that take or
return socket addresses use an additional length parameter to specify
the address size.
bill> It's used only in one place, so this is not "clever" use of an
bill> existing struct to save space, it's just the wrong type struct.
You have completely missed the point.
Yes, the prototypes for some socket functions are slightly strange,
because the functions were around long before prototypes existed.
Some systems have actually had different types for those parameters
(e.g. accept(int, caddr_t, int*) or equally improbable things).
Now, it happens that in this case the code doesn't appear to look at
the actual address that's returned (one assumes it retrieves it again
later via getpeername, because that's the traditional nnrpd behaviour
(when started from inetd or innd, the accept has already been done
and the returned address is never passed to nnrpd, so it has to fetch
it for itself).
But nevertheless, the correct way to do accept() for an IPv4-only
server is:
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int rc;
rc = accept(sock, (struct sockaddr *) &addr, &addrlen);
(substitute "int" for "socklen_t" if the latter is absent; socklen_t
is a relatively recent invention.)
Yes, it's an ugly cast, but it's not a portability problem, and
neither is it a bug, and you shouldn't try and "fix" it.
bill> Pardon my asking, but are you new to writing portable code or
bill> something?
No.
Incidentally, I recommend Stevens' "Unix Network Programming", second
edition, volume 1. You'll find this issue covered in chapter 3.
--
Andrew.
Technical Details
Message-ID:
<87g18eyo7a.fsf@erlenstar.demon.co.uk>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailReferences:
<79eufj$7nh0$1@newssvr04-int.news.prodigy.com> <873e4kprj6.fsf@erlenstar.demon.co.uk> <79smvr$7ni8$1@newssvr04-int.news.prodigy.com>Show Full Headers
Complete Article Headers
Message-ID: <87g18eyo7a.fsf@erlenstar.demon.co.uk> Subject: Re: nnrpd daemon (-D) mode fixes Date: Wed, 10 Feb 1999 00:00:00 +0000 From: Andrew Gierth <andrew@erlenstar.demon.co.uk> References: <79eufj$7nh0$1@newssvr04-int.news.prodigy.com> <873e4kprj6.fsf@erlenstar.demon.co.uk> <79smvr$7ni8$1@newssvr04-int.news.prodigy.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 442951402 X-NNTP-Posting-Host: erlenstar.demon.co.uk:194.222.144.22 X-Mayan-Date: Long count = 12.19.5.16.15; tzolkin = 9 Men; haab = 8 Pax X-Complaints-To: abuse@demon.net X-Trace: news.demon.co.uk 918680087 nnrp-08:29185 NO-IDENT erlenstar.demon.co.uk:194.222.144.22 Organization: disorganised X-Attribution: AG X-Pgp-0x0E9FFBE9: 87 25 7F 14 41 24 B3 51 E0 19 8B DE 49 74 0C 29 X-P-Persistent-Headers: are a bad idea. Newsgroups: news.software.nntp X-Post-Filter: postfilter v0.01 alpha