public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
To: Ahmad Gani <reyuki@gnuweeb.org>
Cc: Ammar Faizi <ammarfaizi2@gnuweeb.org>,
	"GNU/Weeb Mailing List" <gwml@vger.gnuweeb.org>
Subject: Re: [PATCH gwproxy v5 2/2] dnsparser: Add dns parser code
Date: Thu, 14 Aug 2025 14:28:08 +0700	[thread overview]
Message-ID: <CAOG64qNhvV7-GeYKqBCjQJwCR5YboGv-1=ktregh8S8ThRCeNA@mail.gmail.com> (raw)
In-Reply-To: <20250814044658.252579-3-reyuki@gnuweeb.org>

On Thu, Aug 14, 2025 at 11:53 AM Ahmad Gani wrote:
> +static int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *out)
> +{
> +       gwdns_header_pkt *hdr;
> +       uint16_t raw_flags;
> +       size_t idx, i;
> +       void *ptr;
> +       int ret;
> +
> +       idx = sizeof(*hdr);
> +       if (idx >= in_len)
> +               return -EAGAIN;
> +
> +       hdr = (void *)in;
> +       if (memcmp(&txid, &hdr->id, sizeof(txid)))
> +               return -EINVAL;
> +
> +       memcpy(&raw_flags, &hdr->flags, sizeof(raw_flags));
> +       raw_flags = ntohs(raw_flags);
> +       /* QR MUST 1 = response from dns server */
> +       if (!DNS_QR(raw_flags))
> +               return -EINVAL;
> +
> +       /* OPCODE MUST 0 = standard query */
> +       if (DNS_OPCODE(raw_flags))
> +               return -EINVAL;
> +
> +       /* RCODE MUST 0 = No error */
> +       if (DNS_RCODE(raw_flags))
> +               return -EPROTO;
> +
> +       // is it safe or recommended to alter the in buffer directly?
> +       hdr->ancount = ntohs(hdr->ancount);
> +       if (!hdr->ancount)
> +               return -ENODATA;
> +
> +       /*
> +        * Check the sizes upfront.
> +        *
> +        * 1 bytes for variable-length
> +        * in[idx] for the length of first name
> +        * 1 bytes for null terminator
> +        * 2 bytes for qtype
> +        * 2 bytes for qclass
> +        */
> +       if ((size_t)(1 + in[idx] + 1 + 2 + 2) >= in_len)
> +               return -EINVAL;
> +
> +       ret = calculate_question_len(&in[idx], in_len - idx);
> +       if (ret <= 0)
> +               return -EINVAL;
> +
> +       idx += ret;
> +       if (idx >= in_len)
> +               return -EAGAIN;
> +
> +       out->hdr.ancount = 0;
> +       ptr = malloc(hdr->ancount * sizeof(uint8_t *));
> +       if (!ptr)
> +               return -ENOMEM;
> +
> +       out->rr_answ = ptr;
> +       for (i = 0; i < hdr->ancount; i++) {
> +               uint16_t is_compressed, rdlength;
> +               gwdns_serialized_answ *item = malloc(sizeof(gwdns_serialized_answ));
> +               if (!item) {
> +                       ret = -ENOMEM;
> +                       goto exit_free;
> +               }
> +
> +               memcpy(&is_compressed, &in[idx], sizeof(is_compressed));
> +               is_compressed = DNS_IS_COMPRESSED(ntohs(is_compressed));
> +               assert(is_compressed);
> +               idx += 2; // NAME
> +               if (idx >= in_len) {
> +                       ret = -EAGAIN;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +
> +               memcpy(&item->rr_type, &in[idx], 2);
> +               item->rr_type = ntohs(item->rr_type);
> +               idx += 2; // TYPE
> +               if (idx >= in_len) {
> +                       ret = -EAGAIN;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +               memcpy(&item->rr_class, &in[idx], 2);
> +               item->rr_class = ntohs(item->rr_class);
> +               idx += 2; // CLASS
> +               if (idx >= in_len) {
> +                       ret = -EAGAIN;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +               memcpy(&item->ttl, &in[idx], 4);
> +               item->ttl = be32toh(item->ttl);
> +               idx += 4; // TTL
> +               if (idx >= in_len) {
> +                       ret = -EAGAIN;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +
> +               memcpy(&rdlength, &in[idx], sizeof(rdlength));
> +               rdlength = ntohs(rdlength);
> +               if (item->rr_type != TYPE_AAAA && item->rr_type != TYPE_A) {
> +                       ret = -EINVAL;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +               if (item->rr_type == TYPE_AAAA && rdlength != sizeof(struct in6_addr)) {
> +                       ret = -EINVAL;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +               if (item->rr_type == TYPE_A && rdlength != sizeof(struct in_addr)) {
> +                       ret = -EINVAL;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +               item->rdlength = rdlength;
> +               idx += 2;
> +               if (idx >= in_len) {
> +                       ret = -EAGAIN;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +
> +               /*
> +                * considering if condition above,
> +                * maybe we don't need a malloc and just allocate fixed size
> +                * for rdata? however if this parser want to be expanded for
> +                * other dns operation (e.g OPCODE_IQUERY, etc), rdata maybe
> +                * contain more than sizeof in6_addr.
> +                */
> +               ptr = malloc(rdlength);
> +               if (!ptr) {
> +                       ret = -ENOMEM;
> +                       free(item);
> +                       goto exit_free;
> +               }
> +
> +               memcpy(ptr, &in[idx], rdlength);
> +               idx += rdlength;
> +               if (idx > in_len) {
> +                       ret = -EINVAL;
> +                       free(item);
> +                       free(ptr);
> +                       goto exit_free;
> +               }
> +
> +               item->rdata = ptr;
> +               out->rr_answ[i] = item;
> +               out->hdr.ancount++;
> +       }
> +
> +       return 0;
> +exit_free:
> +       for (i = 0; i < out->hdr.ancount; i++) {
> +               free(out->rr_answ[i]->rdata);
> +               free(out->rr_answ[i]);
> +       }
> +       free(out->rr_answ);
> +       return ret;
> +}

I think free(item); can be deduplicated. It's repeated several times
here. Consider using goto exit_free_item and add the appropriate
label. Do the same to other places if you have the same pattern
elsewhere.

  reply	other threads:[~2025-08-14  7:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-14  4:46 [PATCH gwproxy v5 0/2] Initial work on integration of DNS parser lib in gwproxy Ahmad Gani
2025-08-14  4:46 ` [PATCH gwproxy v5 1/2] dns: Allow only port string number Ahmad Gani
2025-08-14  7:15   ` Alviro Iskandar Setiawan
2025-08-14  4:46 ` [PATCH gwproxy v5 2/2] dnsparser: Add dns parser code Ahmad Gani
2025-08-14  7:28   ` Alviro Iskandar Setiawan [this message]
2025-08-14  7:43     ` Alviro Iskandar Setiawan
2025-08-16 16:03   ` Ammar Faizi
2025-08-16 16:30     ` Ahmad Gani
2025-08-16 16:40       ` Ammar Faizi
2025-08-21 16:50       ` Alviro Iskandar Setiawan
2025-08-22  5:43         ` Ahmad Gani
2025-08-22 13:48           ` Alviro Iskandar Setiawan
2025-08-22 19:09             ` Alviro Iskandar Setiawan
2025-08-22 19:52               ` Ammar Faizi
2025-08-23  1:07                 ` Ahmad Gani
2025-08-23  1:52                   ` Ammar Faizi
2025-08-23  2:17                     ` Ahmad Gani
2025-08-23  2:22                       ` Ammar Faizi
2025-08-23  2:20                   ` Alviro Iskandar Setiawan
2025-08-23  2:28                     ` Ammar Faizi
2025-08-24 13:36                       ` Ahmad Gani
2025-08-25 14:37                         ` Ammar Faizi
2025-08-28  0:13                         ` Ammar Faizi
2025-08-28  1:51                           ` Ahmad Gani
2025-08-28  2:29                             ` Ahmad Gani
2025-08-28  2:45                               ` Ahmad Gani
2025-08-28  2:52                                 ` Ahmad Gani
2025-08-28  4:19                                 ` Ammar Faizi
2025-08-28  4:00                             ` Ammar Faizi
2025-08-16 16:41 ` (subset) [PATCH gwproxy v5 0/2] Initial work on integration of DNS parser lib in gwproxy Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOG64qNhvV7-GeYKqBCjQJwCR5YboGv-1=ktregh8S8ThRCeNA@mail.gmail.com' \
    --to=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    --cc=reyuki@gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox