Last active
December 21, 2016 14:53
-
-
Save yarshure/c751112d66ea204c1ce9 to your computer and use it in GitHub Desktop.
process_ip
This file contains hidden or 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
| // | |
| // ViewController.swift | |
| // testgcdt | |
| // | |
| // Created by yarshure on 16/1/11. | |
| // Copyright © 2016年 yarshure. All rights reserved. | |
| // | |
| import Cocoa | |
| extension NSMutableData { | |
| func write<T>(value:T){ | |
| var v = value | |
| if v is String{ | |
| if let v = v as? String,let data = v.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true){ | |
| self.appendData(data) | |
| } | |
| }else if v is NSData{ | |
| if let v = v as? NSData{ | |
| self.appendData(v) | |
| } | |
| }else if v is UInt8 || v is UInt16 || v is UInt32 || v is UInt64 || v is Int8 || v is Int16 || v is Int32 || v is Int64{ | |
| self.appendBytes(&v, length: sizeof(T)) | |
| }else{ | |
| assertionFailure("write unsupport type") | |
| } | |
| } | |
| } | |
| class ViewController: NSViewController { | |
| var count = 0 | |
| var db = NSMutableData() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let path = NSBundle.mainBundle().pathForResource("ipcn.txt", ofType: nil) | |
| let x = try! NSString.init(contentsOfFile: path!, encoding: NSUTF8StringEncoding) | |
| let lins = x.componentsSeparatedByString("\n") | |
| //count = lins.count | |
| for line in lins{ | |
| if line != "" { | |
| let result = ipFromString(line) | |
| if result.start != 0{ | |
| db.write(result.start) | |
| db.write(result.end) | |
| count++ | |
| } | |
| //print("\(ipString(result.start))-\(ipString(result.end))") | |
| } | |
| } | |
| let req = inet_addr("125.90.93.20".cStringUsingEncoding(NSUTF8StringEncoding)) | |
| if binSearch(req.byteSwapped) { | |
| print("found record ") | |
| } | |
| // Do any additional setup after loading the view. | |
| } | |
| func ipFromString(str:String) -> (start:UInt32,end:UInt32) { | |
| //print("org \(str)") | |
| let iprange = str.componentsSeparatedByString("/") | |
| let x:UInt32 = 0xffffffff | |
| let start:UInt32 = inet_addr(iprange.first!.cStringUsingEncoding(NSUTF8StringEncoding)!) | |
| var end:UInt32 = UInt32(iprange.last!)! | |
| if end == 32 { | |
| return (0,0) | |
| } | |
| end = x >> end | |
| end = start.byteSwapped | end | |
| //print("end \(ipString(end.byteSwapped))") | |
| //print("start:\(start.byteSwapped) end:\(end)") | |
| //let ipInt:UInt32 = ipFromString(iprange.first!) | |
| //print(ipInt.byteSwapped) | |
| return (start,end.byteSwapped) | |
| } | |
| func binSearch(ip:UInt32) ->Bool { | |
| var startPtr:UnsafePointer<UInt32> = UnsafePointer(db.bytes) | |
| var endPtr:UnsafePointer<UInt32> = UnsafePointer(startPtr + (count-1)*2) | |
| var midPtr:UnsafePointer<UInt32> = UnsafePointer(startPtr + (count-1)*2) | |
| var startip:UInt32 = 0 | |
| var midip:UInt32 = 0 | |
| var endip:UInt32 = 0 | |
| repeat { | |
| //test first range | |
| memcpy(&startip, startPtr,4) | |
| memcpy(&endip,startPtr + 1,4) | |
| print("\(ipString(startip))-\(ipString(endip)) req:\(ipString(ip.byteSwapped))") | |
| if ip >= startip.byteSwapped && ip <= endip.byteSwapped { | |
| return true | |
| } | |
| //test last range | |
| memcpy(&startip, endPtr,4) | |
| memcpy(&endip,endPtr + 1,4) | |
| print("\(ipString(startip))-\(ipString(endip)) req:\(ipString(ip.byteSwapped))") | |
| if ip >= startip.byteSwapped && ip <= endip.byteSwapped { | |
| return true | |
| } | |
| // test mid range | |
| let x = (endPtr - startPtr) / 2 | |
| midPtr = startPtr + x | |
| memcpy(&startip, midPtr,4) | |
| memcpy(&endip,midPtr + 1,4) | |
| print("\(ipString(startip))-\(ipString(endip)) req:\(ipString(ip.byteSwapped))") | |
| if ip >= startip.byteSwapped && ip <= endip.byteSwapped { | |
| return true | |
| } | |
| if ip < startip.byteSwapped { | |
| endPtr = midPtr | |
| } | |
| if ip > endip.byteSwapped{ | |
| startPtr = midPtr | |
| } | |
| } while (endPtr - startPtr) > 2 | |
| return true | |
| } | |
| func ipString(ip:UInt32) ->String{ | |
| let a = (ip & 0xFF) | |
| let b = (ip >> 8 & 0xFF) | |
| let c = (ip >> 16 & 0xFF) | |
| let d = (ip >> 24 & 0xFF) | |
| return "\(a)." + "\(b)." + "\(c)." + "\(d)" | |
| } | |
| override var representedObject: AnyObject? { | |
| didSet { | |
| // Update the view, if already loaded. | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment