diff options
Diffstat (limited to 'src/cache/udpqueue.c')
-rw-r--r-- | src/cache/udpqueue.c | 70 |
1 files changed, 19 insertions, 51 deletions
diff --git a/src/cache/udpqueue.c b/src/cache/udpqueue.c index 8750d18..901292e 100644 --- a/src/cache/udpqueue.c +++ b/src/cache/udpqueue.c @@ -18,65 +18,35 @@ void udpqueue_drop (udpqueue *q) tain_add_g(&q->deadline, &tain_infinite_relative) ; } -int udpqueue_add4 (udpqueue *q, char const *ip, uint16_t port, char const *s, uint16_t len) +int udpqueue_add (udpqueue *q, uint8_t source, char const *ip, uint16_t port, char const *s, uint16_t len) { - udp4msg msg = { .port = port, .len = len } ; - if (!stralloc_readyplus(&q->storage, len)) return 0 ; - memcpy(msg.ip, ip, 4) ; - if (!genalloc_append(udp4msg, &q->messages, &msg)) return 0 ; + size_t iplen = source ? 16 : 4 ; + udpaux msg = { .port = port, .len = len } ; + if (!stralloc_readyplus(&q->storage, iplen + len)) return 0 ; + if (!genalloc_append(udpaux, &q->messages, &msg)) return 0 ; if (!q->storage.len) tain_add_g(&q->deadline, &g->wtto) ; + stralloc_catb(&q->storage, ip, iplen) ; stralloc_catb(&q->storage, s, len) ; return 1 ; } -int udpqueue_flush4 (udpqueue *q) +int udpqueue_flush (udpqueue *q, uint8_t is6) { - size_t n = genalloc_len(udp4msg, &q->messages) ; + size_t n = genalloc_len(udpaux, &q->messages) ; size_t shead = 0, head = 0 ; ssize_t r = 1 ; while (head < n) { - udp4msg const *msg = genalloc_s(udp4msg, &q->messages) + head ; - ssize_t r = socket_send4(q->fd, q->storage.s + shead, msg->len, msg->ip, msg->port) ; - if (r <= 0) goto adjust ; - shead += msg->len ; - } - udpqueue_drop(q) ; - return 1 ; - - adjust: - memmove(q->storage.s, q->storage.s + shead, q->storage.len - shead) ; - q->storage.len -= shead ; - memmove(genalloc_s(udp4msg, &q->messages), genalloc_s(udp4msg, &q->messages) + head, (n - head) * sizeof(udp4msg)) ; - genalloc_setlen(udp4msg, &q->messages, n - head) ; - if (shead) tain_add_g(&q->deadline, &g->wtto) ; - return sanitize_read(r) ; -} - -#ifdef SKALIBS_IPv6_ENABLED - -int udpqueue_add6 (udpqueue *q, char const *ip, uint16_t port, char const *s, uint16_t len) -{ - udp6msg msg = { .port = port, .len = len } ; - if (!stralloc_readyplus(&q->storage, len)) return 0 ; - memcpy(msg.ip, ip, 16) ; - if (!genalloc_append(udp6msg, &q->messages, &msg)) return 0 ; - if (!q->storage.len) tain_add_g(&q->deadline, &g->wtto) ; - stralloc_catb(&q->storage, s, len) ; - return 1 ; -} - -int udpqueue_flush6 (udpqueue *q) -{ - size_t n = genalloc_len(udp6msg, &q->messages) ; - size_t shead = 0, head = 0 ; - ssize_t r = 1 ; - while (head < n) - { - udp6msg const *msg = genalloc_s(udp4msg, &q->messages) + head ; - r = socket_send6(q->fd, q->storage.s + shead, msg->len, msg->ip, msg->port) ; + udpaux const *msg = genalloc_s(udpaux, &q->messages) + head ; + ssize_t r ; +#if SKALIBS_IPV6_ENABLED + if (is6) + r = socket_send6(q->fd, q->storage.s + shead + 16, msg->len, q->storage.s + shead, msg->port) ; + else +#endif + r = socket_send4(q->fd, q->storage.s + shead + 4, msg->len, q->storage.s + shead, msg->port) ; if (r <= 0) goto adjust ; - shead += msg->len ; + shead += (is6 ? 16 : 4) + msg->len ; } udpqueue_drop(q) ; return 1 ; @@ -84,10 +54,8 @@ int udpqueue_flush6 (udpqueue *q) adjust: memmove(q->storage.s, q->storage.s + shead, q->storage.len - shead) ; q->storage.len -= shead ; - memmove(genalloc_s(udp6msg, &q->messages), genalloc_s(udp6msg, &q->messages) + head, (n - head) * sizeof(udp6msg)) ; - genalloc_setlen(udp6msg, &q->messages, n - head) ; + memmove(genalloc_s(udpaux, &q->messages), genalloc_s(udpaux, &q->messages) + head, (n - head) * sizeof(udpaux)) ; + genalloc_setlen(udpaux, &q->messages, n - head) ; if (shead) tain_add_g(&q->deadline, &g->wtto) ; return sanitize_read(r) ; } - -#endif |