]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/PlatformCommon.c
mDNSResponder-108.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 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23
24 Change History (most recent first):
25
26 $Log: PlatformCommon.c,v $
27 Revision 1.6 2005/04/08 21:30:16 ksekar
28 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
29 Patch submitted by Bernd Kuhls
30
31 Revision 1.5 2005/02/01 19:33:30 ksekar
32 <rdar://problem/3985239> Keychain format too restrictive
33
34 Revision 1.4 2005/01/19 19:19:21 ksekar
35 <rdar://problem/3960191> Need a way to turn off domain discovery
36
37 Revision 1.3 2004/12/13 17:46:52 cheshire
38 Use sizeof(buf) instead of fixed constant 1024
39
40 Revision 1.2 2004/12/01 03:30:29 cheshire
41 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
42
43 Revision 1.1 2004/12/01 01:51:35 cheshire
44 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
45
46 */
47
48 #include <stdio.h> // Needed for fopen() etc.
49 #include <unistd.h> // Needed for close()
50 #include <string.h> // Needed for strlen() etc.
51 #include <errno.h> // Needed for errno etc.
52 #include <sys/socket.h> // Needed for socket() etc.
53 #include <netinet/in.h> // Needed for sockaddr_in
54
55 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
56 #include "PlatformCommon.h"
57
58 #ifdef NOT_HAVE_SOCKLEN_T
59 typedef unsigned int socklen_t;
60 #endif
61
62 // Bind a UDP socket to a global destination to find the default route's interface address
63 mDNSexport void FindDefaultRouteIP(mDNSAddr *a)
64 {
65 struct sockaddr_in addr;
66 socklen_t len = sizeof(addr);
67 int sock = socket(AF_INET,SOCK_DGRAM,0);
68 a->type = mDNSAddrType_None;
69 if (sock == -1) return;
70 addr.sin_family = AF_INET;
71 addr.sin_port = 1; // Not important, any port and public address will do
72 addr.sin_addr.s_addr = 0x11111111;
73 if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return; }
74 if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return; }
75 close(sock);
76 a->type = mDNSAddrType_IPv4;
77 a->ip.v4.NotAnInteger = addr.sin_addr.s_addr;
78 }
79
80 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
81 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
82 {
83 char buf[32+1+MAX_ESCAPED_DOMAIN_NAME]; // Option name, one space, option value
84 unsigned int len = strlen(option);
85 if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; }
86 fseek(f, 0, SEEK_SET); // set position to beginning of stream
87 while (fgets(buf, sizeof(buf), f)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
88 {
89 if (!strncmp(buf, option, len))
90 {
91 strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1);
92 if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0';
93 len = strlen(dst);
94 if (len && dst[len-1] == '\n') dst[len-1] = '\0'; // chop newline
95 return mDNStrue;
96 }
97 }
98 debugf("Option %s not set", option);
99 return mDNSfalse;
100 }
101
102 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
103 {
104 char buf [MAX_ESCAPED_DOMAIN_NAME];
105 char secret[MAX_ESCAPED_DOMAIN_NAME] = "";
106 mStatus err;
107 FILE *f = fopen(filename, "r");
108
109 if (hostname) hostname->c[0] = 0;
110 if (domain) domain->c[0] = 0;
111 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
112
113 if (f)
114 {
115 if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue;
116 if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf;
117 if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf;
118 GetConfigOption(secret, "secret-64", f); // failure means no authentication
119 fclose(f);
120 f = NULL;
121 }
122 else
123 {
124 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
125 return;
126 }
127
128 if (domain && domain->c[0] && secret[0])
129 {
130 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
131 err = mDNS_SetSecretForZone(m, domain, domain, secret);
132 if (err) LogMsg("ERROR: mDNS_SetSecretForZone returned %d for domain %##s", err, domain->c);
133 }
134
135 return;
136
137 badf:
138 LogMsg("ERROR: malformatted config file");
139 if (f) fclose(f);
140 }