1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: This is the IPv4 & IPv6 address tools implementation.
15 * Author: none
16 * Create: 2020
17 */
18
19 #include "lwip/opt.h"
20 #include "lwip/inet.h"
21
22 #if LWIP_IPV4 || LWIP_IPV6
23
24 #include "lwip/ip_addr.h"
25
26 #if LWIP_INET_ADDR_FUNC
inet_addr(const char * cp)27 in_addr_t inet_addr(const char *cp)
28 {
29 LWIP_ERROR("inet_aton:cp is NULL", (cp != NULL), return (INADDR_NONE));
30 return ipaddr_addr(cp);
31 }
32 #endif
33
34 #if LWIP_INET_ATON_FUNC
inet_aton(const char * cp,struct in_addr * inp)35 int inet_aton(const char *cp, struct in_addr *inp)
36 {
37 LWIP_ERROR("inet_aton:cp is NULL", (cp != NULL), return 0);
38 return ip4addr_aton(cp, (ip4_addr_t *)inp);
39 }
40 #endif
41
42 #if LWIP_INET_NTOA_FUNC
inet_ntoa(struct in_addr in)43 char *inet_ntoa(struct in_addr in)
44 {
45 return ip4addr_ntoa((const ip4_addr_t *)&in);
46 }
47 #endif
48 #endif /* LWIP_IPV4 || LWIP_IPV6 */
49
50 #if LWIP_IPV4 && LWIP_IPV6
51 #if LWIP_SMALL_SIZE
ip_addr_cmp(const ip_addr_t * addr1,const ip_addr_t * addr2)52 int ip_addr_cmp(const ip_addr_t *addr1, const ip_addr_t *addr2)
53 {
54 if (addr1 == NULL || addr2 == NULL) {
55 return 0;
56 }
57
58 return ((IP_GET_TYPE(addr1) != IP_GET_TYPE(addr2)) ? 0 : (IP_IS_V6_VAL(*(addr1)) ? \
59 ip6_addr_cmp(ip_2_ip6(addr1), ip_2_ip6(addr2)) : \
60 ip4_addr_cmp(ip_2_ip4(addr1), ip_2_ip4(addr2))));
61 }
62
ip_addr_isany(const ip_addr_t * ipaddr)63 int ip_addr_isany(const ip_addr_t *ipaddr)
64 {
65 return (((ipaddr) == NULL) ? 1 : ((IP_IS_V6(ipaddr)) ? ip6_addr_isany(ip_2_ip6(ipaddr)) :
66 ip4_addr_isany(ip_2_ip4(ipaddr))));
67 }
68
ip_addr_isloopback(const ip_addr_t * ipaddr)69 int ip_addr_isloopback(const ip_addr_t *ipaddr)
70 {
71 if (ipaddr == NULL) {
72 return 0;
73 }
74 return ((IP_IS_V6(ipaddr)) ? ip6_addr_isloopback(ip_2_ip6(ipaddr)) :
75 ip4_addr_isloopback(ip_2_ip4(ipaddr)));
76 }
77
ip_addr_copy_fun(ip_addr_t * dest,ip_addr_t * src)78 void ip_addr_copy_fun(ip_addr_t *dest, ip_addr_t *src)
79 {
80 *dest = *src;
81 if (!IP_IS_V6_VAL(*src)) {
82 ip_clear_no4(dest);
83 }
84 }
85
86 #endif /* LWIP_SMALL_SIZE */
87 #endif /* LWIP_IPV4 && LWIP_IPV6 */