🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
10 messages
10 total messages Started by bill davidsen Fri, 05 Feb 1999 00:00
nnrpd daemon (-D) mode fixes
#35206
Author: bill davidsen
Date: Fri, 05 Feb 1999 00:00
132 lines
4527 bytes
There are performance and memory benefits from running nnrpd as a daemon
instead of letting innd start readers, particularly on machines with
moderate to heavy (1k+) readers.

This patch addresses one bug (wrong data type) and one missing feature
(nicekids). The type was a struct defined as sockaddr_in and used with a
case where sockaddr was required. Because I'm not sure all systems will
have the same size for both, and my compiler whines about the original,
I defined the value correctly and dropped the cast.

When running from innd, a nice() value is gotten from the nicekids value
in inn.conf. In daemon mode that wasn't being done, so I added the code
here. Note: you do *not* want to just start the daemon niced, as it will
then open sockets more slowly (I measured with NSLG). Nicekids is the
way to go here.

Patch against 2.2 release.

================================================================

*** nnrpd.c	1998/11/15 22:45:15	1.1
--- nnrpd.c	1999/02/01 21:29:22
***************
*** 1,6 ****
! /*  $Revision: 1.1 $
  **
  **  NNTP server for readers (NNRP) for InterNetNews.
  **  This server doesn't do any real load-limiting, except for what has
  **  proven empirically necesary (i.e., look at GRPscandir).
  */
--- 1,6 ----
! /*  $Revision: 1.4 $
  **
  **  NNTP server for readers (NNRP) for InterNetNews.
  **  This server doesn't do any real load-limiting, except for what has
  **  proven empirically necesary (i.e., look at GRPscandir).
  */
***************
*** 771,781 ****
      struct		timeval tv;
      unsigned short	ListenPort = NNTP_PORT;
      unsigned long	ListenAddr = htonl(INADDR_ANY);
      int			lfd, fd;
      ARGTYPE		clen;
!     struct sockaddr_in	ssa, csa;
      struct stat		Sb;
      PID_T		pid = -1;
      GID_T               NewsGID;
      UID_T               NewsUID;
      int                 one = 1;
--- 771,782 ----
      struct		timeval tv;
      unsigned short	ListenPort = NNTP_PORT;
      unsigned long	ListenAddr = htonl(INADDR_ANY);
      int			lfd, fd;
      ARGTYPE		clen;
!     struct sockaddr_in	ssa;
!     struct sockaddr     csa;
      struct stat		Sb;
      PID_T		pid = -1;
      GID_T               NewsGID;
      UID_T               NewsUID;
      int                 one = 1;
***************
*** 936,950 ****
  	(void)signal(SIGCHLD, WaitChild);
  	SetupDaemon();
   
  	TITLEset("nnrpd: accepting connections");
   	
! 	listen(lfd, 5);	
  
  listen_loop:
  	clen = sizeof(csa);
