]> git.meshlink.io Git - catta/blob - src/internal.h
6c602d7deb3deb33eeaa41f27a3981d76800b893
[catta] / src / internal.h
1 #ifndef foointernalhfoo
2 #define foointernalhfoo
3
4 /***
5   This file is part of catta.
6
7   catta is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as
9   published by the Free Software Foundation; either version 2.1 of the
10   License, or (at your option) any later version.
11
12   catta is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15   Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with catta; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 /** A locally registered DNS resource record */
24 typedef struct CattaEntry CattaEntry;
25
26 #include <catta/llist.h>
27 #include <catta/watch.h>
28 #include <catta/timeval.h>
29 #include <catta/core.h>
30
31 #include "iface.h"
32 #include "prioq.h"
33 #include "timeeventq.h"
34 #include "announce.h"
35 #include "browse.h"
36 #include "dns.h"
37 #include "rrlist.h"
38 #include "hashmap.h"
39 #include "wide-area.h"
40 #include "multicast-lookup.h"
41 #include "dns-srv-rr.h"
42
43 #define CATTA_LEGACY_UNICAST_REFLECT_SLOTS_MAX 100
44
45 #define CATTA_FLAGS_VALID(flags, max) (!((flags) & ~(max)))
46
47 #define CATTA_RR_HOLDOFF_MSEC 1000
48 #define CATTA_RR_HOLDOFF_MSEC_RATE_LIMIT 20000
49 #define CATTA_RR_RATE_LIMIT_COUNT 15
50
51 typedef struct CattaLegacyUnicastReflectSlot CattaLegacyUnicastReflectSlot;
52
53 struct CattaLegacyUnicastReflectSlot {
54     CattaServer *server;
55
56     uint16_t id, original_id;
57     CattaAddress address;
58     uint16_t port;
59     int iface;
60     struct timeval elapse_time;
61     CattaTimeEvent *time_event;
62 };
63
64 struct CattaEntry {
65     CattaServer *server;
66     CattaSEntryGroup *group;
67
68     int dead;
69
70     CattaPublishFlags flags;
71     CattaRecord *record;
72     CattaIfIndex iface;
73     CattaProtocol protocol;
74
75     CATTA_LLIST_FIELDS(CattaEntry, entries);
76     CATTA_LLIST_FIELDS(CattaEntry, by_key);
77     CATTA_LLIST_FIELDS(CattaEntry, by_group);
78
79     CATTA_LLIST_HEAD(CattaAnnouncer, announcers);
80 };
81
82 struct CattaSEntryGroup {
83     CattaServer *server;
84     int dead;
85
86     CattaEntryGroupState state;
87     void* userdata;
88     CattaSEntryGroupCallback callback;
89
90     unsigned n_probing;
91
92     unsigned n_register_try;
93     struct timeval register_time;
94     CattaTimeEvent *register_time_event;
95
96     struct timeval established_at;
97
98     CATTA_LLIST_FIELDS(CattaSEntryGroup, groups);
99     CATTA_LLIST_HEAD(CattaEntry, entries);
100 };
101
102 struct CattaServer {
103     const CattaPoll *poll_api;
104
105     CattaInterfaceMonitor *monitor;
106     CattaServerConfig config;
107
108     CATTA_LLIST_HEAD(CattaEntry, entries);
109     CattaHashmap *entries_by_key;
110
111     CATTA_LLIST_HEAD(CattaSEntryGroup, groups);
112
113     CATTA_LLIST_HEAD(CattaSRecordBrowser, record_browsers);
114     CattaHashmap *record_browser_hashmap;
115     CATTA_LLIST_HEAD(CattaSHostNameResolver, host_name_resolvers);
116     CATTA_LLIST_HEAD(CattaSAddressResolver, address_resolvers);
117     CATTA_LLIST_HEAD(CattaSDomainBrowser, domain_browsers);
118     CATTA_LLIST_HEAD(CattaSServiceTypeBrowser, service_type_browsers);
119     CATTA_LLIST_HEAD(CattaSServiceBrowser, service_browsers);
120     CATTA_LLIST_HEAD(CattaSServiceResolver, service_resolvers);
121     CATTA_LLIST_HEAD(CattaSDNSServerBrowser, dns_server_browsers);
122
123     int need_entry_cleanup, need_group_cleanup, need_browser_cleanup;
124
125     /* Used for scheduling RR cleanup */
126     CattaTimeEvent *cleanup_time_event;
127
128     CattaTimeEventQueue *time_event_queue;
129
130     char *host_name, *host_name_fqdn, *domain_name;
131
132     int fd_ipv4, fd_ipv6,
133         /* The following two sockets two are used for reflection only */
134         fd_legacy_unicast_ipv4, fd_legacy_unicast_ipv6;
135
136     CattaWatch *watch_ipv4, *watch_ipv6,
137         *watch_legacy_unicast_ipv4, *watch_legacy_unicast_ipv6;
138
139     CattaServerState state;
140     CattaServerCallback callback;
141     void* userdata;
142
143     CattaSEntryGroup *hinfo_entry_group;
144     CattaSEntryGroup *browse_domain_entry_group;
145     unsigned n_host_rr_pending;
146
147     /* Used for assembling responses */
148     CattaRecordList *record_list;
149
150     /* Used for reflection of legacy unicast packets */
151     CattaLegacyUnicastReflectSlot **legacy_unicast_reflect_slots;
152     uint16_t legacy_unicast_reflect_id;
153
154     /* The last error code */
155     int error;
156
157     /* The local service cookie */
158     uint32_t local_service_cookie;
159
160     CattaMulticastLookupEngine *multicast_lookup_engine;
161     CattaWideAreaLookupEngine *wide_area_lookup_engine;
162 };
163
164 void catta_entry_free(CattaServer*s, CattaEntry *e);
165 void catta_entry_group_free(CattaServer *s, CattaSEntryGroup *g);
166
167 void catta_cleanup_dead_entries(CattaServer *s);
168
169 void catta_server_prepare_response(CattaServer *s, CattaInterface *i, CattaEntry *e, int unicast_response, int auxiliary);
170 void catta_server_prepare_matching_responses(CattaServer *s, CattaInterface *i, CattaKey *k, int unicast_response);
171 void catta_server_generate_response(CattaServer *s, CattaInterface *i, CattaDnsPacket *p, const CattaAddress *a, uint16_t port, int legacy_unicast, int is_probe);
172
173 void catta_s_entry_group_change_state(CattaSEntryGroup *g, CattaEntryGroupState state);
174
175 int catta_entry_is_commited(CattaEntry *e);
176
177 void catta_server_enumerate_aux_records(CattaServer *s, CattaInterface *i, CattaRecord *r, void (*callback)(CattaServer *s, CattaRecord *r, int flush_cache, void* userdata), void* userdata);
178
179 void catta_host_rr_entry_group_callback(CattaServer *s, CattaSEntryGroup *g, CattaEntryGroupState state, void *userdata);
180
181 void catta_server_decrease_host_rr_pending(CattaServer *s);
182
183 int catta_server_set_errno(CattaServer *s, int error);
184
185 int catta_server_is_service_local(CattaServer *s, CattaIfIndex iface, CattaProtocol protocol, const char *name);
186 int catta_server_is_record_local(CattaServer *s, CattaIfIndex iface, CattaProtocol protocol, CattaRecord *record);
187
188 int catta_server_add_ptr(
189     CattaServer *s,
190     CattaSEntryGroup *g,
191     CattaIfIndex iface,
192     CattaProtocol protocol,
193     CattaPublishFlags flags,
194     uint32_t ttl,
195     const char *name,
196     const char *dest);
197
198 #define CATTA_CHECK_VALIDITY(server, expression, error) { \
199         if (!(expression)) \
200             return catta_server_set_errno((server), (error)); \
201 }
202
203 #define CATTA_CHECK_VALIDITY_RETURN_NULL(server, expression, error) { \
204         if (!(expression)) { \
205             catta_server_set_errno((server), (error)); \
206             return NULL; \
207         } \
208 }
209
210 #define CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(server, expression, error) {\
211     if (!(expression)) { \
212         ret = catta_server_set_errno((server), (error)); \
213         goto fail; \
214     } \
215 }
216
217 #define CATTA_ASSERT_TRUE(expression) { \
218     int __tmp = !!(expression); \
219     assert(__tmp); \
220 }
221
222 #define CATTA_ASSERT_SUCCESS(expression) { \
223     int __tmp = (expression); \
224     assert(__tmp == 0); \
225 }
226
227 #endif