]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/PlatformCommon.c
mDNSResponder-170.tar.gz
[apple/mdnsresponder.git] / mDNSShared / PlatformCommon.c
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
19 $Log: PlatformCommon.c,v $
20 Revision 1.14 2007/12/03 18:37:26 cheshire
21 Moved mDNSPlatformWriteLogMsg & mDNSPlatformWriteDebugMsg
22 from mDNSMacOSX.c to PlatformCommon.c, so that Posix build can use them
23
24 Revision 1.13 2007/10/22 20:07:07 cheshire
25 Moved mDNSPlatformSourceAddrForDest from mDNSMacOSX.c to PlatformCommon.c so
26 Posix build can share the code (better than just pasting it into mDNSPosix.c)
27
28 Revision 1.12 2007/10/16 17:19:53 cheshire
29 <rdar://problem/3557903> Performance: Core code will not work on platforms with small stacks
30 Cut ReadDDNSSettingsFromConfFile stack from 2112 to 1104 bytes
31
32 Revision 1.11 2007/07/31 23:08:34 mcguire
33 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
34
35 Revision 1.10 2007/07/11 02:59:58 cheshire
36 <rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services
37 Add AutoTunnel parameter to mDNS_SetSecretForDomain
38
39 Revision 1.9 2007/01/09 22:37:44 cheshire
40 Remove unused ClearDomainSecrets() function
41
42 Revision 1.8 2006/12/22 20:59:51 cheshire
43 <rdar://problem/4742742> Read *all* DNS keys from keychain,
44 not just key for the system-wide default registration domain
45
46 Revision 1.7 2006/08/14 23:24:56 cheshire
47 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
48
49 Revision 1.6 2005/04/08 21:30:16 ksekar
50 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
51 Patch submitted by Bernd Kuhls
52
53 Revision 1.5 2005/02/01 19:33:30 ksekar
54 <rdar://problem/3985239> Keychain format too restrictive
55
56 Revision 1.4 2005/01/19 19:19:21 ksekar
57 <rdar://problem/3960191> Need a way to turn off domain discovery
58
59 Revision 1.3 2004/12/13 17:46:52 cheshire
60 Use sizeof(buf) instead of fixed constant 1024
61
62 Revision 1.2 2004/12/01 03:30:29 cheshire
63 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
64
65 Revision 1.1 2004/12/01 01:51:35 cheshire
66 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
67
68 */
69
70 #include <stdio.h> // Needed for fopen() etc.
71 #include <unistd.h> // Needed for close()
72 #include <string.h> // Needed for strlen() etc.
73 #include <errno.h> // Needed for errno etc.
74 #include <sys/socket.h> // Needed for socket() etc.
75 #include <netinet/in.h> // Needed for sockaddr_in
76 #include <syslog.h>
77
78 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
79 #include "DNSCommon.h"
80 #include "PlatformCommon.h"
81
82 #ifdef NOT_HAVE_SOCKLEN_T
83 typedef unsigned int socklen_t;
84 #endif
85
86 // Bind a UDP socket to find the source address to a destination
87 mDNSexport void mDNSPlatformSourceAddrForDest(mDNSAddr *const src, const mDNSAddr *const dst)
88 {
89 union { struct sockaddr s; struct sockaddr_in a4; struct sockaddr_in6 a6; } addr;
90 socklen_t len = sizeof(addr);
91 int sock = socket(AF_INET, SOCK_DGRAM, 0);
92 src->type = mDNSAddrType_None;
93 if (sock == -1) return;
94 if (dst->type == mDNSAddrType_IPv4)
95 {
96 addr.a4.sin_len = sizeof(addr.a4);
97 addr.a4.sin_family = AF_INET;
98 addr.a4.sin_port = 1; // Not important, any port will do
99 addr.a4.sin_addr.s_addr = dst->ip.v4.NotAnInteger;
100 }
101 else if (dst->type == mDNSAddrType_IPv6)
102 {
103 addr.a6.sin6_len = sizeof(addr.a6);
104 addr.a6.sin6_family = AF_INET6;
105 addr.a6.sin6_flowinfo = 0;
106 addr.a6.sin6_port = 1; // Not important, any port will do
107 addr.a6.sin6_addr = *(struct in6_addr*)&dst->ip.v6;
108 addr.a6.sin6_scope_id = 0;
109 }
110 else return;
111
112 if ((connect(sock, &addr.s, addr.s.sa_len)) < 0)
113 { LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst, errno, strerror(errno)); goto exit; }
114
115 if ((getsockname(sock, &addr.s, &len)) < 0)
116 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno, strerror(errno)); goto exit; }
117
118 src->type = dst->type;
119 if (dst->type == mDNSAddrType_IPv4) src->ip.v4.NotAnInteger = addr.a4.sin_addr.s_addr;
120 else src->ip.v6 = *(mDNSv6Addr*)&addr.a6.sin6_addr;
121 exit:
122 close(sock);
123 }
124
125 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
126 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
127 {
128 char buf[32+1+MAX_ESCAPED_DOMAIN_NAME]; // Option name, one space, option value
129 unsigned int len = strlen(option);
130 if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; }
131 fseek(f, 0, SEEK_SET); // set position to beginning of stream
132 while (fgets(buf, sizeof(buf), f)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
133 {
134 if (!strncmp(buf, option, len))
135 {
136 strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1);
137 if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0';
138 len = strlen(dst);
139 if (len && dst[len-1] == '\n') dst[len-1] = '\0'; // chop newline
140 return mDNStrue;
141 }
142 }
143 debugf("Option %s not set", option);
144 return mDNSfalse;
145 }
146
147 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
148 {
149 char buf[MAX_ESCAPED_DOMAIN_NAME] = "";
150 mStatus err;
151 FILE *f = fopen(filename, "r");
152
153 if (hostname) hostname->c[0] = 0;
154 if (domain) domain->c[0] = 0;
155 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
156
157 if (f)
158 {
159 if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue;
160 if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf;
161 if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf;
162 buf[0] = 0;
163 GetConfigOption(buf, "secret-64", f); // failure means no authentication
164 fclose(f);
165 f = NULL;
166 }
167 else
168 {
169 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
170 return;
171 }
172
173 if (domain && domain->c[0] && buf[0])
174 {
175 DomainAuthInfo *info = (DomainAuthInfo*)mDNSPlatformMemAllocate(sizeof(*info));
176 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
177 err = mDNS_SetSecretForDomain(m, info, domain, domain, buf, mDNSfalse);
178 if (err) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err, domain->c);
179 }
180
181 return;
182
183 badf:
184 LogMsg("ERROR: malformatted config file");
185 if (f) fclose(f);
186 }
187
188 #if MDNS_DEBUGMSGS
189 mDNSexport void mDNSPlatformWriteDebugMsg(const char *msg)
190 {
191 fprintf(stderr,"%s\n", msg);
192 fflush(stderr);
193 }
194 #endif
195
196 mDNSexport void mDNSPlatformWriteLogMsg(const char *ident, const char *buffer, int logoptflags)
197 {
198 if (mDNS_DebugMode) // In debug mode we write to stderr
199 {
200 fprintf(stderr,"%s\n", buffer);
201 fflush(stderr);
202 }
203 else // else, in production mode, we write to syslog
204 {
205 openlog(ident, LOG_CONS | logoptflags, LOG_DAEMON);
206 syslog(LOG_ERR, "%s", buffer);
207 closelog();
208 }
209 }