From bf5f10def9e7011f493d5c9b3d6f3b711ae68fea Mon Sep 17 00:00:00 2001 From: Job Snijders Date: Wed, 18 Aug 2021 16:56:13 +0000 Subject: [PATCH] Rework where newlines are inserted --- expander.c | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/expander.c b/expander.c index 1fa64aa..00cb88d 100644 --- a/expander.c +++ b/expander.c @@ -31,17 +31,18 @@ #include #include -#include -#include #include #include -#include +#include +#include +#include #include -#include -#include +#include #include #include #include +#include +#include #include #include "extern.h" @@ -300,9 +301,9 @@ bgpq_expanded_macro_limit(char *as, struct bgpq_expander *b, bgpq_expander_add_already(b, as); if (pipelining) { struct bgpq_request *req1 = bgpq_pipeline(b, - bgpq_expanded_macro_limit, NULL, "!i%s\n", - as); - req1->depth = req->depth+1; + bgpq_expanded_macro_limit, NULL, "!i%s", + as); + req1->depth = req->depth + 1; } else { b->cdepth++; bgpq_expand_irrd(b, bgpq_expanded_macro_limit, @@ -724,7 +725,7 @@ bgpq_expand_irrd(struct bgpq_expander *b, { char request[128], response[128]; va_list ap; - size_t ret; + ssize_t ret; int off = 0; struct bgpq_request *req; @@ -736,12 +737,13 @@ bgpq_expand_irrd(struct bgpq_expander *b, SX_DEBUG(debug_expander, "expander: sending '%s'\n", request); - ret=write(b->fd, request, strlen(request)); - if (ret != strlen(request)) { - sx_report(SX_FATAL,"Partial write to IRRd, only %lu bytes " - "written: %s\n", ret, strerror(errno)); - exit(1); - } + if ((ret = write(b->fd, request, strlen(request)) == 0) || ret == -1) + err(1, "write"); + + // push the button by sending a newline + if ((ret = write(b->fd, "\n", 1) == 0) || ret == -1) + err(1, "write"); + memset(response, 0, sizeof(response)); repeat: @@ -935,7 +937,7 @@ bgpq_expand(struct bgpq_expander *b) b->fd = fd; SX_DEBUG(debug_expander, "Sending '!!' to server to request for the" - "connection to remain open\n"); + " connection to remain open\n"); if ((ret = write(fd, "!!\n", 3)) != 3) { sx_report(SX_ERROR, "Partial write of multiple command mode " "to IRRd: %i bytes, %s\n", ret, strerror(errno)); @@ -1029,20 +1031,20 @@ bgpq_expand(struct bgpq_expander *b) if (!b->maxdepth && RB_EMPTY(&b->stoplist)) { if (aquery) bgpq_expand_irrd(b, bgpq_expanded_prefix, b, - "!a%s%s\n", + "!a%s%s", b->family == AF_INET ? "4" : "6", mc->text); else bgpq_expand_irrd(b, bgpq_expanded_macro, b, - "!i%s,1\n", mc->text); + "!i%s,1", mc->text); } else { bgpq_expander_add_already(b, mc->text); if (pipelining) bgpq_pipeline(b, bgpq_expanded_macro_limit, - NULL, "!i%s\n", mc->text); + NULL, "!i%s", mc->text); else bgpq_expand_irrd(b, bgpq_expanded_macro_limit, - NULL, "!i%s\n", mc->text); + NULL, "!i%s", mc->text); } } @@ -1058,10 +1060,10 @@ bgpq_expand(struct bgpq_expander *b) STAILQ_FOREACH(mc, &b->rsets, entries) { if (b->family == AF_INET) bgpq_expand_irrd(b, bgpq_expanded_prefix, - NULL, "!i%s,1\n", mc->text); + NULL, "!i%s,1", mc->text); else bgpq_expand_irrd(b, bgpq_expanded_v6prefix, - NULL, "!i%s,1\n", mc->text); + NULL, "!i%s,1", mc->text); } for (k=0; k < sizeof(b->asn32s) / sizeof(unsigned char *); k++) { if (!b->asn32s[k]) @@ -1072,18 +1074,18 @@ bgpq_expand(struct bgpq_expander *b) if (b->family == AF_INET6) { if (!pipelining) { bgpq_expand_irrd(b, bgpq_expanded_v6prefix, - NULL, "!6as%" PRIu32 "\n", ( k << 16) + i * 8 + j); + NULL, "!6as%" PRIu32, ( k << 16) + i * 8 + j); } else { bgpq_pipeline(b, bgpq_expanded_v6prefix, - NULL, "!6as%" PRIu32 "\n", (k << 16) + i * 8 + j); + NULL, "!6as%" PRIu32, (k << 16) + i * 8 + j); } } else { if (!pipelining) { bgpq_expand_irrd(b, bgpq_expanded_prefix, - NULL, "!gas%" PRIu32 "\n", (k << 16) + i * 8 + j); + NULL, "!gas%" PRIu32, (k << 16) + i * 8 + j); } else { bgpq_pipeline(b, bgpq_expanded_prefix, - NULL, "!gas%" PRIu32 "\n", ( k<< 16) + i* 8 + j); + NULL, "!gas%" PRIu32, ( k<< 16) + i* 8 + j); } } }