Skip to content

Commit

Permalink
Removed third party code and replaced it with a start of a more gener…
Browse files Browse the repository at this point in the history
…al interface intended to simplify the access of SystemConfiguration in terms of network status and information.
  • Loading branch information
monkeydom committed Aug 10, 2018
1 parent 33eda53 commit b2e8e62
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 137 deletions.
16 changes: 8 additions & 8 deletions Port Map.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
F290D63F0EDAEBFB00E643C0 /* portmap_releasenotes.html in Resources */ = {isa = PBXBuildFile; fileRef = F290D63D0EDAEBFB00E643C0 /* portmap_releasenotes.html */; };
F29472930ED3091500DEBA97 /* getgateway.c in Sources */ = {isa = PBXBuildFile; fileRef = F29472890ED3091000DEBA97 /* getgateway.c */; };
F29472940ED3091800DEBA97 /* natpmp.c in Sources */ = {isa = PBXBuildFile; fileRef = F294728D0ED3091000DEBA97 /* natpmp.c */; };
F2B94C29211D7DC300A9EFC1 /* TCMSystemConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = F2B94C27211D7DC300A9EFC1 /* TCMSystemConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
F2B94C2A211D7DC300A9EFC1 /* TCMSystemConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = F2B94C28211D7DC300A9EFC1 /* TCMSystemConfiguration.m */; };
F2C30DD1210DFB28008653EE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F2C30DD0210DFB28008653EE /* Assets.xcassets */; };
F2C30DDC210DFC8C008653EE /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F2C30DD6210DFC8C008653EE /* Localizable.strings */; };
F2C30DDE210DFC8C008653EE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F2C30DDA210DFC8C008653EE /* InfoPlist.strings */; };
F2C65C2D0D50B2D800820CAE /* IXSCNotificationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = F2C65BE70D50B17B00820CAE /* IXSCNotificationManager.h */; };
F2C65C2E0D50B2D900820CAE /* IXSCNotificationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F2C65BE60D50B17B00820CAE /* IXSCNotificationManager.m */; };
F2C65C2F0D50B2E000820CAE /* TCMNATPMPPortMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = F2C65BE30D50B17B00820CAE /* TCMNATPMPPortMapper.m */; };
F2C65C310D50B2E400820CAE /* TCMPortMapper.h in Headers */ = {isa = PBXBuildFile; fileRef = F2C65BE10D50B17B00820CAE /* TCMPortMapper.h */; settings = {ATTRIBUTES = (Public, ); }; };
F2C65C320D50B2E500820CAE /* TCMPortMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = F2C65BE00D50B17B00820CAE /* TCMPortMapper.m */; };
Expand Down Expand Up @@ -217,6 +217,8 @@
F29472900ED3091000DEBA97 /* natpmpc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = natpmpc.c; sourceTree = "<group>"; };
F29472910ED3091000DEBA97 /* testgetgateway.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = testgetgateway.c; sourceTree = "<group>"; };
F29472920ED3091000DEBA97 /* wingettimeofday.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = wingettimeofday.c; sourceTree = "<group>"; };
F2B94C27211D7DC300A9EFC1 /* TCMSystemConfiguration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = TCMSystemConfiguration.h; path = framework/TCMSystemConfiguration.h; sourceTree = "<group>"; };
F2B94C28211D7DC300A9EFC1 /* TCMSystemConfiguration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = TCMSystemConfiguration.m; path = framework/TCMSystemConfiguration.m; sourceTree = "<group>"; };
F2C30DD0210DFB28008653EE /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
F2C30DD7210DFC8C008653EE /* English */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = English; path = Localizable.strings; sourceTree = "<group>"; };
F2C30DDB210DFC8C008653EE /* English */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = English; path = InfoPlist.strings; sourceTree = "<group>"; };
Expand All @@ -228,8 +230,6 @@
F2C65BE30D50B17B00820CAE /* TCMNATPMPPortMapper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TCMNATPMPPortMapper.m; path = framework/TCMNATPMPPortMapper.m; sourceTree = "<group>"; };
F2C65BE40D50B17B00820CAE /* TCMNATPMPPortMapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TCMNATPMPPortMapper.h; path = framework/TCMNATPMPPortMapper.h; sourceTree = "<group>"; };
F2C65BE50D50B17B00820CAE /* NSNotificationCenterThreadingAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSNotificationCenterThreadingAdditions.m; path = framework/NSNotificationCenterThreadingAdditions.m; sourceTree = "<group>"; };
F2C65BE60D50B17B00820CAE /* IXSCNotificationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IXSCNotificationManager.m; path = framework/IXSCNotificationManager.m; sourceTree = "<group>"; };
F2C65BE70D50B17B00820CAE /* IXSCNotificationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IXSCNotificationManager.h; path = framework/IXSCNotificationManager.h; sourceTree = "<group>"; };
F2C65BE80D50B17B00820CAE /* NSNotificationCenterThreadingAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSNotificationCenterThreadingAdditions.h; path = framework/NSNotificationCenterThreadingAdditions.h; sourceTree = "<group>"; };
F2C65FFF0D50C2D100820CAE /* Port Map.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Port Map.app"; sourceTree = BUILT_PRODUCTS_DIR; };
F2C6602A0D50C58800820CAE /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = portmap/main.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -465,8 +465,8 @@
F2C65BE30D50B17B00820CAE /* TCMNATPMPPortMapper.m */,
F2C65BE80D50B17B00820CAE /* NSNotificationCenterThreadingAdditions.h */,
F2C65BE50D50B17B00820CAE /* NSNotificationCenterThreadingAdditions.m */,
F2C65BE70D50B17B00820CAE /* IXSCNotificationManager.h */,
F2C65BE60D50B17B00820CAE /* IXSCNotificationManager.m */,
F2B94C27211D7DC300A9EFC1 /* TCMSystemConfiguration.h */,
F2B94C28211D7DC300A9EFC1 /* TCMSystemConfiguration.m */,
F2C65C2C0D50B2B700820CAE /* Resources */,
);
name = TCMPortMapper.framework;
Expand Down Expand Up @@ -538,7 +538,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
F2C65C2D0D50B2D800820CAE /* IXSCNotificationManager.h in Headers */,
F2B94C29211D7DC300A9EFC1 /* TCMSystemConfiguration.h in Headers */,
F2C65C310D50B2E400820CAE /* TCMPortMapper.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -720,7 +720,6 @@
buildActionMask = 2147483647;
files = (
F2DF551F210B77440071DF2D /* upnpdev.c in Sources */,
F2C65C2E0D50B2D900820CAE /* IXSCNotificationManager.m in Sources */,
F2C65C2F0D50B2E000820CAE /* TCMNATPMPPortMapper.m in Sources */,
F2C65C320D50B2E500820CAE /* TCMPortMapper.m in Sources */,
F2DF551A210B76240071DF2D /* portlistingparse.c in Sources */,
Expand All @@ -730,6 +729,7 @@
F2301B5B0D685C2700728699 /* igd_desc_parse.c in Sources */,
F2301B5C0D685C2A00728699 /* minisoap.c in Sources */,
F2301B5D0D685C3300728699 /* minissdpc.c in Sources */,
F2B94C2A211D7DC300A9EFC1 /* TCMSystemConfiguration.m in Sources */,
F2301B5F0D685C3B00728699 /* miniupnpc.c in Sources */,
F2301B600D685C3F00728699 /* miniwget.c in Sources */,
F2301B610D685C4200728699 /* minixml.c in Sources */,
Expand Down
39 changes: 0 additions & 39 deletions framework/IXSCNotificationManager.h

