| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "log.h" |
| 5 |
#include "string-table.h" |
| 6 |
#include "timesyncd-manager.h" |
| 7 |
#include "timesyncd-server.h" |
| 8 |
|
| 9 |
static const char * const server_type_table[_SERVER_TYPE_MAX] = { |
| 10 |
[SERVER_SYSTEM] = "system", |
| 11 |
[SERVER_FALLBACK] = "fallback", |
| 12 |
[SERVER_LINK] = "link", |
| 13 |
[SERVER_RUNTIME] = "runtime", |
| 14 |
}; |
| 15 |
|
| 16 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(server_type, ServerType); |
| 17 |
|
| 18 |
int server_address_new( |
| 19 |
ServerName *n, |
| 20 |
ServerAddress **ret, |
| 21 |
const union sockaddr_union *sockaddr, |
| 22 |
socklen_t socklen) { |
| 23 |
|
| 24 |
ServerAddress *a, *tail; |
| 25 |
|
| 26 |
assert(n); |
| 27 |
assert(sockaddr); |
| 28 |
assert(socklen >= offsetof(struct sockaddr, sa_data)); |
| 29 |
assert(socklen <= sizeof(union sockaddr_union)); |
| 30 |
|
| 31 |
a = new(ServerAddress, 1); |
| 32 |
if (!a) |
| 33 |
return -ENOMEM; |
| 34 |
|
| 35 |
*a = (ServerAddress) { |
| 36 |
.name = n, |
| 37 |
.socklen = socklen, |
| 38 |
}; |
| 39 |
|
| 40 |
memcpy(&a->sockaddr, sockaddr, socklen); |
| 41 |
|
| 42 |
tail = LIST_FIND_TAIL(addresses, n->addresses); |
| 43 |
LIST_INSERT_AFTER(addresses, n->addresses, tail, a); |
| 44 |
|
| 45 |
if (ret) |
| 46 |
*ret = a; |
| 47 |
|
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
ServerAddress* server_address_free(ServerAddress *a) { |
| 52 |
if (!a) |
| 53 |
return NULL; |
| 54 |
|
| 55 |
if (a->name) { |
| 56 |
LIST_REMOVE(addresses, a->name->addresses, a); |
| 57 |
|
| 58 |
if (a->name->manager && a->name->manager->current_server_address == a) |
| 59 |
manager_set_server_address(a->name->manager, NULL); |
| 60 |
} |
| 61 |
|
| 62 |
return mfree(a); |
| 63 |
} |
| 64 |
|
| 65 |
static int enable_ntp_server_defer_event(Manager *m, ServerType type) { |
| 66 |
int r; |
| 67 |
|
| 68 |
assert(m); |
| 69 |
assert((type >= 0) && (type < _SERVER_TYPE_MAX)); |
| 70 |
|
| 71 |
m->ntp_server_change_mask |= 1U << type; |
| 72 |
|
| 73 |
r = bus_manager_emit_ntp_server_changed(m); |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
int server_name_new( |
| 81 |
Manager *m, |
| 82 |
ServerName **ret, |
| 83 |
ServerType type, |
| 84 |
const char *string) { |
| 85 |
int r; |
| 86 |
ServerName *n; |
| 87 |
|
| 88 |
assert(m); |
| 89 |
assert(string); |
| 90 |
|
| 91 |
n = new(ServerName, 1); |
| 92 |
if (!n) |
| 93 |
return -ENOMEM; |
| 94 |
|
| 95 |
*n = (ServerName) { |
| 96 |
.manager = m, |
| 97 |
.type = type, |
| 98 |
.string = strdup(string), |
| 99 |
}; |
| 100 |
|
| 101 |
if (!n->string) { |
| 102 |
free(n); |
| 103 |
return -ENOMEM; |
| 104 |
} |
| 105 |
|
| 106 |
switch (type) { |
| 107 |
case SERVER_SYSTEM: |
| 108 |
LIST_APPEND(names, m->system_servers, n); |
| 109 |
break; |
| 110 |
case SERVER_LINK: |
| 111 |
LIST_APPEND(names, m->link_servers, n); |
| 112 |
break; |
| 113 |
case SERVER_FALLBACK: |
| 114 |
LIST_APPEND(names, m->fallback_servers, n); |
| 115 |
break; |
| 116 |
case SERVER_RUNTIME: |
| 117 |
LIST_APPEND(names, m->runtime_servers, n); |
| 118 |
break; |
| 119 |
default: |
| 120 |
assert_not_reached(); |
| 121 |
} |
| 122 |
|
| 123 |
r = enable_ntp_server_defer_event(m, type); |
| 124 |
if (r < 0) |
| 125 |
log_debug_errno(r, "Failed to enable ntp server defer event, ignoring: %m"); |
| 126 |
|
| 127 |
if (type != SERVER_FALLBACK && |
| 128 |
m->current_server_name && |
| 129 |
m->current_server_name->type == SERVER_FALLBACK) |
| 130 |
manager_set_server_name(m, NULL); |
| 131 |
|
| 132 |
log_debug("Added new %s server %s.", server_type_to_string(type), string); |
| 133 |
|
| 134 |
if (ret) |
| 135 |
*ret = n; |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
ServerName *server_name_free(ServerName *n) { |
| 141 |
int r; |
| 142 |
|
| 143 |
if (!n) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
server_name_flush_addresses(n); |
| 147 |
|
| 148 |
if (n->manager) { |
| 149 |
if (n->type == SERVER_SYSTEM) |
| 150 |
LIST_REMOVE(names, n->manager->system_servers, n); |
| 151 |
else if (n->type == SERVER_LINK) |
| 152 |
LIST_REMOVE(names, n->manager->link_servers, n); |
| 153 |
else if (n->type == SERVER_FALLBACK) |
| 154 |
LIST_REMOVE(names, n->manager->fallback_servers, n); |
| 155 |
else if (n->type == SERVER_RUNTIME) |
| 156 |
LIST_REMOVE(names, n->manager->runtime_servers, n); |
| 157 |
else |
| 158 |
assert_not_reached(); |
| 159 |
|
| 160 |
r = enable_ntp_server_defer_event(n->manager, n->type); |
| 161 |
if (r < 0) |
| 162 |
log_debug_errno(r, "Failed to enable ntp server defer event, ignoring: %m"); |
| 163 |
|
| 164 |
if (n->manager->current_server_name == n) |
| 165 |
manager_set_server_name(n->manager, NULL); |
| 166 |
} |
| 167 |
|
| 168 |
log_debug("Removed server %s.", n->string); |
| 169 |
|
| 170 |
free(n->string); |
| 171 |
return mfree(n); |
| 172 |
} |
| 173 |
|
| 174 |
void server_name_flush_addresses(ServerName *n) { |
| 175 |
assert(n); |
| 176 |
|
| 177 |
while (n->addresses) |
| 178 |
server_address_free(n->addresses); |
| 179 |
} |