]>
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 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
24 Change History (most recent first):
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
30 Revision 1.2 2004/12/01 03:30:29 cheshire
31 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
33 Revision 1.1 2004/12/01 01:51:35 cheshire
34 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
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
45 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
46 #include "PlatformCommon.h"
48 #ifdef NOT_HAVE_SOCKLEN_T
49 typedef unsigned int socklen_t
;
52 // Bind a UDP socket to a global destination to find the default route's interface address
53 mDNSexport
void FindDefaultRouteIP(mDNSAddr
*a
)
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; }
66 a
->type
= mDNSAddrType_IPv4
;
67 a
->ip
.v4
.NotAnInteger
= addr
.sin_addr
.s_addr
;
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
)
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
79 if (!strncmp(buf
, option
, len
))
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';
84 if (len
&& dst
[len
-1] == '\n') dst
[len
-1] = '\0'; // chop newline
88 debugf("Option %s not set", option
);
92 mDNSexport
void ReadDDNSSettingsFromConfFile(mDNS
*const m
, const char *const filename
, domainname
*const hostname
, domainname
*const domain
)
94 char zone
[MAX_ESCAPED_DOMAIN_NAME
];
95 char fqdn
[MAX_ESCAPED_DOMAIN_NAME
];
96 char secret
[MAX_ESCAPED_DOMAIN_NAME
] = "";
99 FILE *f
= fopen(filename
, "r");
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
114 if (errno
!= ENOENT
) LogMsg("ERROR: Config file exists, but cannot be opened.");
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
);
129 LogMsg("ERROR: malformatted config file");