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 18 #include <stdio.h> // Needed for fopen() etc. 19 #include <unistd.h> // Needed for close() 20 #include <string.h> // Needed for strlen() etc. 21 #include <errno.h> // Needed for errno etc. 22 #include <sys/socket.h> // Needed for socket() etc. 23 #include <netinet/in.h> // Needed for sockaddr_in 24 #include <syslog.h> 25 26 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above 27 #include "DNSCommon.h" 28 #include "PlatformCommon.h" 29 30 #ifdef NOT_HAVE_SOCKLEN_T 31 typedef unsigned int socklen_t; 32 #endif 33 34 // Bind a UDP socket to find the source address to a destination 35 mDNSexport void mDNSPlatformSourceAddrForDest(mDNSAddr *const src, const mDNSAddr *const dst) 36 { 37 union { struct sockaddr s; struct sockaddr_in a4; struct sockaddr_in6 a6; } addr; 38 socklen_t len = sizeof(addr); 39 socklen_t inner_len = 0; 40 int sock = socket(AF_INET, SOCK_DGRAM, 0); 41 src->type = mDNSAddrType_None; 42 if (sock == -1) return; 43 if (dst->type == mDNSAddrType_IPv4) 44 { 45 inner_len = sizeof(addr.a4); 46 #ifndef NOT_HAVE_SA_LEN 47 addr.a4.sin_len = inner_len; 48 #endif 49 addr.a4.sin_family = AF_INET; 50 addr.a4.sin_port = 7; // Not important, any port will do 51 addr.a4.sin_addr.s_addr = dst->ip.v4.NotAnInteger; 52 } 53 else if (dst->type == mDNSAddrType_IPv6) 54 { 55 inner_len = sizeof(addr.a6); 56 #ifndef NOT_HAVE_SA_LEN 57 addr.a6.sin6_len = inner_len; 58 #endif 59 addr.a6.sin6_family = AF_INET6; 60 addr.a6.sin6_flowinfo = 0; 61 addr.a6.sin6_port = 7; // Not important, any port will do 62 addr.a6.sin6_addr = *(struct in6_addr*)&dst->ip.v6; 63 addr.a6.sin6_scope_id = 0; 64 } 65 else return; 66 67 if ((connect(sock, &addr.s, inner_len)) < 0) 68 { 69 static mDNSv4Addr dummy = { 198, 51, 100, 42 }; 70 71 // Don't spam if we can't connect to 198.51.100.42 to the console. 72 // That is our test address to out which interfaces/address should be primary and is also 73 // configured in mDNSPosix/PosixDaemon.c:Reconfigure() 74 // Failing to connect to it with EADDRNOTAVAIL is a common situation, especially on boot up. 75 if (dst->type == mDNSAddrType_IPv4 && dst->ip.v4.NotAnInteger == dummy.NotAnInteger && errno == EADDRNOTAVAIL) 76 LogInfo("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst, errno, strerror(errno)); 77 else 78 LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst, errno, strerror(errno)); 79 goto exit; 80 } 81 82 if ((getsockname(sock, &addr.s, &len)) < 0) 83 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno, strerror(errno)); goto exit; } 84 85 src->type = dst->type; 86 if (dst->type == mDNSAddrType_IPv4) src->ip.v4.NotAnInteger = addr.a4.sin_addr.s_addr; 87 else src->ip.v6 = *(mDNSv6Addr*)&addr.a6.sin6_addr; 88 exit: 89 close(sock); 90 } 91 92 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length 93 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f) 94 { 95 char buf[32+1+MAX_ESCAPED_DOMAIN_NAME]; // Option name, one space, option value 96 unsigned int len = strlen(option); 97 if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; } 98 fseek(f, 0, SEEK_SET); // set position to beginning of stream 99 while (fgets(buf, sizeof(buf), f)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator 100 { 101 if (!strncmp(buf, option, len)) 102 { 103 strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1); 104 if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0'; 105 len = strlen(dst); 106 if (len && dst[len-1] == '\n') dst[len-1] = '\0'; // chop newline 107 return mDNStrue; 108 } 109 } 110 debugf("Option %s not set", option); 111 return mDNSfalse; 112 } 113 114 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled) 115 { 116 char buf[MAX_ESCAPED_DOMAIN_NAME] = ""; 117 mStatus err; 118 FILE *f = fopen(filename, "r"); 119 120 if (hostname) hostname->c[0] = 0; 121 if (domain) domain->c[0] = 0; 122 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse; 123 124 if (f) 125 { 126 if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue; 127 if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf; 128 if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf; 129 buf[0] = 0; 130 GetConfigOption(buf, "secret-64", f); // failure means no authentication 131 fclose(f); 132 f = NULL; 133 } 134 else 135 { 136 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened."); 137 return; 138 } 139 140 if (domain && domain->c[0] && buf[0]) 141 { 142 DomainAuthInfo *info = (DomainAuthInfo*)mDNSPlatformMemAllocate(sizeof(*info)); 143 // for now we assume keyname = service reg domain and we use same key for service and hostname registration 144 err = mDNS_SetSecretForDomain(m, info, domain, domain, buf, NULL, 0, NULL); 145 if (err) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err, domain->c); 146 } 147 148 return; 149 150 badf: 151 LogMsg("ERROR: malformatted config file"); 152 if (f) fclose(f); 153 } 154 155 #if MDNS_DEBUGMSGS 156 mDNSexport void mDNSPlatformWriteDebugMsg(const char *msg) 157 { 158 fprintf(stderr,"%s\n", msg); 159 fflush(stderr); 160 } 161 #endif 162 163 mDNSexport void mDNSPlatformWriteLogMsg(const char *ident, const char *buffer, mDNSLogLevel_t loglevel) 164 { 165 #if APPLE_OSX_mDNSResponder && LogTimeStamps 166 extern mDNS mDNSStorage; 167 extern mDNSu32 mDNSPlatformClockDivisor; 168 mDNSs32 t = mDNSStorage.timenow ? mDNSStorage.timenow : mDNSPlatformClockDivisor ? mDNS_TimeNow_NoLock(&mDNSStorage) : 0; 169 int ms = ((t < 0) ? -t : t) % 1000; 170 #endif 171 172 if (mDNS_DebugMode) // In debug mode we write to stderr 173 { 174 #if APPLE_OSX_mDNSResponder && LogTimeStamps 175 if (ident && ident[0] && mDNSPlatformClockDivisor) 176 fprintf(stderr,"%8d.%03d: %s\n", (int)(t/1000), ms, buffer); 177 else 178 #endif 179 fprintf(stderr,"%s\n", buffer); 180 fflush(stderr); 181 } 182 else // else, in production mode, we write to syslog 183 { 184 static int log_inited = 0; 185 186 int syslog_level = LOG_ERR; 187 switch (loglevel) 188 { 189 case MDNS_LOG_MSG: syslog_level = LOG_ERR; break; 190 case MDNS_LOG_OPERATION: syslog_level = LOG_WARNING; break; 191 case MDNS_LOG_SPS: syslog_level = LOG_NOTICE; break; 192 case MDNS_LOG_INFO: syslog_level = LOG_INFO; break; 193 case MDNS_LOG_DEBUG: syslog_level = LOG_DEBUG; break; 194 default: 195 fprintf(stderr, "Unknown loglevel %d, assuming LOG_ERR\n", loglevel); 196 fflush(stderr); 197 } 198 199 if (!log_inited) { openlog(ident, LOG_CONS, LOG_DAEMON); log_inited++; } 200 201 #if APPLE_OSX_mDNSResponder && LogTimeStamps 202 if (ident && ident[0] && mDNSPlatformClockDivisor) 203 syslog(syslog_level, "%8d.%03d: %s", (int)(t/1000), ms, buffer); 204 else 205 #endif 206 syslog(syslog_level, "%s", buffer); 207 } 208 } 209