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