]>
Commit | Line | Data |
---|---|---|
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 | |
84e51726 LP |
6 | ***/ |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
84e51726 | 9 | #include "timesyncd-server.h" |
874ff7bf LP |
10 | |
11 | int server_address_new( | |
12 | ServerName *n, | |
13 | ServerAddress **ret, | |
14 | const union sockaddr_union *sockaddr, | |
15 | socklen_t socklen) { | |
16 | ||
17 | ServerAddress *a, *tail; | |
18 | ||
19 | assert(n); | |
20 | assert(sockaddr); | |
21 | assert(socklen >= offsetof(struct sockaddr, sa_data)); | |
22 | assert(socklen <= sizeof(union sockaddr_union)); | |
23 | ||
24 | a = new0(ServerAddress, 1); | |
25 | if (!a) | |
26 | return -ENOMEM; | |
27 | ||
28 | memcpy(&a->sockaddr, sockaddr, socklen); | |
29 | a->socklen = socklen; | |
30 | ||
31 | LIST_FIND_TAIL(addresses, n->addresses, tail); | |
32 | LIST_INSERT_AFTER(addresses, n->addresses, tail, a); | |
33 | a->name = n; | |
34 | ||
35 | if (ret) | |
36 | *ret = a; | |
37 | ||
38 | return 0; | |
39 | } | |
40 | ||
41 | ServerAddress* server_address_free(ServerAddress *a) { | |
42 | if (!a) | |
43 | return NULL; | |
44 | ||
45 | if (a->name) { | |
46 | LIST_REMOVE(addresses, a->name->addresses, a); | |
47 | ||
48 | if (a->name->manager && a->name->manager->current_server_address == a) | |
49 | manager_set_server_address(a->name->manager, NULL); | |
50 | } | |
51 | ||
6b430fdb | 52 | return mfree(a); |
874ff7bf LP |
53 | } |
54 | ||
55 | int server_name_new( | |
56 | Manager *m, | |
57 | ServerName **ret, | |
58 | ServerType type, | |
59 | const char *string) { | |
60 | ||
61 | ServerName *n, *tail; | |
62 | ||
63 | assert(m); | |
64 | assert(string); | |
65 | ||
66 | n = new0(ServerName, 1); | |
67 | if (!n) | |
68 | return -ENOMEM; | |
69 | ||
70 | n->type = type; | |
71 | n->string = strdup(string); | |
72 | if (!n->string) { | |
73 | free(n); | |
74 | return -ENOMEM; | |
75 | } | |
76 | ||
77 | if (type == SERVER_SYSTEM) { | |
78 | LIST_FIND_TAIL(names, m->system_servers, tail); | |
79 | LIST_INSERT_AFTER(names, m->system_servers, tail, n); | |
80 | } else if (type == SERVER_LINK) { | |
81 | LIST_FIND_TAIL(names, m->link_servers, tail); | |
82 | LIST_INSERT_AFTER(names, m->link_servers, tail, n); | |
83 | } else if (type == SERVER_FALLBACK) { | |
84 | LIST_FIND_TAIL(names, m->fallback_servers, tail); | |
85 | LIST_INSERT_AFTER(names, m->fallback_servers, tail, n); | |
86 | } else | |
87 | assert_not_reached("Unknown server type"); | |
88 | ||
89 | n->manager = m; | |
90 | ||
91 | if (type != SERVER_FALLBACK && | |
92 | m->current_server_name && | |
93 | m->current_server_name->type == SERVER_FALLBACK) | |
94 | manager_set_server_name(m, NULL); | |
95 | ||
96 | log_debug("Added new server %s.", string); | |
97 | ||
98 | if (ret) | |
99 | *ret = n; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | ServerName *server_name_free(ServerName *n) { | |
105 | if (!n) | |
106 | return NULL; | |
107 | ||
108 | server_name_flush_addresses(n); | |
109 | ||
110 | if (n->manager) { | |
111 | if (n->type == SERVER_SYSTEM) | |
112 | LIST_REMOVE(names, n->manager->system_servers, n); | |
113 | else if (n->type == SERVER_LINK) | |
114 | LIST_REMOVE(names, n->manager->link_servers, n); | |
115 | else if (n->type == SERVER_FALLBACK) | |
116 | LIST_REMOVE(names, n->manager->fallback_servers, n); | |
117 | else | |
118 | assert_not_reached("Unknown server type"); | |
119 | ||
120 | if (n->manager->current_server_name == n) | |
121 | manager_set_server_name(n->manager, NULL); | |
122 | } | |
123 | ||
124 | log_debug("Removed server %s.", n->string); | |
125 | ||
126 | free(n->string); | |
6b430fdb | 127 | return mfree(n); |
874ff7bf LP |
128 | } |
129 | ||
130 | void server_name_flush_addresses(ServerName *n) { | |
131 | assert(n); | |
132 | ||
133 | while (n->addresses) | |
134 | server_address_free(n->addresses); | |
135 | } |