Files
OpenVPNAdapter/OpenVPN Adapter/OpenVPNAdapter.h
2018-01-15 12:50:25 +03:00

206 lines
6.7 KiB
Objective-C

//
// OpenVPNAdapter.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, OpenVPNAdapterEvent);
@class NEPacketTunnelFlow;
@class NEPacketTunnelNetworkSettings;
@class OpenVPNAdapter;
@class OpenVPNConfiguration;
@class OpenVPNConnectionInfo;
@class OpenVPNCredentials;
@class OpenVPNInterfaceStats;
@class OpenVPNProperties;
@class OpenVPNTransportStats;
@class OpenVPNSessionToken;
@protocol OpenVPNAdapterPacketFlow <NSObject>
/**
Read IP packets from the TUN interface.
@param completionHandler A block that is executed when some packets are read from the TUN interface. The packets that were
read are passed to this block in the packets array. The protocol numbers of the packets that were read are passed to this
block in the protocols array. Each packet has a protocol number in the corresponding index in the protocols array. The
protocol numbers are given in host byte order. Valid protocol numbers include PF_INET and PF_INET6. See /usr/include/sys/socket.h.
*/
- (void)readPacketsWithCompletionHandler:(void (^)(NSArray<NSData *> *packets, NSArray<NSNumber *> *protocols))completionHandler;
/**
Write IP packets to the TUN interface
@param packets An array of NSData objects containing the IP packets to the written.
@param protocols An array of NSNumber objects containing the protocol numbers (e.g. PF_INET or PF_INET6) of the IP packets
in packets in host byte order.
@discussion The number of NSData objects in packets must be exactly equal to the number of NSNumber objects in protocols.
@return YES on success, otherwise NO.
*/
- (BOOL)writePackets:(NSArray<NSData *> *)packets withProtocols:(NSArray<NSNumber *> *)protocols;
@end
@protocol OpenVPNAdapterDelegate <NSObject>
/**
This method is called once the network settings to be used have been established.
The receiver should call the completion handler once these settings have been set, returning a NEPacketTunnelFlow object for
the TUN interface, or nil if an error occurred.
@param openVPNAdapter The OpenVPNAdapter instance requesting this information.
@param networkSettings The NEPacketTunnelNetworkSettings to be used for the tunnel.
@param completionHandler The completion handler to be called with a NEPacketTunnelFlow object, or nil if an error occurred.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter
configureTunnelWithNetworkSettings:(NEPacketTunnelNetworkSettings *)networkSettings
completionHandler:(void (^)(id<OpenVPNAdapterPacketFlow> _Nullable packetFlow))completionHandler
NS_SWIFT_NAME(openVPNAdapter(_:configureTunnelWithNetworkSettings:completionHandler:));
/**
Informs the receiver that an OpenVPN error has occurred.
Some errors are fatal and should trigger the diconnection of the tunnel, check for fatal errors with the
OpenVPNAdapterErrorFatalKey.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the error.
@param error The error which has occurred.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter handleError:(NSError *)error;
/**
Informs the receiver that an OpenVPN event has occurred.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the event.
@param event The event which has occurred.
@param message An accompanying message, may be nil.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter
handleEvent:(OpenVPNAdapterEvent)event
message:(nullable NSString *)message
NS_SWIFT_NAME(openVPNAdapter(_:handleEvent:message:));
@optional
/**
Informs the receiver that an OpenVPN message has been logged.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the log message.
@param logMessage The log message.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter handleLogMessage:(NSString *)logMessage;
/**
Informs the receiver that a clock tick has occurred.
Clock ticks can be configured with an OpenVPNConfiguration object.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the clock tick.
*/
- (void)openVPNAdapterDidReceiveClockTick:(OpenVPNAdapter *)openVPNAdapter;
@end
@interface OpenVPNAdapter : NSObject
/**
The OpenVPN core copyright message.
*/
@property (nonatomic, class, readonly) NSString *copyright;
/**
The OpenVPN platform.
*/
@property (nonatomic, class, readonly) NSString *platform;
/**
The object that acts as the delegate of the adapter.
*/
@property (nonatomic, weak) id<OpenVPNAdapterDelegate> delegate;
/**
The session name, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) NSString *sessionName;
/**
The connection information, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) OpenVPNConnectionInfo *connectionInformation;
/**
The interface statistics.
*/
@property (nonatomic, readonly) OpenVPNInterfaceStats *interfaceStatistics;
/**
The session token, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) OpenVPNSessionToken *sessionToken;
/**
The transport statistics.
*/
@property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics;
/**
Applies the given configuration object.
Call this method prior to connecting, this method has no effect after calling connect.
@param configuration The configuration object.
@param error If there is an error applying the configuration, upon return contains an error object that describes the problem.
@return A properties object describing the configuration which has been applied.
*/
- (nullable OpenVPNProperties *)applyConfiguration:(OpenVPNConfiguration *)configuration
error:(NSError **)error
NS_SWIFT_NAME(apply(configuration:));
/**
Provides credentials to the receiver.
@param credentials The credentials object.
@param error If there is an error providing the credentials, upon return contains an error object that describes the problem.
@return Returns YES if this method was successful, otherwise NO.
*/
- (BOOL)provideCredentials:(OpenVPNCredentials *)credentials error:(NSError **)error NS_SWIFT_NAME(provide(credentials:));
/**
Starts the tunnel.
*/
- (void)connect;
/**
Pauses the tunnel.
@param reason The reason for pausing the tunnel.
*/
- (void)pauseWithReason:(NSString *)reason NS_SWIFT_NAME(pause(withReason:));
/**
Resumes the connection.
*/
- (void)resume;
/**
Reconnects after a given time period, perhaps due to an interface change.
@param timeInterval The time interval to wait before reconnecting.
*/
- (void)reconnectAfterTimeInterval:(NSTimeInterval)timeInterval NS_SWIFT_NAME(reconnect(afterTimeInterval:));
/**
Disconnect from the tunnel.
*/
- (void)disconnect;
@end
NS_ASSUME_NONNULL_END