mirror of
https://github.com/bobbimanners/emailler.git
synced 2025-01-17 02:30:10 +00:00
NNTP65: More development. Improved robustness of w5100_tcp_send_recv().
This commit is contained in:
parent
5608df5d6a
commit
f52d76f990
@ -231,6 +231,7 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
if (written != len) {
|
if (written != len) {
|
||||||
printf("Write error");
|
printf("Write error");
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
w5100_disconnect();
|
||||||
error_exit();
|
error_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,12 +244,14 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//
|
//
|
||||||
// Handle short single packet ASCII text responses
|
// Handle short single line ASCII text responses
|
||||||
|
// Must fit in recvbuf[]
|
||||||
//
|
//
|
||||||
uint16_t rcv;
|
uint16_t rcv;
|
||||||
uint16_t len = 0;
|
uint16_t len = 0;
|
||||||
|
uint8_t cont = 1;
|
||||||
|
|
||||||
while (1) {
|
while (cont) {
|
||||||
if (input_check_for_abort_key()) {
|
if (input_check_for_abort_key()) {
|
||||||
printf("User abort\n");
|
printf("User abort\n");
|
||||||
w5100_disconnect();
|
w5100_disconnect();
|
||||||
@ -256,19 +259,24 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
}
|
}
|
||||||
|
|
||||||
rcv = w5100_receive_request();
|
rcv = w5100_receive_request();
|
||||||
if (rcv)
|
if (!rcv) {
|
||||||
break;
|
cont = w5100_connected();
|
||||||
if (!w5100_connected()) {
|
if (cont)
|
||||||
printf("Connection lost\n");
|
continue;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rcv > length - len)
|
if (rcv > length - len)
|
||||||
rcv = length - len;
|
rcv = length - len;
|
||||||
|
|
||||||
|
if (rcv == 0) {
|
||||||
|
printf("Buffer overflow\n"); // Should never happen
|
||||||
|
w5100_disconnect();
|
||||||
|
error_exit();
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// One less to allow for faster pre-increment below
|
// One less to allow for faster pre-increment below
|
||||||
|
// 4 bytes of overlap between blocks
|
||||||
char *dataptr = recvbuf + len - 1;
|
char *dataptr = recvbuf + len - 1;
|
||||||
uint16_t i;
|
uint16_t i;
|
||||||
for (i = 0; i < rcv; ++i) {
|
for (i = 0; i < rcv; ++i) {
|
||||||
@ -276,6 +284,9 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
// suitable to access the W5100 auto-increment register.
|
// suitable to access the W5100 auto-increment register.
|
||||||
char data = *w5100_data;
|
char data = *w5100_data;
|
||||||
*++dataptr = data;
|
*++dataptr = data;
|
||||||
|
if (!memcmp(dataptr - 1, "\r\n", 2))
|
||||||
|
cont = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
w5100_receive_commit(rcv);
|
w5100_receive_commit(rcv);
|
||||||
len += rcv;
|
len += rcv;
|
||||||
@ -291,7 +302,7 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
*/
|
*/
|
||||||
void expect(char *buf, char *s) {
|
void expect(char *buf, char *s) {
|
||||||
if (strncmp(buf, s, strlen(s)) != 0) {
|
if (strncmp(buf, s, strlen(s)) != 0) {
|
||||||
printf("\nExpected '%s' got '%s\n", s, buf);
|
printf("\nExpected '%s' got '%s'\n", s, buf);
|
||||||
error_exit();
|
error_exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -331,8 +342,9 @@ void readconfigfile(void) {
|
|||||||
* Expects CRLF line endings (CRLF) and converts to Apple II (CR) convention
|
* Expects CRLF line endings (CRLF) and converts to Apple II (CR) convention
|
||||||
* fp - file to read from
|
* fp - file to read from
|
||||||
* writep - Pointer to buffer into which line will be written
|
* writep - Pointer to buffer into which line will be written
|
||||||
|
* n - length of buffer. Longer lines will be truncated and terminated with CR.
|
||||||
*/
|
*/
|
||||||
int16_t get_line(FILE *fp, char *writep) {
|
int16_t get_line(FILE *fp, char *writep, uint16_t n) {
|
||||||
static uint16_t rd = 0; // Read
|
static uint16_t rd = 0; // Read
|
||||||
static uint16_t end = 0; // End of valid data in buf
|
static uint16_t end = 0; // End of valid data in buf
|
||||||
uint16_t i = 0;
|
uint16_t i = 0;
|
||||||
@ -343,6 +355,10 @@ int16_t get_line(FILE *fp, char *writep) {
|
|||||||
}
|
}
|
||||||
if (end == 0)
|
if (end == 0)
|
||||||
return -1; // EOF
|
return -1; // EOF
|
||||||
|
if (i == n - 2) {
|
||||||
|
writep[i] = '\r';
|
||||||
|
writep[i + 1] = '\0';
|
||||||
|
}
|
||||||
writep[i++] = buf[rd++];
|
writep[i++] = buf[rd++];
|
||||||
// The following line is safe because of linebuf_pad[]
|
// The following line is safe because of linebuf_pad[]
|
||||||
if ((writep[i - 1] == '\n') && (writep[i - 2] == '\r')) {
|
if ((writep[i - 1] == '\n') && (writep[i - 2] == '\r')) {
|
||||||
@ -441,7 +457,7 @@ void update_mailbox(char *mbox) {
|
|||||||
hdrs.skipbytes = 0; // Just in case it doesn't get set
|
hdrs.skipbytes = 0; // Just in case it doesn't get set
|
||||||
hdrs.status = 'N';
|
hdrs.status = 'N';
|
||||||
hdrs.tag = ' ';
|
hdrs.tag = ' ';
|
||||||
while ((chars = get_line(fp, linebuf)) != -1) {
|
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
|
||||||
if (headers) {
|
if (headers) {
|
||||||
headerchars += chars;
|
headerchars += chars;
|
||||||
if (!strncmp(linebuf, "Date: ", 6)) {
|
if (!strncmp(linebuf, "Date: ", 6)) {
|
||||||
@ -571,6 +587,7 @@ void main(int argc, char *argv[]) {
|
|||||||
break;
|
break;
|
||||||
printf("*************************************************************\n");
|
printf("*************************************************************\n");
|
||||||
printf("* NEWSGROUP: %s\n", newsgroup);
|
printf("* NEWSGROUP: %s\n", newsgroup);
|
||||||
|
printf("* MAILBOX: %s\n", mailbox);
|
||||||
printf("* START MSG: %ld\n", msgnum);
|
printf("* START MSG: %ld\n", msgnum);
|
||||||
printf("*************************************************************\n");
|
printf("*************************************************************\n");
|
||||||
|
|
||||||
@ -585,10 +602,13 @@ void main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sscanf(buf, "211 %lu %lu %lu", &nummsgs, &lownum, &highnum);
|
sscanf(buf, "211 %lu %lu %lu", &nummsgs, &lownum, &highnum);
|
||||||
printf(" %lu messages, numbered from %lu to %lu\n", nummsgs, lownum, highnum);
|
printf(" Approx. %lu messages, numbered from %lu to %lu\n", nummsgs, lownum, highnum);
|
||||||
|
|
||||||
if (msgnum == 0)
|
if (msgnum == 0)
|
||||||
msgnum = highnum - 50;
|
msgnum = highnum - 200; // If 0 is specified grab 200 messages to start
|
||||||
|
|
||||||
|
if (msgnum < lownum)
|
||||||
|
msgnum = lownum;
|
||||||
|
|
||||||
for (msg = msgnum; msg <= highnum; ++msg) {
|
for (msg = msgnum; msg <= highnum; ++msg) {
|
||||||
sprintf(sendbuf, "STAT %ld\r\n", msgnum);
|
sprintf(sendbuf, "STAT %ld\r\n", msgnum);
|
||||||
|
@ -285,7 +285,7 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
*/
|
*/
|
||||||
void expect(char *buf, char *s) {
|
void expect(char *buf, char *s) {
|
||||||
if (strncmp(buf, s, strlen(s)) != 0) {
|
if (strncmp(buf, s, strlen(s)) != 0) {
|
||||||
printf("\nExpected '%s' got '%s\n", s, buf);
|
printf("\nExpected '%s' got '%s'\n", s, buf);
|
||||||
error_exit();
|
error_exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|||||||
*/
|
*/
|
||||||
uint8_t expect(char *buf, char *s) {
|
uint8_t expect(char *buf, char *s) {
|
||||||
if (strncmp(buf, s, strlen(s)) != 0) {
|
if (strncmp(buf, s, strlen(s)) != 0) {
|
||||||
printf("\nExpected '%s' got '%s\n", s, buf);
|
printf("\nExpected '%s' got '%s'\n", s, buf);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user