about summary refs log tree commit diff
path: root/src/libwpactrl/wpactrl_update.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-07-06 21:42:01 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-07-06 21:42:01 +0000
commit330f54ac7f780872223f9cac62347393ffb4ef86 (patch)
tree1de817bd4b556e487b93884ac21561e1e5779635 /src/libwpactrl/wpactrl_update.c
downloadbcnm-330f54ac7f780872223f9cac62347393ffb4ef86.tar.gz
bcnm-330f54ac7f780872223f9cac62347393ffb4ef86.tar.xz
bcnm-330f54ac7f780872223f9cac62347393ffb4ef86.zip
Initial commit
Diffstat (limited to 'src/libwpactrl/wpactrl_update.c')
-rw-r--r--src/libwpactrl/wpactrl_update.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libwpactrl/wpactrl_update.c b/src/libwpactrl/wpactrl_update.c
new file mode 100644
index 0000000..edbdc6f
--- /dev/null
+++ b/src/libwpactrl/wpactrl_update.c
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#include <string.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/stralloc.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+static inline int filter_search (char const *s, size_t len, char const *filters, size_t filterlen)
+{
+  while (filterlen)
+  {
+    size_t flen = strlen(filters) ;
+    if (len >= flen && !strncmp(filters, s, flen)) return 1 ;
+    filters += flen+1 ;
+    filterlen -= flen+1 ;
+  }
+  return 0 ;
+}
+
+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 ;
+  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] ;
+  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) && filter_search(buf, r, a->filters.s, a->filters.len)))
+    {
+      if (!stralloc_catb(&a->data, buf, r)) return -1 ;
+      count++ ;
+    }
+  }
+  return (int)count ;
+}