Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / apache2 / mDNSResponder / dist / mDNSShared / PlatformCommon.c
blobaa0a5ca4a2024b78a3d8566ee1ab22f5f8e9ef7c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
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
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.
17 Change History (most recent first):
19 Log: PlatformCommon.c,v $
20 Revision 1.21 2009/04/11 00:20:24 jessic2
21 <rdar://problem/4426780> Daemon: Should be able to turn on LogOperation dynamically
23 Revision 1.20 2008/10/09 22:26:05 cheshire
24 Save space by not showing high-resolution timestamp in LogMsgNoIdent() lines
26 Revision 1.19 2008/07/14 17:43:36 mkrochma
27 Fix previous check in so connect still gets called
29 Revision 1.18 2008/07/12 17:19:41 mkrochma
30 <rdar://problem/6068351> mDNSResponder PlatformCommon.c uses sin_len even on non-compliant platforms
32 Revision 1.17 2008/03/05 00:19:09 cheshire
33 Conditionalize LogTimeStamps so it's specific to APPLE_OSX, for now
35 Revision 1.16 2008/02/26 21:47:45 cheshire
36 Added cast to avoid compiler warning
38 Revision 1.15 2008/02/26 21:42:26 cheshire
39 Added 'LogTimeStamps' option, to show ms-granularity timestamps on every log message
41 Revision 1.14 2007/12/03 18:37:26 cheshire
42 Moved mDNSPlatformWriteLogMsg & mDNSPlatformWriteDebugMsg
43 from mDNSMacOSX.c to PlatformCommon.c, so that Posix build can use them
45 Revision 1.13 2007/10/22 20:07:07 cheshire
46 Moved mDNSPlatformSourceAddrForDest from mDNSMacOSX.c to PlatformCommon.c so
47 Posix build can share the code (better than just pasting it into mDNSPosix.c)
49 Revision 1.12 2007/10/16 17:19:53 cheshire
50 <rdar://problem/3557903> Performance: Core code will not work on platforms with small stacks
51 Cut ReadDDNSSettingsFromConfFile stack from 2112 to 1104 bytes
53 Revision 1.11 2007/07/31 23:08:34 mcguire
54 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
56 Revision 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
58 Add AutoTunnel parameter to mDNS_SetSecretForDomain
60 Revision 1.9 2007/01/09 22:37:44 cheshire
61 Remove unused ClearDomainSecrets() function
63 Revision 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
67 Revision 1.7 2006/08/14 23:24:56 cheshire
68 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
70 Revision 1.6 2005/04/08 21:30:16 ksekar
71 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
72 Patch submitted by Bernd Kuhls
74 Revision 1.5 2005/02/01 19:33:30 ksekar
75 <rdar://problem/3985239> Keychain format too restrictive
77 Revision 1.4 2005/01/19 19:19:21 ksekar
78 <rdar://problem/3960191> Need a way to turn off domain discovery
80 Revision 1.3 2004/12/13 17:46:52 cheshire
81 Use sizeof(buf) instead of fixed constant 1024
83 Revision 1.2 2004/12/01 03:30:29 cheshire
84 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
86 Revision 1.1 2004/12/01 01:51:35 cheshire
87 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
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>
99 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
100 #include "DNSCommon.h"
101 #include "PlatformCommon.h"
103 #ifdef NOT_HAVE_SOCKLEN_T
104 typedef unsigned int socklen_t;
105 #endif
107 // Bind a UDP socket to find the source address to a destination
108 mDNSexport void mDNSPlatformSourceAddrForDest(mDNSAddr *const src, const mDNSAddr *const dst)
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)
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;
126 else if (dst->type == mDNSAddrType_IPv6)
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;
138 else return;
140 if ((connect(sock, &addr.s, inner_len)) < 0)
141 { LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst, errno, strerror(errno)); goto exit; }
143 if ((getsockname(sock, &addr.s, &len)) < 0)
144 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno, strerror(errno)); goto exit; }
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;
149 exit:
150 close(sock);
153 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
154 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
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
162 if (!strncmp(buf, option, len))
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;
171 debugf("Option %s not set", option);
172 return mDNSfalse;
175 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
177 char buf[MAX_ESCAPED_DOMAIN_NAME] = "";
178 mStatus err;
179 FILE *f = fopen(filename, "r");
181 if (hostname) hostname->c[0] = 0;
182 if (domain) domain->c[0] = 0;
183 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
185 if (f)
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;
195 else
197 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
198 return;
201 if (domain && domain->c[0] && buf[0])
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);
209 return;
211 badf:
212 LogMsg("ERROR: malformatted config file");
213 if (f) fclose(f);
216 #if MDNS_DEBUGMSGS
217 mDNSexport void mDNSPlatformWriteDebugMsg(const char *msg)
219 fprintf(stderr,"%s\n", msg);
220 fflush(stderr);
222 #endif
224 mDNSexport void mDNSPlatformWriteLogMsg(const char *ident, const char *buffer, mDNSLogLevel_t loglevel)
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
233 if (mDNS_DebugMode) // In debug mode we write to stderr
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);
243 else // else, in production mode, we write to syslog
245 static int log_inited = 0;
247 int syslog_level = LOG_ERR;
248 switch (loglevel)
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);
260 if (!log_inited) { openlog(ident, LOG_CONS, LOG_DAEMON); log_inited++; }
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);