1/* 2 * Copyright (c) 2000, 2001, 2003-2007 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24/* 25 * Modification History 26 * 27 * January 19, 2003 Allan Nathanson <ajn@apple.com> 28 * - add advanced reachability APIs 29 * 30 * June 10, 2001 Allan Nathanson <ajn@apple.com> 31 * - updated to use service-based "State:" information 32 * 33 * June 1, 2001 Allan Nathanson <ajn@apple.com> 34 * - public API conversion 35 * 36 * January 30, 2001 Allan Nathanson <ajn@apple.com> 37 * - initial revision 38 */ 39 40#include <sys/types.h> 41#include <netinet/in.h> 42#include <unistd.h> 43#include <sys/socket.h> 44 45#include <SystemConfiguration/SystemConfiguration.h> 46#include <SystemConfiguration/SCPrivate.h> 47 48Boolean 49SCNetworkCheckReachabilityByAddress(const struct sockaddr *address, 50 socklen_t addrlen, 51 SCNetworkConnectionFlags *flags) 52{ 53 SCNetworkReachabilityRef networkAddress; 54 Boolean ok; 55 struct sockaddr_storage ss; 56 57 if (!address || 58 (addrlen == 0) || 59 (addrlen > (int)sizeof(struct sockaddr_storage))) { 60 _SCErrorSet(kSCStatusInvalidArgument); 61 return FALSE; 62 } 63 64 bzero(&ss, sizeof(ss)); 65 bcopy(address, &ss, addrlen); 66 ss.ss_len = addrlen; 67 68 networkAddress = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&ss); 69 if (networkAddress == NULL) { 70 return FALSE; 71 } 72 73 ok = SCNetworkReachabilityGetFlags(networkAddress, flags); 74 CFRelease(networkAddress); 75 return ok; 76} 77 78 79Boolean 80SCNetworkCheckReachabilityByName(const char *nodename, 81 SCNetworkConnectionFlags *flags) 82{ 83 SCNetworkReachabilityRef networkAddress; 84 Boolean ok; 85 86 if (!nodename) { 87 _SCErrorSet(kSCStatusInvalidArgument); 88 return FALSE; 89 } 90 91 networkAddress = SCNetworkReachabilityCreateWithName(NULL, nodename); 92 if (networkAddress == NULL) { 93 return FALSE; 94 } 95 96 ok = SCNetworkReachabilityGetFlags(networkAddress, flags); 97 CFRelease(networkAddress); 98 return ok; 99} 100