]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/PlatformCommon.c
mDNSResponder-98.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.4 2005/01/19 19:19:21 ksekar
28 <rdar://problem/3960191> Need a way to turn off domain discovery
29
30 Revision 1.3 2004/12/13 17:46:52 cheshire
31 Use sizeof(buf) instead of fixed constant 1024
32
33 Revision 1.2 2004/12/01 03:30:29 cheshire
34 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
35
36 Revision 1.1 2004/12/01 01:51:35 cheshire
37 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
38
39 */
40
41 #include <stdio.h> // Needed for fopen() etc.
42 #include <unistd.h> // Needed for close()
43 #include <string.h> // Needed for strlen() etc.
44 #include <sys/errno.h> // Needed for errno etc.
45 #include <sys/socket.h> // Needed for socket() etc.
46 #include <netinet/in.h> // Needed for sockaddr_in
47
48 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
49 #include "PlatformCommon.h"
50
51 #ifdef NOT_HAVE_SOCKLEN_T
52 typedef unsigned int socklen_t;
53 #endif
54
55 // Bind a UDP socket to a global destination to find the default route's interface address
56 mDNSexport void FindDefaultRouteIP(mDNSAddr *a)
57 {
58 struct sockaddr_in addr;
59 socklen_t len = sizeof(addr);
60 int sock = socket(AF_INET,SOCK_DGRAM,0);
61 a->type = mDNSAddrType_None;
62 if (sock == -1) return;
63 addr.sin_family = AF_INET;
64 addr.sin_port = 1; // Not important, any port and public address will do
65 addr.sin_addr.s_addr = 0x11111111;
66 if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return; }
67 if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return; }
68 close(sock);
69 a->type = mDNSAddrType_IPv4;
70 a->ip.v4.NotAnInteger = addr.sin_addr.s_addr;
71 }
72
73 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
74 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
75 {
76 char buf[32+1+MAX_ESCAPED_DOMAIN_NAME]; // Option name, one space, option value
77 unsigned int len = strlen(option);
78 if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; }
79 fseek(f, 0, SEEK_SET); // set position to beginning of stream
80 while (fgets(buf, sizeof(buf), f)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
81 {
82 if (!strncmp(buf, option, len))
83 {
84 strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1);
85 if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0';
86 len = strlen(dst);
87 if (len && dst[len-1] == '\n') dst[len-1] = '\0'; // chop newline
88 return mDNStrue;
89 }
90 }
91 debugf("Option %s not set", option);
92 return mDNSfalse;
93 }
94
95 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
96 {
97 char buf [MAX_ESCAPED_DOMAIN_NAME];
98 char secret[MAX_ESCAPED_DOMAIN_NAME] = "";
99 int slen;
100 mStatus err;
101 FILE *f = fopen(filename, "r");
102
103 if (hostname) hostname->c[0] = 0;
104 if (domain) domain->c[0] = 0;
105 if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
106
107 if (f)
108 {
109 if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue;
110 if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf;
111 if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf;
112 GetConfigOption(secret, "secret-64", f); // failure means no authentication
113 fclose(f);
114 f = NULL;
115 }
116 else
117 {
118 if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
119 return;
120 }
121
122 if (domain && domain->c[0] && secret[0])
123 {
124 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
125 slen = strlen(secret);
126 err = mDNS_SetSecretForZone(m, domain, domain, secret, slen, mDNStrue);
127 if (err) LogMsg("ERROR: mDNS_SetSecretForZone returned %d for domain %##s", err, domain->c);
128 }
129
130 return;
131
132 badf:
133 LogMsg("ERROR: malformatted config file");
134 if (f) fclose(f);
135 }