]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timesync/timesyncd-server.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / timesync / timesyncd-server.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
84e51726
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2014 Kay Sievers, Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
b5efdb8a 21#include "alloc-util.h"
84e51726 22#include "timesyncd-server.h"
874ff7bf
LP
23
24int server_address_new(
25 ServerName *n,
26 ServerAddress **ret,
27 const union sockaddr_union *sockaddr,
28 socklen_t socklen) {
29
30 ServerAddress *a, *tail;
31
32 assert(n);
33 assert(sockaddr);
34 assert(socklen >= offsetof(struct sockaddr, sa_data));
35 assert(socklen <= sizeof(union sockaddr_union));
36
37 a = new0(ServerAddress, 1);
38 if (!a)
39 return -ENOMEM;
40
41 memcpy(&a->sockaddr, sockaddr, socklen);
42 a->socklen = socklen;
43
44 LIST_FIND_TAIL(addresses, n->addresses, tail);
45 LIST_INSERT_AFTER(addresses, n->addresses, tail, a);
46 a->name = n;
47
48 if (ret)
49 *ret = a;
50
51 return 0;
52}
53
54ServerAddress* server_address_free(ServerAddress *a) {
55 if (!a)
56 return NULL;
57
58 if (a->name) {
59 LIST_REMOVE(addresses, a->name->addresses, a);
60
61 if (a->name->manager && a->name->manager->current_server_address == a)
62 manager_set_server_address(a->name->manager, NULL);
63 }
64
6b430fdb 65 return mfree(a);
874ff7bf
LP
66}
67
68int server_name_new(
69 Manager *m,
70 ServerName **ret,
71 ServerType type,
72 const char *string) {
73
74 ServerName *n, *tail;
75
76 assert(m);
77 assert(string);
78
79 n = new0(ServerName, 1);
80 if (!n)
81 return -ENOMEM;
82
83 n->type = type;
84 n->string = strdup(string);
85 if (!n->string) {
86 free(n);
87 return -ENOMEM;
88 }
89
90 if (type == SERVER_SYSTEM) {
91 LIST_FIND_TAIL(names, m->system_servers, tail);
92 LIST_INSERT_AFTER(names, m->system_servers, tail, n);
93 } else if (type == SERVER_LINK) {
94 LIST_FIND_TAIL(names, m->link_servers, tail);
95 LIST_INSERT_AFTER(names, m->link_servers, tail, n);
96 } else if (type == SERVER_FALLBACK) {
97 LIST_FIND_TAIL(names, m->fallback_servers, tail);
98 LIST_INSERT_AFTER(names, m->fallback_servers, tail, n);
99 } else
100 assert_not_reached("Unknown server type");
101
102 n->manager = m;
103
104 if (type != SERVER_FALLBACK &&
105 m->current_server_name &&
106 m->current_server_name->type == SERVER_FALLBACK)
107 manager_set_server_name(m, NULL);
108
109 log_debug("Added new server %s.", string);
110
111 if (ret)
112 *ret = n;
113
114 return 0;
115}
116
117ServerName *server_name_free(ServerName *n) {
118 if (!n)
119 return NULL;
120
121 server_name_flush_addresses(n);
122
123 if (n->manager) {
124 if (n->type == SERVER_SYSTEM)
125 LIST_REMOVE(names, n->manager->system_servers, n);
126 else if (n->type == SERVER_LINK)
127 LIST_REMOVE(names, n->manager->link_servers, n);
128 else if (n->type == SERVER_FALLBACK)
129 LIST_REMOVE(names, n->manager->fallback_servers, n);
130 else
131 assert_not_reached("Unknown server type");
132
133 if (n->manager->current_server_name == n)
134 manager_set_server_name(n->manager, NULL);
135 }
136
137 log_debug("Removed server %s.", n->string);
138
139 free(n->string);
6b430fdb 140 return mfree(n);
874ff7bf
LP
141}
142
143void server_name_flush_addresses(ServerName *n) {
144 assert(n);
145
146 while (n->addresses)
147 server_address_free(n->addresses);
148}