• 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 with the given parameters. Ignore any requests
34     // and discoveries coming on the network interface identified by
35     // |excludeInterface|.
36     DhcpServer(in_addr_t dhcpRangeStart,
37                in_addr_t dhcpRangeEnd,
38                in_addr_t netmask,
39                in_addr_t gateway,
40                unsigned int excludeInterface);
41 
42     Result init();
43     Result run();
44 
45 private:
46     Result sendMessage(unsigned int interfaceIndex,
47                        in_addr_t sourceAddress,
48                        const Message& message);
49 
50     void sendDhcpOffer(const Message& message, unsigned int interfaceIndex);
51     void sendAck(const Message& message, unsigned int interfaceIndex);
52     void sendNack(const Message& message, unsigned int interfaceIndex);
53 
54     bool isValidDhcpRequest(const Message& message,
55                             unsigned int interfaceIndex);
56     void updateDnsServers();
57     Result getInterfaceAddress(unsigned int interfaceIndex,
58                                in_addr_t* address);
59     Result getOfferAddress(unsigned int interfaceIndex,
60                            const uint8_t* macAddress,
61                            in_addr_t* address);
62 
63     Socket mSocket;
64     // This is the next address offset. This will be added to whatever the base
65     // address of the DHCP address range is. For each new MAC address seen this
66     // value will increase by one.
67     in_addr_t mNextAddressOffset;
68     in_addr_t mDhcpRangeStart;
69     in_addr_t mDhcpRangeEnd;
70     in_addr_t mNetmask;
71     in_addr_t mGateway;
72     std::vector<in_addr_t> mDnsServers;
73     // Map a lease to an IP address for that lease
74     std::unordered_map<Lease, in_addr_t> mLeases;
75     unsigned int mExcludeInterface;
76 };
77 
78