🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nn
Article #6178

accessing news articles via pipes / nntp

#6178
From: Arvind Viranga Ratnaike
Date: Fri, 29 Jul 1994 12:42
212 lines
7614 bytes
Hi,
	I'm rather new at anything to do inter process comms so please forgive
me if I reveal my ignorance on these issues.

	I apologise if what I'm asking appears on the FAQs of any of the 
groups I post to and if my questions are inappropriate for the news group.
I am trying to find and read the FAQs but I have a rather tight deadline in 
my project...  I'm getting a lot stressed...

	I thank in advance (and prob. afterwards as well) anyone who can give 
me any advice/thoughts/solutions to my problems or other means of accessing
news articles from C code / scripts:

	(also for pointing out any glaring errors/not so glaring errors;
	sorry for asking so much)

---------------------------------------------------------------------------

	I'm trying to access news articles so that they can be examined by a 
information filter.  

	I'd rather not have to learn any socket stuff as I'm pressed for time
with my project.  Systems programming is not my area at all.

	So nntp commands and responses seemed the easiest approach.
(rfc977 & rfc850 - anonymous ftp to archie.au)

	However I've been running into a few difficulties.

	I'm forking a child process and redirecting its stdin and stdout 
to 2 pipes which I'll be using for communication.  One pipe to send nntp 
commands; the other to receive responses.  The child will execl a telnet
session which will telnet to the nntp port of the news server (in this case
goanna).  

My Problems are:

(1) since news articles don't have a standard length, it's hard for me to 
efficiently use read() as read requires the number of bytes (or at least an
upper bound)

(2) timing the reads.  Sometimes the read occurs before the telnet has had a
chance to write anything to the pipe.  I don't know how to tell when the telnet
has 
	- written something to the pipe 
	- finished writing to the pipe

so far I've only been able to do hacks with sleep(); I don't know how to do
asynchronous stuff.  

(3) somehow my read instructions manage to read the same data in the pipe 
over and over - I'd assumed that a pipe was a FIFO which emptied data as it
was read.



The parent process will be running an interactive user session (motif) so
my first idea to read character by character will probably be unacceptably
slow (as each character will require a system call).

I think I'm running on a Sun Sparc (I don't know the number)

Any suggestions/advice/thoughts would be really appreciated,

Viranga

ratnaike@numbat.cs.rmit.OZ.AU

P.S.  The little test program I've been using is as follows:

-----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h> 

#define READ_SIZE    1024
#define COMMAND_SIZE  80

int get_response(int (*)[], FILE *);

void main()
{
    int pid;   /* process id of child process which will exec telnet    */
    FILE *fp;  /* file ptr to my test file where I store nntp responses */
    int ctl;   /* set of flags for pipe */

    char command_buffer[COMMAND_SIZE];  /* holds nntp instruction */
    char connection_info[READ_SIZE];    /* captures nntp response */

    int records[2];      /* the pipe used by telnet to send responses to me */
    int instructions[2]; /* the pipe I use to send instructions to telnet   */

    /* create the pipe which telnet will read from */
    pipe(instructions);

    /* create the pipe which telnet will write to */
    pipe(records);
    /* set O_NONBLOCK to allow read to return as soon as it finds something */
    ctl = fcntl(records[0], F_GETFL, 0);
    fcntl(records[0], F_SETFL, ctl | O_NONBLOCK);

    pid = fork();
    if (!pid){      
        close(0);    /* redirect telnet stdin from instructions pipe */
	dup(instructions[0]);

        close(1);    /* redirect telnet stdout to records pipe */
	dup(records[1]);

	/* telnetting to the nntp port on the server goanna */
        execl("/usr/ucb/telnet", "telnet", "goanna", "nntp", 0);
    }

    /* ideally I'd like just empty/flush the pipe of any contents before I
     * send any nntp commands
     */
    read(records[0], connection_info, READ_SIZE);

    if((fp = fopen("myfile", "a")) == NULL){
        fprintf(stderr, "Could not open myfile\n");
        exit(0);
    };

    /* ask telnet to examine newsgroup "rmit.cs.835" */
    strcpy(command_buffer, "group rmit.cs.835\n");
    write(instructions[1], command_buffer, strlen(command_buffer));

    get_response(&records, fp);

    strcpy(command_buffer, "quit\n");
    write(instructions[1], command_buffer, strlen(command_buffer));

    fclose(fp);
    kill(0, SIGKILL);
}


