AmendHub

Download:

jcs

/

subtext

/

amendments

/

355

fidopkt: Parse and store dest and orig node addresses

Also don't bail if there's no AREA: line

jcs made amendment 355 about 1 year ago
--- fidopkt.c Wed Mar 1 09:21:27 2023 +++ fidopkt.c Fri Mar 3 21:52:07 2023 @@ -29,51 +29,50 @@ /* #define FIDOPKT_DEBUG */ -#define PKTHEADER_ORIGNODE 0 -#define PKTHEADER_DESTNODE 2 -#define PKTHEADER_YEAR 4 -#define PKTHEADER_MONTH 6 -#define PKTHEADER_DAY 8 -#define PKTHEADER_HOUR 10 -#define PKTHEADER_MINUTE 12 -#define PKTHEADER_SECOND 14 +#define FIDOPKT_ORIGNODE 0 +#define FIDOPKT_DESTNODE 2 +#define FIDOPKT_YEAR 4 +#define FIDOPKT_MONTH 6 +#define FIDOPKT_DAY 8 +#define FIDOPKT_HOUR 10 +#define FIDOPKT_MINUTE 12 +#define FIDOPKT_SECOND 14 -#define PKTHEADER_BAUD 16 -#define PKTHEADER_PKTTYPE 18 +#define FIDOPKT_BAUD 16 +#define FIDOPKT_PKTTYPE 18 -#define PKTHEADER_ORIGNET 20 -#define PKTHEADER_DESTNET 22 -#define PKTHEADER_PRODCODELOW 24 -#define PKTHEADER_REVMAJOR 25 -#define PKTHEADER_PASSWORD 26 +#define FIDOPKT_ORIGNET 20 +#define FIDOPKT_DESTNET 22 +#define FIDOPKT_PRODCODELOW 24 +#define FIDOPKT_REVMAJOR 25 +#define FIDOPKT_PASSWORD 26 -#define PKTHEADER_QORIGZONE 34 -#define PKTHEADER_QDESTZONE 36 +#define FIDOPKT_QORIGZONE 34 +#define FIDOPKT_QDESTZONE 36 -#define PKTHEADER_AUXNET 38 -#define PKTHEADER_CWVALIDCOPY 40 -#define PKTHEADER_PRODCODEHIGH 42 -#define PKTHEADER_REVMINOR 43 -#define PKTHEADER_CAPABILWORD 44 +#define FIDOPKT_AUXNET 38 +#define FIDOPKT_CWVALIDCOPY 40 +#define FIDOPKT_PRODCODEHIGH 42 +#define FIDOPKT_REVMINOR 43 +#define FIDOPKT_CAPABILWORD 44 -#define PKTHEADER_ORIGZONE 46 -#define PKTHEADER_DESTZONE 48 -#define PKTHEADER_ORIGPOINT 50 -#define PKTHEADER_DESTPOINT 52 -#define PKTHEADER_PRODDATA 54 +#define FIDOPKT_ORIGZONE 46 +#define FIDOPKT_DESTZONE 48 +#define FIDOPKT_ORIGPOINT 50 +#define FIDOPKT_DESTPOINT 52 +#define FIDOPKT_PRODDATA 54 -#define SIZE_PKTHEADER 58 +#define FIDOPKT_HEADER_SIZE 58 -/* PktMsgHeader */ -#define PKTMSGHEADER_PKTTYPE 0 -#define PKTMSGHEADER_ORIGNODE 2 -#define PKTMSGHEADER_DESTNODE 4 -#define PKTMSGHEADER_ORIGNET 6 -#define PKTMSGHEADER_DESTNET 8 -#define PKTMSGHEADER_ATTR 10 -#define PKTMSGHEADER_COST 12 +#define FIDOPKT_MSG_PKTTYPE 0 +#define FIDOPKT_MSG_ORIGNODE 2 +#define FIDOPKT_MSG_DESTNODE 4 +#define FIDOPKT_MSG_ORIGNET 6 +#define FIDOPKT_MSG_DESTNET 8 +#define FIDOPKT_MSG_ATTR 10 +#define FIDOPKT_MSG_COST 12 -#define SIZE_PKTMSGHEADER 14 +#define FIDOPKT_MSG_HEADER_SIZE 14 static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", @@ -112,6 +111,29 @@ fidopkt_read_until(char **buf, size_t *buf_len, char d } bool +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) + ; + /* 21:1/137 */ + else if (sscanf(str, "%d:%d/%d", &tmp.zone, &tmp.net, &tmp.node) == 3) + tmp.point = 0; + else + return false; + + *address = xmalloc(sizeof(struct fidopkt_address)); + if (*address == NULL) + return false; + + memcpy(*address, &tmp, sizeof(struct fidopkt_address)); + return true; +} + +bool fidopkt_parse_msgid(struct fidopkt *pkt) { static char garbage[50]; @@ -152,16 +174,16 @@ fidopkt_parse_msgid(struct fidopkt *pkt) return false; found: - pkt->msgid = id; - pkt->msgid_zone = zone; - pkt->msgid_net = net; - pkt->msgid_node = node; - pkt->msgid_point = point; + pkt->msgid.id = id; + pkt->msgid.zone = zone; + pkt->msgid.net = net; + pkt->msgid.node = node; + pkt->msgid.point = point; #ifdef FIDOPKT_DEBUG logger_printf("[fidopkt] %s -> zone:%d net:%d node:%d point:%d id:%lu", - pkt->msgid_orig, pkt->msgid_zone, pkt->msgid_net, pkt->msgid_node, - pkt->msgid_point, pkt->msgid); + pkt->msgid_orig, pkt->msgid.zone, pkt->msgid.net, + pkt->msgid.node, pkt->msgid.point, pkt->msgid.id); #endif return true; } @@ -175,9 +197,10 @@ fidopkt_parse(char *packet_filename, char *buf, size_t char *lastbreak, was; #endif struct fidopkt *ret; - size_t message_len, llen; - long tzoff; struct tm tm = { 0 }; + struct fidopkt_address orig_address, dest_address; + size_t message_len, llen, olen; + long tzoff; char dmon[6]; short dday, dyear, dhour, dmin, dsec, dutc, n; bool tear; @@ -186,48 +209,70 @@ fidopkt_parse(char *packet_filename, char *buf, size_t logger_printf("[fidopkt] Parsing %s ==========", packet_filename); #endif - if (len < SIZE_PKTHEADER) { + if (len < FIDOPKT_HEADER_SIZE) { logger_printf("[fidopkt] packet too small (%ld)", len); return; } - if (GET_U16(buf + PKTHEADER_PKTTYPE) != 2) { + if (GET_U16(buf + FIDOPKT_PKTTYPE) != 2) { logger_printf("[fidopkt] not a fidonet packet"); return; } - if (GET_U16(buf + PKTHEADER_BAUD) == 2) { + if (GET_U16(buf + FIDOPKT_BAUD) == 2) { logger_printf("[fidopkt] 5d format not supported"); return; } - if (buf[PKTHEADER_CWVALIDCOPY] != buf[PKTHEADER_CAPABILWORD + 1] || - buf[PKTHEADER_CWVALIDCOPY + 1] != buf[PKTHEADER_CAPABILWORD]) { + if (buf[FIDOPKT_CWVALIDCOPY] != buf[FIDOPKT_CAPABILWORD + 1] || + buf[FIDOPKT_CWVALIDCOPY + 1] != buf[FIDOPKT_CAPABILWORD]) { logger_printf("[fidopkt] not in 4d format"); return; } + + if (GET_U16(buf + FIDOPKT_ORIGZONE) != 0) { + orig_address.zone = GET_U16(buf + FIDOPKT_ORIGZONE); + dest_address.zone = GET_U16(buf + FIDOPKT_DESTZONE); + } else if (GET_U16(buf + FIDOPKT_QORIGZONE) != 0) { + orig_address.zone = GET_U16(buf + FIDOPKT_QORIGZONE); + dest_address.zone = GET_U16(buf + FIDOPKT_QDESTZONE); + } else { + orig_address.zone = 0; + dest_address.zone = 0; + } + + orig_address.net = GET_U16(buf + FIDOPKT_ORIGNET); + orig_address.node = GET_U16(buf + FIDOPKT_ORIGNODE); + orig_address.point = GET_U16(buf + FIDOPKT_ORIGPOINT); + + dest_address.net = GET_U16(buf + FIDOPKT_DESTNET); + dest_address.node = GET_U16(buf + FIDOPKT_DESTNODE); + dest_address.point = GET_U16(buf + FIDOPKT_DESTPOINT); - buf += SIZE_PKTHEADER; - len -= SIZE_PKTHEADER; + buf += FIDOPKT_HEADER_SIZE; + len -= FIDOPKT_HEADER_SIZE; - if (len < SIZE_PKTMSGHEADER) { + if (len < FIDOPKT_MSG_HEADER_SIZE) { logger_printf("[fidopkt] message header too short"); return; } - if (GET_U16(buf + PKTMSGHEADER_PKTTYPE) != 2) { + if (GET_U16(buf + FIDOPKT_MSG_PKTTYPE) != 2) { logger_printf("[fidopkt] header packet type != 2"); return; } - buf += SIZE_PKTMSGHEADER; - len -= SIZE_PKTMSGHEADER; + buf += FIDOPKT_MSG_HEADER_SIZE; + len -= FIDOPKT_MSG_HEADER_SIZE; ret = calloc(sizeof(struct fidopkt), 1); if (ret == NULL) { logger_printf("[fidopkt] malloc failed"); goto parse_fail; } + + ret->orig = orig_address; + ret->dest = dest_address; fidopkt_read_until(&buf, &len, '\0', sizeof(line), line, true); if (len == 0) { @@ -277,28 +322,24 @@ fidopkt_parse(char *packet_filename, char *buf, size_t goto parse_fail; } - fidopkt_read_until(&buf, &len, '\r', sizeof(line), line, true); - if (len == 0) { - logger_printf("[fidopkt] short read area"); - goto parse_fail; - } - if (strncmp(line, "AREA:", 5) != 0) { - logger_printf("[fidopkt] no AREA in first line"); - goto parse_fail; - } - strlcpy(ret->area, line + 5, sizeof(ret->area)); - ret->message = malloc(len); if (ret->message == NULL) { logger_printf("[fidopkt] malloc(%lu) failed", len); goto parse_fail; } + message_len = 0; - tear = false; while (len) { llen = fidopkt_read_until(&buf, &len, '\r', sizeof(line), line, false); + + if (message_len == 0 && strncmp(line, "AREA:", 5) == 0) { + line[llen] = '\0'; + strlcpy(ret->area, line + 5, sizeof(ret->area)); + continue; + } + /* * We are not terminating long lines now, so don't use unbounded * string comparisons. @@ -367,9 +408,13 @@ fidopkt_parse(char *packet_filename, char *buf, size_t ret->time -= tzoff; #ifdef FIDOPKT_DEBUG - logger_printf("[fidopkt] ID: %d:%d/%d.%d %08lx (%s)", ret->msgid_zone, - ret->msgid_net, ret->msgid_node, ret->msgid_point, ret->msgid, + logger_printf("[fidopkt] ID: %d:%d/%d.%d %08lx (%s)", ret->msgid.zone, + ret->msgid.net, ret->msgid.node, ret->msgid.point, ret->msgid.id, ret->msgid_orig); + logger_printf("[fidopkt] Orig Node: %d:%d/%d.%d", ret->orig.zone, + ret->orig.net, ret->orig.node, ret->orig.point); + logger_printf("[fidopkt] Dest Node: %d:%d/%d.%d", ret->dest.zone, + ret->dest.net, ret->dest.node, ret->dest.point); logger_printf("[fidopkt] Origin: %s", ret->origin); logger_printf("[fidopkt] Reply To: %s", ret->reply); logger_printf("[fidopkt] Area: %s", ret->area); --- fidopkt.h Wed Mar 1 09:21:27 2023 +++ fidopkt.h Fri Mar 3 09:10:13 2023 @@ -19,8 +19,25 @@ #include "util.h" +struct fidopkt_address { + u_int16_t zone; + u_int16_t net; + u_int16_t node; + u_int16_t point; +}; + +struct fidopkt_msgid { + u_int32_t id; + u_int16_t zone; + u_int16_t net; + u_int16_t node; + u_int16_t point; +}; + struct fidopkt { time_t time; + struct fidopkt_address orig; + struct fidopkt_address dest; char to[36]; char from[36]; char subject[72]; @@ -28,15 +45,12 @@ struct fidopkt { char origin[72]; char reply[32]; char msgid_orig[64]; - unsigned long msgid; - unsigned short msgid_zone; - unsigned short msgid_net; - unsigned short msgid_node; - unsigned short msgid_point; + struct fidopkt_msgid msgid; char *message; size_t message_len; }; +bool fidopkt_parse_address(char *str, struct fidopkt_address **address); struct fidopkt * fidopkt_parse(char *packet_filename, char *buf, size_t len); void fidopkt_free(struct fidopkt **pktp);