1 /*
2 * Copyright (c) 2003-2024 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 <stdio.h>
18 #include <string.h>
19
20 #if defined(WIN32) || defined(EFI32) || defined(EFI64) || defined(EFIX64)
21 // Need to add Windows/EFI syslog support here
22 #define LOG_PID 0x01
23 #define LOG_CONS 0x02
24 #define LOG_PERROR 0x20
25 #else
26 #include <syslog.h>
27 #endif
28
29 #include "mDNSEmbeddedAPI.h"
30 #include "mdns_strict.h"
31
32 mDNSexport int mDNS_LoggingEnabled = 0;
33 mDNSexport int mDNS_DebugLoggingEnabled = 0;
34 mDNSexport int mDNS_PacketLoggingEnabled = 0;
35 mDNSexport int mDNS_McastLoggingEnabled = 0;
36 mDNSexport int mDNS_McastTracingEnabled = 0;
37
38 #if MDNS_DEBUGMSGS && defined(__APPLE__)
39 mDNSexport int mDNS_DebugMode = mDNStrue;
40 #else
41 mDNSexport int mDNS_DebugMode = mDNSfalse;
42 #endif
43
44 #if MDNSRESPONDER_SUPPORTS(APPLE, LOG_PRIVACY_LEVEL)
45 mDNSexport int gNumOfSensitiveLoggingEnabledQuestions = 0;
46 mDNSexport int gSensitiveLoggingEnabled = 0;
47 #endif
48
49 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
50 // how to print special data types like IP addresses and length-prefixed domain names
51 #if MDNS_DEBUGMSGS > 1
verbosedebugf_(const char * format,...)52 mDNSexport void verbosedebugf_(const char *format, ...)
53 {
54 char buffer[512];
55 va_list args;
56 va_start(args, format);
57 mDNS_vsnprintf(buffer, sizeof(buffer), format, args);
58 va_end(args);
59 mDNSPlatformWriteDebugMsg(buffer);
60 }
61 #endif
62
63 // Log message with default "mDNSResponder" ident string at the start
64 #if MDNSRESPONDER_SUPPORTS(APPLE, OS_LOG)
LogMsgWithLevelv(os_log_t category,os_log_type_t level,const char * format,va_list args)65 mDNSlocal void LogMsgWithLevelv(os_log_t category, os_log_type_t level, const char *format, va_list args)
66 {
67 char buffer[512];
68 mDNS_vsnprintf(buffer, (mDNSu32)sizeof(buffer), format, args);
69 os_log_with_type(category ? category : mDNSLogCategory_Default, level, "%{private}s", buffer);
70 }
71 #else
LogMsgWithLevelv(const char * category,mDNSLogLevel_t level,const char * format,va_list args)72 mDNSlocal void LogMsgWithLevelv(const char *category, mDNSLogLevel_t level, const char *format, va_list args)
73 {
74 // Do not print the logs if the log category is MDNS_LOG_CATEGORY_DISABLED.
75 if (strcmp(category, MDNS_LOG_CATEGORY_DISABLED) == 0)
76 {
77 return;
78 }
79
80 char buffer[512];
81 char *dst = buffer;
82 const char *const lim = &buffer[512];
83 if (category) mDNS_snprintf_add(&dst, lim, "%s: ", category);
84 mDNS_vsnprintf(dst, (mDNSu32)(lim - dst), format, args);
85 mDNSPlatformWriteLogMsg(ProgramName, buffer, level);
86 }
87 #endif
88
89 #define LOG_HELPER_BODY(CATEGORY, LEVEL) \
90 { \
91 va_list args; \
92 va_start(args,format); \
93 LogMsgWithLevelv(CATEGORY, LEVEL, format, args); \
94 va_end(args); \
95 }
96
97 // see mDNSDebug.h
98 #if !MDNS_HAS_VA_ARG_MACROS
LogMsg_(const char * format,...)99 void LogMsg_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEFAULT)
100 void LogOperation_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEFAULT)
101 void LogSPS_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEFAULT)
102 void LogInfo_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEFAULT)
103 void LogDebug_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEBUG)
104 #endif
105
106 #if MDNS_DEBUGMSGS
107 void debugf_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEBUG)
108 #endif
109
110 // Log message with default "mDNSResponder" ident string at the start
111 mDNSexport void LogMsgWithLevel(mDNSLogCategory_t category, mDNSLogLevel_t level, const char *format, ...)
112 LOG_HELPER_BODY(category, level)
113
114 mDNSexport void LogToFD(int fd, const char *format, ...)
115 {
116 va_list args;
117 va_start(args, format);
118 (void)fd;
119 LogMsgWithLevelv(NULL, MDNS_LOG_DEFAULT, format, args);
120 va_end(args);
121 }
122