]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/mdns_objects/mdns_symptoms.c
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / mdns_objects / mdns_symptoms.c
1 /*
2 * Copyright (c) 2019 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 #include "mdns_symptoms.h"
18
19 #include <CoreUtils/CoreUtils.h>
20 #include <SymptomReporter/SymptomReporter.h>
21
22 #define MAX_DOMAIN_NAME 256
23
24 #define SYMPTOM_REPORTER_mDNSResponder_NUMERIC_ID 101
25 #define SYMPTOM_REPORTER_mDNSResponder_TEXT_ID "com.apple.mDNSResponder"
26
27 #define SYMPTOM_DNS_NO_REPLIES 0x00065001
28 #define SYMPTOM_DNS_RESUMED_RESPONDING 0x00065002
29 #define SYMPTOM_DNS_ENCRYPTED_CONNECTION_FAILURE 0x00065004
30
31 //======================================================================================================================
32 // MARK: - Soft Linking
33
34 SOFT_LINK_FRAMEWORK_EX(PrivateFrameworks, SymptomReporter);
35 SOFT_LINK_FUNCTION_EX(SymptomReporter, symptom_framework_init,
36 symptom_framework_t,
37 (symptom_ident_t id, const char *originator_string),
38 (id, originator_string));
39 SOFT_LINK_FUNCTION_EX(SymptomReporter, symptom_new,
40 symptom_t,
41 (symptom_framework_t framework, symptom_ident_t id),
42 (framework, id));
43 SOFT_LINK_FUNCTION_EX(SymptomReporter, symptom_set_additional_qualifier,
44 int,
45 (symptom_t symptom, uint32_t qualifier_type, size_t qualifier_len, const void *qualifier_data),
46 (symptom, qualifier_type, qualifier_len, qualifier_data));
47 SOFT_LINK_FUNCTION_EX(SymptomReporter, symptom_send,
48 int,
49 (symptom_t symptom),
50 (symptom));
51
52 //======================================================================================================================
53 // MARK: - Local Helper Prototypes
54
55 static symptom_framework_t
56 _mdns_symptoms_get_reporter(void);
57
58 static void
59 _mdns_symptoms_report_dns_server_symptom(symptom_ident_t id, const struct sockaddr *address);
60
61 static void
62 _mdns_symptoms_report_dns_host_symptom(symptom_ident_t id, const char *host);
63
64 //======================================================================================================================
65 // MARK - External Functions
66
67 void
68 mdns_symptoms_report_unresponsive_server(const struct sockaddr *address)
69 {
70 _mdns_symptoms_report_dns_server_symptom(SYMPTOM_DNS_NO_REPLIES, address);
71 }
72
73 //======================================================================================================================
74
75 void
76 mdns_symptoms_report_encrypted_dns_connection_failure(const char *host)
77 {
78 _mdns_symptoms_report_dns_host_symptom(SYMPTOM_DNS_ENCRYPTED_CONNECTION_FAILURE, host);
79 }
80
81 //======================================================================================================================
82
83 void
84 mdns_symptoms_report_responsive_server(const struct sockaddr *address)
85 {
86 _mdns_symptoms_report_dns_server_symptom(SYMPTOM_DNS_RESUMED_RESPONDING, address);
87 }
88
89 //======================================================================================================================
90 // MARK - Local Helpers
91
92 static symptom_framework_t
93 _mdns_symptoms_get_reporter(void)
94 {
95 static dispatch_once_t s_once = 0;
96 static symptom_framework_t s_reporter = NULL;
97
98 dispatch_once(&s_once,
99 ^{
100 if (SOFT_LINK_HAS_FUNCTION(SymptomReporter, symptom_framework_init)) {
101 s_reporter = soft_symptom_framework_init(SYMPTOM_REPORTER_mDNSResponder_NUMERIC_ID,
102 SYMPTOM_REPORTER_mDNSResponder_TEXT_ID);
103 }
104 });
105 return s_reporter;
106 }
107
108 //======================================================================================================================
109
110 static void
111 _mdns_symptoms_report_dns_server_symptom(symptom_ident_t id, const struct sockaddr *address)
112 {
113 const symptom_framework_t reporter = _mdns_symptoms_get_reporter();
114 require_quiet(reporter, exit);
115
116 size_t address_len;
117 if (address->sa_family == AF_INET) {
118 address_len = sizeof(struct sockaddr_in);
119 } else if (address->sa_family == AF_INET6) {
120 address_len = sizeof(struct sockaddr_in6);
121 } else {
122 goto exit;
123 }
124 const symptom_t symptom = soft_symptom_new(reporter, id);
125 soft_symptom_set_additional_qualifier(symptom, 1, address_len, address);
126 soft_symptom_send(symptom);
127
128 exit:
129 return;
130 }
131
132 //======================================================================================================================
133
134 static void
135 _mdns_symptoms_report_dns_host_symptom(symptom_ident_t id, const char *host)
136 {
137 const symptom_framework_t reporter = _mdns_symptoms_get_reporter();
138 require_quiet(reporter, exit);
139
140 size_t hostname_len = strnlen(host, MAX_DOMAIN_NAME);
141 const symptom_t symptom = soft_symptom_new(reporter, id);
142 soft_symptom_set_additional_qualifier(symptom, 2, hostname_len, host);
143 soft_symptom_send(symptom);
144
145 exit:
146 return;
147 }