Browse Source

update:兼容lwip不带IPV6的情况

alienwalker 3 years ago
parent
commit
dc820a0227

+ 18 - 13
components/network/adapter/luat_lib_socket.c

@@ -53,7 +53,7 @@ static int l_socket_local_ip(lua_State *L)
 		return 0;
 	}
 #ifdef LUAT_USE_LWIP
-	ipv6.type = 0xff;
+	network_set_ip_invaild(&ipv6);
 	int ret = network_get_full_local_ip_info(NULL, adapter_index, &local_ip, &net_mask, &gate_way, &ipv6);
 #else
 	void* userdata = NULL;
@@ -68,12 +68,14 @@ static int l_socket_local_ip(lua_State *L)
 		lua_pushfstring(L, "%s", ipaddr_ntoa(&local_ip));
 		lua_pushfstring(L, "%s", ipaddr_ntoa(&net_mask));
 		lua_pushfstring(L, "%s", ipaddr_ntoa(&gate_way));
+#ifdef LWIP_IPV6
 		if (IPADDR_TYPE_V6 == ipv6.type)
 		{
 			char *ipv6_string = ip6addr_ntoa(&ipv6.u_addr.ip6);
 			lua_pushfstring(L, "%s", ipv6_string);
 		}
 		else
+#endif
 		{
 			lua_pushnil(L);
 		}
@@ -387,11 +389,10 @@ static int l_socket_connect(lua_State *L)
 	luat_ip_addr_t ip_addr;
 	const char *ip = NULL;
 	size_t ip_len;
-	ip_addr.type = 0xff;
+	network_set_ip_invaild(&ip_addr);
 	if (lua_isinteger(L, 2))
 	{
-		ip_addr.type = IPADDR_TYPE_V4;
-		ip_addr.u_addr.ip4.addr = lua_tointeger(L, 2);
+		network_set_ip_ipv4(&ip_addr, lua_tointeger(L, 2));
 		ip = NULL;
 		ip_len = 0;
 	}
@@ -402,7 +403,7 @@ static int l_socket_connect(lua_State *L)
 	}
 	uint16_t remote_port = luaL_checkinteger(L, 3);
 	LLOGD("connect to %s,%d", ip, remote_port);
-	if (ip_addr.type != IPADDR_TYPE_V4)
+	if (!network_ip_is_vaild_ipv4(&ip_addr))
 	{
 		if (LUA_TBOOLEAN == lua_type(L, 4))
 		{
@@ -413,7 +414,7 @@ static int l_socket_connect(lua_State *L)
 			network_connect_ipv6_domain(l_ctrl->netc, 0);
 		}
 	}
-	int result = network_connect(l_ctrl->netc, ip, ip_len, (ip_addr.type != IPADDR_TYPE_V4)?NULL:&ip_addr, remote_port, 0);
+	int result = network_connect(l_ctrl->netc, ip, ip_len, (!network_ip_is_vaild_ipv4(&ip_addr))?NULL:&ip_addr, remote_port, 0);
 	lua_pushboolean(L, (result < 0)?0:1);
 	lua_pushboolean(L, result == 0);
 	return 2;
@@ -496,7 +497,7 @@ static int l_socket_tx(lua_State *L)
 	const char *ip = NULL;
 	const char *data = NULL;
 	size_t ip_len = 0, data_len = 0;
-	ip_addr.type = 0xff;
+	network_set_ip_invaild(&ip_addr);
 	if (lua_isstring(L, 2))
 	{
 		data_len = 0;
@@ -510,8 +511,7 @@ static int l_socket_tx(lua_State *L)
 	}
 	if (lua_isinteger(L, 3))
 	{
-		ip_addr.type = 0;
-		ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
+		network_set_ip_ipv4(&ip_addr, lua_tointeger(L, 3));
 	}
 	else if (lua_isstring(L, 3))
 	{
@@ -523,7 +523,7 @@ static int l_socket_tx(lua_State *L)
 
 	}
 	uint32_t tx_len;
