2 * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
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
8 * https://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __MDNS_OBJECTS_H__
18 #define __MDNS_OBJECTS_H__
20 #include "mdns_private.h"
22 #include <os/object_private.h>
27 //======================================================================================================================
28 // MARK: - Kind Declarations
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)
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.
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))]
45 #define _MDNS_OBJECT_SUBKIND_DEFINE_TEST(NAME, ...) \
46 static const struct mdns_kind_s _ ## NAME ## _kind = { \
47 .superkind = &_mdns_object_kind, \
51 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
53 #define _MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(NAME) \
55 _ ## NAME ## _copy_description(NAME ## _t object, bool debug, bool privacy); \
58 _ ## NAME ## _finalize(NAME ## _t object); \
60 _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT( \
62 _ ## NAME ## _copy_description, \
64 _ ## NAME ## _finalize \
67 #define _MDNS_OBJECT_SUBKIND_DEFINE(NAME) \
68 _MDNS_OBJECT_SUBKIND_DEFINE_ABSTRACT(NAME); \
69 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
71 #define _MDNS_OBJECT_SUBKIND_DEFINE_FULL(NAME) \
73 _ ## NAME ## _copy_description(NAME ## _t object, bool debug, bool privacy); \
76 _ ## NAME ## _equal(NAME ## _t object1, NAME ## _t object2); \
79 _ ## NAME ## _finalize(NAME ## _t object); \
81 _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT( \
83 _ ## NAME ## _copy_description, \
84 _ ## NAME ## _equal, \
85 _ ## NAME ## _finalize \
87 _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME)
89 #define _MDNS_OBJECT_SUBKIND_DEFINE_STRUCT(NAME, COPY_DESCRIPTION_METHOD, EQUAL_METHOD, FINALIZE_METHOD) \
90 static const struct mdns_kind_s _ ## NAME ## _kind = { \
93 (COPY_DESCRIPTION_METHOD), \
97 _MDNS_BASE_CHECK(NAME, mdns_object)
99 #define _MDNS_OBJECT_SUBKIND_DEFINE_ALLOC(NAME) \
101 _ ## NAME ## _alloc(void) \
103 NAME ## _t obj = NAME ## _object_alloc(sizeof(*obj)); \
104 require_quiet(obj, exit); \
106 const mdns_object_t base = (mdns_object_t)obj; \
107 base->kind = &_ ## NAME ## _kind; \
112 extern int _mdns_dummy_variable
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
);
118 typedef const struct mdns_kind_s
* mdns_kind_t
;
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.
127 //======================================================================================================================
128 // MARK: - Object Kind Definition
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.
135 extern const struct mdns_kind_s _mdns_object_kind
;
137 //======================================================================================================================
138 // MARK: - Object Private Method Declarations
141 mdns_object_copy_description(mdns_any_t object
, bool debug
, bool privacy
);
144 mdns_object_copy_description_as_cfstring_ex(mdns_any_t object
, bool debug
, bool privacy
);
146 #define mdns_object_copy_description_as_cfstring(OBJ) \
147 mdns_object_copy_description_as_cfstring_ex(OBJ, false, false)
149 #define mdns_object_copy_debug_description_as_cfstring(OBJ) \
150 mdns_object_copy_description_as_cfstring_ex(OBJ, true, false)
152 #define mdns_object_copy_redacted_description_as_cfstring(OBJ) \
153 mdns_object_copy_description_as_cfstring_ex(OBJ, false, true)
156 mdns_object_finalize(mdns_any_t object
);
158 #define MDNS_OBJECT_ALLOC_DECLARE(NAME) \
159 mdns_ ## NAME ## _t \
160 mdns_ ## NAME ## _object_alloc(size_t size)
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
);
175 //======================================================================================================================
176 // MARK: - Class Declarations
178 _OS_OBJECT_DECL_SUBCLASS_INTERFACE(mdns_object
, object
)
180 #endif // __MDNS_OBJECTS_H__