]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/PlatformCommon.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
19 $Log: PlatformCommon.c,v $
20 Revision 1.14 2007/12/03 18:37:26 cheshire
21 Moved mDNSPlatformWriteLogMsg & mDNSPlatformWriteDebugMsg
22 from mDNSMacOSX.c to PlatformCommon.c, so that Posix build can use them
24 Revision 1.13 2007/10/22 20:07:07 cheshire
25 Moved mDNSPlatformSourceAddrForDest from mDNSMacOSX.c to PlatformCommon.c so
26 Posix build can share the code (better than just pasting it into mDNSPosix.c)
28 Revision 1.12 2007/10/16 17:19:53 cheshire
29 <rdar://problem/3557903> Performance: Core code will not work on platforms with small stacks
30 Cut ReadDDNSSettingsFromConfFile stack from 2112 to 1104 bytes
32 Revision 1.11 2007/07/31 23:08:34 mcguire
33 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
35 Revision 1.10 2007/07/11 02:59:58 cheshire
36 <rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services
37 Add AutoTunnel parameter to mDNS_SetSecretForDomain
39 Revision 1.9 2007/01/09 22:37:44 cheshire
40 Remove unused ClearDomainSecrets() function
42 Revision 1.8 2006/12/22 20:59:51 cheshire
43 <rdar://problem/4742742> Read *all* DNS keys from keychain,
44 not just key for the system-wide default registration domain
46 Revision 1.7 2006/08/14 23:24:56 cheshire
47 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
49 Revision 1.6 2005/04/08 21:30:16 ksekar
50 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
51 Patch submitted by Bernd Kuhls
53 Revision 1.5 2005/02/01 19:33:30 ksekar
54 <rdar://problem/3985239> Keychain format too restrictive
56 Revision 1.4 2005/01/19 19:19:21 ksekar
57 <rdar://problem/3960191> Need a way to turn off domain discovery
59 Revision 1.3 2004/12/13 17:46:52 cheshire
60 Use sizeof(buf) instead of fixed constant 1024
62 Revision 1.2 2004/12/01 03:30:29 cheshire
63 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
65 Revision 1.1 2004/12/01 01:51:35 cheshire
66 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
70 #include <stdio.h> // Needed for fopen() etc.
71 #include <unistd.h> // Needed for close()
72 #include <string.h> // Needed for strlen() etc.
73 #include <errno.h> // Needed for errno etc.
74 #include <sys/socket.h> // Needed for socket() etc.
75 #include <netinet/in.h> // Needed for sockaddr_in
78 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
79 #include "DNSCommon.h"
80 #include "PlatformCommon.h"
82 #ifdef NOT_HAVE_SOCKLEN_T
83 typedef unsigned int socklen_t
;
86 // Bind a UDP socket to find the source address to a destination
87 mDNSexport
void mDNSPlatformSourceAddrForDest(mDNSAddr
*const src
, const mDNSAddr
*const dst
)
89 union { struct sockaddr s
; struct sockaddr_in a4
; struct sockaddr_in6 a6
; } addr
;
90 socklen_t len
= sizeof(addr
);
91 int sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
92 src
->type
= mDNSAddrType_None
;
93 if (sock
== -1) return;
94 if (dst
->type
== mDNSAddrType_IPv4
)
96 addr
.a4
.sin_len
= sizeof(addr
.a4
);
97 addr
.a4
.sin_family
= AF_INET
;
98 addr
.a4
.sin_port
= 1; // Not important, any port will do
99 addr
.a4
.sin_addr
.s_addr
= dst
->ip
.v4
.NotAnInteger
;
101 else if (dst
->type
== mDNSAddrType_IPv6
)
103 addr
.a6
.sin6_len
= sizeof(addr
.a6
);
104 addr
.a6
.sin6_family
= AF_INET6
;
105 addr
.a6
.sin6_flowinfo
= 0;
106 addr
.a6
.sin6_port
= 1; // Not important, any port will do
107 addr
.a6
.sin6_addr
= *(struct in6_addr
*)&dst
->ip
.v6
;
108 addr
.a6
.sin6_scope_id
= 0;
112 if ((connect(sock
, &addr
.s
, addr
.s
.sa_len
)) < 0)
113 { LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst
, errno
, strerror(errno
)); goto exit
; }
115 if ((getsockname(sock
, &addr
.s
, &len
)) < 0)
116 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno
, strerror(errno
)); goto exit
; }
118 src
->type
= dst
->type
;
119 if (dst
->type
== mDNSAddrType_IPv4
) src
->ip
.v4
.NotAnInteger
= addr
.a4
.sin_addr
.s_addr
;
120 else src
->ip
.v6
= *(mDNSv6Addr
*)&addr
.a6
.sin6_addr
;
125 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
126 mDNSlocal mDNSBool
GetConfigOption(char *dst
, const char *option
, FILE *f
)
128 char buf
[32+1+MAX_ESCAPED_DOMAIN_NAME
]; // Option name, one space, option value
129 unsigned int len
= strlen(option
);
130 if (len
+ 1 + MAX_ESCAPED_DOMAIN_NAME
> sizeof(buf
)-1) { LogMsg("GetConfigOption: option %s too long", option
); return mDNSfalse
; }
131 fseek(f
, 0, SEEK_SET
); // set position to beginning of stream
132 while (fgets(buf
, sizeof(buf
), f
)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
134 if (!strncmp(buf
, option
, len
))
136 strncpy(dst
, buf
+ len
+ 1, MAX_ESCAPED_DOMAIN_NAME
-1);
137 if (dst
[MAX_ESCAPED_DOMAIN_NAME
-1]) dst
[MAX_ESCAPED_DOMAIN_NAME
-1] = '\0';
139 if (len
&& dst
[len
-1] == '\n') dst
[len
-1] = '\0'; // chop newline
143 debugf("Option %s not set", option
);
147 mDNSexport
void ReadDDNSSettingsFromConfFile(mDNS
*const m
, const char *const filename
, domainname
*const hostname
, domainname
*const domain
, mDNSBool
*DomainDiscoveryDisabled
)
149 char buf
[MAX_ESCAPED_DOMAIN_NAME
] = "";
151 FILE *f
= fopen(filename
, "r");
153 if (hostname
) hostname
->c
[0] = 0;
154 if (domain
) domain
->c
[0] = 0;
155 if (DomainDiscoveryDisabled
) *DomainDiscoveryDisabled
= mDNSfalse
;
159 if (DomainDiscoveryDisabled
&& GetConfigOption(buf
, "DomainDiscoveryDisabled", f
) && !strcasecmp(buf
, "true")) *DomainDiscoveryDisabled
= mDNStrue
;
160 if (hostname
&& GetConfigOption(buf
, "hostname", f
) && !MakeDomainNameFromDNSNameString(hostname
, buf
)) goto badf
;
161 if (domain
&& GetConfigOption(buf
, "zone", f
) && !MakeDomainNameFromDNSNameString(domain
, buf
)) goto badf
;
163 GetConfigOption(buf
, "secret-64", f
); // failure means no authentication
169 if (errno
!= ENOENT
) LogMsg("ERROR: Config file exists, but cannot be opened.");
173 if (domain
&& domain
->c
[0] && buf
[0])
175 DomainAuthInfo
*info
= (DomainAuthInfo
*)mDNSPlatformMemAllocate(sizeof(*info
));
176 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
177 err
= mDNS_SetSecretForDomain(m
, info
, domain
, domain
, buf
, mDNSfalse
);
178 if (err
) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err
, domain
->c
);
184 LogMsg("ERROR: malformatted config file");
189 mDNSexport
void mDNSPlatformWriteDebugMsg(const char *msg
)
191 fprintf(stderr
,"%s\n", msg
);
196 mDNSexport
void mDNSPlatformWriteLogMsg(const char *ident
, const char *buffer
, int logoptflags
)
198 if (mDNS_DebugMode
) // In debug mode we write to stderr
200 fprintf(stderr
,"%s\n", buffer
);
203 else // else, in production mode, we write to syslog
205 openlog(ident
, LOG_CONS
| logoptflags
, LOG_DAEMON
);
206 syslog(LOG_ERR
, "%s", buffer
);