| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-event.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "dhcp-protocol.h" |
| 7 |
#include "dhcp-relay-internal.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "in-addr-util.h" |
| 10 |
#include "iovec-util.h" |
| 11 |
#include "prioq.h" |
| 12 |
#include "tlv-util.h" |
| 13 |
|
| 14 |
static sd_dhcp_relay* dhcp_relay_free(sd_dhcp_relay *relay) { |
| 15 |
if (!relay) |
| 16 |
return NULL; |
| 17 |
|
| 18 |
assert(hashmap_isempty(relay->interfaces)); |
| 19 |
hashmap_free(relay->interfaces); |
| 20 |
assert(hashmap_isempty(relay->downstream_interfaces)); |
| 21 |
hashmap_free(relay->downstream_interfaces); |
| 22 |
assert(prioq_isempty(relay->upstream_interfaces)); |
| 23 |
prioq_free(relay->upstream_interfaces); |
| 24 |
|
| 25 |
sd_event_unref(relay->event); |
| 26 |
|
| 27 |
iovec_done(&relay->remote_id); |
| 28 |
tlv_unref(relay->extra_options); |
| 29 |
return mfree(relay); |
| 30 |
} |
| 31 |
|
| 32 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_relay, sd_dhcp_relay, dhcp_relay_free); |
| 33 |
|
| 34 |
int sd_dhcp_relay_new(sd_dhcp_relay **ret) { |
| 35 |
assert_return(ret, -EINVAL); |
| 36 |
|
| 37 |
sd_dhcp_relay *relay = new(sd_dhcp_relay, 1); |
| 38 |
if (!relay) |
| 39 |
return -ENOMEM; |
| 40 |
|
| 41 |
*relay = (sd_dhcp_relay) { |
| 42 |
.n_ref = 1, |
| 43 |
.server_port = DHCP_PORT_SERVER, |
| 44 |
}; |
| 45 |
|
| 46 |
*ret = TAKE_PTR(relay); |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
int sd_dhcp_relay_is_running(sd_dhcp_relay *relay) { |
| 51 |
if (!relay) |
| 52 |
return false; |
| 53 |
|
| 54 |
return |
| 55 |
!prioq_isempty(relay->upstream_interfaces) || |
| 56 |
!hashmap_isempty(relay->downstream_interfaces); |
| 57 |
} |
| 58 |
|
| 59 |
int sd_dhcp_relay_attach_event(sd_dhcp_relay *relay, sd_event *event, int64_t priority) { |
| 60 |
int r; |
| 61 |
|
| 62 |
assert_return(relay, -EINVAL); |
| 63 |
assert_return(!relay->event, -EBUSY); |
| 64 |
|
| 65 |
if (event) |
| 66 |
relay->event = sd_event_ref(event); |
| 67 |
else { |
| 68 |
r = sd_event_default(&relay->event); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
} |
| 72 |
|
| 73 |
relay->event_priority = priority; |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int sd_dhcp_relay_detach_event(sd_dhcp_relay *relay) { |
| 78 |
assert_return(relay, -EINVAL); |
| 79 |
assert_return(!sd_dhcp_relay_is_running(relay), -EBUSY); |
| 80 |
|
| 81 |
relay->event = sd_event_unref(relay->event); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
sd_event* sd_dhcp_relay_get_event(sd_dhcp_relay *relay) { |
| 86 |
assert_return(relay, NULL); |
| 87 |
|
| 88 |
return relay->event; |
| 89 |
} |
| 90 |
|
| 91 |
int sd_dhcp_relay_set_server_address(sd_dhcp_relay *relay, const struct in_addr *address) { |
| 92 |
assert_return(relay, -EINVAL); |
| 93 |
assert_return(!sd_dhcp_relay_is_running(relay), -EBUSY); |
| 94 |
|
| 95 |
if (address) |
| 96 |
relay->server_address = *address; |
| 97 |
else |
| 98 |
relay->server_address = (struct in_addr) {}; |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
int sd_dhcp_relay_get_server_address(sd_dhcp_relay *relay, struct in_addr *ret) { |
| 104 |
assert_return(relay, -EINVAL); |
| 105 |
|
| 106 |
if (ret) |
| 107 |
*ret = relay->server_address; |
| 108 |
|
| 109 |
return in4_addr_is_set(&relay->server_address); |
| 110 |
} |
| 111 |
|
| 112 |
int sd_dhcp_relay_set_server_port(sd_dhcp_relay *relay, uint16_t port) { |
| 113 |
assert_return(relay, -EINVAL); |
| 114 |
assert_return(!sd_dhcp_relay_is_running(relay), -EBUSY); |
| 115 |
|
| 116 |
relay->server_port = port; |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
int sd_dhcp_relay_set_remote_id(sd_dhcp_relay *relay, const struct iovec *iov) { |
| 121 |
assert_return(relay, -EINVAL); |
| 122 |
|
| 123 |
return iovec_done_and_memdup(&relay->remote_id, iov); |
| 124 |
} |
| 125 |
|
| 126 |
int sd_dhcp_relay_set_server_identifier_override(sd_dhcp_relay *relay, int b) { |
| 127 |
assert_return(relay, -EINVAL); |
| 128 |
|
| 129 |
relay->server_identifier_override = !!b; |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
int dhcp_relay_set_extra_options(sd_dhcp_relay *relay, TLV *options) { |
| 134 |
assert(relay); |
| 135 |
|
| 136 |
return unref_and_replace_new_ref(relay->extra_options, options, tlv_ref, tlv_unref); |
| 137 |
} |