1/*
2 * Copyright (c) 2024 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#ifndef NETSYS_CLATD_H
16#define NETSYS_CLATD_H
17
18#include <netinet/in.h>
19#include <string>
20
21#include "clatd_packet_converter.h"
22#include "ffrt.h"
23
24namespace OHOS {
25namespace nmd {
26using namespace OHOS::NetManagerStandard;
27class Clatd {
28public:
29    Clatd(){};
30    Clatd(int tunFd, int readSock6, int writeSock6, const std::string &v6Iface, const std::string &prefixAddrStr,
31          const std::string &v4AddrStr, const std::string &v6AddrStr);
32
33    ~Clatd();
34    Clatd(const Clatd &clatd) = delete;
35    Clatd &operator=(const Clatd &clatd) = delete;
36
37    void Start();
38
39    void Stop();
40
41private:
42    void SendDadPacket();
43    void RunLoop();
44    int32_t MaybeCalculateL4Checksum(int packetLen, ClatdReadV6Buf &readBuf);
45    void ProcessV6Packet();
46    void ProcessV4Packet();
47    int32_t ReadV6Packet(msghdr &msgHdr, ssize_t &readLen);
48    int32_t ReadV4Packet(ClatdReadTunBuf &readBuf, ssize_t &readLen);
49    void SendV6OnRawSocket(int fd, std::vector<iovec> &iovPackets, int effectivePos);
50
51    int tunFd_;
52    int readSock6_;
53    int writeSock6_;
54    int stopFd_;
55    std::string v6Iface_;
56    std::string tunIface_;
57    in6_addr v6Addr_;
58    in_addr v4Addr_;
59    in6_addr prefixAddr_;
60    bool isSocketClosed_;
61    ffrt::mutex mutex_;
62    ffrt::condition_variable cv_;
63    std::atomic<bool> stopStatus_;
64};
65} // namespace nmd
66} // namespace OHOS
67
68#endif