1 /* Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without modification,
4 * are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice, this list of
7 * conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 * of conditions and the following disclaimer in the documentation and/or other materials
11 * provided with the distribution.
12 *
13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "lwip/opt.h"
31
32 #if LWIP_ENABLE_DISTRIBUTED_NET
33
34 #include "lwip/distributed_net/distributed_net.h"
35 #include "lwip/priv/sockets_priv.h"
36
37 static sys_mutex_t g_mutex = {0};
38
39 static u8_t g_is_distributed_net_enabled = 0;
40
41 static u16_t g_local_tcp_server_port = 0;
42
43 static u16_t g_local_udp_server_port = 0;
44
45 static u8_t g_is_distributed_net_socket[NUM_SOCKETS] = {0};
46
47 #if LWIP_DISTRIBUTED_NET_TRY_CONNECT
try_connect_to_local_tcp_server(u16_t tcp_port)48 static int try_connect_to_local_tcp_server(u16_t tcp_port)
49 {
50 struct sockaddr_in addr = {0};
51 (void)memset_s(&addr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
52 INIT_SOCK_ADDR(&addr, LOCAL_SERVER_IP, tcp_port);
53
54 int sock = lwip_socket(AF_INET, SOCK_STREAM, 0);
55 if (sock < 0) {
56 return -1;
57 }
58
59 int ret = lwip_connect_internal(sock, (struct sockaddr *)&addr, sizeof(addr));
60 (void)lwip_close_internal(sock);
61 return ret;
62 }
63 #endif /* LWIP_DISTRIBUTED_NET_TRY_CONNECT */
64
set_distributed_net_socket(int sock)65 void set_distributed_net_socket(int sock)
66 {
67 int index = SOCKET_TO_INDEX(sock);
68 if (index >= 0 && index < NUM_SOCKETS) {
69 sys_mutex_lock(&g_mutex);
70 g_is_distributed_net_socket[index] = 1;
71 sys_mutex_unlock(&g_mutex);
72 }
73 }
74
reset_distributed_net_socket(int sock)75 void reset_distributed_net_socket(int sock)
76 {
77 int index = SOCKET_TO_INDEX(sock);
78 if (index >= 0 && index < NUM_SOCKETS) {
79 sys_mutex_lock(&g_mutex);
80 g_is_distributed_net_socket[index] = 0;
81 sys_mutex_unlock(&g_mutex);
82 }
83 }
84
get_local_tcp_server_port(void)85 u16_t get_local_tcp_server_port(void)
86 {
87 sys_mutex_lock(&g_mutex);
88 u16_t ret = g_local_tcp_server_port;
89 sys_mutex_unlock(&g_mutex);
90 return ret;
91 }
92
get_local_udp_server_port(void)93 u16_t get_local_udp_server_port(void)
94 {
95 sys_mutex_lock(&g_mutex);
96 u16_t ret = g_local_udp_server_port;
97 sys_mutex_unlock(&g_mutex);
98 return ret;
99 }
100
is_distributed_net_enabled(void)101 u8_t is_distributed_net_enabled(void)
102 {
103 sys_mutex_lock(&g_mutex);
104 u8_t ret = g_is_distributed_net_enabled;
105 sys_mutex_unlock(&g_mutex);
106 return ret;
107 }
108
enable_distributed_net(u16_t tcp_port,u16_t udp_port)109 int enable_distributed_net(u16_t tcp_port, u16_t udp_port)
110 {
111 LWIP_DEBUGF(SOCKETS_DEBUG, ("enable distributed_net"));
112 if (is_distributed_net_enabled()) {
113 if (get_local_tcp_server_port() == tcp_port && get_local_udp_server_port() == udp_port) {
114 return 0;
115 }
116 set_errno(EINVAL);
117 return -1;
118 }
119
120 #if LWIP_DISTRIBUTED_NET_TRY_CONNECT
121 if (try_connect_to_local_tcp_server(tcp_port) < 0) {
122 return -1;
123 }
124 #endif /* LWIP_DISTRIBUTED_NET_TRY_CONNECT */
125
126 sys_mutex_lock(&g_mutex);
127 g_is_distributed_net_enabled = 1;
128 g_local_tcp_server_port = tcp_port;
129 g_local_udp_server_port = udp_port;
130 sys_mutex_unlock(&g_mutex);
131 return 0;
132 }
133
disable_distributed_net(void)134 int disable_distributed_net(void)
135 {
136 LWIP_DEBUGF(SOCKETS_DEBUG, ("disable distributed_net"));
137 sys_mutex_lock(&g_mutex);
138 for (int i = 0; i < NUM_SOCKETS; ++i) {
139 if (g_is_distributed_net_socket[i]) {
140 (void)lwip_close_internal(INDEX_TO_SOCKET(i));
141 }
142 }
143 g_local_tcp_server_port = 0;
144 g_local_udp_server_port = 0;
145 g_is_distributed_net_enabled = 0;
146 (void)memset_s(g_is_distributed_net_socket, sizeof(g_is_distributed_net_socket), 0,
147 sizeof(g_is_distributed_net_socket));
148 sys_mutex_unlock(&g_mutex);
149 return 0;
150 }
151
152 #endif /* LWIP_ENABLE_DISTRIBUTED_NET */