#import "Controller.h" #import void PMInitCallback(enum PMResult result, void *context) { [(id)context updateRouterInfo:result]; } void PMResultCallback(enum PMResult result, int publicPort, int privatePort, enum PMProtocol protocol, void *context) { NSString *message; NSString *title = @"Failed"; // figure out if we succeded or not, and let the user know switch(result) { case kPMNotInitializedError: message = @"Not Initialized"; break; case kPMPortMappingInUseError: message = @"External port is mapped to a different computer."; break; case kPMPortMappingError: message = @"Failed to map/unmap the port."; break; case kPMPortMappingDisabledError: message = @"User has disabled the port mapping feature on their router."; break; case kPMNoSuchPortMappingError: message = @"Can't remove the port map since it doesn't exist."; break; default: message = @"No Error"; title = @"Succeded"; break; } NSBeginAlertSheet(title, @"OK", NULL, NULL, (id)context, NULL, NULL, NULL, NULL, message); } @implementation Controller - (void)awakeFromNib { PMInitializeAsync(PMInitCallback, [[NSRunLoop currentRunLoop] getCFRunLoop] , self); } - (IBAction)addPortMap:(id)sender { PMAddPortMappingAsync([oPublicPort intValue], [oPrivatePort intValue], [[oProtocol selectedItem] tag], PMResultCallback, [[NSRunLoop currentRunLoop] getCFRunLoop], oWindow); } - (IBAction)removePortMap:(id)sender { PMRemovePortMappingAsync([oPublicPort intValue], [oPrivatePort intValue], [[oProtocol selectedItem] tag], PMResultCallback, [[NSRunLoop currentRunLoop] getCFRunLoop], oWindow); } - (void)updateRouterInfo:(int)result { // check to make sure we found a router if(result == kPMSuccessful) { // get some info about the router and display it [oRouterName setStringValue:(NSString*)PMGetRouterName()]; [oRouterType setStringValue:(PMGetRouterType() == kPMUPnPRouterType) ? @"UPnP" : @"NAT-PMP"]; [oPublicIP setStringValue:(NSString*)PMGetPublicIPAddress()]; [oRemoveButton setEnabled:YES]; [oAddButton setEnabled:YES]; } else { // we failed... figure out why and show a nice error message NSString *msg = nil; switch(result) { case kPMInitializationError: msg = @"Generic Initialization Error"; break; case kPMNoRouterFoundError: msg = @"Couldn't find a router"; break; default: msg = @"Known Error"; } NSBeginAlertSheet(@"PortMapper Initialization Failed", @"OK", nil, nil, oWindow, nil, nil,nil,nil, @"%@\nResult Code: %d", msg, result); } } @end