1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci#ifndef NETSYS_CLATD_H
16b1b8bc3fSopenharmony_ci#define NETSYS_CLATD_H
17b1b8bc3fSopenharmony_ci
18b1b8bc3fSopenharmony_ci#include <netinet/in.h>
19b1b8bc3fSopenharmony_ci#include <string>
20b1b8bc3fSopenharmony_ci
21b1b8bc3fSopenharmony_ci#include "clatd_packet_converter.h"
22b1b8bc3fSopenharmony_ci#include "ffrt.h"
23b1b8bc3fSopenharmony_ci
24b1b8bc3fSopenharmony_cinamespace OHOS {
25b1b8bc3fSopenharmony_cinamespace nmd {
26b1b8bc3fSopenharmony_ciusing namespace OHOS::NetManagerStandard;
27b1b8bc3fSopenharmony_ciclass Clatd {
28b1b8bc3fSopenharmony_cipublic:
29b1b8bc3fSopenharmony_ci    Clatd(){};
30b1b8bc3fSopenharmony_ci    Clatd(int tunFd, int readSock6, int writeSock6, const std::string &v6Iface, const std::string &prefixAddrStr,
31b1b8bc3fSopenharmony_ci          const std::string &v4AddrStr, const std::string &v6AddrStr);
32b1b8bc3fSopenharmony_ci
33b1b8bc3fSopenharmony_ci    ~Clatd();
34b1b8bc3fSopenharmony_ci    Clatd(const Clatd &clatd) = delete;
35b1b8bc3fSopenharmony_ci    Clatd &operator=(const Clatd &clatd) = delete;
36b1b8bc3fSopenharmony_ci
37b1b8bc3fSopenharmony_ci    void Start();
38b1b8bc3fSopenharmony_ci
39b1b8bc3fSopenharmony_ci    void Stop();
40b1b8bc3fSopenharmony_ci
41b1b8bc3fSopenharmony_ciprivate:
42b1b8bc3fSopenharmony_ci    void SendDadPacket();
43b1b8bc3fSopenharmony_ci    void RunLoop();
44b1b8bc3fSopenharmony_ci    int32_t MaybeCalculateL4Checksum(int packetLen, ClatdReadV6Buf &readBuf);
45b1b8bc3fSopenharmony_ci    void ProcessV6Packet();
46b1b8bc3fSopenharmony_ci    void ProcessV4Packet();
47b1b8bc3fSopenharmony_ci    int32_t ReadV6Packet(msghdr &msgHdr, ssize_t &readLen);
48b1b8bc3fSopenharmony_ci    int32_t ReadV4Packet(ClatdReadTunBuf &readBuf, ssize_t &readLen);
49b1b8bc3fSopenharmony_ci    void SendV6OnRawSocket(int fd, std::vector<iovec> &iovPackets, int effectivePos);
50b1b8bc3fSopenharmony_ci
51b1b8bc3fSopenharmony_ci    int tunFd_;
52b1b8bc3fSopenharmony_ci    int readSock6_;
53b1b8bc3fSopenharmony_ci    int writeSock6_;
54b1b8bc3fSopenharmony_ci    int stopFd_;
55b1b8bc3fSopenharmony_ci    std::string v6Iface_;
56b1b8bc3fSopenharmony_ci    std::string tunIface_;
57b1b8bc3fSopenharmony_ci    in6_addr v6Addr_;
58b1b8bc3fSopenharmony_ci    in_addr v4Addr_;
59b1b8bc3fSopenharmony_ci    in6_addr prefixAddr_;
60b1b8bc3fSopenharmony_ci    bool isSocketClosed_;
61b1b8bc3fSopenharmony_ci    ffrt::mutex mutex_;
62b1b8bc3fSopenharmony_ci    ffrt::condition_variable cv_;
63b1b8bc3fSopenharmony_ci    std::atomic<bool> stopStatus_;
64b1b8bc3fSopenharmony_ci};
65b1b8bc3fSopenharmony_ci} // namespace nmd
66b1b8bc3fSopenharmony_ci} // namespace OHOS
67b1b8bc3fSopenharmony_ci
68b1b8bc3fSopenharmony_ci#endif