• 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 "file_adapter.h"
17 
18 #include <securec.h>
19 #include <unistd.h>
20 
21 #include "softbus_adapter_errcode.h"
22 #include "softbus_adapter_socket.h"
23 #include "softbus_errcode.h"
24 #include "softbus_log.h"
25 #include "softbus_tcp_socket.h"
26 
27 
SetReuseAddr(int fd,int on)28 static int SetReuseAddr(int fd, int on)
29 {
30     int rc = SoftBusSocketSetOpt(fd, SOFTBUS_SOL_SOCKET, SOFTBUS_SO_REUSEADDR, &on, sizeof(on));
31     if (rc != SOFTBUS_OK) {
32         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "fd=%d set SO_REUSEADDR error", fd);
33         return SOFTBUS_ERR;
34     }
35     return SOFTBUS_OK;
36 }
37 
SetReusePort(int fd,int on)38 static int SetReusePort(int fd, int on)
39 {
40     int rc = SoftBusSocketSetOpt(fd, SOFTBUS_SOL_SOCKET, SOFTBUS_SO_REUSEPORT, &on, sizeof(on));
41     if (rc != 0) {
42         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "fd=%d set SO_REUSEPORT error", fd);
43         return SOFTBUS_ERR;
44     }
45     return SOFTBUS_OK;
46 }
47 
OpenTcpServer(const char * ip,int port)48 static int OpenTcpServer(const char *ip, int port)
49 {
50     SoftBusSockAddrIn addr;
51     (void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
52     int rc = SoftBusInetPtoN(SOFTBUS_AF_INET, ip, &addr.sinAddr);
53     if (rc != SOFTBUS_ADAPTER_OK) {
54         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "rc=%d", rc);
55         return SOFTBUS_ERR;
56     }
57     addr.sinFamily = SOFTBUS_AF_INET;
58     addr.sinPort = SoftBusHtoNs(port);
59     int fd;
60     int ret = SoftBusSocketCreate(SOFTBUS_AF_INET, SOFTBUS_SOCK_STREAM | SOFTBUS_SOCK_NONBLOCK |
61         SOFTBUS_SOCK_CLOEXEC, 0, &fd);
62     if (ret != SOFTBUS_OK) {
63         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenTcpServer Create error, ret=%d.", ret);
64         return SOFTBUS_ERR;
65     }
66 
67     (void)SetReuseAddr(fd, 1);
68     (void)SetReusePort(fd, 1);
69     rc = TEMP_FAILURE_RETRY(SoftBusSocketBind(fd, (SoftBusSockAddr *)&addr, sizeof(addr)));
70     if (rc != SOFTBUS_ADAPTER_OK) {
71         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenTcpServer Bind error");
72         TcpShutDown(fd);
73         return SOFTBUS_ERR;
74     }
75     return fd;
76 }
77 
StartNStackXDFileServer(const char * myIP,const uint8_t * key,uint32_t keyLen,DFileMsgReceiver msgReceiver,int32_t * filePort)78 int32_t StartNStackXDFileServer(const char *myIP, const uint8_t *key,
79     uint32_t keyLen, DFileMsgReceiver msgReceiver, int32_t *filePort)
80 {
81     if (myIP == NULL || filePort == NULL) {
82         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s:invalid param.", __func__);
83         return SOFTBUS_INVALID_PARAM;
84     }
85     int fd = OpenTcpServer(myIP, 0);
86     if (fd < 0) {
87         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "failed to start tcp server for getting port");
88         return SOFTBUS_ERR;
89     }
90     int port = GetTcpSockPort(fd);
91     if (port < 0) {
92         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "failed to get port from tcp socket");
93         TcpShutDown(fd);
94         return SOFTBUS_ERR;
95     }
96     *filePort = port;
97     struct sockaddr_in localAddr;
98     (void)memset_s(&localAddr, sizeof(localAddr), 0, sizeof(localAddr));
99     localAddr.sin_family = AF_INET;
100     localAddr.sin_port = (uint16_t)port;
101     localAddr.sin_addr.s_addr = SoftBusNtoHl(SoftBusInetAddr(myIP));
102     socklen_t addrLen = sizeof(struct sockaddr_in);
103 
104     int sessionId = NSTACKX_DFileServer(&localAddr, addrLen, key, keyLen, msgReceiver);
105     TcpShutDown(fd);
106     if (sessionId < 0) {
107         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "failed to start dfile server.");
108         return SOFTBUS_ERR;
109     }
110     return sessionId;
111 }
112 
StartNStackXDFileClient(const char * peerIp,int32_t peerPort,const uint8_t * key,uint32_t keyLen,DFileMsgReceiver msgReceiver)113 int32_t StartNStackXDFileClient(const char *peerIp, int32_t peerPort, const uint8_t *key,
114     uint32_t keyLen, DFileMsgReceiver msgReceiver)
115 {
116     if (peerIp == NULL) {
117         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param.");
118         return SOFTBUS_INVALID_PARAM;
119     }
120 
121     struct sockaddr_in localAddr;
122     (void)memset_s(&localAddr, sizeof(localAddr), 0, sizeof(localAddr));
123     localAddr.sin_family = AF_INET;
124     localAddr.sin_port = (uint16_t)peerPort;
125     localAddr.sin_addr.s_addr = SoftBusNtoHl(SoftBusInetAddr(peerIp));
126     socklen_t addrLen = sizeof(struct sockaddr_in);
127 
128     int32_t sessionId = NSTACKX_DFileClient(&localAddr, addrLen, key, keyLen, msgReceiver);
129     if (sessionId < 0) {
130         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "failed to start dfile client");
131         return SOFTBUS_ERR;
132     }
133     return sessionId;
134 }
135