]>
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.7 2006/08/14 23:24:56 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.6 2005/04/08 21:30:16 ksekar
24 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
25 Patch submitted by Bernd Kuhls
27 Revision 1.5 2005/02/01 19:33:30 ksekar
28 <rdar://problem/3985239> Keychain format too restrictive
30 Revision 1.4 2005/01/19 19:19:21 ksekar
31 <rdar://problem/3960191> Need a way to turn off domain discovery
33 Revision 1.3 2004/12/13 17:46:52 cheshire
34 Use sizeof(buf) instead of fixed constant 1024
36 Revision 1.2 2004/12/01 03:30:29 cheshire
37 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
39 Revision 1.1 2004/12/01 01:51:35 cheshire
40 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
44 #include <stdio.h> // Needed for fopen() etc.
45 #include <unistd.h> // Needed for close()
46 #include <string.h> // Needed for strlen() etc.
47 #include <errno.h> // Needed for errno etc.
48 #include <sys/socket.h> // Needed for socket() etc.
49 #include <netinet/in.h> // Needed for sockaddr_in
51 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
52 #include "PlatformCommon.h"
54 #ifdef NOT_HAVE_SOCKLEN_T
55 typedef unsigned int socklen_t
;
58 // Bind a UDP socket to a global destination to find the default route's interface address
59 mDNSexport
void FindDefaultRouteIP(mDNSAddr
*a
)
61 struct sockaddr_in addr
;
62 socklen_t len
= sizeof(addr
);
63 int sock
= socket(AF_INET
,SOCK_DGRAM
,0);
64 a
->type
= mDNSAddrType_None
;
65 if (sock
== -1) return;
66 addr
.sin_family
= AF_INET
;
67 addr
.sin_port
= 1; // Not important, any port and public address will do
68 addr
.sin_addr
.s_addr
= 0x11111111;
69 if ((connect(sock
,(const struct sockaddr
*)&addr
,sizeof(addr
))) == -1) { close(sock
); return; }
70 if ((getsockname(sock
,(struct sockaddr
*)&addr
, &len
)) == -1) { close(sock
); return; }
72 a
->type
= mDNSAddrType_IPv4
;
73 a
->ip
.v4
.NotAnInteger
= addr
.sin_addr
.s_addr
;
76 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
77 mDNSlocal mDNSBool
GetConfigOption(char *dst
, const char *option
, FILE *f
)
79 char buf
[32+1+MAX_ESCAPED_DOMAIN_NAME
]; // Option name, one space, option value
80 unsigned int len
= strlen(option
);
81 if (len
+ 1 + MAX_ESCAPED_DOMAIN_NAME
> sizeof(buf
)-1) { LogMsg("GetConfigOption: option %s too long", option
); return mDNSfalse
; }
82 fseek(f
, 0, SEEK_SET
); // set position to beginning of stream
83 while (fgets(buf
, sizeof(buf
), f
)) // Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
85 if (!strncmp(buf
, option
, len
))
87 strncpy(dst
, buf
+ len
+ 1, MAX_ESCAPED_DOMAIN_NAME
-1);
88 if (dst
[MAX_ESCAPED_DOMAIN_NAME
-1]) dst
[MAX_ESCAPED_DOMAIN_NAME
-1] = '\0';
90 if (len
&& dst
[len
-1] == '\n') dst
[len
-1] = '\0'; // chop newline
94 debugf("Option %s not set", option
);
98 mDNSexport
void ReadDDNSSettingsFromConfFile(mDNS
*const m
, const char *const filename
, domainname
*const hostname
, domainname
*const domain
, mDNSBool
*DomainDiscoveryDisabled
)
100 char buf
[MAX_ESCAPED_DOMAIN_NAME
];
101 char secret
[MAX_ESCAPED_DOMAIN_NAME
] = "";
103 FILE *f
= fopen(filename
, "r");
105 if (hostname
) hostname
->c
[0] = 0;
106 if (domain
) domain
->c
[0] = 0;
107 if (DomainDiscoveryDisabled
) *DomainDiscoveryDisabled
= mDNSfalse
;
111 if (DomainDiscoveryDisabled
&& GetConfigOption(buf
, "DomainDiscoveryDisabled", f
) && !strcasecmp(buf
, "true")) *DomainDiscoveryDisabled
= mDNStrue
;
112 if (hostname
&& GetConfigOption(buf
, "hostname", f
) && !MakeDomainNameFromDNSNameString(hostname
, buf
)) goto badf
;
113 if (domain
&& GetConfigOption(buf
, "zone", f
) && !MakeDomainNameFromDNSNameString(domain
, buf
)) goto badf
;
114 GetConfigOption(secret
, "secret-64", f
); // failure means no authentication
120 if (errno
!= ENOENT
) LogMsg("ERROR: Config file exists, but cannot be opened.");
124 if (domain
&& domain
->c
[0] && secret
[0])
126 // for now we assume keyname = service reg domain and we use same key for service and hostname registration
127 err
= mDNS_SetSecretForZone(m
, domain
, domain
, secret
);
128 if (err
) LogMsg("ERROR: mDNS_SetSecretForZone returned %d for domain %##s", err
, domain
->c
);
134 LogMsg("ERROR: malformatted config file");