Avoiding perl, UDP QOTD (Re: QOTD server (Re: Help with socket 'select'))
Bart Massey
bart at videovax.tv.tek.com
Tue Nov 28 09:16:49 AEST 1989
In article <13937 at orstcs.CS.ORST.EDU> pvo3366 at sapphire.OCE.ORST.EDU (Paul O'Neill) writes:
>
> Only a few hosts in the NIC host tables advertise supporting QOTD service.
> (Quote of the day.) This should change that!!
Note that for Berkeley-derived systems, at least, the above-referenced
perl code is unnecessary -- inetd will do exactly the right thing. Try
putting the following line in /etc/inetd.conf (or wherever you hide your
inetd config).
quote stream tcp nowait nobody /usr/games/fortune fortune -a
This works great on my 4.3-Tahoe box.
FTP Software's PC TCP/IP stuff includes a client known as "cookie", which
uses the UDP version of the quote service. Since we have a bunch of those
boxes here, I spent 15 minutes whacking together a UDP quote server which
runs under inetd...
cookie dgram udp wait nobody /usr/local/cookied cookied
The following line should also be added to /etc/services.
cookie 17/udp # FTP software's cookie server
And here's the C code for the server. Yes, I know this isn't production
quality, but I typed it in *really fast* :-). Please send patches with any
bug reports, and be aware that I'm not really very interested in making it
run on anything but 4BSD boxes...
Bart Massey
..tektronix!videovax.tv.tek.com!bart
..tektronix!reed.bitnet!bart
/*
--- cut here -----
/*
* cookied.c -- UDP fortune server
* Bart Massey 11/27/89
*/
#include <stdio.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define FORTUNEBUFSIZE (64 * 1024)
#define FORTUNEPROG "/usr/bin/fortune -a"
main(argc, argv)
int argc;
char **argv;
{
char *fortunebuf, *fbp;
FILE *pfile;
int nextchar, fromlen;
struct sockaddr_in from;
extern char *malloc();
openlog( argv[0], LOG_PID|LOG_NOWAIT, LOG_DAEMON );
fortunebuf = malloc( FORTUNEBUFSIZE );
if( !fortunebuf )
{
syslog( LOG_ERR, "out of memory" );
exit( 1 );
}
fromlen = sizeof( from );
if( recvfrom( 0, fortunebuf, FORTUNEBUFSIZE, 0, (struct sockaddr *) &from, &fromlen ) == -1 )
{
syslog( LOG_ERR, "recvfrom: %m" );
exit( 1 );
}
pfile = popen( FORTUNEPROG, "r" );
if( !pfile )
{
syslog( LOG_ERR, "popen \"%s\": %m", FORTUNEPROG );
exit( 1 );
}
for( fbp = fortunebuf; fbp < fortunebuf + FORTUNEBUFSIZE; fbp++ )
{
nextchar = getc( pfile );
if( nextchar == EOF )
{
*fbp = '\0'; /* we don't *send* this, but... */
break;
}
*fbp = nextchar;
}
if( nextchar != EOF )
{
syslog( LOG_ERR, "buffer too small" );
exit( 1 );
}
pclose( pfile );
if( sendto( 0, fortunebuf, fbp - fortunebuf, 0, &from, fromlen ) == -1 )
{
syslog( LOG_ERR, "send: %m" );
exit( 1 );
}
syslog( LOG_DEBUG, "sent fortune to %s (%d)",
inet_ntoa( * (long *) (&from.sin_addr) ), ntohs( from.sin_port ) );
exit( 0 );
}
More information about the Alt.sources
mailing list