jens@26: // PortMapperTest.m jens@26: // MYNetwork jens@26: // jens@26: // Created by Jens Alfke on 1/4/08. jens@26: // Copyright 2008 Jens Alfke. All rights reserved. jens@26: // jens@26: jens@26: jens@26: jens@26: #import "MYPortMapper.h" jens@26: #import "IPAddress.h" jens@26: #import "Test.h" jens@26: #import "Logging.h" jens@26: jens@26: #if DEBUG jens@26: jens@26: jens@26: /** A trivial class that just demonstrates how to create a MYPortMapper and listen for changes. */ jens@26: @interface MYPortMapperTest : NSObject jens@26: { jens@26: MYPortMapper *_mapper; jens@26: } jens@26: jens@26: @end jens@26: jens@26: jens@26: @implementation MYPortMapperTest jens@26: jens@26: jens@26: - (id) init jens@26: { jens@26: self = [super init]; jens@26: if( self ) { jens@26: // Create MYPortMapper. This example doesn't open a real socket; just pretend that there's jens@26: // already a TCP socket listening on port 8080. To experiment, you could turn on Web jens@26: // Sharing, then change this port number to 80 -- that will make your computer's local jens@26: // Apache web server reachable from the outside world while this program is running. jens@26: _mapper = [[MYPortMapper alloc] initWithLocalPort: 80]; jens@26: jens@26: // Optionally, request a public port number. jens@26: // The NAT is free to ignore this and return a random number instead. jens@26: _mapper.desiredPublicPort = 22222; jens@26: jens@26: // Now open the mapping (asynchronously): jens@26: if( [_mapper open] ) { jens@26: Log(@"Opening port mapping..."); jens@26: // Now listen for notifications to find out when the mapping opens, fails, or changes: jens@26: [[NSNotificationCenter defaultCenter] addObserver: self jens@26: selector: @selector(portMappingChanged:) jens@26: name: MYPortMapperChangedNotification jens@26: object: _mapper]; jens@26: } else { jens@26: // MYPortMapper failed -- this is unlikely, but be graceful: jens@26: Log(@"!! Error: MYPortMapper wouldn't start: %i",_mapper.error); jens@26: [self release]; jens@26: return nil; jens@26: } jens@26: } jens@26: return self; jens@26: } jens@26: jens@26: jens@26: - (void) portMappingChanged: (NSNotification*)n jens@26: { jens@26: // This is where we get notified that the mapping was created, or that no mapping exists, jens@26: // or that mapping failed. jens@26: if( _mapper.error ) jens@26: Log(@"!! MYPortMapper error %i", _mapper.error); jens@26: else { jens@26: NSString *message = @""; jens@26: if( !_mapper.isMapped ) jens@26: message = @" (no address translation!)"; jens@26: Log(@"** Public address:port is %@%@", _mapper.publicAddress, message); jens@26: Log(@" local address:port is %@", _mapper.localAddress); jens@26: } jens@26: } jens@26: jens@26: jens@26: - (void) dealloc jens@26: { jens@26: [_mapper close]; jens@26: [_mapper release]; jens@26: [super dealloc]; jens@26: } jens@26: jens@26: jens@26: @end jens@26: jens@26: jens@26: jens@26: TestCase(MYPortMapper) { jens@26: jens@26: EnableLogTo(PortMapper,YES); jens@26: // Here's how to simply obtain your local and public address(es): jens@26: IPAddress *addr = [IPAddress localAddress]; jens@26: Log(@"** Local address is %@%@ ...getting public addr...", jens@26: addr, (addr.isPrivate ?@" (private)" :@"")); jens@26: addr = [MYPortMapper findPublicAddress]; jens@26: Log(@"** Public address is %@", addr); jens@26: jens@26: // Start up the test class to create a mapping: jens@26: MYPortMapperTest *test = [[MYPortMapperTest alloc] init]; jens@26: jens@26: // Now let the runloop run forever... jens@26: Log(@"Running the runloop forever..."); jens@26: [[NSRunLoop currentRunLoop] run]; jens@26: jens@26: [test release]; jens@26: } jens@26: jens@26: #endif DEBUG