! 	fd = accept(lfd, (struct sockaddr *) &csa, &clen);
  	if (fd < 0)
  		goto listen_loop;
      
  	for (i = 0; (pid = fork()) < 0; i++) {
  	    if (i == MAX_FORKS) {
--- 937,951 ----
  	(void)signal(SIGCHLD, WaitChild);
  	SetupDaemon();
   
  	TITLEset("nnrpd: accepting connections");
   	
! 	listen(lfd, 5);
  
  listen_loop:
  	clen = sizeof(csa);
! 	fd = accept(lfd, &csa, &clen);
  	if (fd < 0)
  		goto listen_loop;
      
  	for (i = 0; (pid = fork()) < 0; i++) {
  	    if (i == MAX_FORKS) {
***************
*** 966,978 ****
  	dup2(fd, 0);
  	close(fd);
  	dup2(0, 1);
  	dup2(0, 2);
  
  	/* Only automatically reap children in the listening process */
  	(void)signal(SIGCHLD, SIG_DFL);
-  
      } else {
  	SetupDaemon();
      }/* DaemonMode */
  
      /* Setup. */
--- 967,984 ----
  	dup2(fd, 0);
  	close(fd);
  	dup2(0, 1);
  	dup2(0, 2);
  
+ 	/* if we are a daemon innd didn't make us nice, so be nice kids */
+ 	if (innconf->nicekids) {
+ 	    if (nice(innconf->nicekids) < 0)
+ 	        syslog(L_ERROR, "Could not nice child to %d: %m", innconf->nicekids);
+ 	}
+ 
  	/* Only automatically reap children in the listening process */
  	(void)signal(SIGCHLD, SIG_DFL);
      } else {
  	SetupDaemon();
      }/* DaemonMode */
  
      /* Setup. */

Re: nnrpd daemon (-D) mode fixes
#35199
Author: Andrew Gierth
Date: Fri, 05 Feb 1999 00:00
19 lines
1735 bytes
>>>>> "bill" == bill davidsen <davidsen@tmr.com> writes:

 bill> This patch addresses one bug (wrong data type) and one missing
 bill> feature (nicekids). The type was a struct defined as
 bill> sockaddr_in and used with a case where sockaddr was
 bill> required. Because I'm not sure all systems will have the same
 bill> size for both, and my compiler whines about the original, I
 bill> defined the value correctly and dropped the cast.

Pardon me asking, but are you new to socket programming or something?

The original code was correct in this respect. This is why accept()
et. al. have an additional size parameter for the address. The cast is
something of a historical crock, but is nevertheless correct.

-- 
Andrew.

Re: nnrpd daemon (-D) mode fixes
#35278
Author: Andrew Gierth
Date: Wed, 10 Feb 1999 00:00
73 lines
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.

Re: nnrpd daemon (-D) mode fixes
#35291
Author: bill davidsen
Date: Wed, 10 Feb 1999 00:00
34 lines
2314 bytes
In article <873e4kprj6.fsf@erlenstar.demon.co.uk>,
Andrew Gierth  <andrew@erlenstar.demon.co.uk> wrote:
| >>>>> "bill" == bill davidsen <davidsen@tmr.com> writes:
| 
|  bill> This patch addresses one bug (wrong data type) and one missing
|  bill> feature (nicekids). The type was a struct defined as
|  bill> sockaddr_in and used with a case where sockaddr was
|  bill> required. Because I'm not sure all systems will have the same
|  bill> size for both, and my compiler whines about the original, I
|  bill> defined the value correctly and dropped the cast.
| 
| Pardon me asking, but are you new to socket programming or something?
| 
| The original code was correct in this respect. This is why accept()
| et. al. have an additional size parameter for the address. The cast is
| something of a historical crock, but is nevertheless correct.

When a procedure prototype requires a struct of one type, and you
provide a struct of another type which mayor may not be the same size
on every system, using a cast is not "correct." What is correct is to
declare the struct of the proper type in the first place. It's used
only in one place, so this is not "clever" use of an existing struct to
save space, it's just the wrong type struct.

Pardon my asking, but are you new to writing portable code or
something?

-- 
  bill davidsen <davidsen@tmr.com>  CTO, TMR Associates, Inc
Politicians and diapers have one thing in common. They should both be
changed regularly and for the same reason.
	--Ted Symons(?)

Re: nnrpd daemon (-D) mode fixes
#35306
Author: Forrest J. Cavalier III
Date: Wed, 10 Feb 1999 00:00
34 lines
2004 bytes
In article <79smvr$7ni8$1@newssvr04-int.news.prodigy.com>, davidsen@tmr.com wrote...
>
>In article <873e4kprj6.fsf@erlenstar.demon.co.uk>,
>Andrew Gierth  <andrew@erlenstar.demon.co.uk> wrote:
[snip]
>| The original code was correct in this respect. This is why accept()
>| et. al. have an additional size parameter for the address. The cast is
>| something of a historical crock, but is nevertheless correct.
>
>When a procedure prototype requires a struct of one type, and you
>provide a struct of another type which mayor may not be the same size
>on every system, using a cast is not "correct." What is correct is to
>declare the struct of the proper type in the first place. It's used
>only in one place, so this is not "clever" use of an existing struct to
>save space, it's just the wrong type struct.
>
>Pardon my asking, but are you new to writing portable code or
>something?

Bill, maybe you can think of it this way:

The accept() function properly handles a number of sockaddr_
types. If it were C++, the problem would be solved by inherited
classes.  

Bill, your "corrected" code fails if a sizeof(sockaddr_in) is
larger than sizeof(sockaddr).  (What should happen is that
accept() would see a wrong length member and return an error.)


Forrest


Re: nnrpd daemon (-D) mode fixes
#35369
Author: bill davidsen
Date: Fri, 12 Feb 1999 00:00
20 lines
1647 bytes
In article <79t2ms$iil$1@news1.epix.net>,
Forrest J. Cavalier III <mibsoft@mibsoftware.com> wrote:

| Bill, your "corrected" code fails if a sizeof(sockaddr_in) is
| larger than sizeof(sockaddr).  (What should happen is that
| accept() would see a wrong length member and return an error.)

Then perhaps the prototype is wrong, since it requires sockaddr. If so,
it's wrong on a LOT of systems... which is why I assumed that the
prototype was correct and the source was wrong. If that's the case it's
wrong on at least Linux, BSD, SCO and AIX, I checked there before I
changed it.
-- 
  bill davidsen <davidsen@tmr.com>  CTO, TMR Associates, Inc
Politicians and diapers have one thing in common. They should both be
changed regularly and for the same reason.
	--Ted Symons(?)


Re: nnrpd daemon (-D) mode fixes
#35376
Author: Russ Allbery
Date: Fri, 12 Feb 1999 00:00
31 lines
2153 bytes
bill davidsen <davidsen@tmr.com> writes:

> Then perhaps the prototype is wrong, since it requires sockaddr. If so,
> it's wrong on a LOT of systems... which is why I assumed that the
> prototype was correct and the source was wrong. If that's the case it's
> wrong on at least Linux, BSD, SCO and AIX, I checked there before I
> changed it.

It's been an axiom of writing socket code under Unix for many, many years
that you always have to cast your addresses to struct sockaddr when
passing them into network functions.

Solaris, IIRC, has recently done some header magic (which may or may not
work with compilers other than theirs) so that one doesn't get a warning
when passing in a struct sockaddr_in.  In my opinion, this just makes the
problem worse, since people then don't realize that will produce warnings
on other platforms.

The fundamental problem is that, in the standard network functions in the
C library, the socket address is actually something very akin to a
polymorphic object.  A C++ implementation would be able to resolve this
problem, since there would be a socket_address superclass, and ip_address,
ip6_address, unix_address, and the like would all inherit from it.  Then
you could let standard polymorphism handle this problem.

In C, you have to use C's version of polymorphism, which is typecasts.  :)

-- 
Russ Allbery (rra@stanford.edu)         <URL:http://www.eyrie.org/~eagle/>

Re: nnrpd daemon (-D) mode fixes
#35398
Author: Aidan Cully
Date: Fri, 12 Feb 1999 00:00
36 lines
2355 bytes
In news.software.nntp, davidsen@tmr.com (bill davidsen) wrote:
> In article <79t2ms$iil$1@news1.epix.net>,
> Forrest J. Cavalier III <mibsoft@mibsoftware.com> wrote:
> 
> | Bill, your "corrected" code fails if a sizeof(sockaddr_in) is
> | larger than sizeof(sockaddr).  (What should happen is that
> | accept() would see a wrong length member and return an error.)
> 
> Then perhaps the prototype is wrong, since it requires sockaddr.

Looked at a certain way, it's wrong that it's prototyped.  But if
you're going to prototype it, you should use a sockaddr, or maybe
extern int accept __P((...)); (but I don't really like that one).

> If so,
> it's wrong on a LOT of systems... which is why I assumed that the
> prototype was correct and the source was wrong. If that's the case it's
> wrong on at least Linux, BSD, SCO and AIX, I checked there before I
> changed it.

The same function can take structures of several different types..
sockaddr_in and sockaddr_un are the most common, I think, but there's
another one for IPv6, and for the appletalk protocol..  All these
types are "derived" from struct sockaddr, and the kernel will dispatch
them to different internal functions based on the s*_family field
(which is at the same offset in all struct sockaddr_*s).

What do you think the correct way to do this is?

--aidan
-- 
Aidan Cully       "Umm..  Could you call back?  My house is on fire."
Panix Staff          --Tom Waits
aidan@panix.com

Re: nnrpd daemon (-D) mode fixes
#35404
Author: Andrew Gierth
Date: Fri, 12 Feb 1999 00:00
46 lines
2964 bytes
>>>>> "bill" == bill davidsen <davidsen@tmr.com> writes:

 [Forrest]
 > | Bill, your "corrected" code fails if a sizeof(sockaddr_in) is
 > | larger than sizeof(sockaddr).  (What should happen is that
 > | accept() would see a wrong length member and return an error.)

 bill> Then perhaps the prototype is wrong, since it requires sockaddr.

The prototype is perhaps misleading, but it's not wrong.

Consider the use of other address families with accept() et. al. The
actual type of address structure passed might be any one of sockaddr_in,
sockaddr_un, sockaddr_in6, sockaddr_???...

The prototype can't specify any single one of these types; that would
be clearly wrong. The only way to avoid casts would be for the
prototype to specify (void*) for the type instead; but that doesn't
tell you anything useful about what the parameter is supposed to
be. For whatever reason, the standards currently dictate that address
parameters are of type (struct sockaddr *), and casts are always
required when passing the address of an actual object.

(If you're writing protocol-independent code, then you wouldn't be
declaring a sockaddr structure of any flavour to use for an accept
call, but instead you'd have to use malloc() to allocate sufficient
space (as determined earlier when you bound the socket). Using
'struct sockaddr' would be wrong because many address families use
longer address structures. But nnrpd in its current form is not
protocol-independent.)

 bill> If so, it's wrong on a LOT of systems... which is why I assumed
 bill> that the prototype was correct and the source was wrong. If
 bill> that's the case it's wrong on at least Linux, BSD, SCO and AIX,
 bill> I checked there before I changed it.

From the Single Unix spec:

int accept (int socket, struct sockaddr *address, socklen_t *address_len);

(note that 'socklen_t' is relatively new, and 'int' is more common)

-- 
Andrew.

Re: nnrpd daemon (-D) mode fixes
#35422
Author: Forrest J. Cavalier III
Date: Sat, 13 Feb 1999 00:00
39 lines
2447 bytes
In article <7a1t47$8rka$1@newssvr04-int.news.prodigy.com>, davidsen@tmr.com wrote...
>
>In article <79t2ms$iil$1@news1.epix.net>,
>Forrest J. Cavalier III <mibsoft@mibsoftware.com> wrote:
>
>| Bill, your "corrected" code fails if a sizeof(sockaddr_in) is
>| larger than sizeof(sockaddr).  (What should happen is that
>| accept() would see a wrong length member and return an error.)
>
>Then perhaps the prototype is wrong, since it requires sockaddr. If so,
>it's wrong on a LOT of systems... which is why I assumed that the
>prototype was correct and the source was wrong. If that's the case it's
>wrong on at least Linux, BSD, SCO and AIX, I checked there before I
>changed it.

No.  That is the point you are missing.

You can't change the prototype to sockaddr_in, because it also takes
takes sockaddr_un, and other types, depending on what protocol family
was used to create the listen socket.  The system can differentiate.

Perhaps you (and your compiler) would have liked it better if the
prototype took a (void *), but that is much less descriptive
documentation.

Or perhaps you would have liked to see separate functions.
accept_in(), accept_un(), accept_iso() for each socket type.
Regardless of what you would have liked, the convention is set
that one function is enough to handle different structure types
passed to it.  One type of structure is not adequate for
all protocol families.  (See the socket() man page...
http://www.mibsoftware.com/reuse/cfn/man2/socket.2.htm
for protocol families.)  You should NEVER declare an object of
sockaddr.  You must use one of the protocol specific versions.

Forrest


Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads