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