This file was deleted.

63 changes: 0 additions & 63 deletions framework/IXSCNotificationManager.m

This file was deleted.

66 changes: 39 additions & 27 deletions framework/TCMPortMapper.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "TCMPortMapper.h"
#import "TCMNATPMPPortMapper.h"
#import "TCMUPNPPortMapper.h"
#import "IXSCNotificationManager.h"
#import "TCMSystemConfiguration.h"
#import "NSNotificationCenterThreadingAdditions.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/SCSchemaDefinitions.h>
Expand Down Expand Up @@ -114,7 +114,6 @@ @interface TCMPortMapper () {
TCMUPNPPortMapper *_UPNPPortMapper;
NSMutableSet *_portMappings;
NSMutableSet *_removeMappingQueue;
IXSCNotificationManager *_systemConfigNotificationManager;
BOOL _isRunning;
int _NATPMPStatus;
int _UPNPStatus;
Expand All @@ -125,6 +124,8 @@ @interface TCMPortMapper () {
NSTimer *_upnpPortMapperTimer;
BOOL _ignoreNetworkChanges;
BOOL _refreshIsScheduled;

NSMutableSet *_systemConfigurationObservations;
}

@property (nonatomic, strong, readwrite) NSString *externalIPAddress;
Expand All @@ -142,20 +143,16 @@ @implementation TCMPortMapper
static TCMPortMapper *S_sharedInstance;

+ (instancetype)sharedInstance {
if (!S_sharedInstance) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
S_sharedInstance = [self new];
}
});
return S_sharedInstance;
}

