[PATCH 5/6] add ctrl_iface_common.c/h files
Janusz Dziedzic
janusz.dziedzic
Fri Sep 25 04:31:23 PDT 2015
Add common code for ctrl interface.
Use common funtions for ctrl unix.
Signed-off-by: Janusz Dziedzic <janusz.dziedzic at tieto.com>
---
src/common/ctrl_iface_common.c | 141 +++++++++++++++++++++++++++++++++++++++
src/common/ctrl_iface_common.h | 36 ++++++++++
wpa_supplicant/Makefile | 4 ++
wpa_supplicant/ctrl_iface_udp.c | 117 ++------------------------------
wpa_supplicant/ctrl_iface_unix.c | 98 ++++-----------------------
5 files changed, 199 insertions(+), 197 deletions(-)
create mode 100644 src/common/ctrl_iface_common.c
create mode 100644 src/common/ctrl_iface_common.h
diff --git a/src/common/ctrl_iface_common.c b/src/common/ctrl_iface_common.c
new file mode 100644
index 0000000..0774318
--- /dev/null
+++ b/src/common/ctrl_iface_common.c
@@ -0,0 +1,141 @@
+/*
+ * Common hostapd/wpa_supplicant ctrl iface code.
+ * Copyright (c) 2002-2013, Jouni Malinen <j at w1.fi>
+ * Copyright (c) 2015, Qualcomm Atheros, Inc.
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#include "includes.h"
+#include "common.h"
+#include "ctrl_iface_common.h"
+#include <netdb.h>
+#include <sys/un.h>
+
+int sockaddr_compare(struct sockaddr_storage *a, struct sockaddr_storage *b)
+{
+ struct sockaddr_in *in_a, *in_b;
+ struct sockaddr_in6 *in6_a, *in6_b;
+ struct sockaddr_un *u_a, *u_b;
+
+ if (a->ss_family != b->ss_family)
+ return 1;
+
+ switch (a->ss_family) {
+ case AF_INET:
+ in_a = (struct sockaddr_in *) a;
+ in_b = (struct sockaddr_in *) b;
+
+ if (in_a->sin_port != in_b->sin_port)
+ return 1;
+ if (in_a->sin_addr.s_addr != in_b->sin_addr.s_addr)
+ return 1;
+ break;
+ case AF_INET6:
+ in6_a = (struct sockaddr_in6 *) a;
+ in6_b = (struct sockaddr_in6 *) b;
+
+ if (in6_a->sin6_port != in6_b->sin6_port)
+ return 1;
+ if (os_memcmp(&in6_a->sin6_addr, &in6_b->sin6_addr, sizeof(in6_a->sin6_addr)))
+ return 1;
+ break;
+ case AF_UNIX:
+ u_a = (struct sockaddr_un *) a;
+ u_b = (struct sockaddr_un *) b;
+
+ if (os_strncmp(u_a->sun_path, u_b->sun_path, sizeof(u_a->sun_path)))
+ return 1;
+ break;
+ default:
+ return 1;
+ }
+
+ return 0;
+}
+
+void sockaddr_print(int level, char *msg, struct sockaddr_storage *sock, socklen_t socklen)
+{
+ char host[NI_MAXHOST] = {0};
+ char service[NI_MAXSERV] = {0};
+ char addr_txt[200];
+
+ switch (sock->ss_family) {
+ case AF_INET:
+ case AF_INET6:
+ getnameinfo((struct sockaddr *) sock, socklen,
+ host, sizeof(host),
+ service, sizeof(service),
+ NI_NUMERICHOST);
+
+ wpa_printf(level, "%s %s:%s", msg, host, service);
+ break;
+ case AF_UNIX:
+ printf_encode(addr_txt, sizeof(addr_txt),
+ (u8 *) ((struct sockaddr_un *) sock)->sun_path,
+ socklen - offsetof(struct sockaddr_un, sun_path));
+ wpa_printf(level, "%s %s", msg, addr_txt);
+ break;
+ default:
+ wpa_printf(level, "%s", msg);
+ break;
+ }
+}
+
+int ctrl_iface_attach(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen)
+{
+ struct wpa_ctrl_dst *dst;
+
+ dst = os_zalloc(sizeof(*dst));
+ if (dst == NULL)
+ return -1;
+ os_memcpy(&dst->addr, from, fromlen);
+ dst->addrlen = fromlen;
+ dst->debug_level = MSG_INFO;
+ dl_list_add(ctrl_dst, &dst->list);
+
+ sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor attached", from, fromlen);
+ return 0;
+}
+
+
+int ctrl_iface_detach(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen)
+{
+ struct wpa_ctrl_dst *dst;
+
+ dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
+ if (!sockaddr_compare(from, &dst->addr)) {
+ sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor detached", from, fromlen);
+ dl_list_del(&dst->list);
+ os_free(dst);
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+int ctrl_iface_level(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen,
+ char *level)
+{
+ struct wpa_ctrl_dst *dst;
+
+ wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
+
+ dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
+ if (!sockaddr_compare(from, &dst->addr)) {
+ sockaddr_print(MSG_DEBUG, "CTRL_IFACE changed monitor level", from, fromlen);
+ dst->debug_level = atoi(level);
+ return 0;
+ }
+ }
+
+ return -1;
+}
diff --git a/src/common/ctrl_iface_common.h b/src/common/ctrl_iface_common.h
new file mode 100644
index 0000000..dcd88e0
--- /dev/null
+++ b/src/common/ctrl_iface_common.h
@@ -0,0 +1,36 @@
+/*
+ * Common hostapd/wpa_supplicant ctrl iface code.
+ * Copyright (c) 2002-2013, Jouni Malinen <j at w1.fi>
+ * Copyright (c) 2015, Qualcomm Atheros, Inc.
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+#ifndef CONTROL_IFACE_COMMON_H
+#define CONTROL_IFACE_COMMON_H
+
+#include "utils/list.h"
+
+struct wpa_ctrl_dst {
+ struct dl_list list;
+ struct sockaddr_storage addr;
+ socklen_t addrlen;
+ int debug_level;
+ int errors;
+};
+
+int sockaddr_compare(struct sockaddr_storage *a, struct sockaddr_storage *b);
+void sockaddr_print(int level, char *msg, struct sockaddr_storage *sock, socklen_t socklen);
+
+int ctrl_iface_attach(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen);
+int ctrl_iface_detach(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen);
+int ctrl_iface_level(struct dl_list *ctrl_dst,
+ struct sockaddr_storage *from,
+ socklen_t fromlen,
+ char *level);
+
+#endif /* CONTROL_IFACE_COMMON_H */
diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile
index 1597412..6fd384c 100644
--- a/wpa_supplicant/Makefile
+++ b/wpa_supplicant/Makefile
@@ -1329,6 +1329,7 @@ endif
CFLAGS += -DCONFIG_CTRL_IFACE
ifeq ($(CONFIG_CTRL_IFACE), unix)
CFLAGS += -DCONFIG_CTRL_IFACE_UNIX
+OBJS += ../src/common/ctrl_iface_common.o
endif
ifeq ($(CONFIG_CTRL_IFACE), udp)
CFLAGS += -DCONFIG_CTRL_IFACE_UDP
@@ -1353,6 +1354,9 @@ CFLAGS += -DCONFIG_CTRL_IFACE_UDP_REMOTE
CFLAGS += -DCONFIG_CTRL_IFACE_UDP_IPV6
endif
OBJS += ctrl_iface.o ctrl_iface_$(CONFIG_CTRL_IFACE).o
+ifeq ($(CONFIG_CTRL_IFACE), udp)
+OBJS += ../src/common/ctrl_iface_common.o
+endif
endif
ifdef CONFIG_CTRL_IFACE_DBUS
diff --git a/wpa_supplicant/ctrl_iface_udp.c b/wpa_supplicant/ctrl_iface_udp.c
index 217a1c4..a2b3a5e 100644
--- a/wpa_supplicant/ctrl_iface_udp.c
+++ b/wpa_supplicant/ctrl_iface_udp.c
@@ -16,6 +16,7 @@
#include "wpa_supplicant_i.h"
#include "ctrl_iface.h"
#include "common/wpa_ctrl.h"
+#include "common/ctrl_iface_common.h"
#include <netdb.h>
#define COOKIE_LEN 8
@@ -29,13 +30,7 @@
* interface monitors into struct wpa_supplicant. This data is private to
* ctrl_iface_udp.c and should not be touched directly from other files.
*/
-struct wpa_ctrl_dst {
- struct dl_list list;
- struct sockaddr_storage addr;
- socklen_t addrlen;
- int debug_level;
- int errors;
-};
+
struct ctrl_iface_priv {
@@ -59,92 +54,13 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
int level, const char *buf,
size_t len);
-static int sockaddr_compare(struct sockaddr_storage *a, struct sockaddr_storage *b)
-{
- struct sockaddr_in *in_a, *in_b;
- struct sockaddr_in6 *in6_a, *in6_b;
- struct sockaddr_un *u_a, *u_b;
-
- if (a->ss_family != b->ss_family)
- return 1;
-
- switch (a->ss_family) {
- case AF_INET:
- in_a = (struct sockaddr_in *) a;
- in_b = (struct sockaddr_in *) b;
-
- if (in_a->sin_port != in_b->sin_port)
- return 1;
- if (in_a->sin_addr.s_addr != in_b->sin_addr.s_addr)
- return 1;
- break;
- case AF_INET6:
- in6_a = (struct sockaddr_in6 *) a;
- in6_b = (struct sockaddr_in6 *) b;
-
- if (in6_a->sin6_port != in6_b->sin6_port)
- return 1;
- if (os_memcmp(&in6_a->sin6_addr, &in6_b->sin6_addr, sizeof(in6_a->sin6_addr)))
- return 1;
- break;
- case AF_UNIX:
- u_a = (struct sockaddr_un *) a;
- u_b = (struct sockaddr_un *) b;
-
- if (os_strncmp(u_a->sun_path, u_b->sun_path, sizeof(u_a->sun_path)))
- return 1;
- break;
- default:
- return 1;
- }
- return 0;
-}
-
-static void sockaddr_print(int level, char *msg, struct sockaddr_storage *sock, socklen_t socklen)
-{
- char host[NI_MAXHOST] = {0};
- char service[NI_MAXSERV] = {0};
- char addr_txt[200];
-
- switch (sock->ss_family) {
- case AF_INET:
- case AF_INET6:
- getnameinfo((struct sockaddr *) sock, socklen,
- host, sizeof(host),
- service, sizeof(service),
- NI_NUMERICHOST);
-
- wpa_printf(level, "%s %s:%s", msg, host, service);
- break;
- case AF_UNIX:
- printf_encode(addr_txt, sizeof(addr_txt),
- (u8 *) ((struct sockaddr_un *) sock)->sun_path,
- socklen - offsetof(struct sockaddr_un, sun_path));
- wpa_printf(level, "%s %s", msg, addr_txt);
- break;
- default:
- wpa_printf(level, "%s", msg);
- break;
- }
-}
static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
struct sockaddr_storage *from,
socklen_t fromlen)
{
- struct wpa_ctrl_dst *dst;
-
- dst = os_zalloc(sizeof(*dst));
- if (dst == NULL)
- return -1;
- os_memcpy(&dst->addr, from, fromlen);
- dst->addrlen = fromlen;
- dst->debug_level = MSG_INFO;
- dl_list_add(ctrl_dst, &dst->list);
-
- sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor attached", from, fromlen);
- return 0;
+ return ctrl_iface_attach(ctrl_dst, from, fromlen);
}
@@ -152,18 +68,7 @@ static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
struct sockaddr_storage *from,
socklen_t fromlen)
{
- struct wpa_ctrl_dst *dst;
-
- dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
- if (!sockaddr_compare(from, &dst->addr)) {
- sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor detached", from, fromlen);
- dl_list_del(&dst->list);
- os_free(dst);
- return 0;
- }
- }
-
- return -1;
+ return ctrl_iface_detach(ctrl_dst, from, fromlen);
}
@@ -172,19 +77,7 @@ static int wpa_supplicant_ctrl_iface_level(struct dl_list *ctrl_dst,
socklen_t fromlen,
char *level)
{
- struct wpa_ctrl_dst *dst;
-
- wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
-
- dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
- if (!sockaddr_compare(from, &dst->addr)) {
- sockaddr_print(MSG_DEBUG, "CTRL_IFACE changed monitor level", from, fromlen);
- dst->debug_level = atoi(level);
- return 0;
- }
- }
-
- return -1;
+ return ctrl_iface_level(ctrl_dst, from, fromlen, level);
}
diff --git a/wpa_supplicant/ctrl_iface_unix.c b/wpa_supplicant/ctrl_iface_unix.c
index e1c4639..912a4d2 100644
--- a/wpa_supplicant/ctrl_iface_unix.c
+++ b/wpa_supplicant/ctrl_iface_unix.c
@@ -28,25 +28,10 @@
#include "config.h"
#include "wpa_supplicant_i.h"
#include "ctrl_iface.h"
+#include "common/ctrl_iface_common.h"
/* Per-interface ctrl_iface */
-/**
- * struct wpa_ctrl_dst - Internal data structure of control interface monitors
- *
- * This structure is used to store information about registered control
- * interface monitors into struct wpa_supplicant. This data is private to
- * ctrl_iface_unix.c and should not be touched directly from other files.
- */
-struct wpa_ctrl_dst {
- struct dl_list list;
- struct sockaddr_un addr;
- socklen_t addrlen;
- int debug_level;
- int errors;
-};
-
-
struct ctrl_iface_priv {
struct wpa_supplicant *wpa_s;
int sock;
@@ -103,81 +88,27 @@ static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
- struct sockaddr_un *from,
+ struct sockaddr_storage *from,
socklen_t fromlen, int global)
{
- struct wpa_ctrl_dst *dst;
- char addr_txt[200];
-
- dst = os_zalloc(sizeof(*dst));
- if (dst == NULL)
- return -1;
- os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
- dst->addrlen = fromlen;
- dst->debug_level = MSG_INFO;
- dl_list_add(ctrl_dst, &dst->list);
- printf_encode(addr_txt, sizeof(addr_txt),
- (u8 *) from->sun_path,
- fromlen - offsetof(struct sockaddr_un, sun_path));
- wpa_printf(MSG_DEBUG, "CTRL_IFACE %smonitor attached %s",
- global ? "global " : "", addr_txt);
- return 0;
+ return ctrl_iface_attach(ctrl_dst, from, fromlen);
}
static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
- struct sockaddr_un *from,
+ struct sockaddr_storage *from,
socklen_t fromlen)
{
- struct wpa_ctrl_dst *dst;
-
- dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
- if (fromlen == dst->addrlen &&
- os_memcmp(from->sun_path, dst->addr.sun_path,
- fromlen - offsetof(struct sockaddr_un, sun_path))
- == 0) {
- char addr_txt[200];
- printf_encode(addr_txt, sizeof(addr_txt),
- (u8 *) from->sun_path,
- fromlen -
- offsetof(struct sockaddr_un, sun_path));
- wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
- addr_txt);
- dl_list_del(&dst->list);
- os_free(dst);
- return 0;
- }
- }
- return -1;
+ return ctrl_iface_detach(ctrl_dst, from, fromlen);
}
static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
- struct sockaddr_un *from,
+ struct sockaddr_storage *from,
socklen_t fromlen,
char *level)
{
- struct wpa_ctrl_dst *dst;
-
- wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
-
- dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
- if (fromlen == dst->addrlen &&
- os_memcmp(from->sun_path, dst->addr.sun_path,
- fromlen - offsetof(struct sockaddr_un, sun_path))
- == 0) {
- char addr_txt[200];
- dst->debug_level = atoi(level);
- printf_encode(addr_txt, sizeof(addr_txt),
- (u8 *) from->sun_path, fromlen -
- offsetof(struct sockaddr_un, sun_path));
- wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
- dst->debug_level, addr_txt);
- return 0;
- }
- }
-
- return -1;
+ return ctrl_iface_level(&priv->ctrl_dst, from, fromlen, level);
}
@@ -188,7 +119,7 @@ static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
struct ctrl_iface_priv *priv = sock_ctx;
char buf[4096];
int res;
- struct sockaddr_un from;
+ struct sockaddr_storage from;
socklen_t fromlen = sizeof(from);
char *reply = NULL, *reply_buf = NULL;
size_t reply_len = 0;
@@ -743,16 +674,13 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
if (level < dst->debug_level)
continue;
- printf_encode(addr_txt, sizeof(addr_txt),
- (u8 *) dst->addr.sun_path, dst->addrlen -
- offsetof(struct sockaddr_un, sun_path));
msg.msg_name = (void *) &dst->addr;
msg.msg_namelen = dst->addrlen;
wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
- wpa_printf(MSG_MSGDUMP,
- "CTRL_IFACE monitor sent successfully to %s",
- addr_txt);
+ sockaddr_print(MSG_MSGDUMP,
+ "CTRL_IFACE monitor sent successfully to ",
+ &dst->addr, dst->addrlen);
dst->errors = 0;
continue;
}
@@ -798,7 +726,7 @@ void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
{
char buf[256];
int res;
- struct sockaddr_un from;
+ struct sockaddr_storage from;
socklen_t fromlen = sizeof(from);
for (;;) {
@@ -858,7 +786,7 @@ static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
struct ctrl_iface_global_priv *priv = sock_ctx;
char buf[4096];
int res;
- struct sockaddr_un from;
+ struct sockaddr_storage from;
socklen_t fromlen = sizeof(from);
char *reply = NULL, *reply_buf = NULL;
size_t reply_len;
--
1.9.1
More information about the Hostap
mailing list