1 /*
2  * Hostapd / Zephyr socket pair -based control interface
3  * Copyright (c) 2022, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2024, NXP
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "ctrl_iface_zephyr.h"
11 
hostapd_ctrl_iface_receive(int sock,void * eloop_ctx,void * sock_ctx)12 void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
13 					      void *sock_ctx)
14 {
15 	struct hostapd_data *hapd = eloop_ctx;
16 	char buf[MAX_CTRL_MSG_LEN + 1];
17 	char *pos;
18 	int res;
19 	char *reply = NULL;
20 	int reply_len = 0;
21 	const int reply_size = MAX_CTRL_MSG_LEN;
22 
23 	res = recv(sock, buf, MAX_CTRL_MSG_LEN, 0);
24 	if (res < 0) {
25 		wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
26 			   strerror(errno));
27 		return;
28 	}
29 
30 	if (!res) {
31 		eloop_unregister_sock(sock, EVENT_TYPE_READ);
32 		wpa_printf(MSG_DEBUG, "ctrl_iface: Peer unexpectedly shut down "
33 			   "socket");
34 		return;
35 	}
36 
37 	if ((size_t) res > MAX_CTRL_MSG_LEN) {
38 		wpa_printf(MSG_ERROR, "recvform(ctrl_iface): input truncated");
39 		return;
40 	}
41 	buf[res] = '\0';
42 
43 	pos = buf;
44 	while (*pos == ' ')
45 		pos++;
46 
47 	reply = os_malloc(reply_size);
48 	if (reply == NULL) {
49 		send(sock, "FAIL\n", 5, 0);
50 		wpa_printf(MSG_ERROR, "hostapd cli malloc fail for reply buffer");
51 		return;
52 	}
53 
54 	reply_len = hostapd_ctrl_iface_receive_process(hapd, pos, reply, reply_size, NULL, 0);
55 	if (reply_len > 0) {
56 		send(sock, reply, reply_len, 0);
57 	} else if (reply_len == 0) {
58 		send(sock, "OK\n", 3, 0);
59 	} else if (reply_len < 0) {
60 		send(sock, "FAIL\n", 5, 0);
61 	}
62 	os_free(reply);
63 }
64