]> git.meshlink.io Git - catta/blob - src/internal.h
use WSAGetlastError/FormatMessage in place of strerror(errno) on Windows
[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 #ifndef _WIN32
52 #define closesocket close
53 #define winsock_init()
54 #define winsock_exit()
55 #define errnostrsocket() strerror(errno)
56 #endif
57
58 typedef struct CattaLegacyUnicastReflectSlot CattaLegacyUnicastReflectSlot;
59
60 struct CattaLegacyUnicastReflectSlot {
61     CattaServer *server;
62
63     uint16_t id, original_id;
64     CattaAddress address;
65     uint16_t port;
66     int iface;
67     struct timeval elapse_time;
68     CattaTimeEvent *time_event;
69 };
70
71 struct CattaEntry {
72     CattaServer *server;
73     CattaSEntryGroup *group;
74
75     int dead;
76
77     CattaPublishFlags flags;
78     CattaRecord *record;
79     CattaIfIndex iface;
80     CattaProtocol protocol;
81
82     CATTA_LLIST_FIELDS(CattaEntry, entries);
83     CATTA_LLIST_FIELDS(CattaEntry, by_key);
84     CATTA_LLIST_FIELDS(CattaEntry, by_group);
85
86     CATTA_LLIST_HEAD(CattaAnnouncer, announcers);
87 };
88
89 struct CattaSEntryGroup {
90     CattaServer *server;
91     int dead;
92
93     CattaEntryGroupState state;
94     void* userdata;
95     CattaSEntryGroupCallback callback;
96
97     unsigned n_probing;
98
99     unsigned n_register_try;
100     struct timeval register_time;
101     CattaTimeEvent *register_time_event;
102
103     struct timeval established_at;
104
105     CATTA_LLIST_FIELDS(CattaSEntryGroup, groups);
106     CATTA_LLIST_HEAD(CattaEntry, entries);
107 };
108
109 struct CattaServer {
110     const CattaPoll *poll_api;
111
112     CattaInterfaceMonitor *monitor;
113     CattaServerConfig config;
114
115     CATTA_LLIST_HEAD(CattaEntry, entries);
116     CattaHashmap *entries_by_key;
117
118     CATTA_LLIST_HEAD(CattaSEntryGroup, groups);
119
120     CATTA_LLIST_HEAD(CattaSRecordBrowser, record_browsers);
121     CattaHashmap *record_browser_hashmap;
122     CATTA_LLIST_HEAD(CattaSHostNameResolver, host_name_resolvers);
123     CATTA_LLIST_HEAD(CattaSAddressResolver, address_resolvers);
124     CATTA_LLIST_HEAD(CattaSDomainBrowser, domain_browsers);
125     CATTA_LLIST_HEAD(CattaSServiceTypeBrowser, service_type_browsers);
126     CATTA_LLIST_HEAD(CattaSServiceBrowser, service_browsers);
127     CATTA_LLIST_HEAD(CattaSServiceResolver, service_resolvers);
128     CATTA_LLIST_HEAD(CattaSDNSServerBrowser, dns_server_browsers);
129
130     int need_entry_cleanup, need_group_cleanup, need_browser_cleanup;
131
132     /* Used for scheduling RR cleanup */
133     CattaTimeEvent *cleanup_time_event;
134
135     CattaTimeEventQueue *time_event_queue;
136
137     char *host_name, *host_name_fqdn, *domain_name;
138
139     int fd_ipv4, fd_ipv6,
140         /* The following two sockets two are used for reflection only */
141         fd_legacy_unicast_ipv4, fd_legacy_unicast_ipv6;
142
143     CattaWatch *watch_ipv4, *watch_ipv6,
144         *watch_legacy_unicast_ipv4, *watch_legacy_unicast_ipv6;
145
146     CattaServerState state;
147     CattaServerCallback callback;
148     void* userdata;
149
150     CattaSEntryGroup *hinfo_entry_group;
151     CattaSEntryGroup *browse_domain_entry_group;
152     unsigned n_host_rr_pending;
153
154     /* Used for assembling responses */
155     CattaRecordList *record_list;
156
157     /* Used for reflection of legacy unicast packets */
158     CattaLegacyUnicastReflectSlot **legacy_unicast_reflect_slots;
159     uint16_t legacy_unicast_reflect_id;
160
161     /* The last error code */
162     int error;
163
164     /* The local service cookie */
165     uint32_t local_service_cookie;
166
167     CattaMulticastLookupEngine *multicast_lookup_engine;
168     CattaWideAreaLookupEngine *wide_area_lookup_engine;
169 };
170
171 void catta_entry_free(CattaServer*s, CattaEntry *e);
172 void catta_entry_group_free(CattaServer *s, CattaSEntryGroup *g);
173
174 void catta_cleanup_dead_entries(CattaServer *s);
175
176 void catta_server_prepare_response(CattaServer *s, CattaInterface *i, CattaEntry *e, int unicast_response, int auxiliary);
177 void catta_server_prepare_matching_responses(CattaServer *s, CattaInterface *i, CattaKey *k, int unicast_response);
178 void catta_server_generate_response(CattaServer *s, CattaInterface *i, CattaDnsPacket *p, const CattaAddress *a, uint16_t port, int legacy_unicast, int is_probe);
179
180 void catta_s_entry_group_change_state(CattaSEntryGroup *g, CattaEntryGroupState state);
181
182 int catta_entry_is_commited(CattaEntry *e);
183
184 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);
185
186 void catta_host_rr_entry_group_callback(CattaServer *s, CattaSEntryGroup *g, CattaEntryGroupState state, void *userdata);
187
188 void catta_server_decrease_host_rr_pending(CattaServer *s);
189
190 int catta_server_set_errno(CattaServer *s, int error);
191
192 int catta_server_is_service_local(CattaServer *s, CattaIfIndex iface, CattaProtocol protocol, const char *name);
193 int catta_server_is_record_local(CattaServer *s, CattaIfIndex iface, CattaProtocol protocol, CattaRecord *record);
194
195 int catta_server_add_ptr(
196     CattaServer *s,
197     CattaSEntryGroup *g,
198     CattaIfIndex iface,
199     CattaProtocol protocol,
200     CattaPublishFlags flags,
201     uint32_t ttl,
202     const char *name,
203     const char *dest);
204
205 #define CATTA_CHECK_VALIDITY(server, expression, error) { \
206         if (!(expression)) \
207             return catta_server_set_errno((server), (error)); \
208 }
209
210 #define CATTA_CHECK_VALIDITY_RETURN_NULL(server, expression, error) { \
211         if (!(expression)) { \
212             catta_server_set_errno((server), (error)); \
213             return NULL; \
214         } \
215 }
216
217 #define CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(server, expression, error) {\
218     if (!(expression)) { \
219         ret = catta_server_set_errno((server), (error)); \
220         goto fail; \
221     } \
222 }
223
224 #define CATTA_ASSERT_TRUE(expression) { \
225     int __tmp = !!(expression); \
226     assert(__tmp); \
227 }
228
229 #define CATTA_ASSERT_SUCCESS(expression) { \
230     int __tmp = (expression); \
231     assert(__tmp == 0); \
232 }
233
234 #endif