1/* -*- Mode: C; tab-width: 4 -*- 2 * 3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 17 Change History (most recent first): 18 19Log: PlatformCommon.c,v $ 20Revision 1.21 2009/04/11 00:20:24 jessic2 21<rdar://problem/4426780> Daemon: Should be able to turn on LogOperation dynamically 22 23Revision 1.20 2008/10/09 22:26:05 cheshire 24Save space by not showing high-resolution timestamp in LogMsgNoIdent() lines 25 26Revision 1.19 2008/07/14 17:43:36 mkrochma 27Fix previous check in so connect still gets called 28 29Revision 1.18 2008/07/12 17:19:41 mkrochma 30<rdar://problem/6068351> mDNSResponder PlatformCommon.c uses sin_len even on non-compliant platforms 31 32Revision 1.17 2008/03/05 00:19:09 cheshire 33Conditionalize LogTimeStamps so it's specific to APPLE_OSX, for now 34 35Revision 1.16 2008/02/26 21:47:45 cheshire 36Added cast to avoid compiler warning 37 38Revision 1.15 2008/02/26 21:42:26 cheshire 39Added 'LogTimeStamps' option, to show ms-granularity timestamps on every log message 40 41Revision 1.14 2007/12/03 18:37:26 cheshire 42Moved mDNSPlatformWriteLogMsg & mDNSPlatformWriteDebugMsg 43from mDNSMacOSX.c to PlatformCommon.c, so that Posix build can use them 44 45Revision 1.13 2007/10/22 20:07:07 cheshire 46Moved mDNSPlatformSourceAddrForDest from mDNSMacOSX.c to PlatformCommon.c so 47Posix build can share the code (better than just pasting it into mDNSPosix.c) 48 49Revision 1.12 2007/10/16 17:19:53 cheshire 50<rdar://problem/3557903> Performance: Core code will not work on platforms with small stacks 51Cut ReadDDNSSettingsFromConfFile stack from 2112 to 1104 bytes 52 53Revision 1.11 2007/07/31 23:08:34 mcguire 54<rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming 55 56Revision 1.10 2007/07/11 02:59:58 cheshire 57<rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services 58Add AutoTunnel parameter to mDNS_SetSecretForDomain 59 60Revision 1.9 2007/01/09 22:37:44 cheshire 61Remove unused ClearDomainSecrets() function 62 63Revision 1.8 2006/12/22 20:59:51 cheshire 64<rdar://problem/4742742> Read *all* DNS keys from keychain, 65 not just key for the system-wide default registration domain 66 67Revision 1.7 2006/08/14 23:24:56 cheshire 68Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0 69 70Revision 1.6 2005/04/08 21:30:16 ksekar 71<rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9 72Patch submitted by Bernd Kuhls 73 74Revision 1.5 2005/02/01 19:33:30 ksekar 75<rdar://problem/3985239> Keychain format too restrictive 76 77Revision 1.4 2005/01/19 19:19:21 ksekar 78<rdar://problem/3960191> Need a way to turn off domain discovery 79 80Revision 1.3 2004/12/13 17:46:52 cheshire 81Use sizeof(buf) instead of fixed constant 1024 82 83Revision 1.2 2004/12/01 03:30:29 cheshire 84<rdar://problem/3889346> Add Unicast DNS support to mDNSPosix 85 86Revision 1.1 2004/12/01 01:51:35 cheshire 87Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c 88 89 */ 90 91#include <stdio.h> // Needed for fopen() etc. 92#include <unistd.h> // Needed for close() 93#include <string.h> // Needed for strlen() etc. 94#include <errno.h> // Needed for errno etc. 95#include <sys/socket.h> // Needed for socket() etc. 96#include <netinet/in.h> // Needed for sockaddr_in 97#include <syslog.h> 98 99#include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above 100#include "DNSCommon.h" 101#include "PlatformCommon.h" 102 103#ifdef NOT_HAVE_SOCKLEN_T 104 typedef unsigned int socklen_t; 105#endif 106 107// Bind a UDP socket to find the source address to a destination 108mDNSexport void mDNSPlatformSourceAddrForDest(mDNSAddr *const src, const mDNSAddr *const dst) 109 { 110 union { struct sockaddr s; struct sockaddr_in a4; struct sockaddr_in6 a6; } addr; 111 socklen_t len = sizeof(addr); 112 socklen_t inner_len = 0; 113 int sock = socket(AF_INET, SOCK_DGRAM, 0); 114 src->type = mDNSAddrType_None; 115 if (sock == -1) return; 116 if (dst->type == mDNSAddrType_IPv4) 117 { 118 inner_len = sizeof(addr.a4); 119 #ifndef NOT_HAVE_SA_LEN 120 addr.a4.sin_len = inner_len; 121 #endif 122 addr.a4.sin_family = AF_INET; 123 addr.a4.sin_port = 1; // Not important, any port will do 124 addr.a4.sin_addr.s_addr = dst->ip.v4.NotAnInteger; 125 } 126 else if (dst->type == mDNSAddrType_IPv6) 127 { 128 inner_len = sizeof(addr.a6); 129 #ifndef NOT_HAVE_SA_LEN 130 addr.a6.sin6_len = inner_len; 131 #endif 132 addr.a6.sin6_family = AF_INET6; 133 addr.a6.sin6_flowinfo = 0; 134 addr.a6.sin6_port = 1; // Not important, any port will do 135 addr.a6.sin6_addr = *(struct in6_addr*)&dst->ip.v6; 136 addr.a6.sin6_scope_id = 0; 137 } 138 else return; 139 140 if ((connect(sock, &addr.s, inner_len)) < 0) 141 { LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst, errno, strerror(errno)); goto exit; } 142 143 if ((getsockname(sock, &addr.s, &len)) < 0) 144 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno, strerror(errno)); goto exit; } 145 146 src->type = dst->type; 147 if (dst->type == mDNSAddrType_IPv4) src->ip.v4.NotAnInteger = addr.a4.sin_addr.s_addr; 148 else src->ip.v6 = *(mDNSv6Addr*)&addr.a6.sin6_addr; 149exit: 150 close(sock); 151 } 152 153// dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length 154mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f) 155 { 156 char buf[32+1+MAX_ESCAPED_DOMAIN_NAME]; // Option name, one space, option value 157 unsigned int len = strlen(option); 158 if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; } 159 fseek(f, 0, SEEK_SET); // set position to beginning of stream 160 while (fgets(buf, sizeof(buf), f)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator 161 { 162 if (!strncmp(buf, option, len)) 163 { 164 strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1); 165 if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0'; 166 len = strlen(dst); 167 if (len && dst[len-1] == '\n') dst[len-1] = '\0'; // chop newline 168 return mDNStrue; 169 } 170 } 171 debugf("Option %s not set", option); 172 return mDNSfalse; 173 } 174 175mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled) 176 { 177 char buf[MAX_ESCAPED_DOMAIN_NAME] = ""; 178 mStatus err; 179 FILE *f = fopen(filename, "r"); 180 181 if (hostname) hostname->c[0] = 0; 182 if (domain) domain->c[0] = 0; 183 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse; 184 185 if (f) 186 { 187 if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue; 188 if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf; 189 if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf; 190 buf[0] = 0; 191 GetConfigOption(buf, "secret-64", f); // failure means no authentication 192 fclose(f); 193 f = NULL; 194 } 195 else 196 { 197 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened."); 198 return; 199 } 200 201 if (domain && domain->c[0] && buf[0]) 202 { 203 DomainAuthInfo *info = (DomainAuthInfo*)mDNSPlatformMemAllocate(sizeof(*info)); 204 // for now we assume keyname = service reg domain and we use same key for service and hostname registration 205 err = mDNS_SetSecretForDomain(m, info, domain, domain, buf, mDNSfalse); 206 if (err) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err, domain->c); 207 } 208 209 return; 210 211 badf: 212 LogMsg("ERROR: malformatted config file"); 213 if (f) fclose(f); 214 } 215 216#if MDNS_DEBUGMSGS 217mDNSexport void mDNSPlatformWriteDebugMsg(const char *msg) 218 { 219 fprintf(stderr,"%s\n", msg); 220 fflush(stderr); 221 } 222#endif 223 224mDNSexport void mDNSPlatformWriteLogMsg(const char *ident, const char *buffer, mDNSLogLevel_t loglevel) 225 { 226#if APPLE_OSX_mDNSResponder && LogTimeStamps 227 extern mDNS mDNSStorage; 228 extern mDNSu32 mDNSPlatformClockDivisor; 229 mDNSs32 t = mDNSStorage.timenow ? mDNSStorage.timenow : mDNSPlatformClockDivisor ? mDNS_TimeNow_NoLock(&mDNSStorage) : 0; 230 int ms = ((t < 0) ? -t : t) % 1000; 231#endif 232 233 if (mDNS_DebugMode) // In debug mode we write to stderr 234 { 235#if APPLE_OSX_mDNSResponder && LogTimeStamps 236 if (ident && ident[0] && mDNSPlatformClockDivisor) 237 fprintf(stderr,"%8d.%03d: %s\n", (int)(t/1000), ms, buffer); 238 else 239#endif 240 fprintf(stderr,"%s\n", buffer); 241 fflush(stderr); 242 } 243 else // else, in production mode, we write to syslog 244 { 245 static int log_inited = 0; 246 247 int syslog_level = LOG_ERR; 248 switch (loglevel) 249 { 250 case MDNS_LOG_MSG: syslog_level = LOG_ERR; break; 251 case MDNS_LOG_OPERATION: syslog_level = LOG_WARNING; break; 252 case MDNS_LOG_SPS: syslog_level = LOG_NOTICE; break; 253 case MDNS_LOG_INFO: syslog_level = LOG_INFO; break; 254 case MDNS_LOG_DEBUG: syslog_level = LOG_DEBUG; break; 255 default: 256 fprintf(stderr, "Unknown loglevel %d, assuming LOG_ERR\n", loglevel); 257 fflush(stderr); 258 } 259 260 if (!log_inited) { openlog(ident, LOG_CONS, LOG_DAEMON); log_inited++; } 261 262#if APPLE_OSX_mDNSResponder && LogTimeStamps 263 if (ident && ident[0] && mDNSPlatformClockDivisor) 264 syslog(syslog_level, "%8d.%03d: %s", (int)(t/1000), ms, buffer); 265 else 266#endif 267 syslog(syslog_level, "%s", buffer); 268 } 269 } 270