Last active
May 12, 2016 09:45
-
-
Save acalism/474ed64bd2882b993a0cfc3b255d2b98 to your computer and use it in GitHub Desktop.
DNS 解析
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Uncomment the following line to preserve selection between presentations. | |
// self.clearsSelectionOnViewWillAppear = false | |
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. | |
// self.navigationItem.rightBarButtonItem = self.editButtonItem | |
let g = ESGlobal.sharedInstance() | |
segmentedControl.selectedSegmentIndex = g.networkType() | |
textView0.text = g.debugInfo().description | |
// 发起域名解析请求 | |
//var variableSelf = self | |
var clientContext = CFHostClientContext(version: 0, info: unsafeBitCast(self, UnsafeMutablePointer<Void>.self), retain: nil, release: nil, copyDescription: { (i: UnsafePointer<Void>) -> Unmanaged<CFString>! in | |
let desc = UnsafePointer<ESNetworkTableViewController>(i).memory.description as CFString | |
return Unmanaged<CFString>.passRetained(desc) | |
//Unmanaged.passRetained(CFCopyDescription(Unmanaged.fromOpaque(COpaquePointer(i)).takeUnretainedValue())) | |
}) | |
byod_showHUD("") | |
var cfError = CFStreamError() | |
let hostRef = CFHostCreateWithName(kCFAllocatorDefault, "mazutest.3g.qq.com").takeRetainedValue() | |
CFHostSetClient(hostRef, { (host: CFHost, hostInfo: CFHostInfoType, error: UnsafePointer<CFStreamError>, info: UnsafeMutablePointer<Void>) in | |
print("callback done"); print(host); print(hostInfo); print(error.memory); print(info) | |
let vc = unsafeBitCast(info, ESNetworkTableViewController.self)// UnsafePointer<ESNetworkTableViewController>(info).memory | |
vc.byod_hideHUD() | |
var resolved: DarwinBoolean = false | |
guard let arr = CFHostGetAddressing(host, &resolved)?.takeRetainedValue() where resolved else { | |
return | |
} | |
let data = (arr as Array).first as! NSData | |
var storage = sockaddr_storage() | |
data.getBytes(&storage, length: sizeof(sockaddr_storage)) | |
if Int32(storage.ss_family) == AF_INET { | |
let addr4 = withUnsafePointer(&storage) { UnsafePointer<sockaddr_in>($0).memory } | |
let ipv4Address = String(CString: inet_ntoa(addr4.sin_addr), encoding: NSASCIIStringEncoding) | |
print(ipv4Address) | |
let inTestEnvironment = ipv4Address == IPv4AddressForNetwork.Test.rawValue | |
vc.segmentedControl.setEnabled(!inTestEnvironment, forSegmentAtIndex: 1) | |
vc.segmentedControl.setEnabled(inTestEnvironment, forSegmentAtIndex: 2) | |
} | |
}, &clientContext) | |
CFHostScheduleWithRunLoop(hostRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode /* kCFRunLoopCommonModes */); | |
CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, &cfError) | |
} | |
//#include <errno.h> | |
//#include <netdb.h> | |
//#include <sys/types.h> | |
//#include <sys/socket.h> | |
//#include <netinet/in.h> | |
//#include <arpa/inet.h> | |
// | |
//@implementation ESNetworkTableViewController | |
// | |
//- (void)viewDidLoad { | |
// [super viewDidLoad]; | |
// CFStreamError err; | |
// | |
// CFHostClientContext cfhostclientcontext; | |
// cfhostclientcontext.version = 0; | |
// cfhostclientcontext.retain = nil; | |
// cfhostclientcontext.release = nil; | |
// cfhostclientcontext.copyDescription = nil; | |
// cfhostclientcontext.info = (__bridge void * _Nullable)(self); | |
// | |
// [self byod_showHUD:@""]; | |
// | |
// CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, CFSTR("mazutest.3g.qq.com")); | |
// CFHostSetClient(hostRef, callBackFunction, &cfhostclientcontext); | |
// CFHostScheduleWithRunLoop(hostRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | |
// CFHostStartInfoResolution(hostRef, kCFHostAddresses, &err); | |
//} | |
// | |
//void callBackFunction(CFHostRef theHost, CFHostInfoType typeInfo, const CFStreamError * __nullable error, void * __nullable info) { | |
// ESNetworkTableViewController *vc = (__bridge ESNetworkTableViewController *)(info); | |
// [vc byod_hideHUD]; | |
// | |
// Boolean resolved = NO; | |
// CFArrayRef arrRef = CFHostGetAddressing(theHost, &resolved); | |
// if (!resolved) { | |
// return; | |
// } | |
// CFDataRef dataRef = CFArrayGetValueAtIndex(arrRef, 0); | |
// /* | |
// struct sockaddr *sa = (struct sockaddr *)CFDataGetBytePtr(dataRef); | |
// NSString *addr0 = @(sa->sa_data); // 空字符串 | |
// NSLog(@"addr0: %@", addr0); | |
// */ | |
// | |
// struct sockaddr_in * remoteAddr = (struct sockaddr_in *)CFDataGetBytePtr(dataRef); | |
// NSString *addr1 = @(inet_ntoa(remoteAddr->sin_addr)); // such as, 14.17.41.149 | |
// | |
// BOOL inTestEnv = [addr1 isEqualToString:kIPForTestNetwork]; | |
// [vc.segmentedControl setEnabled:!inTestEnv forSegmentAtIndex:1]; | |
// [vc.segmentedControl setEnabled:inTestEnv forSegmentAtIndex:2]; | |
//} | |
//@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment