• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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  */
15 
16 #include "wifi_direct_ipv4_info.h"
17 #include "softbus_log.h"
18 #include "softbus_error_code.h"
19 #include "wifi_direct_defines.h"
20 #include "wifi_direct_network_utils.h"
21 
22 #define LOG_LABEL "[WifiDirect] WifiDirectIpv4: "
23 
WifiDirectIpStringToIpv4(const char * ipString,struct WifiDirectIpv4Info * ipv4)24 int32_t WifiDirectIpStringToIpv4(const char *ipString, struct WifiDirectIpv4Info *ipv4)
25 {
26     CONN_CHECK_AND_RETURN_RET_LOG(ipString, SOFTBUS_INVALID_PARAM, LOG_LABEL "ip is null");
27     CONN_CHECK_AND_RETURN_RET_LOG(ipv4, SOFTBUS_INVALID_PARAM, LOG_LABEL "ipv4 is null");
28 
29     struct WifiDirectNetWorkUtils *netWorkUtils = GetWifiDirectNetWorkUtils();
30     int32_t ret = netWorkUtils->ipStringToAddr(ipString, &ipv4->address);
31     CONN_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, SOFTBUS_ERR, LOG_LABEL "convert ip to addr failed");
32     ipv4->prefixLength = DEFAULT_PREFIX_LEN;
33     return SOFTBUS_OK;
34 }
35 
WifiDirectIpv4ToString(struct WifiDirectIpv4Info * ipv4,char * ipString,size_t ipStringSize)36 int32_t WifiDirectIpv4ToString(struct WifiDirectIpv4Info *ipv4, char *ipString, size_t ipStringSize)
37 {
38     CONN_CHECK_AND_RETURN_RET_LOG(ipv4, SOFTBUS_INVALID_PARAM, LOG_LABEL "ipv4 is null");
39     CONN_CHECK_AND_RETURN_RET_LOG(ipString, SOFTBUS_INVALID_PARAM, LOG_LABEL "ip is null");
40 
41     struct WifiDirectNetWorkUtils *netWorkUtils = GetWifiDirectNetWorkUtils();
42     int32_t ret = netWorkUtils->ipAddrToString(ipv4->address, ipString, ipStringSize);
43     CONN_CHECK_AND_RETURN_RET_LOG(ret == SOFTBUS_OK, SOFTBUS_ERR, LOG_LABEL "convert addr to ip failed");
44 
45     return SOFTBUS_OK;
46 }
47 
48 #define IPV4_INFO_TO_ARRAY(array, ipv4Info) do { \
49     (array)[0] = (uint8_t)(((ipv4Info)->address & 0xff000000) >> 24); \
50     (array)[1] = (uint8_t)(((ipv4Info)->address & 0x00ff0000) >> 16); \
51     (array)[2] = (uint8_t)(((ipv4Info)->address & 0x0000ff00) >> 8); \
52     (array)[3] = (uint8_t)(((ipv4Info)->address & 0x000000ff)); \
53     (array)[4] = (ipv4Info)->prefixLength; \
54 } while (0)
55 
56 #define IPV4_ARRAY_TO_INFO(ipv4Info, array) do { \
57     (ipv4Info)->address = ((uint32_t)((array)[0]) << 24) | \
58         ((uint32_t)((array)[1]) << 16) | \
59         ((uint32_t)((array)[2]) << 8) | \
60         ((uint32_t)((array)[3])); \
61     (ipv4Info)->prefixLength = (array)[4]; \
62 } while (0)
63 
WifiDirectIpv4InfoToBytes(const struct WifiDirectIpv4Info * ipv4,size_t ipv4Count,uint8_t * data,size_t * dataLen)64 int32_t WifiDirectIpv4InfoToBytes(const struct WifiDirectIpv4Info *ipv4, size_t ipv4Count,
65                                   uint8_t *data, size_t *dataLen)
66 {
67     size_t offset = 0;
68     for (size_t i = 0; i < ipv4Count; i++) {
69         if (offset + IPV4_INFO_BYTES_ARRAY_LEN > *dataLen) {
70             CLOGE(LOG_LABEL "[%zu] invalid data len: %zu, ipv4 count: %zu, offset: %zu",
71                   i, *dataLen, ipv4Count, offset);
72             return SOFTBUS_ERR;
73         }
74 
75         IPV4_INFO_TO_ARRAY(&data[offset], &ipv4[i]);
76         offset += IPV4_INFO_BYTES_ARRAY_LEN;
77     }
78 
79     *dataLen = offset;
80     return SOFTBUS_OK;
81 }
82 
WifiDirectIpv4BytesToInfo(const uint8_t * ipv4Bytes,size_t ipv4BytesLen,struct WifiDirectIpv4Info * ipv4,size_t * ipv4Count)83 void WifiDirectIpv4BytesToInfo(const uint8_t *ipv4Bytes, size_t ipv4BytesLen,
84                                struct WifiDirectIpv4Info *ipv4, size_t *ipv4Count)
85 {
86     size_t offset = 0;
87     if ((ipv4BytesLen % IPV4_INFO_BYTES_ARRAY_LEN) != 0) {
88         CLOGE(LOG_LABEL "invalid ip bytes len %zu", ipv4BytesLen);
89         *ipv4Count = 0;
90         return;
91     }
92 
93     for (size_t i = 0; i + IPV4_INFO_BYTES_ARRAY_LEN <= ipv4BytesLen; i += IPV4_INFO_BYTES_ARRAY_LEN) {
94         if (offset == *ipv4Count) {
95             CLOGE(LOG_LABEL "invalid ipv4 count: %zu, ipv4 bytes len: %zu", *ipv4Count, ipv4BytesLen);
96             *ipv4Count = 0;
97             return;
98         }
99 
100         IPV4_ARRAY_TO_INFO(&ipv4[offset], &ipv4Bytes[i]);
101         offset++;
102     }
103 
104     *ipv4Count = offset;
105 }