From 74146df90ae1a525b1345989b32100f7614c2161 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Mon, 10 Feb 2025 22:46:06 -0800 Subject: [PATCH] Rename WifiLan to NWFramework(Network.Framework) and put them as common codes. PiperOrigin-RevId: 725486114 --- .../implementation/apple/Mediums/BUILD | 22 +++--- .../{WiFiLAN => NWFramework}/GNCIPv4Address.h | 2 +- .../{WiFiLAN => NWFramework}/GNCIPv4Address.m | 4 +- .../GNCNWFramework.h} | 34 ++++++---- .../GNCNWFramework.m} | 68 ++++++++++--------- .../GNCNWFrameworkError.h} | 16 ++--- .../GNCNWFrameworkError.m} | 6 +- .../GNCNWFrameworkServerSocket+Internal.h} | 10 +-- .../GNCNWFrameworkServerSocket.h} | 8 +-- .../GNCNWFrameworkServerSocket.m} | 32 ++++----- .../GNCNWFrameworkSocket.h} | 4 +- .../GNCNWFrameworkSocket.m} | 8 +-- .../apple/Tests/GNCIPAddressTest.mm | 2 +- .../platform/implementation/apple/wifi_lan.h | 24 +++---- .../platform/implementation/apple/wifi_lan.mm | 37 ++++++---- 15 files changed, 149 insertions(+), 128 deletions(-) rename internal/platform/implementation/apple/Mediums/{WiFiLAN => NWFramework}/GNCIPv4Address.h (98%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN => NWFramework}/GNCIPv4Address.m (94%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANMedium.h => NWFramework/GNCNWFramework.h} (73%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANMedium.m => NWFramework/GNCNWFramework.m} (80%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANError.h => NWFramework/GNCNWFrameworkError.h} (60%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANError.m => NWFramework/GNCNWFrameworkError.m} (73%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANServerSocket+Internal.h => NWFramework/GNCNWFrameworkServerSocket+Internal.h} (79%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANServerSocket.h => NWFramework/GNCNWFrameworkServerSocket.h} (90%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANServerSocket.m => NWFramework/GNCNWFrameworkServerSocket.m} (89%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANSocket.h => NWFramework/GNCNWFrameworkSocket.h} (96%) rename internal/platform/implementation/apple/Mediums/{WiFiLAN/GNCWiFiLANSocket.m => NWFramework/GNCNWFrameworkSocket.m} (94%) diff --git a/internal/platform/implementation/apple/Mediums/BUILD b/internal/platform/implementation/apple/Mediums/BUILD index 5eb6e58d..2059d42d 100644 --- a/internal/platform/implementation/apple/Mediums/BUILD +++ b/internal/platform/implementation/apple/Mediums/BUILD @@ -35,12 +35,12 @@ objc_library( "GNCLeaks.h", "GNCLeaks.m", "GNCMConnection.m", - "WiFiLAN/GNCIPv4Address.m", - "WiFiLAN/GNCWiFiLANError.m", - "WiFiLAN/GNCWiFiLANMedium.m", - "WiFiLAN/GNCWiFiLANServerSocket.m", - "WiFiLAN/GNCWiFiLANServerSocket+Internal.h", - "WiFiLAN/GNCWiFiLANSocket.m", + "NWFramework/GNCIPv4Address.m", + "NWFramework/GNCNWFramework.m", + "NWFramework/GNCNWFrameworkError.m", + "NWFramework/GNCNWFrameworkServerSocket.m", + "NWFramework/GNCNWFrameworkServerSocket+Internal.h", + "NWFramework/GNCNWFrameworkSocket.m", ], hdrs = [ "BLEv2/GNCBLEError.h", @@ -55,11 +55,11 @@ objc_library( "Ble/GNCMBleConnection.h", "Ble/GNCMBleUtils.h", "GNCMConnection.h", - "WiFiLAN/GNCIPv4Address.h", - "WiFiLAN/GNCWiFiLANError.h", - "WiFiLAN/GNCWiFiLANMedium.h", - "WiFiLAN/GNCWiFiLANServerSocket.h", - "WiFiLAN/GNCWiFiLANSocket.h", + "NWFramework/GNCIPv4Address.h", + "NWFramework/GNCNWFramework.h", + "NWFramework/GNCNWFrameworkError.h", + "NWFramework/GNCNWFrameworkServerSocket.h", + "NWFramework/GNCNWFrameworkSocket.h", ], deps = [ "//internal/platform/implementation/apple:Shared", diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h similarity index 98% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h index 6ffce1cc..10386fcf 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.m b/internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.m similarity index 94% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.m rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.m index 3333dda7..6fc72996 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.m +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.m @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h" @implementation GNCIPv4Address diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h similarity index 73% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h index 9ac55431..b07b5ebd 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,18 +15,18 @@ #import @class GNCIPv4Address; -@class GNCWiFiLANServerSocket; -@class GNCWiFiLANSocket; +@class GNCNWFrameworkServerSocket; +@class GNCNWFrameworkSocket; /** A handler that delivers updates about discovered services. */ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName, NSDictionary *_Nonnull txtRecords); /** - * The @c GNCWiFiLANMedium object is used as a delegate by the platform abstraction layer for - * Wi-Fi LAN related functionality on Apple platforms. + * The @c GNCNWFramework object is used as a delegate by the platform abstraction layer for + * Network.Framework medium related functionality on Apple platforms. */ -@interface GNCWiFiLANMedium : NSObject +@interface GNCNWFramework : NSObject /** * Listens for incoming connections on a given port. @@ -41,11 +41,13 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName, * @param port The port on which the listener can accept connections. Should be a number between 1 * and 65536 to open a server socket on that exact port. Zero can be used to listen on * a random port. + * @param includePeerToPeer Whether to include peer-to-peer services. * @param[out] error Error that will be populated on failure. * @return Returns a server socket or nil if an error has occured. */ -- (nullable GNCWiFiLANServerSocket *)listenForServiceOnPort:(NSInteger)port - error:(NSError **_Nullable)error; +- (nullable GNCNWFrameworkServerSocket *)listenForServiceOnPort:(NSInteger)port + includePeerToPeer:(BOOL)includePeerToPeer + error:(NSError **_Nullable)error; /** * Creates a Bonjour service that advertises the listener on the local network. @@ -71,12 +73,14 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName, * Starts browsing for a Bonjour service. * * @param serviceType The Bonjour type of the service. + * @param includePeerToPeer Whether to include peer-to-peer services. * @param serviceFoundHandler A handler called when a new service is found. * @param serviceLostHandler A handler called when a previously discovered service is lost. * @param[out] error Error that will be populated on failure. * @return Returns YES when discovery has successfully started. */ - (BOOL)startDiscoveryForServiceType:(nonnull NSString *)serviceType + includePeerToPeer:(BOOL)includePeerToPeer serviceFoundHandler:(ServiceUpdateHandler)serviceFoundHandler serviceLostHandler:(ServiceUpdateHandler)serviceLostHandler error:(NSError **_Nullable)error; @@ -93,12 +97,14 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName, * * @param serviceName The Bonjour name of the service. * @param serviceType The Bonjour type of the service. + * @param includePeerToPeer Whether to include peer-to-peer services. * @param[out] error Error that will be populated on failure. * @return Returns a connected socket or nil if an error has occured. */ -- (nullable GNCWiFiLANSocket *)connectToServiceName:(nonnull NSString *)serviceName - serviceType:(nonnull NSString *)serviceType - error:(NSError **_Nullable)error; +- (nullable GNCNWFrameworkSocket *)connectToServiceName:(nonnull NSString *)serviceName + serviceType:(nonnull NSString *)serviceType + includePeerToPeer:(BOOL)includePeerToPeer + error:(NSError **_Nullable)error; /** * Connects to an IP address and port. @@ -108,8 +114,8 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName, * @param[out] error Error that will be populated on failure. * @return Returns a connected socket or nil if an error has occured. */ -- (nullable GNCWiFiLANSocket *)connectToHost:(nonnull GNCIPv4Address *)host - port:(NSInteger)port - error:(NSError **_Nullable)error; +- (nullable GNCNWFrameworkSocket *)connectToHost:(nonnull GNCIPv4Address *)host + port:(NSInteger)port + error:(NSError **_Nullable)error; @end diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.m similarity index 80% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.m index b1783208..64e994dd 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.m @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,16 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h" #import #import -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" // An arbitrary timeout that should be pretty lenient. @@ -60,14 +60,14 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu return txtRecords; } -@implementation GNCWiFiLANMedium { +@implementation GNCNWFramework { // Holds a weak reference to a server socket that is retrievable by port. This allows us to stop // advertisements for a given port without taking a strong reference. This keeps the ownership // of the server socket's lifetime with the caller of listenForServiceOnPort:error:. // // Usage of .count should be avoided. Zombie keys won't show up in -keyEnumerator, but WILL be // included in .count until the next time that the internal hashtable is resized. - NSMapTable *_serverSockets; + NSMapTable *_serverSockets; // Maps a Bonjour service type to its browser. This allows us to stop the discovery given the // service type. We maintain ownership of the browser's lifetime, so we can maintain a strong @@ -83,9 +83,11 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu return self; } -- (GNCWiFiLANServerSocket *)listenForServiceOnPort:(NSInteger)port error:(NSError **)error { - GNCWiFiLANServerSocket *serverSocket = [[GNCWiFiLANServerSocket alloc] initWithPort:port]; - BOOL success = [serverSocket startListeningWithError:error]; +- (GNCNWFrameworkServerSocket *)listenForServiceOnPort:(NSInteger)port + includePeerToPeer:(BOOL)includePeerToPeer + error:(NSError **)error { + GNCNWFrameworkServerSocket *serverSocket = [[GNCNWFrameworkServerSocket alloc] initWithPort:port]; + BOOL success = [serverSocket startListeningWithError:error includePeerToPeer:includePeerToPeer]; if (success) { [_serverSockets setObject:serverSocket forKey:@(serverSocket.port)]; return serverSocket; @@ -97,36 +99,37 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu serviceName:(NSString *)serviceName serviceType:(NSString *)serviceType txtRecords:(NSDictionary *)txtRecords { - GNCWiFiLANServerSocket *serverSocket = [_serverSockets objectForKey:@(port)]; + GNCNWFrameworkServerSocket *serverSocket = [_serverSockets objectForKey:@(port)]; [serverSocket startAdvertisingServiceName:serviceName serviceType:serviceType txtRecords:txtRecords]; } - (void)stopAdvertisingPort:(NSInteger)port { - GNCWiFiLANServerSocket *serverSocket = [_serverSockets objectForKey:@(port)]; + GNCNWFrameworkServerSocket *serverSocket = [_serverSockets objectForKey:@(port)]; [serverSocket stopAdvertising]; } - (BOOL)startDiscoveryForServiceType:(NSString *)serviceType + includePeerToPeer:(BOOL)includePeerToPeer serviceFoundHandler:(ServiceUpdateHandler)serviceFoundHandler serviceLostHandler:(ServiceUpdateHandler)serviceLostHandler error:(NSError **)error { if ([_serviceBrowsers objectForKey:serviceType] != nil) { if (error != nil) { - *error = [NSError errorWithDomain:GNCWiFiLANErrorDomain - code:GNCWiFiLANErrorDuplicateDiscovererForServiceType + *error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain + code:GNCNWFrameworkErrorDuplicateDiscovererForServiceType userInfo:nil]; } return NO; } // Create a parameters object configured to support TCP. TLS MUST be disabled for Nearby to - // function properly. This also is set to include peer-to-peer, but unsure if it's required. + // function properly. nw_parameters_t parameters = nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL, /*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION); - nw_parameters_set_include_peer_to_peer(parameters, true); + nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer); nw_browse_descriptor_t descriptor = nw_browse_descriptor_create_bonjour_service([serviceType UTF8String], /*domain=*/nil); nw_browse_descriptor_set_include_txt_record(descriptor, YES); @@ -223,8 +226,8 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu if (!didSignal) { [self stopDiscoveryForServiceType:serviceType]; if (error != nil) { - *error = [NSError errorWithDomain:GNCWiFiLANErrorDomain - code:GNCWiFiLANErrorTimedOut + *error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain + code:GNCNWFrameworkErrorTimedOut userInfo:nil]; } return NO; @@ -251,23 +254,26 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu nw_browser_cancel(browser); } -- (GNCWiFiLANSocket *)connectToServiceName:(NSString *)serviceName - serviceType:(NSString *)serviceType - error:(NSError **)error { +- (GNCNWFrameworkSocket *)connectToServiceName:(NSString *)serviceName + serviceType:(NSString *)serviceType + includePeerToPeer:(BOOL)includePeerToPeer + error:(NSError **)error { nw_endpoint_t endpoint = nw_endpoint_create_bonjour_service([serviceName UTF8String], [serviceType UTF8String], "local"); - return [self connectToEndpoint:endpoint error:error]; + return [self connectToEndpoint:endpoint includePeerToPeer:includePeerToPeer error:error]; } -- (GNCWiFiLANSocket *)connectToHost:(GNCIPv4Address *)host +- (GNCNWFrameworkSocket *)connectToHost:(GNCIPv4Address *)host port:(NSInteger)port error:(NSError **)error { nw_endpoint_t endpoint = nw_endpoint_create_host(host.dottedRepresentation.UTF8String, @(port).stringValue.UTF8String); - return [self connectToEndpoint:endpoint error:error]; + return [self connectToEndpoint:endpoint includePeerToPeer:(BOOL)NO error:error]; } -- (GNCWiFiLANSocket *)connectToEndpoint:(nw_endpoint_t)endpoint error:(NSError **)error { +- (GNCNWFrameworkSocket *)connectToEndpoint:(nw_endpoint_t)endpoint + includePeerToPeer:(BOOL)includePeerToPeer + error:(NSError **)error { NSCondition *condition = [[NSCondition alloc] init]; [condition lock]; @@ -277,7 +283,7 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu nw_parameters_t parameters = nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL, /*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION); - nw_parameters_set_include_peer_to_peer(parameters, true); + nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer); nw_connection_t connection = nw_connection_create(endpoint, parameters); nw_connection_set_queue(connection, dispatch_get_main_queue()); nw_connection_set_state_changed_handler( @@ -303,8 +309,8 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu if (!didSignal) { nw_connection_cancel(connection); if (error != nil) { - *error = [NSError errorWithDomain:GNCWiFiLANErrorDomain - code:GNCWiFiLANErrorTimedOut + *error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain + code:GNCNWFrameworkErrorTimedOut userInfo:nil]; } return nil; @@ -322,7 +328,7 @@ NSDictionary *GNCTXTRecordForBrowseResult(nw_browse_resu case nw_connection_state_cancelled: return nil; case nw_connection_state_ready: - return [[GNCWiFiLANSocket alloc] initWithConnection:connection]; + return [[GNCNWFrameworkSocket alloc] initWithConnection:connection]; } } diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h similarity index 60% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h index 952fe61a..1893dbd6 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,15 +15,15 @@ #import /** - * The domain for @c NSErrors raised by the Wi-Fi LAN medium. + * The domain for @c NSErrors raised by the Apple Network.Framework medium. */ -extern NSErrorDomain const GNCWiFiLANErrorDomain; +extern NSErrorDomain const GNCNWFrameworkErrorDomain; /** - * Wi-Fi LAN medium error codes. + * Apple Network.Framework medium error codes. */ -typedef NS_ERROR_ENUM(GNCWiFiLANErrorDomain, GNCWiFiLANError){ - GNCWiFiLANErrorUnknown, - GNCWiFiLANErrorTimedOut, - GNCWiFiLANErrorDuplicateDiscovererForServiceType, +typedef NS_ERROR_ENUM(GNCNWFrameworkErrorDomain, GNCNWFrameworkError){ + GNCNWFrameworkErrorUnknown, + GNCNWFrameworkErrorTimedOut, + GNCNWFrameworkErrorDuplicateDiscovererForServiceType, }; diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.m similarity index 73% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.m index 062ce267..267b14f2 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.m @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,6 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h" -NSErrorDomain const GNCWiFiLANErrorDomain = @"com.google.nearby.wifilan.error"; +NSErrorDomain const GNCNWFrameworkErrorDomain = @"com.google.nearby.network.error"; diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h similarity index 79% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h index e96f0637..1f569962 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,19 +14,21 @@ #import -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h" -@interface GNCWiFiLANServerSocket (Internal) +@interface GNCNWFrameworkServerSocket (Internal) /** * Starts listening for inbound connections. * * Blocks execution until listening has started or failed. * + * @param includePeerToPeer Indicates if the server should be configured for peer-to-peer + * connections. * @param[out] error Error that will be populated on failure. * @return Returns YES when listening has successfully started. */ -- (BOOL)startListeningWithError:(NSError **_Nullable)error; +- (BOOL)startListeningWithError:(NSError **_Nullable)error includePeerToPeer:(BOOL)peerToPeer; /** * Creates a Bonjour service that advertises the listener on the local network. diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h similarity index 90% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h index 278193f5..2e934b41 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,9 +15,9 @@ #import @class GNCIPv4Address; -@class GNCWiFiLANSocket; +@class GNCNWFrameworkSocket; -@interface GNCWiFiLANServerSocket : NSObject +@interface GNCNWFrameworkServerSocket : NSObject /** * @remark init is not an available initializer. @@ -52,7 +52,7 @@ * @param[out] error Error that will be populated on failure. * @return Returns a connected socket or nil if an error has occured. */ -- (nullable GNCWiFiLANSocket *)acceptWithError:(NSError **_Nullable)error; +- (nullable GNCNWFrameworkSocket *)acceptWithError:(NSError **_Nullable)error; /** * Stops listening for inbound connections. diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.m similarity index 89% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.m index 66a6bac8..f50e8c31 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.m @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h" #import #import @@ -22,20 +22,20 @@ #include #include -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" -@interface GNCWiFiLANServerSocket () +@interface GNCNWFrameworkServerSocket () @property(nonatomic, readonly) NSMutableArray *pendingConnections; @property(nonatomic, readonly) NSMutableArray *readyConnections; @end -@implementation GNCWiFiLANServerSocket { +@implementation GNCNWFrameworkServerSocket { NSCondition *_condition; nw_listener_t _listener; nw_listener_state_t _listenerState; @@ -74,12 +74,12 @@ - (GNCIPv4Address *)ipAddress { if (!_ipAddress) { - _ipAddress = [GNCWiFiLANServerSocket lookupIpAddress]; + _ipAddress = [GNCNWFrameworkServerSocket lookupIpAddress]; } return _ipAddress; } -- (GNCWiFiLANSocket *)acceptWithError:(NSError **)error { +- (GNCNWFrameworkSocket *)acceptWithError:(NSError **)error { // Wait until we have a ready connection and listener is in a ready/invalid state. [_condition lock]; nw_connection_t connection = [self.readyConnections lastObject]; @@ -99,7 +99,7 @@ if (connection == nil) { return nil; } - return [[GNCWiFiLANSocket alloc] initWithConnection:connection]; + return [[GNCNWFrameworkSocket alloc] initWithConnection:connection]; } - (void)close { @@ -110,13 +110,13 @@ _listener = nil; } -- (BOOL)startListeningWithError:(NSError **)error { +- (BOOL)startListeningWithError:(NSError **)error includePeerToPeer:(BOOL)includePeerToPeer { // Create a parameters object configured to support TCP. TLS MUST be disabled for Nearby to - // function properly. This also is set to include peer-to-peer, but unsure if it's required. + // function properly. nw_parameters_t parameters = nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL, /*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION); - nw_parameters_set_include_peer_to_peer(parameters, true); + nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer); // If the server socket port is zero, a random port will be selected. The server socket port will // be updated to reflect the port it's listening on, once the listener transitions to the ready @@ -192,8 +192,8 @@ // We timed out waiting for the listener to transition into a state. if (!didSignal) { [self close]; - _listenerError = [NSError errorWithDomain:GNCWiFiLANErrorDomain - code:GNCWiFiLANErrorTimedOut + _listenerError = [NSError errorWithDomain:GNCNWFrameworkErrorDomain + code:GNCNWFrameworkErrorTimedOut userInfo:nil]; return NO; } diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h similarity index 96% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h index cc99cb7d..939dd5ef 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ #import #import -@interface GNCWiFiLANSocket : NSObject +@interface GNCNWFrameworkSocket : NSObject /** * @remark init is not an available initializer. diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.m similarity index 94% rename from internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m rename to internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.m index 62744ca7..2fca548c 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m +++ b/internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.m @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,20 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h" #import #import #import "GoogleToolboxForMac/GTMLogger.h" -@interface GNCWiFiLANSocket () +@interface GNCNWFrameworkSocket () @property(nonatomic, readonly) nw_connection_t connection; @end -@implementation GNCWiFiLANSocket { +@implementation GNCNWFrameworkSocket { } - (instancetype)initWithConnection:(nw_connection_t)connection { diff --git a/internal/platform/implementation/apple/Tests/GNCIPAddressTest.mm b/internal/platform/implementation/apple/Tests/GNCIPAddressTest.mm index 65166717..70269658 100644 --- a/internal/platform/implementation/apple/Tests/GNCIPAddressTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCIPAddressTest.mm @@ -17,7 +17,7 @@ #include -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h" @interface GNCIPv4AddressTest : XCTestCase @end diff --git a/internal/platform/implementation/apple/wifi_lan.h b/internal/platform/implementation/apple/wifi_lan.h index 3d38a8f3..56aed56b 100644 --- a/internal/platform/implementation/apple/wifi_lan.h +++ b/internal/platform/implementation/apple/wifi_lan.h @@ -26,9 +26,9 @@ @class GNCMBonjourBrowser; @class GNCMBonjourService; -@class GNCWiFiLANMedium; -@class GNCWiFiLANServerSocket; -@class GNCWiFiLANSocket; +@class GNCNWFramework; +@class GNCNWFrameworkServerSocket; +@class GNCNWFrameworkSocket; namespace nearby { namespace apple { @@ -38,14 +38,14 @@ namespace apple { */ class WifiLanInputStream : public InputStream { public: - explicit WifiLanInputStream(GNCWiFiLANSocket* socket); + explicit WifiLanInputStream(GNCNWFrameworkSocket* socket); ~WifiLanInputStream() override = default; ExceptionOr Read(std::int64_t size) override; Exception Close() override; private: - GNCWiFiLANSocket* socket_; + GNCNWFrameworkSocket* socket_; }; /** @@ -53,7 +53,7 @@ class WifiLanInputStream : public InputStream { */ class WifiLanOutputStream : public OutputStream { public: - explicit WifiLanOutputStream(GNCWiFiLANSocket* socket); + explicit WifiLanOutputStream(GNCNWFrameworkSocket* socket); ~WifiLanOutputStream() override = default; Exception Write(const ByteArray& data) override; @@ -61,7 +61,7 @@ class WifiLanOutputStream : public OutputStream { Exception Close() override; private: - GNCWiFiLANSocket* socket_; + GNCNWFrameworkSocket* socket_; }; /** @@ -69,7 +69,7 @@ class WifiLanOutputStream : public OutputStream { */ class WifiLanSocket : public api::WifiLanSocket { public: - explicit WifiLanSocket(GNCWiFiLANSocket* socket); + explicit WifiLanSocket(GNCNWFrameworkSocket* socket); ~WifiLanSocket() override = default; InputStream& GetInputStream() override; @@ -77,7 +77,7 @@ class WifiLanSocket : public api::WifiLanSocket { Exception Close() override; private: - GNCWiFiLANSocket* socket_; + GNCNWFrameworkSocket* socket_; std::unique_ptr input_stream_; std::unique_ptr output_stream_; }; @@ -87,7 +87,7 @@ class WifiLanSocket : public api::WifiLanSocket { */ class WifiLanServerSocket : public api::WifiLanServerSocket { public: - explicit WifiLanServerSocket(GNCWiFiLANServerSocket* server_socket); + explicit WifiLanServerSocket(GNCNWFrameworkServerSocket* server_socket); ~WifiLanServerSocket() override = default; std::string GetIPAddress() const override; @@ -96,7 +96,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket { Exception Close() override; private: - GNCWiFiLANServerSocket* server_socket_; + GNCNWFrameworkServerSocket* server_socket_; }; /** @@ -126,7 +126,7 @@ class WifiLanMedium : public api::WifiLanMedium { std::unique_ptr ListenForService(int port) override; private: - GNCWiFiLANMedium* medium_; + GNCNWFramework* medium_; absl::AnyInvocable service_discovered_cb_; absl::AnyInvocable service_lost_cb_; }; diff --git a/internal/platform/implementation/apple/wifi_lan.mm b/internal/platform/implementation/apple/wifi_lan.mm index 5f2cc663..c5b32a5d 100644 --- a/internal/platform/implementation/apple/wifi_lan.mm +++ b/internal/platform/implementation/apple/wifi_lan.mm @@ -19,10 +19,10 @@ #include #include -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" -#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" namespace nearby { @@ -30,7 +30,7 @@ namespace apple { #pragma mark - WifiLanInputStream -WifiLanInputStream::WifiLanInputStream(GNCWiFiLANSocket* socket) : socket_(socket) {} +WifiLanInputStream::WifiLanInputStream(GNCNWFrameworkSocket* socket) : socket_(socket) {} ExceptionOr WifiLanInputStream::Read(std::int64_t size) { NSError* error = nil; @@ -50,7 +50,7 @@ Exception WifiLanInputStream::Close() { #pragma mark - WifiLanOutputStream -WifiLanOutputStream::WifiLanOutputStream(GNCWiFiLANSocket* socket) : socket_(socket) {} +WifiLanOutputStream::WifiLanOutputStream(GNCNWFrameworkSocket* socket) : socket_(socket) {} Exception WifiLanOutputStream::Write(const ByteArray& data) { NSError* error = nil; @@ -76,7 +76,7 @@ Exception WifiLanOutputStream::Close() { #pragma mark - WifiLanSocket -WifiLanSocket::WifiLanSocket(GNCWiFiLANSocket* socket) +WifiLanSocket::WifiLanSocket(GNCNWFrameworkSocket* socket) : socket_(socket), input_stream_(std::make_unique(socket)), output_stream_(std::make_unique(socket)) {} @@ -92,7 +92,7 @@ Exception WifiLanSocket::Close() { #pragma mark - WifiLanServerSocket -WifiLanServerSocket::WifiLanServerSocket(GNCWiFiLANServerSocket* server_socket) +WifiLanServerSocket::WifiLanServerSocket(GNCNWFrameworkServerSocket* server_socket) : server_socket_(server_socket) {} std::string WifiLanServerSocket::GetIPAddress() const { @@ -104,7 +104,7 @@ int WifiLanServerSocket::GetPort() const { return server_socket_.port; } std::unique_ptr WifiLanServerSocket::Accept() { NSError* error = nil; - GNCWiFiLANSocket* socket = [server_socket_ acceptWithError:&error]; + GNCNWFrameworkSocket* socket = [server_socket_ acceptWithError:&error]; if (socket != nil) { return std::make_unique(socket); } @@ -121,7 +121,7 @@ Exception WifiLanServerSocket::Close() { #pragma mark - WifiLanMedium -WifiLanMedium::WifiLanMedium() : medium_([[GNCWiFiLANMedium alloc] init]) {} +WifiLanMedium::WifiLanMedium() : medium_([[GNCNWFramework alloc] init]) {} bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) { NSInteger port = nsd_service_info.GetPort(); @@ -149,8 +149,10 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_type, __block NSString* serviceType = @(service_type.c_str()); __block DiscoveredServiceCallback client_callback = std::move(callback); + // Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices. NSError* error = nil; BOOL result = [medium_ startDiscoveryForServiceType:serviceType + includePeerToPeer:YES serviceFoundHandler:^(NSString* name, NSDictionary* txtRecords) { NsdServiceInfo nsd_service_info; nsd_service_info.SetServiceType([serviceType UTF8String]); @@ -188,12 +190,14 @@ bool WifiLanMedium::StopDiscovery(const std::string& service_type) { std::unique_ptr WifiLanMedium::ConnectToService( const NsdServiceInfo& remote_service_info, CancellationFlag* cancellation_flag) { + // Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices. NSError* error = nil; NSString* serviceName = @(remote_service_info.GetServiceName().c_str()); NSString* serviceType = @(remote_service_info.GetServiceType().c_str()); - GNCWiFiLANSocket* socket = [medium_ connectToServiceName:serviceName - serviceType:serviceType - error:&error]; + GNCNWFrameworkSocket* socket = [medium_ connectToServiceName:serviceName + serviceType:serviceType + includePeerToPeer:YES + error:&error]; if (socket != nil) { return std::make_unique(socket); } @@ -213,7 +217,7 @@ std::unique_ptr WifiLanMedium::ConnectToService( } NSData* hostData = [NSData dataWithBytes:ip_address.data() length:ip_address.size()]; GNCIPv4Address* host = [GNCIPv4Address addressFromData:hostData]; - GNCWiFiLANSocket* socket = [medium_ connectToHost:host port:port error:&error]; + GNCNWFrameworkSocket* socket = [medium_ connectToHost:host port:port error:&error]; if (socket != nil) { return std::make_unique(socket); } @@ -225,7 +229,10 @@ std::unique_ptr WifiLanMedium::ConnectToService( std::unique_ptr WifiLanMedium::ListenForService(int port) { NSError* error = nil; - GNCWiFiLANServerSocket* serverSocket = [medium_ listenForServiceOnPort:port error:&error]; + // Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices. + GNCNWFrameworkServerSocket* serverSocket = [medium_ listenForServiceOnPort:port + includePeerToPeer:YES + error:&error]; if (serverSocket != nil) { return std::make_unique(serverSocket); }