summaryrefslogtreecommitdiff
path: root/src/skadns/skadns_update.c
blob: 39b1dd085c2565152ceb5dcb32639c64b3516bc8 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* ISC license. */

/* Hey, OpenBSD, are you aware ECANCELED is POSIX? */
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif

#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <skalibs/error.h>
#include <skalibs/uint16.h>
#include <skalibs/alloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/unixmessage.h>
#include <skalibs/skaclient.h>
#include <s6-dns/skadns.h>

static int msghandler (unixmessage_t const *m, void *context)
{
  skadns_t *a = (skadns_t *)context ;
  skadnsanswer_t *p ;
  uint16_t id ;
  if (m->len < 3 || m->nfds) return (errno = EPROTO, 0) ;
  uint16_unpack_big(m->s, &id) ;
  p = GENSETDYN_P(skadnsanswer_t, &a->q, id) ;
  if (p->status == ECANCELED)
  {
    p->status = EINVAL ;
    return gensetdyn_delete(&a->q, id) ;
  }
  if (!error_isagain(p->status)) return (errno = EINVAL, 0) ;
  if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ;
  if (!m->s[2])
  {
    p->data = alloc(m->len-3) ;
    if (!p->data) return 0 ;
    memcpy(p->data, m->s+3, m->len-3) ;
    p->len = m->len-3 ;
  }
  p->status = m->s[2] ;
  genalloc_append(uint16_t, &a->list, &id) ;
  return 1 ;
}

int skadns_update (skadns_t *a)
{
  genalloc_setlen(uint16_t, &a->list, 0) ;
  return skaclient_update(&a->connection, &msghandler, a) ;
}