]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/mdns_objects/mdns_objects.h
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / mdns_objects / mdns_objects.h
1 /*
2 * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __MDNS_OBJECTS_H__
18 #define __MDNS_OBJECTS_H__
19
20 #include "mdns_private.h"
21
22 #include <os/object_private.h>
23
24 MDNS_DECL(server);
25 MDNS_DECL(session);
26
27 //======================================================================================================================
28 // MARK: - Kind Declarations
29
30 #define MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(NAME) _MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(mdns_ ## NAME)
31 #define MDNS_OBJECT_SUBKIND_DEFINE(NAME) _MDNS_OBJECT_SUBKIND_DEFINE(mdns_ ## NAME)
32 #define MDNS_OBJECT_SUBKIND_DEFINE_FULL(NAME) _MDNS_OBJECT_SUBKIND_DEFINE_FULL(mdns_ ## NAME)
33 #define MDNS_OBJECT_SUBKIND_DEFINE_TEST(NAME) _MDNS_OBJECT_SUBKIND_DEFINE_TEST(mdns_ ## NAME)
34
35 // Note: The last check checks if the base's type is equal to that of the superkind. If it's not, then the pointer
36 // comparison used as the argument to sizeof will cause a "comparison of distinct pointer types" warning, so long as
37 // the warning hasn't been disabled.
38
39 #define MDNS_BASE_CHECK(NAME, SUPER) _MDNS_BASE_CHECK(mdns_ ## NAME, mdns_ ## SUPER)
40 #define _MDNS_BASE_CHECK(NAME, SUPER) \
41 check_compile_time(offsetof(struct NAME ## _s, base) == 0); \
42 check_compile_time(sizeof_field(struct NAME ## _s, base) == sizeof(struct SUPER ## _s)); \
43 extern int _mdns_base_type_check[sizeof(&(((NAME ## _t)0)->base) == ((SUPER ## _t)0))]
44
45 #define _MDNS_OBJECT_SUBKIND_DEFINE_TEST(NAME, ...) \
46 static const struct mdns_kind_s _ ## NAME ## _kind = { \
47 .superkind = &_mdns_object_kind, \
48 .name = # NAME, \
49 __VA_ARGS__ \
50 }; \
51 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
52
53 #define _MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(NAME) \
54 static char * \
55 _ ## NAME ## _copy_description(NAME ## _t object, bool debug, bool privacy); \
56 \
57 static void \
58 _ ## NAME ## _finalize(NAME ## _t object); \
59 \
60 _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT( \
61 NAME, \
62 _ ## NAME ## _copy_description, \
63 NULL, \
64 _ ## NAME ## _finalize \
65 )
66
67 #define _MDNS_OBJECT_SUBKIND_DEFINE(NAME) \
68 _MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(NAME); \
69 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
70
71 #define _MDNS_OBJECT_SUBKIND_DEFINE_FULL(NAME) \
72 static char * \
73 _ ## NAME ## _copy_description(NAME ## _t object, bool debug, bool privacy); \
74 \
75 static bool \
76 _ ## NAME ## _equal(NAME ## _t object1, NAME ## _t object2); \
77 \
78 static void \
79 _ ## NAME ## _finalize(NAME ## _t object); \
80 \
81 _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT( \
82 NAME, \
83 _ ## NAME ## _copy_description, \
84 _ ## NAME ## _equal, \
85 _ ## NAME ## _finalize \
86 ); \
87 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
88
89 #define _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT(NAME, COPY_DESCRIPTION_METHOD, EQUAL_METHOD, FINALIZE_METHOD) \
90 static const struct mdns_kind_s _ ## NAME ## _kind = { \
91 &_mdns_object_kind, \
92 # NAME, \
93 (COPY_DESCRIPTION_METHOD), \
94 (EQUAL_METHOD), \
95 (FINALIZE_METHOD) \
96 }; \
97 _MDNS_BASE_CHECK(NAME, mdns_object)
98
99 #define _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME) \
100 static NAME ## _t \
101 _ ## NAME ## _alloc(void) \
102 { \
103 NAME ## _t obj = NAME ## _object_alloc(sizeof(*obj)); \
104 require_quiet(obj, exit); \
105 \
106 const mdns_object_t base = (mdns_object_t)obj; \
107 base->kind = &_ ## NAME ## _kind; \
108 \
109 exit: \
110 return obj; \
111 } \
112 extern int _mdns_dummy_variable
113
114 typedef char * (*mdns_copy_description_f)(mdns_any_t object, bool debug, bool privacy);
115 typedef bool (*mdns_equal_f)(mdns_any_t object1, mdns_any_t object2);
116 typedef void (*mdns_finalize_f)(mdns_any_t object);
117
118 typedef const struct mdns_kind_s * mdns_kind_t;
119 struct mdns_kind_s {
120 mdns_kind_t superkind; // This kind's superkind.
121 const char * name; // Name of this kind.
122 mdns_copy_description_f copy_description; // Creates a textual description of an object as a UTF-8 C string.
123 mdns_equal_f equal; // Compares two objects for equality.
124 mdns_finalize_f finalize; // Releases object's resources right before the object is freed.
125 };
126
127 //======================================================================================================================
128 // MARK: - Object Kind Definition
129
130 struct mdns_object_s {
131 _OS_OBJECT_HEADER(const void * __ptrauth_objc_isa_pointer _os_obj_isa, _os_obj_refcnt, _os_obj_xref_cnt);
132 mdns_kind_t kind; // Pointer to an object's kind.
133 };
134
135 extern const struct mdns_kind_s _mdns_object_kind;
136
137 //======================================================================================================================
138 // MARK: - Object Private Method Declarations
139
140 char *
141 mdns_object_copy_description(mdns_any_t object, bool debug, bool privacy);
142
143 CFStringRef
144 mdns_object_copy_description_as_cfstring_ex(mdns_any_t object, bool debug, bool privacy);
145
146 #define mdns_object_copy_description_as_cfstring(OBJ) \
147 mdns_object_copy_description_as_cfstring_ex(OBJ, false, false)
148
149 #define mdns_object_copy_debug_description_as_cfstring(OBJ) \
150 mdns_object_copy_description_as_cfstring_ex(OBJ, true, false)
151
152 #define mdns_object_copy_redacted_description_as_cfstring(OBJ) \
153 mdns_object_copy_description_as_cfstring_ex(OBJ, false, true)
154
155 void
156 mdns_object_finalize(mdns_any_t object);
157
158 #define MDNS_OBJECT_ALLOC_DECLARE(NAME) \
159 mdns_ ## NAME ## _t \
160 mdns_ ## NAME ## _object_alloc(size_t size)
161
162 MDNS_OBJECT_ALLOC_DECLARE(address);
163 MDNS_OBJECT_ALLOC_DECLARE(dns_service);
164 MDNS_OBJECT_ALLOC_DECLARE(dns_service_manager);
165 MDNS_OBJECT_ALLOC_DECLARE(interface_monitor);
166 MDNS_OBJECT_ALLOC_DECLARE(message);
167 MDNS_OBJECT_ALLOC_DECLARE(query_message);
168 MDNS_OBJECT_ALLOC_DECLARE(querier);
169 MDNS_OBJECT_ALLOC_DECLARE(resolver);
170 MDNS_OBJECT_ALLOC_DECLARE(server);
171 MDNS_OBJECT_ALLOC_DECLARE(session);
172 MDNS_OBJECT_ALLOC_DECLARE(set);
173 MDNS_OBJECT_ALLOC_DECLARE(trust);
174
175 //======================================================================================================================
176 // MARK: - Class Declarations
177
178 _OS_OBJECT_DECL_SUBCLASS_INTERFACE(mdns_object, object)
179
180 #endif // __MDNS_OBJECTS_H__