/* for some reason this seems to repeatedly get the same data from the buffer
 * my notion of a pipe was a FIFO where data once read was no longer in the 
 * pipe - seems I was wrong
 */
int get_response(int (*the_pipe)[], FILE *fp)
{
    char buffer[READ_SIZE];

    while(read((*the_pipe)[0], buffer, READ_SIZE)){
        printf("%s\n", buffer);     /* so I can see what's happening */
        fprintf(fp, "%s", buffer);
    }
}

---------------------------------------------------------------------------

following is some output (the first lot from "myfile" and the second lot from 
my screen

---------------------------------------------------------------------------


  ...

Trying 131.170.24.40 ...
Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.
Trying 131.170.24.40 ...
Connected to goannaTrying 131.170.24.40 ...
Trying 131.170.24.40 ...
Trying 131.170.24.40 ...
Trying 131.170.24.40 ...
Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.
Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.

  ...

  ...

Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.

Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.

Connected to goanna.cs.rmit.OZ.AU.
Escape character is '^]'.

200 goanna.cs.rmit.oz.au InterNetNews NNRP server INN 1.4 20-Mar-93 ready (posti
ng ok).
211 4 1 4 rmit.cs.835

200 goanna.cs.rmit.oz.au InterNetNews NNRP server INN 1.4 20-Mar-93 ready (posti
ng ok).
211 4 1 4 rmit.cs.835

200 goanna.cs.rmit.oz.au InterNetNews NNRP server INN 1.4 20-Mar-93 ready (posti
ng ok).
211 4 1 4 rmit.cs.835

  ...

--------------------------------------------------------------------------

-- 
-----------------------------------------------------------------
Viranga Ratnaike
ratnaike@numbat.cs.rmit.OZ.AU
-----------------------------------------------------------------
Technical Details
Message-ID: <ratnaike.775485740@numbat>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.nn.mbox.zip!bga.com!news.sprintlink.net!rtp.vnet.net!ralph.vnet.net!ankh.iia.org!babbage.ece.uc.edu!news.kei.com!yeshua.marcam.com!zip.eecs.umich.edu!newsxfer.itd.umich.edu!europa.eng.gtefsd.com!howland.reston.ans.net!agate!msuinfo!harbinger.cc.monash.edu.au!aggedor.rmit.EDU.AU!goanna.cs.rmit.oz.au!numbat!ratnaike

Show Full Headers
Complete Article Headers
Message-ID: <ratnaike.775485740@numbat>
Subject: accessing news articles via pipes / nntp
Date: Fri, 29 Jul 1994 12:42:20 +0000
From: ratnaike@numbat.cs.rmit.OZ.AU (Arvind Viranga Ratnaike)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.nn.mbox.zip!bga.com!news.sprintlink.net!rtp.vnet.net!ralph.vnet.net!ankh.iia.org!babbage.ece.uc.edu!news.kei.com!yeshua.marcam.com!zip.eecs.umich.edu!newsxfer.itd.umich.edu!europa.eng.gtefsd.com!howland.reston.ans.net!agate!msuinfo!harbinger.cc.monash.edu.au!aggedor.rmit.EDU.AU!goanna.cs.rmit.oz.au!numbat!ratnaike
Newsgroups: news.software.nn
Organization: Comp Sci, RMIT, Melbourne, Australia
Lines: 212
NNTP-Posting-Host: numbat.cs.rmit.oz.au
NNTP-Posting-User: ratnaike
Keywords: pipe nntp read