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