summaryrefslogtreecommitdiff
path: root/src/libwpactrl/wpactrl_update.c
blob: a116d5001c0fef2014bcb316facda62b6772e66c (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
/* ISC license. */

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

#include <skalibs/posixishard.h>
#include <skalibs/error.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/stralloc.h>

#include <bcnm/wpactrl.h>
#include "wpactrl-internal.h"

static inline int validate (char const *s, size_t len)
{
  if (len < 4) return 0 ;
  if (s[0] != '<') return 0 ;
  if (!memchr("123456789", s[1], 9)) return 0 ;
  if (s[2] != '>') return 0 ;
  if (strnlen(s, len) < len) return 0 ;
  return s[len-1] == '\n' ;
}

int wpactrl_update (wpactrl_t *a)
{
  unsigned int n = WPACTRL_RECV_MAX ;
  unsigned int count = 0 ;
  char buf[WPACTRL_PACKET_MAX+1] ;
  while (n--)
  {
    ssize_t r = sanitize_read(wpactrl_fd_recv(a->fda, buf, WPACTRL_PACKET_MAX)) ;
    if (r < 0) return -1 ;
    if (!r) break ;
    if (a->options & WPACTRL_OPTION_NOFILTER
     || (validate(buf, r) && wpactrl_filter_match(a, buf, r)))
    {
      buf[r] = 0 ;
      if (!stralloc_catb(&a->data, buf, r+1)) return -1 ;
      count++ ;
    }
  }
  return (int)count ;
}