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