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 #ifndef OHOS_DHCP_MESSAGE_SIM_H
16 #define OHOS_DHCP_MESSAGE_SIM_H
17 
18 #include <queue>
19 #include <mutex>
20 #include "dhcp_s_define.h"
21 #include "dhcp_message.h"
22 
23 using DhcpClientConfig = struct DhcpClientConfig;
24 using DhcpClientContext = struct DhcpClientContext;
25 
26 namespace OHOS {
27 namespace DHCP {
28 class DhcpMsgManager {
29 public:
30     static DhcpMsgManager &GetInstance(void);
31 
32     int SendTotal();
33 
34     int RecvTotal();
35 
36     int PushSendMsg(const DhcpMessage &msg);
37     int PushRecvMsg(const DhcpMessage &msg);
38 
39     void PopSendMsg();
40 
41     bool FrontSendMsg(DhcpMessage *msg);
42     void PopRecvMsg();
43 
44     void SetClientIp(uint32_t ipAddr);
45     uint32_t GetClientIp() const;
46 private:
DhcpMsgManager()47     DhcpMsgManager() {};
~DhcpMsgManager()48     ~DhcpMsgManager() {};
49 
50     std::mutex m_recvQueueLocker;
51     std::queue<DhcpMessage> m_recvMessages;
52     std::mutex m_sendQueueLocker;
53     std::queue<DhcpMessage> m_sendMessages;
54     uint32_t m_clientIpAddress = 0;
55 };
56 }  // namespace DHCP
57 }  // namespace OHOS
58 
59 struct DhcpClientConfig {
60     char ifname[IFACE_NAME_SIZE];
61     uint8_t chaddr[DHCP_HWADDR_LENGTH];
62 };
63 
64 struct DhcpClientContext
65 {
66     int clientFd;
67     int state;
68     DhcpClientConfig config;
69 };
70 
71 DhcpClientContext *InitialDhcpClient(DhcpClientConfig *config);
72 
73 int *StatrDhcpClient(DhcpClientContext *config);
74 
75 int SendDhcpMessage(const DhcpClientContext *ctx, PDhcpMsgInfo msg);
76 
77 int DhcpDiscover(DhcpClientContext *ctx);
78 
79 int DhcpRequest(DhcpClientContext *ctx);
80 
81 int DhcpInform(DhcpClientContext *ctx);
82 
83 int DhcpDecline(DhcpClientContext *ctx);
84 
85 int DhcpRelease(DhcpClientContext *ctx);
86 
87 int StopDhcpClient(DhcpClientContext *ctx);
88 
89 int GetDhcpClinetState(DhcpClientContext *ctx);
90 
91 int FreeDhcpClient(DhcpClientContext *ctx);
92 int InitMessage(DhcpClientContext *ctx, PDhcpMsgInfo msg, uint8_t msgType);
93 int FillHwAddr(uint8_t *dst, size_t dsize, uint8_t *src, size_t ssize);
94 int ParseReceivedOptions(PDhcpMsgInfo msg);
95 
96 #endif