AmendHub

Download:

jcs

/

subtext

/

amendments

/

389

fidopkt: Compatibility fixes, add attr field

Make fidopkt_parse_address write into the struct allocated by the
caller.

jcs made amendment 389 about 1 year ago
--- fidopkt.c Thu Mar 9 10:14:27 2023 +++ fidopkt.c Fri Mar 10 13:52:11 2023 @@ -111,25 +111,21 @@ fidopkt_read_until(char **buf, size_t *buf_len, char d } bool -fidopkt_parse_address(char *str, struct fidopkt_address **address) +fidopkt_parse_address(char *str, struct fidopkt_address *address) { - struct fidopkt_address tmp; - /* 21:1/137.1 */ - if (sscanf(str, "%d:%d/%d.%d", &tmp.zone, &tmp.net, &tmp.node, - &tmp.point) == 4) + if (sscanf(str, "%u:%u/%u.%u", &address->zone, &address->net, + &address->node, &address->point) == 4) ; /* 21:1/137 */ - else if (sscanf(str, "%d:%d/%d", &tmp.zone, &tmp.net, &tmp.node) == 3) - tmp.point = 0; - else + else if (sscanf(str, "%u:%u/%u", &address->zone, &address->net, + &address->node) == 3) + address->point = 0; + else { + address->zone = address->net = address->node = address->point = 0; return false; + } - *address = xmalloc(sizeof(struct fidopkt_address)); - if (*address == NULL) - return false; - - memcpy(*address, &tmp, sizeof(struct fidopkt_address)); return true; } @@ -200,9 +196,9 @@ fidopkt_parse(char *packet_filename, char *buf, size_t struct tm tm = { 0 }; struct fidopkt_address orig_address, dest_address; size_t body_len, llen; - long tzoff; + long tzoff = 0; char dmon[6]; - short dday, dyear, dhour, dmin, dsec, dutc, n; + short dday, dyear, dhour, dmin, dsec, dutc, attr, n; bool tear; #ifdef FIDOPKT_DEBUG @@ -262,6 +258,8 @@ fidopkt_parse(char *packet_filename, char *buf, size_t return; } + attr = GET_U16(buf + FIDOPKT_MSG_ATTR); + buf += FIDOPKT_MSG_HEADER_SIZE; len -= FIDOPKT_MSG_HEADER_SIZE; @@ -273,6 +271,7 @@ fidopkt_parse(char *packet_filename, char *buf, size_t ret->orig = orig_address; ret->dest = dest_address; + ret->attr = attr; fidopkt_read_until(&buf, &len, '\0', sizeof(line), line, true); if (len == 0) { @@ -285,6 +284,7 @@ fidopkt_parse(char *packet_filename, char *buf, size_t goto parse_fail; } + memset(&tm, 0, sizeof(tm)); tm.tm_sec = dsec; tm.tm_min = dmin; tm.tm_hour = dhour; @@ -405,6 +405,7 @@ fidopkt_parse(char *packet_filename, char *buf, size_t logger_printf("[fidopkt] parsed date but mktime failed"); goto parse_fail; } + /* make time UTC */ ret->time -= tzoff; #ifdef FIDOPKT_DEBUG @@ -418,14 +419,15 @@ fidopkt_parse(char *packet_filename, char *buf, size_t logger_printf("[fidopkt] Origin: %s", ret->origin); logger_printf("[fidopkt] Reply To: %s", ret->reply); logger_printf("[fidopkt] Area: %s", ret->area); + logger_printf("[fidopkt] Attr: 0x%x", ret->attr); logger_printf("[fidopkt] From: %s", ret->from); logger_printf("[fidopkt] To: %s", ret->to); logger_printf("[fidopkt] Subject: %s", ret->subject); llen = strlcpy(ct, ctime(&ret->time), sizeof(ct)); ct[llen - 1] = '\0'; - logger_printf("[fidopkt] Date: %s UTC (%s %s%s)", ct, datetime, - (tzoff >= 0 ? "+" : ""), tz); + logger_printf("[fidopkt] Date: %s UTC (%s %s%s) (%ul)", ct, datetime, + (tzoff >= 0 ? "+" : ""), tz, ret->time); logger_printf("[fidopkt] Message:"); lastbreak = ret->body; @@ -454,7 +456,7 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch static char scratch[30]; struct tm *tm; char *buf; - size_t len, off, buf_size, subject_len; + size_t len, off, buf_size, subject_len, n; subject_len = strlen(pkt->subject); @@ -475,6 +477,8 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch PUT_U16(buf + FIDOPKT_DESTNET, pkt->dest.net); PUT_U16(buf + FIDOPKT_DESTNODE, pkt->dest.node); + PUT_U32(buf + FIDOPKT_PRODDATA, 'SUBT'); + PUT_U16(buf + FIDOPKT_YEAR, tm->tm_year + 1900); PUT_U16(buf + FIDOPKT_MONTH, tm->tm_mon + 1); PUT_U16(buf + FIDOPKT_DAY, tm->tm_mday); @@ -487,6 +491,9 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch buf[FIDOPKT_CWVALIDCOPY + 1] = 1; buf[FIDOPKT_CAPABILWORD] = 1; + buf[FIDOPKT_PRODCODELOW] = 0; + buf[FIDOPKT_PRODCODEHIGH] = 0xfe; /* FTA-1005.003 */ + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_PKTTYPE, 2); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_DESTNET, @@ -498,6 +505,8 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch pkt->orig.net); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ORIGNODE, pkt->orig.node); + + PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ATTR, pkt->attr); off = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE; @@ -528,9 +537,9 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch memcpy(buf + off, pkt->subject, subject_len); off += subject_len + 1; - /* optional area (non-kludge) \r-terminated */ + /* optional area (non-kludge) \r-terminated, no space */ if (pkt->area[0] != '\0') { - len = snprintf(scratch, sizeof(scratch), "AREA: %s\r", pkt->area); + len = snprintf(scratch, sizeof(scratch), "AREA:%s\r", pkt->area); if (len > sizeof(scratch)) goto fail; if (!grow_to_fit(&buf, &buf_size, off, len, len)) @@ -592,13 +601,37 @@ fidopkt_encode(struct fidopkt *pkt, char **ret_buf, ch memcpy(buf + off, scratch, len); off += len; - if (!grow_to_fit(&buf, &buf_size, off, pkt->body_len + 6, - pkt->body_len + 6)) + if (!grow_to_fit(&buf, &buf_size, off, pkt->body_len + 7, + pkt->body_len + 7)) goto fail; - memcpy(buf + off, pkt->body, pkt->body_len); - off += pkt->body_len; - off += sprintf(buf + off, "\r--- \r"); + /* + * FidoNet messages can only have \r line-breaks and we probably used + * \r\n during the message editor, so strip out \n. + */ + for (n = 0; n < pkt->body_len; n++) { + if (n == pkt->body_len - 1 && pkt->body[n] == '\r') + /* supress trailing \r, we'll add two next */ + continue; + + if (pkt->body[n] == '\n') { + if (pkt->body[n - 1] == '\r') + continue; + buf[off++] = '\r'; + } else + buf[off++] = pkt->body[n]; + } + /* add tear line */ + off += sprintf(buf + off, "\r\r--- \r"); + if (pkt->origin[0]) { + len = 11 + strlen(pkt->origin) + 2; + + if (!grow_to_fit(&buf, &buf_size, off, len, len)) + goto fail; + + off += sprintf(buf + off, " * Origin: %s\r", pkt->origin); + } + *ret_buf = buf; return off; --- fidopkt.h Wed Mar 8 17:43:46 2023 +++ fidopkt.h Thu Mar 9 14:49:58 2023 @@ -46,11 +46,14 @@ struct fidopkt { char reply[32]; char msgid_orig[64]; struct fidopkt_msgid msgid; + u_int16_t attr; +#define FIDOPKT_MSG_ATTR_PRIVATE (1 << 0) +#define FIDOPKT_MSG_ATTR_CRASH (1 << 1) char *body; size_t body_len; }; -bool fidopkt_parse_address(char *str, struct fidopkt_address **address); +bool fidopkt_parse_address(char *str, struct fidopkt_address *address); struct fidopkt * fidopkt_parse(char *packet_filename, char *buf, size_t len); size_t fidopkt_encode(struct fidopkt *pkt, char **buf, char *pkt_password,