]>
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.11 2007/07/31 23:08:34 mcguire
21 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
23 Revision 1.10 2007/07/11 02:59:58 cheshire
24 <rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services
25 Add AutoTunnel parameter to mDNS_SetSecretForDomain
27 Revision 1.9 2007/01/09 22:37:44 cheshire
28 Remove unused ClearDomainSecrets() function
30 Revision 1.8 2006/12/22 20:59:51 cheshire
31 <rdar://problem/4742742> Read *all* DNS keys from keychain,
32 not just key for the system-wide default registration domain
34 Revision 1.7 2006/08/14 23:24:56 cheshire
35 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
37 Revision 1.6 2005/04/08 21:30:16 ksekar
38 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
39 Patch submitted by Bernd Kuhls
41 Revision 1.5 2005/02/01 19:33:30 ksekar
42 <rdar://problem/3985239> Keychain format too restrictive
44 Revision 1.4 2005/01/19 19:19:21 ksekar
45 <rdar://problem/3960191> Need a way to turn off domain discovery
47 Revision 1.3 2004/12/13 17:46:52 cheshire
48 Use sizeof(buf) instead of fixed constant 1024
50 Revision 1.2 2004/12/01 03:30:29 cheshire
51 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
53 Revision 1.1 2004/12/01 01:51:35 cheshire
54 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
58 #include <stdio.h> // Needed for fopen() etc.
59 #include <unistd.h> // Needed for close()
60 #include <string.h> // Needed for strlen() etc.
61 #include <errno.h> // Needed for errno etc.
62 #include <sys/socket.h> // Needed for socket() etc.
63 #include <netinet/in.h> // Needed for sockaddr_in
65 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
66 #include "DNSCommon.h"
67 #include "PlatformCommon.h"
69 #ifdef NOT_HAVE_SOCKLEN_T
70 typedef unsigned int socklen_t
;
73 // Bind a UDP socket to find the source address to a destination
74 mDNSexport
void FindSourceAddrForIP(mDNSAddr
*const dst
, mDNSAddr
*src
)
76 struct sockaddr_in addr
;
77 socklen_t len
= sizeof(addr
);
78 int sock
= socket(AF_INET
,SOCK_DGRAM
,0);
79 src
->type
= mDNSAddrType_None
;
80 if (sock
== -1) return;
81 if (dst
->type
!= mDNSAddrType_IPv4
) return; // Only support v4 for now
82 addr
.sin_family
= AF_INET
;
83 addr
.sin_port
= 1; // Not important, any port will do
84 if (dst
== NULL
) addr
.sin_addr
.s_addr
= 0x11111111;
85 else addr
.sin_addr
.s_addr
= dst
->ip
.v4
.NotAnInteger
;
86 if ((connect(sock
,(const struct sockaddr
*)&addr
,sizeof(addr
))) == -1) { close(sock
); return; }
87 if ((getsockname(sock
,(struct sockaddr
*)&addr
, &len
)) == -1) { close(sock
); return; }
89 src
->type
= mDNSAddrType_IPv4
;
90 src
->ip
.v4
.NotAnInteger
= addr
.sin_addr
.s_addr
;
93 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
94 mDNSlocal mDNSBool
GetConfigOption(char *dst
, const char *option
, FILE *f
)
96 char buf
[32+1+MAX_ESCAPED_DOMAIN_NAME
]; // Option name, one space, option value
97 unsigned int len
= strlen(option
);
98 if (len
+ 1 + MAX_ESCAPED_DOMAIN_NAME
> sizeof(buf
)-1) { LogMsg("GetConfigOption: option %s too long", option
); return mDNSfalse
; }
99 fseek(f
, 0, SEEK_SET
); // set position to beginning of stream
100 while (fgets(buf
, sizeof(buf
), f
)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
102 if (!strncmp(buf
, option
, len
))
104 strncpy(dst
, buf
+ len
+ 1, MAX_ESCAPED_DOMAIN_NAME
-1);
105 if (dst
[MAX_ESCAPED_DOMAIN_NAME
-1]) dst
[MAX_ESCAPED_DOMAIN_NAME
-1] = '\0';
107 if (len
&& dst
[len
-1] == '\n') dst
[len
-1] = '\0'; // chop newline
111 debugf("Option %s not set", option
);
115 mDNSexport
void ReadDDNSSettingsFromConfFile(mDNS
*const m
, const char *const filename
, domainname
*const hostname
, domainname
*const domain
, mDNSBool
*DomainDiscoveryDisabled
)
117 char buf
[MAX_ESCAPED_DOMAIN_NAME
];
118 char secret
[MAX_ESCAPED_DOMAIN_NAME
] = "";
120 FILE *f
= fopen(filename
, "r");
122 if (hostname
) hostname
->c
[0] = 0;
123 if (domain
) domain
->c
[0] = 0;
124 if (DomainDiscoveryDisabled
) *DomainDiscoveryDisabled
= mDNSfalse
;
128 if (DomainDiscoveryDisabled
&& GetConfigOption(buf
, "DomainDiscoveryDisabled", f
) && !strcasecmp(buf
, "true")) *DomainDiscoveryDisabled
= mDNStrue
;
129 if (hostname
&& GetConfigOption(buf
, "hostname", f
) && !MakeDomainNameFromDNSNameString(hostname
, buf
)) goto badf
;
130 if (domain
&& GetConfigOption(buf
, "zone", f
) && !MakeDomainNameFromDNSNameString(domain
, buf
)) goto badf
;
131 GetConfigOption(secret
, "secret-64", f
); // failure means no authentication
137 if (errno
!= ENOENT
) LogMsg("ERROR: Config file exists, but cannot be opened.");
141 if (domain
&& domain
->c
[0] && secret
[0])
143 DomainAuthInfo
*info
= (DomainAuthInfo
*)mDNSPlatformMemAllocate(sizeof(*info
));
144 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
145 err
= mDNS_SetSecretForDomain(m
, info
, domain
, domain
, secret
, mDNSfalse
);
146 if (err
) LogMsg("ERROR: mDNS_SetSecretForDomain returned %d for domain %##s", err
, domain
->c
);
152 LogMsg("ERROR: malformatted config file");