summaryrefslogtreecommitdiff
path: root/src/libwpactrl
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
downloadbcnm-330f54ac7f780872223f9cac62347393ffb4ef86.tar.xz
Initial commit
Diffstat (limited to 'src/libwpactrl')
-rw-r--r--src/libwpactrl/deps-lib/wpactrl12
-rw-r--r--src/libwpactrl/wpactrl-internal.h18
-rw-r--r--src/libwpactrl/wpactrl_command.c41
-rw-r--r--src/libwpactrl/wpactrl_end.c11
-rw-r--r--src/libwpactrl/wpactrl_fd_recv.c47
-rw-r--r--src/libwpactrl/wpactrl_fd_timed_recv.c29
-rw-r--r--src/libwpactrl/wpactrl_filter_add.c12
-rw-r--r--src/libwpactrl/wpactrl_filter_exact_search.c12
-rw-r--r--src/libwpactrl/wpactrl_filter_remove.c16
-rw-r--r--src/libwpactrl/wpactrl_query.c11
-rw-r--r--src/libwpactrl/wpactrl_querysa.c15
-rw-r--r--src/libwpactrl/wpactrl_start.c45
-rw-r--r--src/libwpactrl/wpactrl_update.c48
-rw-r--r--src/libwpactrl/wpactrl_zero.c5
14 files changed, 322 insertions, 0 deletions
diff --git a/src/libwpactrl/deps-lib/wpactrl b/src/libwpactrl/deps-lib/wpactrl
new file mode 100644
index 0000000..98cd9e6
--- /dev/null
+++ b/src/libwpactrl/deps-lib/wpactrl
@@ -0,0 +1,12 @@
+wpactrl_command.o
+wpactrl_end.o
+wpactrl_fd_recv.o
+wpactrl_fd_timed_recv.o
+wpactrl_filter_add.o
+wpactrl_filter_exact_search.o
+wpactrl_filter_remove.o
+wpactrl_query.o
+wpactrl_querysa.o
+wpactrl_start.o
+wpactrl_update.o
+wpactrl_zero.o
diff --git a/src/libwpactrl/wpactrl-internal.h b/src/libwpactrl/wpactrl-internal.h
new file mode 100644
index 0000000..b271eae
--- /dev/null
+++ b/src/libwpactrl/wpactrl-internal.h
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#ifndef BCNM_WPACTRL_INTERNAL_H
+#define BCNM_WPACTRL_INTERNAL_H
+
+#include <sys/types.h>
+#include <skalibs/gccattributes.h>
+#include <skalibs/tai.h>
+#include <bcnm/wpactrl.h>
+
+#define WPACTRL_PACKET_MAX 8192
+#define WPACTRL_RECV_MAX 32
+
+extern ssize_t wpactrl_fd_recv (int, char *, size_t) ;
+extern ssize_t wpactrl_fd_timed_recv (int, char *, size_t, tain_t const *, tain_t *) ;
+extern size_t wpactrl_filter_exact_search (wpactrl_t const *, char const *) gccattr_pure ;
+
+#endif
diff --git a/src/libwpactrl/wpactrl_command.c b/src/libwpactrl/wpactrl_command.c
new file mode 100644
index 0000000..c29a58a
--- /dev/null
+++ b/src/libwpactrl/wpactrl_command.c
@@ -0,0 +1,41 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+#include <bcnm/wpactrl.h>
+
+#define WPARESPONSE_MAXLEN 28
+
+wparesponse_t wpactrl_command (wpactrl_t *a, char const *s, size_t len, tain_t const *deadline, tain_t *stamp)
+{
+ static char const *wparesponses[] =
+ {
+ "OK\n",
+ "PONG\n",
+ "UNKNOWN COMMAND\n",
+ "FAIL\n",
+ "FAIL-BUSY\n",
+ "FAIL-CHECKSUM\n",
+ "FAIL-INVALID-PIN\n",
+ "FAIL-CHANNEL-UNAVAILABLE\n",
+ "FAIL-CHANNEL-UNSUPPORTED\n",
+ "FAIL-Invalid range\n",
+ "FAIL-Too long response\n",
+ "FAIL-PBC-OVERLAP\n",
+ "FAIL-UNKNOWN-UUID\n",
+ "FAIL-NO-AP-SETTINGS\n",
+ "FAIL-NO-IFNAME-MATCH\n",
+ 0
+ } ;
+ char buf[WPARESPONSE_MAXLEN] ;
+ ssize_t r = wpactrl_query(a, s, len, buf, WPARESPONSE_MAXLEN, deadline, stamp) ;
+ if (r < 0) return WPA_ERROR ;
+ if (!r) return (errno = EPIPE, WPA_ERROR) ;
+ {
+ wparesponse_t i = 0 ;
+ for (; wparesponses[i] ; i++)
+ if (!strncmp(buf, wparesponses[i], r))
+ break ;
+ return i ;
+ }
+}
diff --git a/src/libwpactrl/wpactrl_end.c b/src/libwpactrl/wpactrl_end.c
new file mode 100644
index 0000000..40fdb4a
--- /dev/null
+++ b/src/libwpactrl/wpactrl_end.c
@@ -0,0 +1,11 @@
+/* ISC license. */
+
+#include <skalibs/djbunix.h>
+#include <bcnm/wpactrl.h>
+
+void wpactrl_free (wpactrl_t *a)
+{
+ fd_close(a->fda) ;
+ fd_close(a->fds) ;
+ *a = wpactrl_zero ;
+}
diff --git a/src/libwpactrl/wpactrl_fd_recv.c b/src/libwpactrl/wpactrl_fd_recv.c
new file mode 100644
index 0000000..3e2a006
--- /dev/null
+++ b/src/libwpactrl/wpactrl_fd_recv.c
@@ -0,0 +1,47 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+#include <skalibs/nonposix.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <errno.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+ssize_t wpactrl_fd_recv (int fd, char *s, size_t len)
+{
+ static int const bsd_braindeadness_workaround_flags =
+#ifdef SKALIBS_HASMSGDONTWAIT
+ MSG_DONTWAIT
+#else
+ 0
+#endif
+ |
+#ifdef SKALIBS_HASNBWAITALL
+ MSG_WAITALL
+#else
+ 0
+#endif
+ |
+#ifdef SKALIBS_HASCMSGCLOEXEC
+ MSG_CMSG_CLOEXEC
+#else
+ 0
+#endif
+ ;
+ struct iovec iov = { .iov_base = s, .iov_len = len } ;
+ struct msghdr msghdr =
+ {
+ .msg_name = 0,
+ .msg_namelen = 0,
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_flags = 0,
+ .msg_control = 0,
+ .msg_controllen = 0
+ } ;
+ ssize_t r ;
+ do r = recvmsg(fd, &msghdr, bsd_braindeadness_workaround_flags) ;
+ while (r == -1 && errno == EINTR) ;
+ return r > 0 && msghdr.msg_flags | MSG_TRUNC ? (errno = EMSGSIZE, -1) : r ;
+}
diff --git a/src/libwpactrl/wpactrl_fd_timed_recv.c b/src/libwpactrl/wpactrl_fd_timed_recv.c
new file mode 100644
index 0000000..db181c1
--- /dev/null
+++ b/src/libwpactrl/wpactrl_fd_timed_recv.c
@@ -0,0 +1,29 @@
+/* ISC license. */
+
+#include <skalibs/functypes.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/unix-timed.h>
+#include "wpactrl-internal.h"
+
+struct blah_s
+{
+ int fd ;
+ char *s ;
+ size_t len ;
+} ;
+
+static int getfd (struct blah_s *blah)
+{
+ return blah->fd ;
+}
+
+static ssize_t get (struct blah_s *blah)
+{
+ return sanitize_read(wpactrl_fd_recv(blah->fd, blah->s, blah->len)) ;
+}
+
+ssize_t wpactrl_fd_timed_recv (int fd, char *s, size_t len, tain_t const *deadline, tain_t *stamp)
+{
+ struct blah_s blah = { .fd = fd, .s = s, .len = len } ;
+ return timed_get(&blah, (initfunc_t_ref)&getfd, (getfunc_t_ref)&get, deadline, stamp) ;
+}
diff --git a/src/libwpactrl/wpactrl_filter_add.c b/src/libwpactrl/wpactrl_filter_add.c
new file mode 100644
index 0000000..8896e23
--- /dev/null
+++ b/src/libwpactrl/wpactrl_filter_add.c
@@ -0,0 +1,12 @@
+/* ISC license. */
+
+#include <string.h>
+#include <skalibs/stralloc.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+int wpactrl_filter_add (wpactrl_t *a, char const *s)
+{
+ if (wpactrl_filter_exact_search(a, s) < a->filters.len) return 1 ;
+ return stralloc_catb(&a->filters, s, strlen(s)) ;
+}
diff --git a/src/libwpactrl/wpactrl_filter_exact_search.c b/src/libwpactrl/wpactrl_filter_exact_search.c
new file mode 100644
index 0000000..c7197d7
--- /dev/null
+++ b/src/libwpactrl/wpactrl_filter_exact_search.c
@@ -0,0 +1,12 @@
+/* ISC license. */
+
+#include <string.h>
+#include "wpactrl-internal.h"
+
+size_t wpactrl_filter_exact_search (wpactrl_t const *a, char const *s)
+{
+ size_t pos = 0 ;
+ for ( ; pos < a->filters.len ; pos += strlen(a->filters.s + pos) + 1)
+ if (!strcmp(s, a->filters.s + pos)) break ;
+ return pos ;
+}
diff --git a/src/libwpactrl/wpactrl_filter_remove.c b/src/libwpactrl/wpactrl_filter_remove.c
new file mode 100644
index 0000000..d41ad6e
--- /dev/null
+++ b/src/libwpactrl/wpactrl_filter_remove.c
@@ -0,0 +1,16 @@
+/* ISC license. */
+
+#include <string.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+void wpactrl_filter_remove (wpactrl_t *a, char const *s)
+{
+ size_t pos = wpactrl_filter_exact_search(a, s) ;
+ if (pos >= a->filters.len)
+ {
+ size_t after = pos + strlen(a->filters.s + pos) + 1 ;
+ memmove(a->filters.s + pos, a->filters.s + after, a->filters.len - after) ;
+ a->filters.len -= after - pos ;
+ }
+}
diff --git a/src/libwpactrl/wpactrl_query.c b/src/libwpactrl/wpactrl_query.c
new file mode 100644
index 0000000..951c536
--- /dev/null
+++ b/src/libwpactrl/wpactrl_query.c
@@ -0,0 +1,11 @@
+/* ISC license. */
+
+#include <skalibs/unix-timed.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+ssize_t wpactrl_query (wpactrl_t *a, char const *q, size_t qlen, char *ans, size_t ansmax, tain_t const *deadline, tain_t *stamp)
+{
+ if (!ipc_timed_send(a->fds, q, qlen, deadline, stamp)) return WPA_ERROR ;
+ return wpactrl_fd_timed_recv(a->fds, ans, ansmax, deadline, stamp) ;
+}
diff --git a/src/libwpactrl/wpactrl_querysa.c b/src/libwpactrl/wpactrl_querysa.c
new file mode 100644
index 0000000..fe06202
--- /dev/null
+++ b/src/libwpactrl/wpactrl_querysa.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/stralloc.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+int wpactrl_querysa (wpactrl_t *a, char const *s, size_t len, stralloc *sa, tain_t const *deadline, tain_t *stamp)
+{
+ char buf[WPACTRL_PACKET_MAX] ;
+ ssize_t r = wpactrl_query(a, s, len, buf, WPACTRL_PACKET_MAX, deadline, stamp) ;
+ if (r < 0) return 0 ;
+ if (!r) return (errno = EPIPE, 0) ;
+ return stralloc_catb(sa, buf, r) ;
+}
diff --git a/src/libwpactrl/wpactrl_start.c b/src/libwpactrl/wpactrl_start.c
new file mode 100644
index 0000000..86a266b
--- /dev/null
+++ b/src/libwpactrl/wpactrl_start.c
@@ -0,0 +1,45 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/webipc.h>
+#include <skalibs/unix-timed.h>
+#include <bcnm/wpactrl.h>
+#include "wpactrl-internal.h"
+
+int wpactrl_start (wpactrl_t *a, char const *path, tain_t const *deadline, tain_t *stamp)
+{
+ int fda, fds ;
+ fds = ipc_datagram_nbcoe() ;
+ if (fds < 0) goto err ;
+ if (!ipc_timed_connect(fds, path, deadline, stamp)) goto errs ;
+ fda = ipc_datagram_nbcoe() ;
+ if (fda < 0) goto errs ;
+ if (!ipc_timed_connect(fda, path, deadline, stamp)) goto erra ;
+ if (!ipc_timed_send(fda, "ATTACH", 6, deadline, stamp)) goto erra ;
+ {
+ ssize_t r ;
+ char answer[3] ;
+ r = wpactrl_fd_timed_recv(fda, answer, 3, deadline, stamp) ;
+ if (r != 3 || memcmp(answer, "OK\n", 3)) goto erra ;
+ }
+ a->fds = fds ;
+ a->fda = fda ;
+ return 1 ;
+
+ erra:
+ {
+ int e = errno ;
+ fd_close(fda) ;
+ errno = e ;
+ }
+ errs:
+ {
+ int e = errno ;
+ fd_close(fds) ;
+ errno = e ;
+ }
+ err:
+ return 0 ;
+}
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 ;
+}
diff --git a/src/libwpactrl/wpactrl_zero.c b/src/libwpactrl/wpactrl_zero.c
new file mode 100644
index 0000000..89b4356
--- /dev/null
+++ b/src/libwpactrl/wpactrl_zero.c
@@ -0,0 +1,5 @@
+/* ISC license. */
+
+#include <bcnm/wpactrl.h>
+
+wpactrl_t const wpactrl_zero = WPACTRL_ZERO ;