chiark / gitweb /
885d68d33594ad960b01bb72f4d74d705cca6ed6
[elogind.git] / src / libsystemd-network / sd-dhcp-server.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2013 Intel Corporation. All rights reserved.
7   Copyright (C) 2014 Tom Gundersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include "sd-dhcp-server.h"
24 #include "dhcp-server-internal.h"
25
26 sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) {
27         if (server)
28                 assert_se(REFCNT_INC(server->n_ref) >= 2);
29
30         return server;
31 }
32
33 sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) {
34         if (server && REFCNT_DEC(server->n_ref) <= 0) {
35                 log_dhcp_server(server, "UNREF");
36
37                 sd_event_unref(server->event);
38                 free(server);
39         }
40
41         return NULL;
42 }
43
44 int sd_dhcp_server_new(sd_dhcp_server **ret) {
45         _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
46
47         assert_return(ret, -EINVAL);
48
49         server = new0(sd_dhcp_server, 1);
50         if (!server)
51                 return -ENOMEM;
52
53         server->n_ref = REFCNT_INIT;
54
55         *ret = server;
56         server = NULL;
57
58         return 0;
59 }
60
61 int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int priority) {
62         int r;
63
64         assert_return(server, -EINVAL);
65         assert_return(!server->event, -EBUSY);
66
67         if (event)
68                 server->event = sd_event_ref(event);
69         else {
70                 r = sd_event_default(&server->event);
71                 if (r < 0)
72                         return r;
73         }
74
75         server->event_priority = priority;
76
77         return 0;
78 }
79
80 int sd_dhcp_server_detach_event(sd_dhcp_server *server) {
81         assert_return(server, -EINVAL);
82
83         server->event = sd_event_unref(server->event);
84
85         return 0;
86 }
87
88 sd_event *sd_dhcp_server_get_event(sd_dhcp_server *server) {
89         assert_return(server, NULL);
90
91         return server->event;
92 }