-	int result = network_tx(l_ctrl->netc, (const uint8_t *)data, data_len, luaL_optinteger(L, 5, 0), (ip_addr.type != 0xff)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
+	int result = network_tx(l_ctrl->netc, (const uint8_t *)data, data_len, luaL_optinteger(L, 5, 0), network_ip_is_vaild(&ip_addr)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
 #else
 	luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
 	luat_ip_addr_t ip_addr = {0};
@@ -648,6 +648,7 @@ static int l_socket_rx(lua_State *L)
 			else
 			{
 #ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
 				if (IPADDR_TYPE_V4 == ip_addr.type)
 				{
 					ip[0] = 0;
@@ -660,6 +661,11 @@ static int l_socket_rx(lua_State *L)
 					memcpy(ip + 1, ip_addr.u_addr.ip6.addr, 16);
 					lua_pushlstring(L, (const char*)ip, 17);
 				}
+#else
+				ip[0] = 0;
+				memcpy(ip + 1, &ip_addr.addr, 4);
+				lua_pushlstring(L, (const char*)ip, 5);
+#endif
 #else
 				if (!ip_addr.is_ipv6)
 				{
@@ -818,11 +824,10 @@ static int l_socket_set_dns(lua_State *L)
 	luat_ip_addr_t ip_addr;
 	const char *ip;
 	size_t ip_len;
-	ip_addr.type = 0xff;
+	network_set_ip_invaild(&ip_addr);
 	if (lua_isinteger(L, 3))
 	{
-		ip_addr.type = 0;
-		ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
+		network_set_ip_ipv4(&ip_addr, lua_tointeger(L, 3));
 		ip = NULL;
 		ip_len = 0;
 	}

+ 78 - 23
components/network/adapter/luat_network_adapter.c

@@ -369,12 +369,13 @@ TLS_RECV:
 static int network_get_host_by_name(network_ctrl_t *ctrl)
 {
 #ifdef LUAT_USE_LWIP
+	network_set_ip_invaild(&ctrl->remote_ip);
 	ctrl->remote_ip.type = 0xff;
 	if (ipaddr_aton(ctrl->domain_name, &ctrl->remote_ip))
 	{
 		return 0;
 	}
-	ctrl->remote_ip.type = 0xff;
+	network_set_ip_invaild(&ctrl->remote_ip);
 	return -1;
 #else
 	ctrl->remote_ip.is_ipv6 = 0xff;
@@ -421,10 +422,10 @@ static int network_base_connect(network_ctrl_t *ctrl, luat_ip_addr_t *remote_ip)
 	}
 	if (remote_ip)
 	{
-		if (network_create_soceket(ctrl, IPADDR_TYPE_V6 == remote_ip->type) < 0)
+		if (network_create_soceket(ctrl, network_ip_is_ipv6(remote_ip)) < 0)
 		{
 			network_clean_invaild_socket(ctrl->adapter_index);
-			if (network_create_soceket(ctrl, IPADDR_TYPE_V6 == remote_ip->type) < 0)
+			if (network_create_soceket(ctrl, network_ip_is_ipv6(remote_ip)) < 0)
 			{
 				return -1;
 			}
@@ -515,11 +516,8 @@ static int network_base_connect(network_ctrl_t *ctrl, luat_ip_addr_t *remote_ip)
 
 static int network_prepare_connect(network_ctrl_t *ctrl)
 {
-#ifdef LUAT_USE_LWIP
-	if (ctrl->remote_ip.type != 0xff)
-#else
-	if (ctrl->remote_ip.is_ipv6 != 0xff)
-#endif
+
+	if (network_ip_is_vaild(&ctrl->remote_ip))
 	{
 		;
 	}
@@ -657,11 +655,7 @@ static int network_state_connecting(network_ctrl_t *ctrl, OS_EVENT *event, netwo
 	case EV_NW_SOCKET_ERROR:
 	case EV_NW_SOCKET_REMOTE_CLOSE:
 	case EV_NW_SOCKET_CLOSE_OK:
-#ifdef LUAT_USE_LWIP
-		if (ctrl->remote_ip.type != 0xff)
-#else
-		if (ctrl->remote_ip.is_ipv6 != 0xff)
-#endif
+		if (network_ip_is_vaild(&ctrl->remote_ip))
 		{
 			return -1;
 		}
@@ -1380,11 +1374,7 @@ void network_init_ctrl(network_ctrl_t *ctrl, HANDLE task_handle, CBFuncEx_t call
 	ctrl->user_data = param;
 	ctrl->socket_id = -1;
 	ctrl->socket_param = ctrl;
-#ifdef LUAT_USE_LWIP
-	ctrl->remote_ip.type = 0xff;
-#else
-	ctrl->remote_ip.is_ipv6 = 0xff;
-#endif
+	network_set_ip_invaild(&ctrl->remote_ip);
 	ctrl->mutex = sem;
 	if (task_handle)
 	{
@@ -1975,11 +1965,7 @@ int network_connect(network_ctrl_t *ctrl, const char *domain_name, uint32_t doma
 	}
 	else
 	{
-#ifdef LUAT_USE_LWIP
-		ctrl->remote_ip.type = 0xff;
-#else
-		ctrl->remote_ip.is_ipv6 = 0xff;
-#endif
+		network_set_ip_invaild(&ctrl->remote_ip);
 	}
 	ctrl->auto_mode = 1;
 	ctrl->remote_port = remote_port;
@@ -2604,4 +2590,73 @@ uint8_t network_check_ready(network_ctrl_t *ctrl, uint8_t adapter_index)
 		return 0;
 	}
 }
+
+//将IP设置成无效状态
+void network_set_ip_invaild(luat_ip_addr_t *ip)
+{
+#ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
+	ip->type = 0xff;
+#else
+	ip->addr = 0;
+#endif
+#else
+	ip->is_ipv6 = 0xff;
+#endif
+}
+//检测IP是不是无效的,无效返回0
+uint8_t network_ip_is_vaild(luat_ip_addr_t *ip)
+{
+#ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
+	return (ip->type != 0xff);
+#else
+	return (ip->addr != 0);
+#endif
+#else
+	return (ip->is_ipv6 != 0xff);
+#endif
+}
+
+uint8_t network_ip_is_ipv6(luat_ip_addr_t *ip)
+{
+#ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
+	return (IPADDR_TYPE_V6 == ip->type);
+#else
+	return 0;
+#endif
+#else
+	return (ip->is_ipv6 && (ip->is_ipv6 != 0xff));
+#endif
+}
+
+//检测IP是不是有效的IPV4类型,不是返回0
+uint8_t network_ip_is_vaild_ipv4(luat_ip_addr_t *ip)
+{
+#ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
+	return (IPADDR_TYPE_V4 == ip->type);
+#else
+	return (ip->addr != 0);
+#endif
+#else
+	return !ip->is_ipv6;
+#endif
+}
+
+void network_set_ip_ipv4(luat_ip_addr_t *ip, uint32_t ipv4)
+{
+#ifdef LUAT_USE_LWIP
+#ifdef LWIP_IPV6
+	ip->type = IPADDR_TYPE_V4;
+	ip->u_addr.ip4.addr = ipv4;
+#else
+	ip->addr = ipv4;
+#endif
+#else
+	ip->is_ipv6 = 0;
+	ip->ipv4 = ipv4;
+#endif
+}
 #endif

+ 11 - 0
components/network/adapter/luat_network_adapter.h

@@ -489,5 +489,16 @@ int network_wait_rx(network_ctrl_t *ctrl, uint32_t timeout_ms, uint8_t *is_break
 
 // 补充函数
 int network_get_full_local_ip_info(network_ctrl_t *ctrl, uint8_t index, luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6);
+
+//将IP设置成无效状态
+void network_set_ip_invaild(luat_ip_addr_t *ip);
+//检测IP是不是无效的,无效返回0
+uint8_t network_ip_is_vaild(luat_ip_addr_t *ip);
+//检测IP是不是IPV6类型,不是返回0
+uint8_t network_ip_is_ipv6(luat_ip_addr_t *ip);
+//检测IP是不是有效的IPV4类型,不是返回0
+uint8_t network_ip_is_vaild_ipv4(luat_ip_addr_t *ip);
+//将IP设置成IPV4
+void network_set_ip_ipv4(luat_ip_addr_t *ip, uint32_t ipv4);
 #endif
 // #endif

+ 3 - 13
components/network/libemqtt/luat_lib_mqtt.c

@@ -289,19 +289,9 @@ static int l_mqtt_create(lua_State *L) {
 	// 连接参数相关
 	// const char *ip;
 	size_t ip_len = 0;
-#ifdef LUAT_USE_LWIP
-	mqtt_ctrl->ip_addr.type = 0xff;
-#else
-	mqtt_ctrl->ip_addr.is_ipv6 = 0xff;
-#endif
+	network_set_ip_invaild(&mqtt_ctrl->ip_addr);
 	if (lua_isinteger(L, 2)){
-#ifdef LUAT_USE_LWIP
-		mqtt_ctrl->ip_addr.type = IPADDR_TYPE_V4;
-		mqtt_ctrl->ip_addr.u_addr.ip4.addr = lua_tointeger(L, 2);
-#else
-		mqtt_ctrl->ip_addr.is_ipv6 = 0;
-		mqtt_ctrl->ip_addr.ipv4 = lua_tointeger(L, 2);
-#endif
+		network_set_ip_ipv4(&mqtt_ctrl->ip_addr, lua_tointeger(L, 2));
 		// ip = NULL;
 		ip_len = 0;
 	}else{
@@ -578,7 +568,7 @@ static int l_mqtt_ready(lua_State *L) {
 @string 遗嘱消息的retain, 默认0, 可以不填
 @return bool 成功返回true,否则返回false
 @usage
--- 要在connect之前调用 
+-- 要在connect之前调用
 mqttc:will("/xxx/xxx", "xxxxxx")
 */
 static int l_mqtt_will(lua_State *L) {

+ 1 - 5
components/network/libftp/luat_ftp_client.c

@@ -888,11 +888,7 @@ static int l_ftp_login(lua_State *L) {
 		network_deinit_tls(g_s_ftp.network->cmd_netc);
 	}
 
-#ifdef LUAT_USE_LWIP
-	g_s_ftp.network->ip_addr.type = 0xff;
-#else
-	g_s_ftp.network->ip_addr.is_ipv6 = 0xff;
-#endif
+	network_set_ip_invaild(&g_s_ftp.network->ip_addr);
 
 	g_s_ftp.idp = luat_pushcwait(L);
 	luat_rtos_event_send(g_s_ftp.task_handle, FTP_EVENT_LOGIN, 0, 0, 0, LUAT_WAIT_FOREVER);

+ 1 - 5
components/network/libhttp/luat_lib_http.c

@@ -230,11 +230,7 @@ static int l_http_request(lua_State *L) {
 		network_deinit_tls(http_ctrl->netc);
 	}
 
-#ifdef LUAT_USE_LWIP
-	http_ctrl->ip_addr.type = 0xff;
-#else
-	http_ctrl->ip_addr.is_ipv6 = 0xff;
-#endif
+	network_set_ip_invaild(&http_ctrl->ip_addr);
 	http_ctrl->idp = luat_pushcwait(L);
 
     if (luat_http_client_start(http_ctrl)) {