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