lwres_gnba.c revision c05e003dce672b2f8555a3e56857f29ce89c1677
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
180c8cf5fa15ca82f9267eb139cebeb93a91b6ceTinderbox User * Copyright (C) 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <assert.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <errno.h>
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include <stdlib.h>
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include <string.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews#include <lwres/lwbuffer.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <lwres/lwpacket.h>
ddfe394c061049bbd71125ad41c3dd3092b2bbfdAndreas Gustafsson#include <lwres/lwres.h>
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include "context_p.h"
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews#include "assert_p.h"
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceint
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrewslwres_gnbarequest_render(lwres_context_t *ctx, lwres_gnbarequest_t *req,
364a82f7c25b62967678027043425201a5e5171aBob Halley lwres_lwpacket_t *pkt, lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley unsigned char *buf;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt size_t buflen;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington int ret;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington size_t payload_length;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence REQUIRE(ctx != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(req != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(req->addr.family != 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(req->addr.length != 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(req->addr.address != NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(pkt != NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(b != NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley payload_length = sizeof(isc_uint32_t) + sizeof(isc_uint16_t)
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews + req->addr.length;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley buflen = LWRES_LWPACKET_LENGTH + payload_length;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley buf = CTXMALLOC(buflen);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (buf == NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley errno = ENOMEM;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (-1);
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley lwres_buffer_init(b, buf, buflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->length = buflen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->version = LWRES_LWPACKETVERSION_0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->flags &= ~LWRES_LWPACKETFLAG_RESPONSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->opcode = LWRES_OPCODE_GETNAMEBYADDR;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->result = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->authtype = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pkt->authlength = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ret = lwres_lwpacket_renderheader(b, pkt);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ret != 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_buffer_invalidate(b);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley CTXFREE(buf, buflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ret);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(SPACE_OK(b, payload_length));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Put the length and the data. We know this will fit because we
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * just checked for it.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_buffer_putuint32(b, req->addr.family);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_buffer_putuint16(b, req->addr.length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_buffer_putmem(b, req->addr.address, req->addr.length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyint
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_gnbaresponse_render(lwres_context_t *ctx, lwres_gnbaresponse_t *req,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_lwpacket_t *pkt, lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *buf;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley size_t buflen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int ret;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley size_t payload_length;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_uint16_t datalen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int x;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(ctx != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(req != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(pkt != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* naliases */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley payload_length = sizeof(isc_uint16_t);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* real name encoding */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley payload_length += LWRES_STRING_LENGTH(req->real_name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* each alias */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley for (x = 0 ; x < req->naliases ; x++)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley payload_length += LWRES_STRING_LENGTH(req->aliases[x]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley buflen = LWRES_LWPACKET_LENGTH + payload_length;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley buf = CTXMALLOC(buflen);
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley if (buf == NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley errno = ENOMEM;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (-1);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley lwres_buffer_init(b, buf, buflen);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->length = buflen;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->version = LWRES_LWPACKETVERSION_0;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->flags |= LWRES_LWPACKETFLAG_RESPONSE;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->opcode = LWRES_OPCODE_GETNAMEBYADDR;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->authtype = 0;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley pkt->authlength = 0;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ret = lwres_lwpacket_renderheader(b, pkt);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (ret != 0) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein lwres_buffer_invalidate(b);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley CTXFREE(buf, buflen);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley return (ret);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley }
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley /* encode naliases */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley INSIST(SPACE_OK(b, sizeof(isc_uint16_t) * 2));
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley lwres_buffer_putuint16(b, req->naliases);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley /* encode the real name */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley datalen = strlen(req->real_name);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein INSIST(SPACE_OK(b, LWRES_STRING_LENGTH(req->real_name)));
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley lwres_buffer_putuint16(b, datalen);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley lwres_buffer_putmem(b, req->real_name, datalen + 1);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley /* encode the aliases */
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley for (x = 0 ; x < req->naliases ; x++) {
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley datalen = strlen(req->aliases[x]);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley INSIST(SPACE_OK(b, LWRES_STRING_LENGTH(req->aliases[x])));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein lwres_buffer_putuint16(b, datalen);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson lwres_buffer_putmem(b, req->aliases[x], datalen + 1);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson }
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson return (0);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson}
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonint
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonlwres_gnbarequest_parse(lwres_context_t *ctx, lwres_lwpacket_t *pkt,
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson lwres_buffer_t *b, lwres_gnbarequest_t **structp)
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson{
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(ctx != NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(pkt != NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(b != NULL);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence REQUIRE(structp != NULL && *structp == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence}
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonint
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonlwres_gnbaresponse_parse(lwres_context_t *ctx, lwres_lwpacket_t *pkt,
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson lwres_buffer_t *b, lwres_gnbaresponse_t **structp)
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson{
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(ctx != NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(pkt != NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(b != NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson REQUIRE(structp != NULL && *structp == NULL);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson}
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonvoid
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halleylwres_gnbarequest_free(lwres_context_t *ctx, lwres_gnbarequest_t **structp)
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley{
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence lwres_gnbarequest_t *gnba;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley REQUIRE(ctx != NULL);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews REQUIRE(structp != NULL && *structp != NULL);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews gnba = *structp;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews *structp = NULL;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews CTXFREE(gnba, sizeof(lwres_gnbarequest_t));
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsvoid
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewslwres_gnbaresponse_free(lwres_context_t *ctx, lwres_gnbaresponse_t **structp)
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews{
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews lwres_gnbaresponse_t *gnba;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews REQUIRE(ctx != NULL);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews REQUIRE(structp != NULL && *structp != NULL);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence gnba = *structp;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence *structp = NULL;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence CTXFREE(gnba, sizeof(lwres_gnbaresponse_t));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley