summaryrefslogtreecommitdiffstats
path: root/dhcp/server/dhcpserver.h
blob: 276cd5b1310cb6c313bd4a8f6c41a7733015f70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "lease.h"
#include "result.h"
#include "socket.h"

#include <netinet/in.h>
#include <stdint.h>

#include <unordered_map>
#include <vector>

class Message;

class DhcpServer {
public:
    // Construct a DHCP server. Ignore any requests and discoveries coming on
    // the network interface identified by |excludeInterface|.
    explicit DhcpServer(unsigned int excludeInterface);

    Result init();
    Result run();

private:
    Result sendMessage(unsigned int interfaceIndex,
                       in_addr_t sourceAddress,
                       const Message& message);

    void sendDhcpOffer(const Message& message, unsigned int interfaceIndex);
    void sendAck(const Message& message, unsigned int interfaceIndex);
    void sendNack(const Message& message, unsigned int interfaceIndex);

    bool isValidDhcpRequest(const Message& message,
                            unsigned int interfaceIndex);
    void updateDnsServers();
    Result getInterfaceData(unsigned int interfaceIndex,
                            unsigned long type,
                            struct ifreq* response);
    Result getInterfaceAddress(unsigned int interfaceIndex,
                               in_addr_t* address);
    Result getInterfaceNetmask(unsigned int interfaceIndex,
                               in_addr_t* netmask);
    Result getOfferAddress(unsigned int interfaceIndex,
                           const uint8_t* macAddress,
                           in_addr_t* address,
                           in_addr_t* netmask,
                           in_addr_t* gateway);

    Socket mSocket;
    // This is the next address offset. This will be added to whatever the base
    // address of the DHCP address range is. For each new MAC address seen this
    // value will increase by one.
    std::vector<in_addr_t> mDnsServers;
    // Map a lease to an IP address for that lease
    std::unordered_map<Lease, in_addr_t> mLeases;
    std::unordered_map<unsigned int, in_addr_t> mNextAddressOffsets;
    unsigned int mExcludeInterface;
};