summaryrefslogtreecommitdiff
path: root/src/libskabus/skabus_pub_update.c
blob: 46e699c5109f3ae01496eca58e3e659efa08dc6b (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
52
53
54
55
56
57
58
/* ISC license. */

#include <string.h>
#include <stdint.h>
#include <errno.h>

#include <skalibs/posixishard.h>
#include <skalibs/uint64.h>
#include <skalibs/tai.h>
#include <skalibs/alloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/unixmessage.h>
#include <skalibs/skaclient.h>

#include <skabus/pub.h>

static int handler (unixmessage_t const *m, void *x)
{
  skabus_pub_t *a = (skabus_pub_t *)x ;
  size_t n = genalloc_len(skabus_pub_cltinfo_t, &a->info) ;
  char const *s = m->s ;
  size_t len = m->len ;
  skabus_pub_cltinfo_t *p ;
  uint8_t idlen ;
  if (len < 11 + TAIN_PACK || !m->nfds) goto errproto ;
  if (!genalloc_readyplus(skabus_pub_cltinfo_t, &a->info, 1)) goto err ;
  p = genalloc_s(skabus_pub_cltinfo_t, &a->info) + n ;
  p->fd = m->fds[0] ;
  p->nfds = m->nfds - 1 ;
  if (p->nfds > 1)
  {
    p->fds = alloc(p->nfds * sizeof(int)) ;
    if (!p->fds) goto err ;
    for (size_t i = 0 ; i < p->nfds ; i++) p->fds[i] = m->fds[1 + i] ;
  }
  else p->fds = 0 ;
  uint64_unpack_big(s, &p->msginfo.serial) ; s += 8 ; len -= 8 ;
  tain_unpack(s, &p->msginfo.timestamp) ; s += TAIN_PACK ; len -= TAIN_PACK ;
  p->msginfo.flags = *s++ ; len-- ;
  idlen = *s++ ; len-- ;
  if (len != (size_t)idlen + 1 || s[idlen] || idlen > SKABUS_PUB_IDSTR_SIZE) goto errprotof ;
  memcpy(p->msginfo.sender, s, len) ;
  genalloc_setlen(skabus_pub_cltinfo_t, &a->info, n+1) ;
  return 1 ;

 errprotof:
  alloc_free(p->fds) ;
 errproto:
  errno = EPROTO ;
 err:
  unixmessage_drop(m) ;
  return 0 ;
}

int skabus_pub_update (skabus_pub_t *a)
{
  return skaclient_update(&a->connection, &handler, a) ;
}