]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/LoggingProfiles/liblog_mdnsresponder.m
mDNSResponder-1310.80.1.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / LoggingProfiles / liblog_mdnsresponder.m
1 //
2 // liblog_mdnsresponder.m
3 // liblog_mdnsresponder
4 //
5
6 #import <Foundation/Foundation.h>
7 #import <arpa/inet.h>
8 #import <os/log_private.h>
9 #import "DNSCommon.h"
10 #undef DomainNameLength // undefines DomainNameLength since we need to use DomainNameLength that is also defined in DNSMessage.h
11 #import "DNSMessage.h"
12
13 // MDNS Mutable Attribute String
14 #define MDNSAS(str) [[NSAttributedString alloc] initWithString:(str)]
15 #define MDNSASWithFormat(format, ...) MDNSAS(([[NSString alloc] initWithFormat:format, ##__VA_ARGS__]))
16 #define MAX_MDNS_ADDR_STRING_LENGTH 45
17
18 // os_log(OS_LOG_DEFAULT, "IP Address(IPv4/IPv6): %{mdnsresponder:ip_addr}.20P", <the address of mDNSAddr structure>);
19 static NS_RETURNS_RETAINED NSAttributedString *
20 MDNSOLCopyFormattedStringmDNSIPAddr(id value)
21 {
22 const mDNSAddr *mdns_addr_p;
23 char buffer[MAX_MDNS_ADDR_STRING_LENGTH + 1];
24 buffer[MAX_MDNS_ADDR_STRING_LENGTH] = 0;
25
26 if ([(NSObject *)value isKindOfClass:[NSData class]]) {
27 NSData *data = (NSData *)value;
28 if (data.bytes == NULL || data.length == 0) {
29 return MDNSAS(@"<NULL IP ADDRESS>");
30 }
31
32 if (data.length != sizeof(mDNSAddr)) {
33 return MDNSASWithFormat(@"<fail decode - size> %zd != %zd", (size_t)data.length, sizeof(mDNSAddr));
34 }
35
36 mdns_addr_p = (const mDNSAddr *)data.bytes;
37 } else {
38 return MDNSASWithFormat(@"<fail decode - data type> %@", [(NSObject *)value description]);
39 }
40
41 bool failed_conversion = false;
42 switch (mdns_addr_p->type) {
43 case mDNSAddrType_IPv4:
44 {
45 __unused char sizecheck_buffer[(sizeof(buffer) >= INET_ADDRSTRLEN) ? 1 : -1];
46 if (!inet_ntop(AF_INET, (const void *)&mdns_addr_p->ip.v4.NotAnInteger, buffer, sizeof(buffer)))
47 failed_conversion = true;
48 break;
49 }
50 case mDNSAddrType_IPv6:
51 {
52 __unused char sizecheck_buffer[(sizeof(buffer) >= INET6_ADDRSTRLEN) ? 1 : -1];
53 if (!inet_ntop(AF_INET6, (const void *)mdns_addr_p->ip.v6.b, buffer, sizeof(buffer)))
54 failed_conversion = true;
55 break;
56 }
57 default:
58 failed_conversion = true;
59 break;
60 }
61 if (failed_conversion) {
62 return MDNSAS(@"<failed conversion>");
63 }
64
65 NSString *str = @(buffer);
66 return MDNSAS(str ? str : @("<Could not create NSString>"));
67 }
68
69 // os_log(OS_LOG_DEFAULT, "MAC Address: %{mdnsresponder:mac_addr}.6P", <the address of 6-byte MAC address>);
70 #define MAC_ADDRESS_LEN 6
71 static NS_RETURNS_RETAINED NSAttributedString *
72 MDNSOLCopyFormattedStringmDNSMACAddr(id value)
73 {
74 const uint8_t *mac_addr = NULL;
75 char buffer[MAX_MDNS_ADDR_STRING_LENGTH + 1];
76 buffer[MAX_MDNS_ADDR_STRING_LENGTH] = 0;
77
78 if ([(NSObject *)value isKindOfClass:[NSData class]]) {
79 NSData *data = (NSData *)value;
80 if (data.bytes == NULL || data.length == 0) {
81 return MDNSAS(@"<NULL MAC ADDRESS>");
82 }
83
84 if (data.length != MAC_ADDRESS_LEN) {
85 return MDNSASWithFormat(@"<fail decode - size> %zd != %zd", (size_t)data.length, MAC_ADDRESS_LEN);
86 }
87
88 mac_addr = (const uint8_t *)data.bytes;
89 } else {
90 return MDNSASWithFormat(@"<fail decode - data type> %@", [(NSObject *)value description]);
91 }
92
93 int ret_snprintf = snprintf(buffer, MAX_MDNS_ADDR_STRING_LENGTH, "%02X:%02X:%02X:%02X:%02X:%02X",
94 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
95 if (ret_snprintf < 0) {
96 return MDNSAS(@"<failed conversion>");
97 }
98
99 NSString *str = @(buffer);
100 return MDNSAS(str ? str : @("<Could not create NSString>"));
101 }
102
103 // os_log(OS_LOG_DEFAULT, "Domain Name: %{mdnsresponder:domain_name}.*P", <the address of domainname structure>);
104 // Leave some extra space to allow log routine to put error message when decode fails at the end of the buffer.
105 static NS_RETURNS_RETAINED NSAttributedString *
106 MDNSOLCopyFormattedStringmDNSLabelSequenceName(id value)
107 {
108 char buffer[kDNSServiceMaxDomainName];
109 NSData *data = (NSData *)value;
110 OSStatus ret;
111
112 if ([(NSObject *)value isKindOfClass:[NSData class]]) {
113 if (data.bytes == NULL || data.length == 0) {
114 return MDNSAS(@"<NULL DOMAIN NAME>");
115 }
116 } else {
117 return MDNSASWithFormat(@"<fail decode - data type> %@", [(NSObject *)value description]);
118 }
119
120 buffer[0] = '\0';
121 ret = DomainNameToString((const uint8_t *)data.bytes, ((const uint8_t *) data.bytes) + data.length, buffer, NULL);
122 if (ret != kNoErr) {
123 snprintf(buffer, sizeof(buffer), "<Malformed Domain Name>");
124 }
125
126 NSString *str = @(buffer);
127 return MDNSAS(str ? str : @("<Could not create NSString>"));
128 }
129
130 struct MDNSOLFormatters {
131 const char *type;
132 NS_RETURNS_RETAINED NSAttributedString *(*function)(id);
133 };
134
135 NS_RETURNS_RETAINED
136 NSAttributedString *
137 OSLogCopyFormattedString(const char *type, id value, __unused os_log_type_info_t info)
138 {
139 static const struct MDNSOLFormatters formatters[] = {
140 { .type = "ip_addr", .function = MDNSOLCopyFormattedStringmDNSIPAddr },
141 { .type = "mac_addr", .function = MDNSOLCopyFormattedStringmDNSMACAddr },
142 { .type = "domain_name", .function = MDNSOLCopyFormattedStringmDNSLabelSequenceName },
143 };
144
145 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) {
146 if (strcmp(type, formatters[i].type) == 0) {
147 return formatters[i].function(value);
148 }
149 }
150
151 return nil;
152 }