diff options
author | David Herrmann <dh.herrmann@gmail.com> | 2013-11-06 03:58:29 +0100 |
---|---|---|
committer | David Herrmann <dh.herrmann@gmail.com> | 2013-11-06 03:58:29 +0100 |
commit | 0339773076040aded86947d764654c0f962c70cc (patch) | |
tree | a73b14307f642c0be6ebeeb1fb271bc4e147bdfe | |
parent | c732b58fb0e0776f456cf0f2e1dcc311a568e4f6 (diff) |
owfd: wpa: add wpa-event parser skeleton
wpa_supplicants sends events formatted as strings. This makes it
human-readable, but rather annoying to parse and compare. Thus, we add a
small parser to turn wpa-events into some binary format for easy
comparison and handling.
We don't support all events but only the events we need. Feel free to add
new events once you need them. Also note that we don't parse parameters,
yet. This will also be added on a per-need basis.
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 18 | ||||
-rw-r--r-- | src/wpa.h | 48 | ||||
-rw-r--r-- | src/wpa_parser.c | 153 | ||||
-rw-r--r-- | test/test_common.h | 1 | ||||
-rw-r--r-- | test/test_wpa.c | 122 |
6 files changed, 338 insertions, 5 deletions
@@ -27,3 +27,4 @@ openwfd_p2pd stamp-h1 test-suite.log test_rtsp +test_wpa diff --git a/Makefile.am b/Makefile.am index 898e08d..7c5ad41 100644 --- a/Makefile.am +++ b/Makefile.am @@ -119,7 +119,8 @@ libowfd_la_SOURCES = \ src/shared.h \ src/shared.c \ src/wpa.h \ - src/wpa_ctrl.c + src/wpa_ctrl.c \ + src/wpa_parser.c libowfd_la_CPPFLAGS = $(AM_CPPFLAGS) libowfd_la_LDFLAGS = $(AM_LDFLAGS) libowfd_la_LIBADD = $(AM_LIBADD) @@ -179,11 +180,13 @@ openwfd_ie_LDFLAGS = $(AM_LDFLAGS) # Tests # +tests = \ + test_rtsp \ + test_wpa + if BUILD_HAVE_CHECK -check_PROGRAMS += \ - test_rtsp -TESTS += \ - test_rtsp +check_PROGRAMS += $(tests) +TESTS += $(tests) endif test_sources = \ @@ -203,6 +206,11 @@ test_rtsp_CPPFLAGS = $(test_cflags) test_rtsp_LDADD = $(test_libs) test_rtsp_LDFLAGS = $(test_lflags) +test_wpa_SOURCES = test/test_wpa.c $(test_sources) +test_wpa_CPPFLAGS = $(test_cflags) +test_wpa_LDADD = $(test_libs) +test_wpa_LDFLAGS = $(test_lflags) + # # Phony targets # @@ -34,6 +34,8 @@ extern "C" { #endif +/* wpa ctrl */ + struct owfd_wpa_ctrl; typedef void (*owfd_wpa_ctrl_cb) (struct owfd_wpa_ctrl *wpa, void *buf, @@ -62,6 +64,52 @@ int owfd_wpa_ctrl_request(struct owfd_wpa_ctrl *wpa, const void *cmd, int owfd_wpa_ctrl_request_ok(struct owfd_wpa_ctrl *wpa, const void *cmd, size_t cmd_len, int timeout); +/* wpa parser */ + +enum owfd_wpa_event_type { + OWFD_WPA_EVENT_UNKNOWN, + OWFD_WPA_EVENT_AP_STA_CONNECTED, + OWFD_WPA_EVENT_AP_STA_DISCONNECTED, + OWFD_WPA_EVENT_P2P_DEVICE_FOUND, + OWFD_WPA_EVENT_P2P_GO_NEG_REQUEST, + OWFD_WPA_EVENT_P2P_GO_NEG_SUCCESS, + OWFD_WPA_EVENT_P2P_GO_NEG_FAILURE, + OWFD_WPA_EVENT_P2P_GROUP_FORMATION_SUCCESS, + OWFD_WPA_EVENT_P2P_GROUP_FORMATION_FAILURE, + OWFD_WPA_EVENT_P2P_GROUP_STARTED, + OWFD_WPA_EVENT_P2P_GROUP_REMOVED, + OWFD_WPA_EVENT_P2P_PROV_DISC_SHOW_PIN, + OWFD_WPA_EVENT_P2P_PROV_DISC_ENTER_PIN, + OWFD_WPA_EVENT_P2P_PROV_DISC_PBC_REQ, + OWFD_WPA_EVENT_P2P_PROV_DISC_PBC_RESP, + OWFD_WPA_EVENT_P2P_SERV_DISC_REQ, + OWFD_WPA_EVENT_P2P_SERV_DISC_RESP, + OWFD_WPA_EVENT_P2P_INVITATION_RECEIVED, + OWFD_WPA_EVENT_P2P_INVITATION_RESULT, + OWFD_WPA_EVENT_COUNT, +}; + +enum owfd_wpa_event_priority { + OWFD_WPA_EVENT_P_MSGDUMP, + OWFD_WPA_EVENT_P_DEBUG, + OWFD_WPA_EVENT_P_INFO, + OWFD_WPA_EVENT_P_WARNING, + OWFD_WPA_EVENT_P_ERROR, + OWFD_WPA_EVENT_P_COUNT +}; + +#define OWFD_WPA_EVENT_MAC_STRLEN 18 + +struct owfd_wpa_event { + unsigned int type; + unsigned int priority; + char *raw; +}; + +void owfd_wpa_event_init(struct owfd_wpa_event *ev); +void owfd_wpa_event_reset(struct owfd_wpa_event *ev); +int owfd_wpa_event_parse(struct owfd_wpa_event *ev, const char *event); + #ifdef __cplusplus } #endif diff --git a/src/wpa_parser.c b/src/wpa_parser.c new file mode 100644 index 0000000..2acec9f --- /dev/null +++ b/src/wpa_parser.c @@ -0,0 +1,153 @@ +/* + * OpenWFD - Open-Source Wifi-Display Implementation + * + * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "shared.h" +#include "wpa.h" + +void owfd_wpa_event_init(struct owfd_wpa_event *ev) +{ + memset(ev, 0, sizeof(*ev)); +} + +void owfd_wpa_event_reset(struct owfd_wpa_event *ev) +{ + free(ev->raw); + + memset(ev, 0, sizeof(*ev)); +} + +static const struct event_type { + const char *name; + size_t len; + unsigned int code; +} event_list[] = { + +#define EVENT(_name, _suffix) { \ + .name = _name, \ + .len = sizeof(_name) - 1, \ + .code = OWFD_WPA_EVENT_ ## _suffix \ + } + + /* MUST BE ORDER ALPHABETICALLY FOR BINARY SEARCH! */ + + EVENT("AP-STA-CONNECTED", AP_STA_CONNECTED), + EVENT("AP-STA-DISCONNECTED", AP_STA_DISCONNECTED), + EVENT("P2P-DEVICE-FOUND", P2P_DEVICE_FOUND), + EVENT("P2P-GO-NEG-FAILURE", P2P_GO_NEG_FAILURE), + EVENT("P2P-GO-NEG-REQUEST", P2P_GO_NEG_REQUEST), + EVENT("P2P-GO-NEG-SUCCESS", P2P_GO_NEG_SUCCESS), + EVENT("P2P-GROUP-FORMATION-FAILURE", P2P_GROUP_FORMATION_FAILURE), + EVENT("P2P-GROUP-FORMATION-SUCCESS", P2P_GROUP_FORMATION_SUCCESS), + EVENT("P2P-GROUP-REMOVED", P2P_GROUP_REMOVED), + EVENT("P2P-GROUP-STARTED", P2P_GROUP_STARTED), + EVENT("P2P-INVITATION-RECEIVED", P2P_INVITATION_RECEIVED), + EVENT("P2P-INVITATION-RESULT", P2P_INVITATION_RESULT), + EVENT("P2P-PROV-DISC-ENTER-PIN", P2P_PROV_DISC_ENTER_PIN), + EVENT("P2P-PROV-DISC-PBC-REQ", P2P_PROV_DISC_PBC_REQ), + EVENT("P2P-PROV-DISC-PBC-RESP", P2P_PROV_DISC_PBC_RESP), + EVENT("P2P-PROV-DISC-SHOW-PIN", P2P_PROV_DISC_SHOW_PIN), + EVENT("P2P-SERV-DISC-REQ", P2P_SERV_DISC_REQ), + EVENT("P2P-SERV-DISC-RESP", P2P_SERV_DISC_RESP), + +#undef EVENT +}; + +static int event_comp(const void *key, const void *type) +{ + const struct event_type *t; + const char *k; + int r; + + k = key; + t = type; + + r = strncmp(k, t->name, t->len); + if (r) + return r; + + if (k[t->len] != 0 && k[t->len] != ' ') + return 1; + + return 0; +} + +int owfd_wpa_event_parse(struct owfd_wpa_event *ev, const char *event) +{ + const char *t; + char *end; + struct event_type *code; + + owfd_wpa_event_reset(ev); + + if (*event == '<') { + t = strchr(event, '>'); + if (!t) + goto unknown; + + ++t; + ev->priority = strtoul(event + 1, &end, 10); + if (ev->priority >= OWFD_WPA_EVENT_P_COUNT || + end + 1 != t || + event[1] == '=' || + event[1] == '-') + ev->priority = OWFD_WPA_EVENT_P_MSGDUMP; + } else { + t = event; + ev->priority = OWFD_WPA_EVENT_P_MSGDUMP; + } + + code = bsearch(t, event_list, + sizeof(event_list) / sizeof(*event_list), + sizeof(*event_list), + event_comp); + if (!code) + goto unknown; + + ev->type = code->code; + t += code->len; + while (*t == ' ') + ++t; + + ev->raw = strdup(t); + if (!ev->raw) + goto oom; + + switch (ev->type) { + } + + return 0; + +unknown: + ev->type = OWFD_WPA_EVENT_UNKNOWN; + return 0; + +oom: + owfd_wpa_event_reset(ev); + return -ENOMEM; +} diff --git a/test/test_common.h b/test/test_common.h index a4f3a84..dda6d97 100644 --- a/test/test_common.h +++ b/test/test_common.h @@ -46,6 +46,7 @@ #include <string.h> #include "shared.h" #include "rtsp.h" +#include "wpa.h" /* lower address-space is protected from user-allocation, so this is invalid */ #define TEST_INVALID_PTR ((void*)0x10) diff --git a/test/test_wpa.c b/test/test_wpa.c new file mode 100644 index 0000000..b3f61c7 --- /dev/null +++ b/test/test_wpa.c @@ -0,0 +1,122 @@ +/* + * OpenWFD - Open-Source Wifi-Display Implementation + * + * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "test_common.h" + +static void parse(struct owfd_wpa_event *ev, const char *event) +{ + int r; + + owfd_wpa_event_init(ev); + r = owfd_wpa_event_parse(ev, event); + ck_assert(!r); + ck_assert(ev->priority < OWFD_WPA_EVENT_P_COUNT); +} + +static const char *event_list[] = { + [OWFD_WPA_EVENT_UNKNOWN] = "", + [OWFD_WPA_EVENT_AP_STA_CONNECTED] = "AP-STA-CONNECTED", + [OWFD_WPA_EVENT_AP_STA_DISCONNECTED] = "AP-STA-DISCONNECTED", + [OWFD_WPA_EVENT_P2P_DEVICE_FOUND] = "P2P-DEVICE-FOUND", + [OWFD_WPA_EVENT_P2P_GO_NEG_REQUEST] = "P2P-GO-NEG-REQUEST", + [OWFD_WPA_EVENT_P2P_GO_NEG_SUCCESS] = "P2P-GO-NEG-SUCCESS", + [OWFD_WPA_EVENT_P2P_GO_NEG_FAILURE] = "P2P-GO-NEG-FAILURE", + [OWFD_WPA_EVENT_P2P_GROUP_FORMATION_SUCCESS] = "P2P-GROUP-FORMATION-SUCCESS", + [OWFD_WPA_EVENT_P2P_GROUP_FORMATION_FAILURE] = "P2P-GROUP-FORMATION-FAILURE", + [OWFD_WPA_EVENT_P2P_GROUP_STARTED] = "P2P-GROUP-STARTED", + [OWFD_WPA_EVENT_P2P_GROUP_REMOVED] = "P2P-GROUP-REMOVED", + [OWFD_WPA_EVENT_P2P_PROV_DISC_SHOW_PIN] = "P2P-PROV-DISC-SHOW-PIN", + [OWFD_WPA_EVENT_P2P_PROV_DISC_ENTER_PIN] = "P2P-PROV-DISC-ENTER-PIN", + [OWFD_WPA_EVENT_P2P_PROV_DISC_PBC_REQ] = "P2P-PROV-DISC-PBC-REQ", + [OWFD_WPA_EVENT_P2P_PROV_DISC_PBC_RESP] = "P2P-PROV-DISC-PBC-RESP", + [OWFD_WPA_EVENT_P2P_SERV_DISC_REQ] = "P2P-SERV-DISC-REQ", + [OWFD_WPA_EVENT_P2P_SERV_DISC_RESP] = "P2P-SERV-DISC-RESP", + [OWFD_WPA_EVENT_P2P_INVITATION_RECEIVED] = "P2P-INVITATION-RECEIVED", + [OWFD_WPA_EVENT_P2P_INVITATION_RESULT] = "P2P-INVITATION-RESULT", + [OWFD_WPA_EVENT_COUNT] = NULL +}; + +START_TEST(test_wpa_parser) +{ + struct owfd_wpa_event ev; + int i; + + parse(&ev, ""); + ck_assert(ev.type == OWFD_WPA_EVENT_UNKNOWN); + + parse(&ev, "asdf"); + ck_assert(ev.type == OWFD_WPA_EVENT_UNKNOWN); + + for (i = 0; i < OWFD_WPA_EVENT_COUNT; ++i) { + ck_assert_msg(event_list[i] != NULL, "event %d missing", i); + parse(&ev, event_list[i]); + ck_assert_msg(ev.type == i, "event %d invalid", i); + } + + parse(&ev, "<5>AP-STA-CONNECTED"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_MSGDUMP); + ck_assert(ev.type == OWFD_WPA_EVENT_AP_STA_CONNECTED); + + parse(&ev, "<4>AP-STA-CONNECTED"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_ERROR); + ck_assert(ev.type == OWFD_WPA_EVENT_AP_STA_CONNECTED); + + parse(&ev, "<4>AP-STA-CONNECTED2"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_ERROR); + ck_assert(ev.type == OWFD_WPA_EVENT_UNKNOWN); + + parse(&ev, "<4asdf>AP-STA-CONNECTED"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_MSGDUMP); + ck_assert(ev.type == OWFD_WPA_EVENT_AP_STA_CONNECTED); + + parse(&ev, "<4>AP-STA-CONNECTED something else"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_ERROR); + ck_assert(ev.type == OWFD_WPA_EVENT_AP_STA_CONNECTED); + ck_assert(ev.raw != NULL); + ck_assert(!strcmp(ev.raw, "something else")); + + parse(&ev, "<4>AP-STA something else"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_ERROR); + ck_assert(ev.type == OWFD_WPA_EVENT_UNKNOWN); + ck_assert(!ev.raw); + + parse(&ev, "<4>AP-STA-CONNECTED"); + ck_assert(ev.priority == OWFD_WPA_EVENT_P_ERROR); + ck_assert(ev.type == OWFD_WPA_EVENT_AP_STA_CONNECTED); + ck_assert(ev.raw != NULL); + ck_assert(!*ev.raw); +} +END_TEST + +TEST_DEFINE_CASE(parser) + TEST(test_wpa_parser) +TEST_END_CASE + +TEST_DEFINE( + TEST_SUITE(wpa, + TEST_CASE(parser), + TEST_END + ) +) |