summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_message_parse.c
blob: eef9410651b5a7b514c81b6e2501a0f1b4e2aaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* ISC license. */

#include <errno.h>
#include <skalibs/error.h>
#include <s6-dns/s6dns-constants.h>
#include <s6-dns/s6dns-message.h>

int s6dns_message_parse (s6dns_message_header_t *h, char const *packet, unsigned int packetlen, s6dns_message_rr_func_t_ref f, void *data)
{
  s6dns_message_counts_t counts ;
  unsigned int pos ;
  unsigned int section ;
  if (!s6dns_message_parse_init(h, &counts, packet, packetlen, &pos)) return 0 ;
  switch (h->rcode)
  {
    case 0 : break ;
    case 1 : return (errno = EILSEQ, 0) ;
    case 2 : return (errno = EBUSY, 0) ;
    case 3 : return (errno = ENOENT, 0) ;
    case 4 : return (errno = ENOTSUP, 0) ;
    case 5 : return (errno = ECONNREFUSED, 0) ;
    default: return (errno = EIO, 0) ;
  }
  section = s6dns_message_parse_skipqd(&counts, packet, packetlen, &pos) ;
  while (section)
  {
    s6dns_message_rr_t rr ;
    if (!s6dns_message_parse_getrr(&rr, packet, packetlen, &pos)) return 0 ;
    if (rr.rclass == S6DNS_C_IN)
    {
      register int r = (*f)(&rr, packet, packetlen, pos, section, data) ;
      if (r < 1) return r ;
    }
    section = s6dns_message_parse_next(&counts, &rr, packet, packetlen, &pos) ;
  }
  return 1 ;
}