• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "lease.h"
20 #include "result.h"
21 #include "socket.h"
22 
23 #include <netinet/in.h>
24 #include <stdint.h>
25 
26 #include <unordered_map>
27 #include <vector>
28 
29 class Message;
30 
31 class DhcpServer {
32 public:
33     // Construct a DHCP server. Ignore any requests and discoveries coming on
34     // the network interface identified by |excludeInterface|.
35     explicit DhcpServer(unsigned int excludeInterface);
36 
37     Result init();
38     Result run();
39 
40 private:
41     Result sendMessage(unsigned int interfaceIndex,
42                        in_addr_t sourceAddress,
43                        const Message& message);
44 
45     void sendDhcpOffer(const Message& message, unsigned int interfaceIndex);
46     void sendAck(const Message& message, unsigned int interfaceIndex);
47     void sendNack(const Message& message, unsigned int interfaceIndex);
48 
49     bool isValidDhcpRequest(const Message& message,
50                             unsigned int interfaceIndex);
51     void updateDnsServers();
52     Result getInterfaceData(unsigned int interfaceIndex,
53                             unsigned long type,
54                             struct ifreq* response);
55     Result getInterfaceAddress(unsigned int interfaceIndex,
56                                in_addr_t* address);
57     Result getInterfaceNetmask(unsigned int interfaceIndex,
58                                in_addr_t* netmask);
59     Result getOfferAddress(unsigned int interfaceIndex,
60                            const uint8_t* macAddress,
61                            in_addr_t* address,
62                            in_addr_t* netmask,
63                            in_addr_t* gateway);
64 
65     Socket mSocket;
66     // This is the next address offset. This will be added to whatever the base
67     // address of the DHCP address range is. For each new MAC address seen this
68     // value will increase by one.
69     std::vector<in_addr_t> mDnsServers;
70     // Map a lease to an IP address for that lease
71     std::unordered_map<Lease, in_addr_t> mLeases;
72     std::unordered_map<unsigned int, in_addr_t> mNextAddressOffsets;
73     unsigned int mExcludeInterface;
74 };
75 
76