--- nntp.c 2001/08/02 14:58:39 1.2
+++ nntp.c 2001/08/07 09:05:55 1.3
@@ -33,6 +33,8 @@
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
+#include <sys/types.h>
+#include <sys/time.h>
#include "nntp.h"
@@ -50,22 +52,24 @@
int wfd;
nntp_io_t io;
nntp_readline_t rl;
+ struct timeval tv;
};
nntp_t *nntp_create(int rfd, int wfd, nntp_io_t *io)
{
nntp_t *nntp;
- char line[NNTP_LINE_MAXLEN];
if ((nntp = (nntp_t *)malloc(sizeof(nntp_t))) == NULL)
return NULL;
if (io == NULL) {
- nntp->io.read = read;
- nntp->io.write = write;
+ nntp->io.select = select;
+ nntp->io.read = read;
+ nntp->io.write = write;
} else {
- nntp->io.read = io->read ? io->read : read;
- nntp->io.write = io->write ? io->write : write;
+ nntp->io.select = io->select ? io->select : select;
+ nntp->io.read = io->read ? io->read : read;
+ nntp->io.write = io->write ? io->write : write;
}
nntp->rfd = rfd;
@@ -73,14 +77,22 @@
nntp->rl.rl_cnt = 0;
nntp->rl.rl_bufptr = NULL;
nntp->rl.rl_buf[0] = '\0';
+ nntp_timeout(nntp, 3);
return nntp;
}
-#if 0 //FIXME
+nntp_rc_t nntp_timeout(nntp_t *nntp, long timeout)
+{
+ nntp->tv.tv_sec = timeout;
+ nntp->tv.tv_usec = 0;
+ return NNTP_OK;
+}
+
nntp_rc_t nntp_init(nntp_t *nntp)
{
nntp_rc_t rc;
+ char line[NNTP_LINE_MAXLEN];
/* RFC0977 2.4.3. General Responses
* In general, 1xx codes may be ignored or displayed as desired; code 200
@@ -101,15 +113,19 @@
*/
do {
- if ((rc = nntp_readline(nntp, line, sizeof(line))) != NNTP_OK)
+ if ((rc = nntp_readline(nntp, line, sizeof(line))) != NNTP_OK) {
+ fprintf(stderr, "DEBUG: nntp_readline returned ***%d***\n", rc);
return rc;
- while (line[0] == '1');
+ }
+ } while (line[0] == '1');
+
+ // fprintf(stderr, "DEBUG: nntp_readline got ***%s***\n", line);
+
if (strncmp(line, "200", 3) == 0)
return NNTP_OK;
return NNTP_ERR_INIT;
}
-#endif
void nntp_destroy(nntp_t *nntp)
{
@@ -124,6 +140,9 @@
size_t n;
char c;
nntp_readline_t *rl = &nntp->rl;
+ struct timeval tv;
+ fd_set fds;
+ int rc;
if (nntp == NULL)
return NNTP_ERR_ARG;
@@ -131,6 +150,15 @@
/* fetch one character (but read more) */
if (rl->rl_cnt <= 0) {
+ FD_ZERO(&fds);
+ FD_SET(nntp->rfd, &fds);
+ tv.tv_sec = nntp->tv.tv_sec;
+ tv.tv_usec = nntp->tv.tv_usec;
+ rc = nntp->io.select(nntp->rfd + 1, &fds, NULL, NULL, &tv);
+ if (rc == 0)
+ return NNTP_TIMEOUT;
+ else if (rc == -1)
+ return NNTP_ERR_SYSTEM;
do {
rl->rl_cnt = nntp->io.read(nntp->rfd, rl->rl_buf, NNTP_LINE_MAXLEN);
} while (rl->rl_cnt == -1 && errno == EINTR);
@@ -240,6 +268,7 @@
str = "NNTP: errorcode has no description";
if (rc == NNTP_OK ) str = "NNTP: no error";
else if (rc == NNTP_EOF ) str = "NNTP: end of file";
+ else if (rc == NNTP_TIMEOUT ) str = "NNTP: timeout";
else if (rc == NNTP_ERR_SYSTEM ) str = "NNTP: see errno";
else if (rc == NNTP_ERR_ARG ) str = "NNTP: invalid argument";
else if (rc == NNTP_ERR_OVERFLOW) str = "NNTP: buffer overflow";
|