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.17 2008/03/05 00:19:09 cheshire
21 Conditionalize LogTimeStamps so it's specific to APPLE_OSX, for now
23 Revision 1.16 2008/02/26 21:47:45 cheshire
24 Added cast to avoid compiler warning
26 Revision 1.15 2008/02/26 21:42:26 cheshire
27 Added 'LogTimeStamps' option, to show ms-granularity timestamps on every log message
29 Revision 1.14 2007/12/03 18:37:26 cheshire
30 Moved mDNSPlatformWriteLogMsg & mDNSPlatformWriteDebugMsg
31 from mDNSMacOSX.c to PlatformCommon.c, so that Posix build can use them
33 Revision 1.13 2007/10/22 20:07:07 cheshire
34 Moved mDNSPlatformSourceAddrForDest from mDNSMacOSX.c to PlatformCommon.c so
35 Posix build can share the code (better than just pasting it into mDNSPosix.c)
37 Revision 1.12 2007/10/16 17:19:53 cheshire
38 <rdar://problem/3557903> Performance: Core code will not work on platforms with small stacks
39 Cut ReadDDNSSettingsFromConfFile stack from 2112 to 1104 bytes
41 Revision 1.11 2007/07/31 23:08:34 mcguire
42 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
44 Revision 1.10 2007/07/11 02:59:58 cheshire
45 <rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services
46 Add AutoTunnel parameter to mDNS_SetSecretForDomain
48 Revision 1.9 2007/01/09 22:37:44 cheshire
49 Remove unused ClearDomainSecrets() function
51 Revision 1.8 2006/12/22 20:59:51 cheshire
52 <rdar://problem/4742742> Read *all* DNS keys from keychain,
53 not just key for the system-wide default registration domain
55 Revision 1.7 2006/08/14 23:24:56 cheshire
56 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
58 Revision 1.6 2005/04/08 21:30:16 ksekar
59 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
60 Patch submitted by Bernd Kuhls
62 Revision 1.5 2005/02/01 19:33:30 ksekar
63 <rdar://problem/3985239> Keychain format too restrictive
65 Revision 1.4 2005/01/19 19:19:21 ksekar
66 <rdar://problem/3960191> Need a way to turn off domain discovery
68 Revision 1.3 2004/12/13 17:46:52 cheshire
69 Use sizeof(buf) instead of fixed constant 1024
71 Revision 1.2 2004/12/01 03:30:29 cheshire
72 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
74 Revision 1.1 2004/12/01 01:51:35 cheshire
75 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
79 #include <stdio.h> // Needed for fopen() etc.
80 #include <unistd.h> // Needed for close()
81 #include <string.h> // Needed for strlen() etc.
82 #include <errno.h> // Needed for errno etc.
83 #include <sys/socket.h> // Needed for socket() etc.
84 #include <netinet/in.h> // Needed for sockaddr_in
87 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
88 #include "DNSCommon.h"
89 #include "PlatformCommon.h"
91 #ifdef NOT_HAVE_SOCKLEN_T
92 typedef unsigned int socklen_t
;
95 // Bind a UDP socket to find the source address to a destination
96 mDNSexport
void mDNSPlatformSourceAddrForDest(mDNSAddr
*const src
, const mDNSAddr
*const dst
)
98 union { struct sockaddr s
; struct sockaddr_in a4
; struct sockaddr_in6 a6
; } addr
;
99 socklen_t len
= sizeof(addr
);
100 int sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
101 src
->type
= mDNSAddrType_None
;
102 if (sock
== -1) return;
103 if (dst
->type
== mDNSAddrType_IPv4
)
105 addr
.a4
.sin_len
= sizeof(addr
.a4
);
106 addr
.a4
.sin_family
= AF_INET
;
107 addr
.a4
.sin_port
= 1; // Not important, any port will do
108 addr
.a4
.sin_addr
.s_addr
= dst
->ip
.v4
.NotAnInteger
;
110 else if (dst
->type
== mDNSAddrType_IPv6
)
112 addr
.a6
.sin6_len
= sizeof(addr
.a6
);
113 addr
.a6
.sin6_family
= AF_INET6
;
114 addr
.a6
.sin6_flowinfo
= 0;
115 addr
.a6
.sin6_port
= 1; // Not important, any port will do
116 addr
.a6
.sin6_addr
= *(struct in6_addr
*)&dst
->ip
.v6
;
117 addr
.a6
.sin6_scope_id
= 0;
121 if ((connect(sock
, &addr
.s
, addr
.s
.sa_len
)) < 0)
122 { LogMsg("mDNSPlatformSourceAddrForDest: connect %#a failed errno %d (%s)", dst
, errno
, strerror(errno
)); goto exit
; }
124 if ((getsockname(sock
, &addr
.s
, &len
)) < 0)
125 { LogMsg("mDNSPlatformSourceAddrForDest: getsockname failed errno %d (%s)", errno
, strerror(errno
)); goto exit
; }
127 src
->type
= dst
->type
;
128 if (dst
->type
== mDNSAddrType_IPv4
) src
->ip
.v4
.NotAnInteger
= addr
.a4
.sin_addr
.s_addr
;
129 else src
->ip
.v6
= *(mDNSv6Addr
*)&addr
.a6
.sin6_addr
;
134 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
135 mDNSlocal mDNSBool
GetConfigOption(char *dst
, const char *option
, FILE *f
)
137 char buf
[32+1+MAX_ESCAPED_DOMAIN_NAME
]; // Option name, one space, option value
138 unsigned int len
= strlen(option
);
139 if (len
+ 1 + MAX_ESCAPED_DOMAIN_NAME
> sizeof(buf
)-1) { LogMsg("GetConfigOption: option %s too long", option
); return mDNSfalse
; }
140 fseek(f
, 0, SEEK_SET
); // set position to beginning of stream
141 while (fgets(buf
, sizeof(buf
), f
)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
143 if (!strncmp(buf
, option
, len
))
145 strncpy(dst
, buf
+ len
+ 1, MAX_ESCAPED_DOMAIN_NAME
-1);
146 if (dst
[MAX_ESCAPED_DOMAIN_NAME
-1]) dst
[MAX_ESCAPED_DOMAIN_NAME
-1] = '\0';
148 if (len
&& dst
[len
-1] == '\n') dst
[len
-1] = '\0'; // chop newline
152 debugf("Option %s not set", option
);
156 mDNSexport
void ReadDDNSSettingsFromConfFile(mDNS
*const m
, const char *const filename
, domainname
*const hostname
, domainname
*const domain
, mDNSBool
*DomainDiscoveryDisabled
)
158 char buf
[MAX_ESCAPED_DOMAIN_NAME
] = "";
160 FILE *f
= fopen(filename
, "r");
162 if (hostname
) hostname
->c
[0] = 0;
163 if (domain
) domain
->c
[0] = 0;
164 if (DomainDiscoveryDisabled
) *DomainDiscoveryDisabled
= mDNSfalse
;
168 if (DomainDiscoveryDisabled
&& GetConfigOption(buf
, "DomainDiscoveryDisabled", f
) && !strcasecmp(buf
, "true")) *DomainDiscoveryDisabled
= mDNStrue
;
169 if (hostname
&& GetConfigOption(buf
, "hostname", f
) && !MakeDomainNameFromDNSNameString(hostname
, buf
)) goto badf
;
170 if (domain
&& GetConfigOption(buf
, "zone", f
) && !MakeDomainNameFromDNSNameString(domain
, buf
)) goto badf
;
172 GetConfigOption(buf
, "secret-64", f
); // failure means no authentication
178 if (errno
!= ENOENT
) LogMsg("ERROR: Config file exists, but cannot be opened.");
182 if (domain
&& domain
->c
[0] && buf
[0])
184 DomainAuthInfo
*info
= (DomainAuthInfo
*)mDNSPlatformMemAllocate(sizeof(*info
));
185 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
186 err
= mDNS_SetSecretForDomain(m
, info
, domain
, domain
, buf
, mDNSfalse
);
187 if (err
) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err
, domain
->c
);
193 LogMsg("ERROR: malformatted config file");
198 mDNSexport
void mDNSPlatformWriteDebugMsg(const char *msg
)
200 fprintf(stderr
,"%s\n", msg
);
205 mDNSexport
void mDNSPlatformWriteLogMsg(const char *ident
, const char *buffer
, int logoptflags
)
207 #if APPLE_OSX_mDNSResponder && LogTimeStamps
208 extern mDNS mDNSStorage
;
209 extern mDNSu32 mDNSPlatformClockDivisor
;
210 mDNSs32 t
= mDNSStorage
.timenow
? mDNSStorage
.timenow
: mDNSPlatformClockDivisor
? mDNS_TimeNow_NoLock(&mDNSStorage
) : 0;
211 int ms
= ((t
< 0) ? -t
: t
) % 1000;
214 if (mDNS_DebugMode
) // In debug mode we write to stderr
216 #if APPLE_OSX_mDNSResponder && LogTimeStamps
217 if (mDNSPlatformClockDivisor
)
218 fprintf(stderr
,"%8d.%03d: %s\n", (int)(t
/1000), ms
, buffer
);
221 fprintf(stderr
,"%s\n", buffer
);
224 else // else, in production mode, we write to syslog
226 openlog(ident
, LOG_CONS
| logoptflags
, LOG_DAEMON
);
227 #if APPLE_OSX_mDNSResponder && LogTimeStamps
228 if (mDNSPlatformClockDivisor
)
229 syslog(LOG_ERR
, "%8d.%03d: %s", (int)(t
/1000), ms
, buffer
);
232 syslog(LOG_ERR
, "%s", buffer
);