• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "lnn_connection_addr_utils.h"
17 
18 #include <securec.h>
19 #include <string.h>
20 
21 #include "softbus_log.h"
22 
LnnIsSameConnectionAddr(const ConnectionAddr * addr1,const ConnectionAddr * addr2)23 bool LnnIsSameConnectionAddr(const ConnectionAddr *addr1, const ConnectionAddr *addr2)
24 {
25     if (addr1 == NULL || addr2 == NULL) {
26         return false;
27     }
28     if (addr1->type != addr2->type) {
29         return false;
30     }
31     if (addr1->type == CONNECTION_ADDR_BR) {
32         return strncmp(addr1->info.br.brMac, addr2->info.br.brMac, BT_MAC_LEN) == 0;
33     }
34     if (addr1->type == CONNECTION_ADDR_BLE) {
35         return memcmp(addr1->info.ble.udidHash, addr2->info.ble.udidHash, UDID_HASH_LEN) == 0 &&
36         strncmp(addr1->info.ble.bleMac, addr2->info.ble.bleMac, BT_MAC_LEN) == 0;
37     }
38     if (addr1->type == CONNECTION_ADDR_WLAN || addr1->type == CONNECTION_ADDR_ETH) {
39         return (strncmp(addr1->info.ip.ip, addr2->info.ip.ip, strlen(addr1->info.ip.ip)) == 0) &&
40         (addr1->info.ip.port == addr2->info.ip.port);
41     }
42     return false;
43 }
44 
LnnConvertAddrToOption(const ConnectionAddr * addr,ConnectOption * option)45 bool LnnConvertAddrToOption(const ConnectionAddr *addr, ConnectOption *option)
46 {
47     if (addr == NULL || option == NULL) {
48         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "addr or option is null");
49         return false;
50     }
51     if (addr->type == CONNECTION_ADDR_BR) {
52         option->type = CONNECT_BR;
53         if (strncpy_s(option->info.brOption.brMac, BT_MAC_LEN, addr->info.br.brMac,
54             strlen(addr->info.br.brMac)) != EOK) {
55             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy br mac to addr fail");
56             return false;
57         }
58         return true;
59     }
60     if (addr->type == CONNECTION_ADDR_BLE) {
61         option->type = CONNECT_BLE;
62         if (strncpy_s(option->info.bleOption.bleMac, BT_MAC_LEN, addr->info.ble.bleMac,
63             strlen(addr->info.ble.bleMac)) != EOK) {
64             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy ble mac to addr fail");
65             return false;
66         }
67         return true;
68     }
69     if (addr->type == CONNECTION_ADDR_ETH || addr->type == CONNECTION_ADDR_WLAN) {
70         option->type = CONNECT_TCP;
71         if (strncpy_s(option->info.ipOption.ip, IP_LEN, addr->info.ip.ip,
72             strlen(addr->info.ip.ip)) != EOK) {
73             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy ip  to addr fail");
74             return false;
75         }
76         option->info.ipOption.port = addr->info.ip.port;
77         return true;
78     }
79     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "not supported type: %d", addr->type);
80     return false;
81 }
82 
LnnConvertOptionToAddr(ConnectionAddr * addr,const ConnectOption * option,ConnectionAddrType hintType)83 bool LnnConvertOptionToAddr(ConnectionAddr *addr, const ConnectOption *option, ConnectionAddrType hintType)
84 {
85     if (addr == NULL || option == NULL) {
86         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "addr or option is null");
87         return false;
88     }
89     if (option->type == CONNECT_BR) {
90         addr->type = CONNECTION_ADDR_BR;
91         if (strncpy_s(addr->info.br.brMac, BT_MAC_LEN, option->info.brOption.brMac,
92             strlen(option->info.brOption.brMac)) != EOK) {
93             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy br mac to addr fail");
94             return false;
95         }
96         return true;
97     }
98     if (option->type == CONNECT_BLE) {
99         addr->type = CONNECTION_ADDR_BLE;
100         if (strncpy_s(addr->info.ble.bleMac, BT_MAC_LEN, option->info.bleOption.bleMac,
101             strlen(option->info.bleOption.bleMac)) != EOK) {
102             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy ble mac to addr fail");
103             return false;
104         }
105         return true;
106     }
107     if (option->type == CONNECT_TCP) {
108         addr->type = hintType;
109         if (strncpy_s(addr->info.ip.ip, IP_LEN, option->info.ipOption.ip,
110             strlen(option->info.ipOption.ip)) != EOK) {
111             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy ip to addr fail");
112             return false;
113         }
114         addr->info.ip.port = (uint16_t)option->info.ipOption.port;
115         return true;
116     }
117     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "not supported type: %d", option->type);
118     return false;
119 }
120 
LnnGetDiscoveryType(ConnectionAddrType type)121 DiscoveryType LnnGetDiscoveryType(ConnectionAddrType type)
122 {
123     if (type == CONNECTION_ADDR_WLAN || type == CONNECTION_ADDR_ETH) {
124         return DISCOVERY_TYPE_WIFI;
125     } else if (type == CONNECTION_ADDR_BR) {
126         return DISCOVERY_TYPE_BR;
127     } else if (type == CONNECTION_ADDR_BLE) {
128         return DISCOVERY_TYPE_BLE;
129     } else {
130         return DISCOVERY_TYPE_COUNT;
131     }
132 }
133