- (id)init {
if (S_sharedInstance) {
return S_sharedInstance;
}
if ((self=[super init])) {
_systemConfigNotificationManager = [IXSCNotificationManager new];
// since we are only interested in this specific key, let us configure it so.
[_systemConfigNotificationManager setObservedKeys:[NSArray arrayWithObject:@"State:/Network/Global/IPv4"] regExes:nil];
- (instancetype)init {
self = [super init];
if (self) {
_isRunning = NO;
_ignoreNetworkChanges = NO;
_refreshIsScheduled = NO;
Expand Down Expand Up @@ -183,6 +180,8 @@ - (id)init {

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(didWake:) name:NSWorkspaceDidWakeNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(willSleep:) name:NSWorkspaceWillSleepNotification object:nil];

[self startObservingSystemConfiguration];
}
return self;
}
Expand All @@ -202,10 +201,7 @@ - (BOOL)networkReachable {
return result;
}

- (void)networkDidChange:(NSNotification *)aNotification {
#ifndef NDEBUG
NSLog(@"%s %@",__FUNCTION__,aNotification);
#endif
- (void)handleNetworkChange {
if (!_ignoreNetworkChanges) {
[self scheduleRefresh];
}
Expand Down Expand Up @@ -321,6 +317,10 @@ - (void)updatePortMappings {

- (void)addPortMapping:(TCMPortMapping *)aMapping {
@synchronized(_portMappings) {
if (aMapping.mappingStatus != TCMPortMappingStatusUnmapped &&
![_portMappings containsObject:aMapping]) {
[aMapping setMappingStatus:TCMPortMappingStatusUnmapped];
}
[_portMappings addObject:aMapping];
}
[self updatePortMappings];
Expand Down Expand Up @@ -360,8 +360,9 @@ - (void)refresh {
NSEnumerator *portMappings = [_portMappings objectEnumerator];
TCMPortMapping *portMapping = nil;
while ((portMapping = [portMappings nextObject])) {
if ([portMapping mappingStatus]==TCMPortMappingStatusMapped)
if (portMapping.mappingStatus != TCMPortMappingStatusUnmapped) {
[portMapping setMappingStatus:TCMPortMappingStatusUnmapped];
}
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:TCMPortMapperWillStartSearchForRouterNotification object:self];
Expand Down Expand Up @@ -542,13 +543,10 @@ + (NSString *)manufacturerForHardwareAddress:(NSString *)MACAddress {

- (void)start {
if (!_isRunning) {
[self startObservingSystemConfiguration];

NSNotificationCenter *center=[NSNotificationCenter defaultCenter];

[center addObserver:self
selector:@selector(networkDidChange:)
name:@"State:/Network/Global/IPv4"
object:_systemConfigNotificationManager];


[center addObserver:self
selector:@selector(NATPMPPortMapperDidGetExternalIPAddress:)
name:TCMNATPMPPortMapperDidGetExternalIPAddressNotification
Expand Down Expand Up @@ -659,18 +657,32 @@ - (void)cleanupUPNPPortMapperTimer {
}
}

- (void)startObservingSystemConfiguration {
_systemConfigurationObservations = [NSMutableSet new];
id observation = [[TCMSystemConfiguration sharedConfiguration] observeConfigurationKeys:@[@"State:/Network/Global/IPv4"/*, @"State:/Network/Global/IPv6" */] observationBlock:^(TCMSystemConfiguration *config, NSArray<NSString *> *changedKeys) {
[self handleNetworkChange];
}];
[_systemConfigurationObservations addObject:observation];
}

- (void)stopObservingSystemConfiguration {
for (id observation in _systemConfigurationObservations) {
[[TCMSystemConfiguration sharedConfiguration] removeConfigurationKeyObservation:observation];
}
[_systemConfigurationObservations removeAllObjects];
}

- (void)internalStop {
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center removeObserver:self name:@"State:/Network/Global/IPv4" object:_systemConfigNotificationManager];
[self stopObservingSystemConfiguration];

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:TCMNATPMPPortMapperDidGetExternalIPAddressNotification object:_NATPMPPortMapper];
[center removeObserver:self name:TCMNATPMPPortMapperDidFailNotification object:_NATPMPPortMapper];
[center removeObserver:self name:TCMNATPMPPortMapperDidReceiveBroadcastedExternalIPChangeNotification object:_NATPMPPortMapper];

[center removeObserver:self name:TCMUPNPPortMapperDidGetExternalIPAddressNotification object:_UPNPPortMapper];
[center removeObserver:self name:TCMUPNPPortMapperDidFailNotification object:_UPNPPortMapper];

[self cleanupUPNPPortMapperTimer];

}

- (void)stop {
Expand Down
19 changes: 19 additions & 0 deletions framework/TCMSystemConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TCMSystemConfiguration.h
// TCMPortMapper
//

@import Foundation;

@class TCMSystemConfiguration;
typedef void (^TCMSystemConfigurationDidChangeCallback)(TCMSystemConfiguration *config, NSArray<NSString *> *changedKeys);

NS_ASSUME_NONNULL_BEGIN
@interface TCMSystemConfiguration : NSObject
+ (instancetype)sharedConfiguration;

- (id)observeConfigurationKeys:(NSArray<NSString *> *)keys observationBlock:(TCMSystemConfigurationDidChangeCallback)callbackBlock;
- (void)removeConfigurationKeyObservation:(id)observation;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit b2e8e62

Please sign in to comment.