summaryrefslogtreecommitdiff
path: root/shared/n-dhcp4/src/n-dhcp4-server.c
blob: 7b971eeb23e4f55a2fc9e4a6c8bba011596ba303 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Server Side of the Dynamic Host Configuration Protocol for IPv4
 *
 * XXX
 */

#include <assert.h>
#include <c-list.h>
#include <c-stdaux.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include "n-dhcp4.h"
#include "n-dhcp4-private.h"
#include "util/packet.h"

/**
 * n_dhcp4_server_config_new() - XXX
 */
_c_public_ int n_dhcp4_server_config_new(NDhcp4ServerConfig **configp) {
        _c_cleanup_(n_dhcp4_server_config_freep) NDhcp4ServerConfig *config = NULL;

        config = calloc(1, sizeof(*config));
        if (!config)
                return -ENOMEM;

        *config = (NDhcp4ServerConfig)N_DHCP4_SERVER_CONFIG_NULL(*config);

        *configp = config;
        config = NULL;
        return 0;
}

/**
 * n_dhcp4_server_config_free() - XXX
 */
_c_public_ NDhcp4ServerConfig *n_dhcp4_server_config_free(NDhcp4ServerConfig *config) {
        if (!config)
                return NULL;

        free(config);

        return NULL;
}

/**
 * n_dhcp4_server_config_set_ifindex() - XXX
 */
_c_public_ void n_dhcp4_server_config_set_ifindex(NDhcp4ServerConfig *config, int ifindex) {
        config->ifindex = ifindex;
}

/**
 * n_dhcp4_s_event_node_new() - XXX
 */
int n_dhcp4_s_event_node_new(NDhcp4SEventNode **nodep) {
        NDhcp4SEventNode *node;

        node = calloc(1, sizeof(*node));
        if (!node)
                return -ENOMEM;

        *node = (NDhcp4SEventNode)N_DHCP4_S_EVENT_NODE_NULL(*node);

        *nodep = node;
        return 0;
}

/**
 * n_dhcp4_s_event_node_free() - XXX
 */
NDhcp4SEventNode *n_dhcp4_s_event_node_free(NDhcp4SEventNode *node) {
        if (!node)
                return NULL;

        c_list_unlink(&node->server_link);
        free(node);

        return NULL;
}

/**
 * n_dhcp4_server_new() - XXX
 */
_c_public_ int n_dhcp4_server_new(NDhcp4Server **serverp, NDhcp4ServerConfig *config) {
        _c_cleanup_(n_dhcp4_server_unrefp) NDhcp4Server *server = NULL;
        int r;

        c_assert(serverp);

        server = malloc(sizeof(*server));
        if (!server)
                return -ENOMEM;

        *server = (NDhcp4Server)N_DHCP4_SERVER_NULL(*server);

        r = n_dhcp4_s_connection_init(&server->connection, config->ifindex);
        if (r)
                return r;

        *serverp = server;
        server = NULL;
        return 0;
}

static void n_dhcp4_server_free(NDhcp4Server *server) {
        NDhcp4SEventNode *node, *t_node;

        c_list_for_each_entry_safe(node, t_node, &server->event_list, server_link)
                n_dhcp4_s_event_node_free(node);

        free(server);
}

/**
 * n_dhcp4_server_ref() - XXX
 */
_c_public_ NDhcp4Server *n_dhcp4_server_ref(NDhcp4Server *server) {
        if (server)
                ++server->n_refs;
        return server;
}

/**
 * n_dhcp4_server_unref() - XXX
 */
_c_public_ NDhcp4Server *n_dhcp4_server_unref(NDhcp4Server *server) {
        if (server && !--server->n_refs)
                n_dhcp4_server_free(server);
        return NULL;
}

/**
 * n_dhcp4_server_raise() - XXX
 */
int n_dhcp4_server_raise(NDhcp4Server *server, NDhcp4SEventNode **nodep, unsigned int event) {
        NDhcp4SEventNode *node;
        int r;

        r = n_dhcp4_s_event_node_new(&node);
        if (r)
                return r;

        node->event.event = event;
        c_list_link_tail(&server->event_list, &node->server_link);

        if (nodep)
                *nodep = node;
        return 0;
}

/**
 * n_dhcp4_server_get_fd() - XXX
 */
_c_public_ void n_dhcp4_server_get_fd(NDhcp4Server *server, int *fdp) {
        n_dhcp4_s_connection_get_fd(&server->connection, fdp);
}

/**
 * n_dhcp4_server_dispatch() - XXX
 */
_c_public_ int n_dhcp4_server_dispatch(NDhcp4Server *server) {
        int r;

        for (unsigned int i = 0; i < 128; ++i) {
                _c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;

                r = n_dhcp4_s_connection_dispatch_io(&server->connection, &message);
                if (r) {
                        if (r == N_DHCP4_E_AGAIN)
                                return 0;
                        return r;
                }
        }

        return N_DHCP4_E_PREEMPTED;
}

/**
 * n_dhcp4_server_pop_event() - XXX
 */
_c_public_ int n_dhcp4_server_pop_event(NDhcp4Server *server, NDhcp4ServerEvent **eventp) {
        NDhcp4SEventNode *node, *t_node;

        c_list_for_each_entry_safe(node, t_node, &server->event_list, server_link) {
                if (node->is_public) {
                        n_dhcp4_s_event_node_free(node);
                        continue;
                }

                node->is_public = true;
                *eventp = &node->event;
                return 0;
        }

        *eventp = NULL;
        return 0;
}

/**
 * n_dhcp4_server_add_ip() - XXX
 */
_c_public_ int n_dhcp4_server_add_ip(NDhcp4Server *server, NDhcp4ServerIp **ipp, struct in_addr addr) {
        _c_cleanup_(n_dhcp4_server_ip_freep) NDhcp4ServerIp *ip = NULL;

        /* XXX: support more than one address */
        if (server->connection.ip)
                return -EBUSY;

        ip = malloc(sizeof(*ip));
        if (!ip)
                return -ENOMEM;

        *ip = (NDhcp4ServerIp)N_DHCP4_SERVER_IP_NULL(*ip);

        n_dhcp4_s_connection_ip_init(&ip->ip, addr);
        n_dhcp4_s_connection_ip_link(&ip->ip, &server->connection);

        *ipp = ip;
        ip = NULL;
        return 0;
}

/**
 * n_dhcp4_server_ip_free() - XXX
 */
_c_public_ NDhcp4ServerIp *n_dhcp4_server_ip_free(NDhcp4ServerIp *ip) {
        if (!ip)
                return NULL;

        n_dhcp4_s_connection_ip_unlink(&ip->ip);
        n_dhcp4_s_connection_ip_deinit(&ip->ip);

        free(ip);
        return NULL;
}