aboutsummaryrefslogtreecommitdiffstats
path: root/src/libunixonacid/sassclient_update.c
blob: de64435fa124184f453c27c92dc24c332036c66e (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
/* ISC license. */

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

#include <skalibs/uint32.h>
#include <skalibs/textclient.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/genqdyn.h>
#include <skalibs/sassclient.h>
#include "sassclient-internal.h"

#include <skalibs/posixishard.h>

static int sassclient_msghandler (struct iovec const *v, void *aux)
{
  sassclient *a = aux ;
  int e ;
  sassclient_data *p ;
  uint32_t id ;
  char res[8] ;
  if (v->iov_len < 8) return (errno = EPROTO, 0) ;
  memcpy(res, v->iov_base, 8) ;
  uint32_unpack_big(res, &id) ;
  p = GENSETDYN_P(sassclient_data, &a->store, id) ;
  if (memcmp(res + 4, "\0\0\0", 4))
  {
    if (v->iov_len != 8) return (errno = EPROTO, 0) ;
  }
  else
  {
    e = (*p->cb)((char *)v->iov_base + 8, v->iov_len - 8, id, p->data) ;
    uint32_pack_big(res + 4, e) ;
  }
  e = pthread_mutex_lock(&a->results_mutex) ;
  if (e) return (errno = e, 0) ;
  if (!genqdyn_push(&a->results, res))
  {
    pthread_mutex_unlock(&a->results_mutex) ;
    return 0 ;
  }
  pthread_mutex_unlock(&a->results_mutex) ;
  return 1 ;
}

int sassclient_update (sassclient *a)
{
  int e = pthread_mutex_lock(&a->connection_mutex) ;
  if (e) return (errno = e, -1) ;
  e = textclient_update(&a->connection, &sassclient_msghandler, a) ;
  pthread_mutex_unlock(&a->connection_mutex) ;
  return e < 0 ? -1 : !!e ;
}