PlatformCommon.c revision 4904:cd464a980538
1234353Sdim/* -*- Mode: C; tab-width: 4 -*-
2193323Sed *
3193323Sed * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4193323Sed *
5193323Sed * Licensed under the Apache License, Version 2.0 (the "License");
6193323Sed * you may not use this file except in compliance with the License.
7193323Sed * You may obtain a copy of the License at
8193323Sed *
9193323Sed *     http://www.apache.org/licenses/LICENSE-2.0
10224145Sdim *
11193323Sed * Unless required by applicable law or agreed to in writing, software
12193323Sed * distributed under the License is distributed on an "AS IS" BASIS,
13193323Sed * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14193323Sed * See the License for the specific language governing permissions and
15193323Sed * limitations under the License.
16193323Sed
17224145Sdim    Change History (most recent first):
18193323Sed
19193323Sed$Log: PlatformCommon.c,v $
20224145SdimRevision 1.7  2006/08/14 23:24:56  cheshire
21224145SdimRe-licensed mDNSResponder daemon source code under Apache License, Version 2.0
22224145Sdim
23193323SedRevision 1.6  2005/04/08 21:30:16  ksekar
24224145Sdim<rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
25198090SrdivackyPatch submitted by Bernd Kuhls
26224145Sdim
27234353SdimRevision 1.5  2005/02/01 19:33:30  ksekar
28193323Sed<rdar://problem/3985239> Keychain format too restrictive
29193323Sed
30193323SedRevision 1.4  2005/01/19 19:19:21  ksekar
31203954Srdivacky<rdar://problem/3960191> Need a way to turn off domain discovery
32203954Srdivacky
33193323SedRevision 1.3  2004/12/13 17:46:52  cheshire
34224145SdimUse sizeof(buf) instead of fixed constant 1024
35224145Sdim
36193323SedRevision 1.2  2004/12/01 03:30:29  cheshire
37193323Sed<rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
38193323Sed
39193323SedRevision 1.1  2004/12/01 01:51:35  cheshire
40193323SedMove ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
41193323Sed
42193323Sed */
43224145Sdim
44203954Srdivacky#pragma ident	"%Z%%M%	%I%	%E% SMI"
45203954Srdivacky
46203954Srdivacky#include <stdio.h>				// Needed for fopen() etc.
47203954Srdivacky#include <unistd.h>				// Needed for close()
48203954Srdivacky#include <string.h>				// Needed for strlen() etc.
49203954Srdivacky#include <errno.h>			// Needed for errno etc.
50203954Srdivacky#include <sys/socket.h>			// Needed for socket() etc.
51203954Srdivacky#include <netinet/in.h>			// Needed for sockaddr_in
52203954Srdivacky
53203954Srdivacky#include "mDNSEmbeddedAPI.h"	// Defines the interface provided to the client layer above
54203954Srdivacky#include "PlatformCommon.h"
55193323Sed
56193323Sed#ifdef NOT_HAVE_SOCKLEN_T
57193323Sed    typedef unsigned int socklen_t;
58193323Sed#endif
59193323Sed
60// Bind a UDP socket to a global destination to find the default route's interface address
61mDNSexport void FindDefaultRouteIP(mDNSAddr *a)
62	{
63	struct sockaddr_in addr;
64	socklen_t len = sizeof(addr);
65	int sock = socket(AF_INET,SOCK_DGRAM,0);
66	a->type = mDNSAddrType_None;
67	if (sock == -1) return;
68	addr.sin_family = AF_INET;
69	addr.sin_port = 1;	// Not important, any port and public address will do
70	addr.sin_addr.s_addr = 0x11111111;
71	if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return; }
72	if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return; }
73	close(sock);
74	a->type = mDNSAddrType_IPv4;
75	a->ip.v4.NotAnInteger = addr.sin_addr.s_addr;
76	}
77
78// dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
79mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
80	{
81	char buf[32+1+MAX_ESCAPED_DOMAIN_NAME];	// Option name, one space, option value
82	unsigned int len = strlen(option);
83	if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; }
84	fseek(f, 0, SEEK_SET);  // set position to beginning of stream
85	while (fgets(buf, sizeof(buf), f))		// Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
86		{
87		if (!strncmp(buf, option, len))
88			{
89			strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1);
90			if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0';
91			len = strlen(dst);
92			if (len && dst[len-1] == '\n') dst[len-1] = '\0';  // chop newline
93			return mDNStrue;
94			}
95		}
96	debugf("Option %s not set", option);
97	return mDNSfalse;
98	}
99
100mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
101	{
102	char buf   [MAX_ESCAPED_DOMAIN_NAME];
103	char secret[MAX_ESCAPED_DOMAIN_NAME] = "";
104	mStatus err;
105	FILE *f = fopen(filename, "r");
106
107    if (hostname)                 hostname->c[0] = 0;
108    if (domain)                   domain->c[0] = 0;
109	if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
110
111	if (f)
112		{
113		if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue;
114		if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf;
115		if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf;
116		GetConfigOption(secret, "secret-64", f);  // failure means no authentication
117		fclose(f);
118		f = NULL;
119		}
120	else
121		{
122		if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
123		return;
124		}
125
126	if (domain && domain->c[0] && secret[0])
127		{
128		// for now we assume keyname = service reg domain and we use same key for service and hostname registration
129		err = mDNS_SetSecretForZone(m, domain, domain, secret);
130		if (err) LogMsg("ERROR: mDNS_SetSecretForZone returned %d for domain %##s", err, domain->c);
131		}
132
133	return;
134
135	badf:
136	LogMsg("ERROR: malformatted config file");
137	if (f) fclose(f);
138	}
139