2001-12-17 14:31:42 +00:00
|
|
|
|
/** Implementation for NSFileHandle for GNUStep
|
1997-09-01 21:59:51 +00:00
|
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
Date: 1997
|
|
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1997-09-01 21:59:51 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1997-09-01 21:59:51 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSFileHandle class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
1997-09-01 21:59:51 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
|
#define EXPOSE_NSFileHandle_IVARS 1
|
|
|
|
|
#import "Foundation/NSData.h"
|
2012-09-24 09:07:55 +00:00
|
|
|
|
#import "Foundation/NSException.h"
|
2012-09-26 16:57:17 +00:00
|
|
|
|
#import "Foundation/NSHost.h"
|
|
|
|
|
#import "Foundation/NSFileHandle.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
|
#import "Foundation/NSPathUtilities.h"
|
2019-01-16 09:56:08 +00:00
|
|
|
|
#import "GNUstepBase/GSTLS.h"
|
2012-09-26 16:57:17 +00:00
|
|
|
|
#import "GNUstepBase/NSString+GNUstepBase.h"
|
2011-10-19 15:25:38 +00:00
|
|
|
|
#import "GSPrivate.h"
|
|
|
|
|
#import "GSNetwork.h"
|
2012-09-26 17:00:26 +00:00
|
|
|
|
|
2012-09-24 09:07:55 +00:00
|
|
|
|
|
|
|
|
|
#define EXPOSE_GSFileHandle_IVARS 1
|
2011-10-19 15:25:38 +00:00
|
|
|
|
#import "GSFileHandle.h"
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
// GNUstep Notification names
|
|
|
|
|
|
|
|
|
|
NSString * const GSFileHandleConnectCompletionNotification
|
|
|
|
|
= @"GSFileHandleConnectCompletionNotification";
|
|
|
|
|
NSString * const GSFileHandleWriteCompletionNotification
|
|
|
|
|
= @"GSFileHandleWriteCompletionNotification";
|
|
|
|
|
|
|
|
|
|
// GNUstep key for getting error message.
|
|
|
|
|
|
|
|
|
|
NSString * const GSFileHandleNotificationError
|
|
|
|
|
= @"GSFileHandleNotificationError";
|
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
static Class NSFileHandle_abstract_class = nil;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
static Class NSFileHandle_concrete_class = nil;
|
2001-12-04 14:59:09 +00:00
|
|
|
|
static Class NSFileHandle_ssl_class = nil;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2016-06-28 10:40:33 +00:00
|
|
|
|
#if defined(HAVE_GNUTLS) && !defined(_WIN32)
|
|
|
|
|
@interface GSTLSHandle : GSFileHandle
|
|
|
|
|
{
|
|
|
|
|
@public
|
|
|
|
|
NSDictionary *opts;
|
|
|
|
|
GSTLSSession *session;
|
|
|
|
|
}
|
|
|
|
|
- (void) sslDisconnect;
|
|
|
|
|
- (BOOL) sslHandshakeEstablished: (BOOL*)result outgoing: (BOOL)isOutgoing;
|
|
|
|
|
- (NSString*) sslSetOptions: (NSDictionary*)options;
|
|
|
|
|
@end
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* <code>NSFileHandle</code> is a class that provides a wrapper for accessing
|
|
|
|
|
* system files and socket connections. You can open connections to a
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* file using class methods such as +fileHandleForReadingAtPath:.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* GNUstep extends the use of this class to allow you to create
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* network connections (sockets), secure connections and also allows
|
|
|
|
|
* you to use compression with these files and connections (as long as
|
|
|
|
|
* GNUstep Base was compiled with the zlib library).
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
|
@implementation NSFileHandle
|
|
|
|
|
|
2001-12-04 14:59:09 +00:00
|
|
|
|
+ (void) initialize
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (self == [NSFileHandle class])
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
NSFileHandle_abstract_class = self;
|
2002-06-30 09:19:30 +00:00
|
|
|
|
NSFileHandle_concrete_class = [GSFileHandle class];
|
2016-06-28 10:40:33 +00:00
|
|
|
|
#if defined(HAVE_GNUTLS) && !defined(_WIN32)
|
|
|
|
|
NSFileHandle_ssl_class = [GSTLSHandle class];
|
|
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (self == NSFileHandle_abstract_class)
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (NSFileHandle_concrete_class, 0, z);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject (self, 0, z);
|
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocating and Initializing a FileHandle Object
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object set up for reading from the
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* file listed at path. If the file does not exist or cannot
|
|
|
|
|
* be opened for some other reason, nil is returned.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleForReadingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initForReadingAtPath: path]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object set up for writing to the
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* file listed at path. If the file does not exist or cannot
|
|
|
|
|
* be opened for some other reason, nil is returned.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleForWritingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initForWritingAtPath: path]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object setup for updating (reading and
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* writing) from the file listed at path. If the file does not exist
|
|
|
|
|
* or cannot be opened for some other reason, nil is returned.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleForUpdatingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initForUpdatingAtPath: path]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object for the standard error
|
|
|
|
|
* descriptor. The returned object is a shared instance as there can only be
|
|
|
|
|
* one standard error per process.
|
2002-10-27 01:38:30 +00:00
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleWithStandardError
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initWithStandardError]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object for the standard input
|
|
|
|
|
* descriptor. The returned object is a shared instance as there can only be
|
|
|
|
|
* one standard input per process.
|
2002-10-27 01:38:30 +00:00
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleWithStandardInput
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initWithStandardInput]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* Returns an <code>NSFileHandle</code> object for the standard output
|
|
|
|
|
* descriptor. The returned object is a shared instance as there can only be
|
|
|
|
|
* one standard output per process.
|
2002-10-27 01:38:30 +00:00
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleWithStandardOutput
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initWithStandardOutput]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
|
/**
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* Returns a file handle object that is connected to the null device
|
|
|
|
|
* (i.e. a device that does nothing.) It is typically used in arrays
|
|
|
|
|
* and other collections of file handle objects as a place holder
|
|
|
|
|
* (null) object, so that all objects can respond to the same
|
|
|
|
|
* messages.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleWithNullDevice
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initWithNullDevice]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialize with desc, which can point to either a regular file or
|
|
|
|
|
* socket connection.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (id) initWithFileDescriptor: (int)desc
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-06-23 14:52:22 +00:00
|
|
|
|
return [self initWithFileDescriptor: desc closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initialize with desc, which can point to either a regular file or
|
|
|
|
|
* socket connection. Close desc when this instance is deallocated if
|
|
|
|
|
* flag is YES.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (id) initWithFileDescriptor: (int)desc closeOnDealloc: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Windows-Unix compatibility support.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (id) initWithNativeHandle: (void*)hdl
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-06-23 14:52:22 +00:00
|
|
|
|
return [self initWithNativeHandle: hdl closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is the designated initializer.
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* <init/>
|
|
|
|
|
* Windows-Unix compatibility support.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returning file handles
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return the underlying file descriptor for this instance.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (int) fileDescriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Windows-Unix compatibility support.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void*) nativeHandle
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Synchronous I/O operations
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Synchronously returns data available through this file or connection.
|
|
|
|
|
* If the handle represents a file, the entire contents from current file
|
|
|
|
|
* pointer to end are returned. If this is a network connection, reads
|
|
|
|
|
* what is available, blocking if nothing is available. Raises
|
|
|
|
|
* <code>NSFileHandleOperationException</code> if problem encountered.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSData*) availableData
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
2008-11-09 10:11:18 +00:00
|
|
|
|
* Reads up to maximum unsigned int bytes from file or communications
|
|
|
|
|
* channel into return data.<br />
|
|
|
|
|
* If the file is empty, returns an empty data item.
|
2004-06-22 22:40:40 +00:00
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSData*) readDataToEndOfFile
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reads up to len bytes from file or communications channel into return data.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSData*) readDataOfLength: (unsigned int)len
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Synchronously writes given data item to file or connection.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) writeData: (NSData*)item
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Asynchronous I/O operations
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Asynchronously accept a stream-type socket connection and act as the
|
|
|
|
|
* (server) end of the communications channel. This instance should have
|
|
|
|
|
* been created by -initWithFileDescriptor: with a stream-type socket created
|
|
|
|
|
* by the appropriate system routine. Posts a
|
|
|
|
|
* <code>NSFileHandleConnectionAcceptedNotification</code> when connection
|
|
|
|
|
* initiated, returning an <code>NSFileHandle</code> for the client side with
|
|
|
|
|
* that notification.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) acceptConnectionInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2002-06-23 19:53:15 +00:00
|
|
|
|
[self acceptConnectionInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>Asynchronously accept a stream-type socket connection and act as the
|
|
|
|
|
* (server) end of the communications channel. This instance should have
|
|
|
|
|
* been created by -initWithFileDescriptor: with a stream-type socket created
|
|
|
|
|
* by the appropriate system routine. Posts a
|
|
|
|
|
* <code>NSFileHandleConnectionAcceptedNotification</code> when connection
|
|
|
|
|
* initiated, returning an <code>NSFileHandle</code> for the client side with
|
|
|
|
|
* that notification.</p>
|
|
|
|
|
*
|
|
|
|
|
* <p>The modes array specifies [NSRunLoop] modes that the notification can
|
|
|
|
|
* be posted in.</p>
|
|
|
|
|
*/
|
2002-06-23 19:53:15 +00:00
|
|
|
|
- (void) acceptConnectionInBackgroundAndNotifyForModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Call -readInBackgroundAndNotifyForModes: with nil modes.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) readInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2002-06-23 19:53:15 +00:00
|
|
|
|
[self readInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Set up an asynchronous read operation which will cause a notification to
|
2002-10-27 01:38:30 +00:00
|
|
|
|
* be sent when any amount of data (or end of file) is read. Note that
|
|
|
|
|
* the file handle will not continuously send notifications when data
|
|
|
|
|
* is available. If you want to continue to receive notifications, you
|
|
|
|
|
* need to send this message again after receiving a notification.
|
2002-06-23 19:53:15 +00:00
|
|
|
|
*/
|
|
|
|
|
- (void) readInBackgroundAndNotifyForModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Call -readToEndOfFileInBackgroundAndNotifyForModes: with nil modes.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) readToEndOfFileInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2002-06-23 19:53:15 +00:00
|
|
|
|
[self readToEndOfFileInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Set up an asynchronous read operation which will cause a notification to
|
2002-06-23 19:53:15 +00:00
|
|
|
|
* be sent when end of file is read.
|
|
|
|
|
*/
|
|
|
|
|
- (void) readToEndOfFileInBackgroundAndNotifyForModes: (NSArray*)modes
|
1998-09-10 04:48:50 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1998-09-10 04:48:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Call -waitForDataInBackgroundAndNotifyForModes: with nil modes.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) waitForDataInBackgroundAndNotify
|
2002-06-23 19:53:15 +00:00
|
|
|
|
{
|
|
|
|
|
[self waitForDataInBackgroundAndNotifyForModes: nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up to provide a notification when data can be read from the handle.
|
|
|
|
|
*/
|
|
|
|
|
- (void) waitForDataInBackgroundAndNotifyForModes: (NSArray*)modes
|
1998-09-10 04:48:50 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1998-09-10 04:48:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
// Seeking within a file
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return current position in file, or raises exception if instance does
|
|
|
|
|
* not represent a regular file.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (unsigned long long) offsetInFile
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Position file pointer at end of file, raising exception if instance does
|
|
|
|
|
* not represent a regular file.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (unsigned long long) seekToEndOfFile
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Position file pointer at pos, raising exception if instance does
|
|
|
|
|
* not represent a regular file.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) seekToFileOffset: (unsigned long long)pos
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Operations on file
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Disallows further reading from read-access files or connections, and sends
|
|
|
|
|
* EOF on write-access files or connections. Descriptor is only
|
|
|
|
|
* <em>deleted</em> when this instance is deallocated.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) closeFile
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Flush in-memory buffer to file or connection, then return.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) synchronizeFile
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Chops file beyond pos then sets file pointer to that point.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (void) truncateFileAtOffset: (unsigned long long)pos
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
// Keys for accessing userInfo dictionary in notification handlers.
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
NSString * const NSFileHandleNotificationDataItem
|
|
|
|
|
= @"NSFileHandleNotificationDataItem";
|
|
|
|
|
NSString * const NSFileHandleNotificationFileHandleItem
|
|
|
|
|
= @"NSFileHandleNotificationFileHandleItem";
|
|
|
|
|
NSString * const NSFileHandleNotificationMonitorModes
|
|
|
|
|
= @"NSFileHandleNotificationMonitorModes";
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
// Notification names
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
NSString * const NSFileHandleConnectionAcceptedNotification
|
|
|
|
|
= @"NSFileHandleConnectionAcceptedNotification";
|
|
|
|
|
NSString * const NSFileHandleDataAvailableNotification
|
|
|
|
|
= @"NSFileHandleDataAvailableNotification";
|
|
|
|
|
NSString * const NSFileHandleReadCompletionNotification
|
|
|
|
|
= @"NSFileHandleReadCompletionNotification";
|
|
|
|
|
NSString * const NSFileHandleReadToEndOfFileCompletionNotification
|
|
|
|
|
= @"NSFileHandleReadToEndOfFileCompletionNotification";
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
// Exceptions
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* An exception used when a file error occurs.
|
|
|
|
|
*/
|
2002-06-06 14:02:59 +00:00
|
|
|
|
NSString * const NSFileHandleOperationException
|
|
|
|
|
= @"NSFileHandleOperationException";
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GNUstep class extensions
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* A set of convenience methods for utilizing the socket communications
|
|
|
|
|
* capabilities of the [NSFileHandle] class.
|
|
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
|
@implementation NSFileHandle (GNUstepExtensions)
|
|
|
|
|
|
2002-06-12 12:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Opens an outgoing network connection by initiating an asynchronous
|
|
|
|
|
* connection (see
|
|
|
|
|
* [+fileHandleAsClientInBackgroundAtAddress:service:protocol:forModes:])
|
|
|
|
|
* and waiting for it to succeed, fail, or time out.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleAsClientAtAddress: (NSString*)address
|
|
|
|
|
service: (NSString*)service
|
|
|
|
|
protocol: (NSString*)protocol
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initAsClientAtAddress: address
|
|
|
|
|
service: service
|
|
|
|
|
protocol: protocol]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-12 12:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Opens an outgoing network connection asynchronously using
|
|
|
|
|
* [+fileHandleAsClientInBackgroundAtAddress:service:protocol:forModes:]
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleAsClientInBackgroundAtAddress: (NSString*)address
|
|
|
|
|
service: (NSString*)service
|
|
|
|
|
protocol: (NSString*)protocol
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initAsClientInBackgroundAtAddress: address
|
|
|
|
|
service: service
|
|
|
|
|
protocol: protocol
|
|
|
|
|
forModes: nil]);
|
1998-07-15 12:47:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-12 12:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* Opens an outgoing network connection asynchronously.
|
|
|
|
|
* </p>
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>
|
|
|
|
|
* The address is the name (or IP dotted quad) of the machine to
|
|
|
|
|
* which the connection should be made.
|
|
|
|
|
* </item>
|
|
|
|
|
* <item>
|
|
|
|
|
* The service is the name (or number) of the port to
|
|
|
|
|
* which the connection should be made.
|
|
|
|
|
* </item>
|
|
|
|
|
* <item>
|
|
|
|
|
* The protocol is provided so support different network protocols,
|
|
|
|
|
* but at present only 'tcp' is supported. However, a protocol
|
|
|
|
|
* specification of the form 'socks-...' can be used to control socks5
|
|
|
|
|
* support.<br />
|
|
|
|
|
* If '...' is empty (ie the string is just 'socks-' then the connection
|
|
|
|
|
* is <em>not</em> made via a socks server.<br />
|
|
|
|
|
* Otherwise, the text '...' must be the name of the host on which the
|
|
|
|
|
* socks5 server is running, with an optional port number separated
|
2004-03-29 14:53:37 +00:00
|
|
|
|
* from the host name by a colon.<br />
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Alternatively a prefix of the form 'bind-' followed by an IP address
|
2004-03-29 14:53:37 +00:00
|
|
|
|
* may be used (for non-socks connections) to ensure that the connection
|
|
|
|
|
* is made from the specified address.
|
2002-06-12 12:02:48 +00:00
|
|
|
|
* </item>
|
|
|
|
|
* <item>
|
|
|
|
|
* If modes is nil or empty, uses NSDefaultRunLoopMode.
|
|
|
|
|
* </item>
|
|
|
|
|
* </list>
|
|
|
|
|
* <p>
|
|
|
|
|
* This method supports connection through a firewall via socks5. The
|
|
|
|
|
* socks5 connection may be controlled via the protocol argument, but if
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* no socks information is supplied here, the <em>GSSOCKS</em> user default
|
2002-06-12 12:02:48 +00:00
|
|
|
|
* will be used, and failing that, the <em>SOCKS5_SERVER</em> or
|
|
|
|
|
* <em>SOCKS_SERVER</em> environment variables will be used to set the
|
|
|
|
|
* socks server. If none of these mechanisms specify a socks server, the
|
|
|
|
|
* connection will be made directly rather than through socks.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleAsClientInBackgroundAtAddress: (NSString*)address
|
|
|
|
|
service: (NSString*)service
|
|
|
|
|
protocol: (NSString*)protocol
|
|
|
|
|
forModes: (NSArray*)modes
|
1998-07-15 12:47:13 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initAsClientInBackgroundAtAddress: address
|
|
|
|
|
service: service
|
|
|
|
|
protocol: protocol
|
|
|
|
|
forModes: modes]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Opens a network server socket and listens for incoming connections
|
|
|
|
|
* using the specified service and protocol.
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>
|
|
|
|
|
* The service is the name (or number) of the port to
|
|
|
|
|
* which the connection should be made.
|
|
|
|
|
* </item>
|
|
|
|
|
* <item>
|
|
|
|
|
* The protocol may at present only be 'tcp'
|
|
|
|
|
* </item>
|
|
|
|
|
* </list>
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
+ (id) fileHandleAsServerAtAddress: (NSString*)address
|
|
|
|
|
service: (NSString*)service
|
|
|
|
|
protocol: (NSString*)protocol
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
id o = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
|
|
return AUTORELEASE([o initAsServerAtAddress: address
|
|
|
|
|
service: service
|
|
|
|
|
protocol: protocol]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Call -readDataInBackgroundAndNotifyLength:forModes: with nil modes.
|
|
|
|
|
*/
|
|
|
|
|
- (void) readDataInBackgroundAndNotifyLength: (unsigned)len
|
|
|
|
|
{
|
|
|
|
|
[self readDataInBackgroundAndNotifyLength: len forModes: nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Set up an asynchronous read operation which will cause a notification to
|
2002-06-23 19:53:15 +00:00
|
|
|
|
* be sent when the specified amount of data (or end of file) is read.
|
|
|
|
|
*/
|
|
|
|
|
- (void) readDataInBackgroundAndNotifyLength: (unsigned)len
|
|
|
|
|
forModes: (NSArray*)modes
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a boolean to indicate whether a read operation of any kind is
|
|
|
|
|
* in progress on the handle.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (BOOL) readInProgress
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the host address of the network connection represented by
|
|
|
|
|
* the file handle. If this handle is an incoming connection which
|
|
|
|
|
* was received by a local server handle, this is the name or address
|
|
|
|
|
* of the client machine.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSString*) socketAddress
|
1998-01-19 15:20:15 +00:00
|
|
|
|
{
|
2000-06-23 14:52:22 +00:00
|
|
|
|
return nil;
|
1998-01-19 15:20:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-18 10:12:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the local address of the network connection or nil.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) socketLocalAddress
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the local service/port of the network connection or nil.
|
|
|
|
|
*/
|
|
|
|
|
- (NSString*) socketLocalService
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the name (or number) of the service (network port) in use for
|
|
|
|
|
* the network connection represented by the file handle.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSString*) socketService
|
1998-01-19 15:20:15 +00:00
|
|
|
|
{
|
2000-06-23 14:52:22 +00:00
|
|
|
|
return nil;
|
1998-01-19 15:20:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the name of the protocol in use for the network connection
|
|
|
|
|
* represented by the file handle.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (NSString*) socketProtocol
|
1998-01-19 15:20:15 +00:00
|
|
|
|
{
|
2000-06-23 14:52:22 +00:00
|
|
|
|
return nil;
|
1998-01-19 15:20:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* <p>
|
|
|
|
|
* Return a flag to indicate whether compression has been turned on for
|
|
|
|
|
* the file handle ... this is only available on systems where GNUstep
|
|
|
|
|
* was built with 'zlib' support for compressing/decompressing data.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* On systems which support it, this method may be called after
|
|
|
|
|
* a file handle has been initialised to turn on compression or
|
|
|
|
|
* decompression of the data being written/read.
|
|
|
|
|
* </p>
|
|
|
|
|
* Returns YES on success, NO on failure.<br />
|
|
|
|
|
* Reasons for failure are - <br />
|
|
|
|
|
* <list>
|
|
|
|
|
* <item>Not supported/built in to GNUstep</item>
|
|
|
|
|
* <item>File handle has been closed</item>
|
|
|
|
|
* <item>File handle is open for both read and write</item>
|
|
|
|
|
* <item>Failure in compression/decompression library</item>
|
|
|
|
|
* </list>
|
2002-08-07 16:58:57 +00:00
|
|
|
|
*/
|
2001-11-10 17:31:39 +00:00
|
|
|
|
- (BOOL) useCompression
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Call -writeInBackgroundAndNotify:forModes: with nil modes.
|
|
|
|
|
*/
|
|
|
|
|
- (void) writeInBackgroundAndNotify: (NSData*)item
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2002-06-23 19:53:15 +00:00
|
|
|
|
[self writeInBackgroundAndNotify: item forModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-23 19:53:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* Write the specified data asynchronously, and notify on completion.
|
|
|
|
|
*/
|
|
|
|
|
- (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a boolean to indicate whether a write operation of any kind is
|
|
|
|
|
* in progress on the handle. An outgoing network connection attempt
|
|
|
|
|
* (as a client) is considered to be a write operation.
|
|
|
|
|
*/
|
2000-06-23 14:52:22 +00:00
|
|
|
|
- (BOOL) writeInProgress
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2012-09-23 12:26:28 +00:00
|
|
|
|
@implementation NSFileHandle (GNUstepTLS)
|
2013-09-18 12:49:07 +00:00
|
|
|
|
|
|
|
|
|
+ (void) setData: (NSData*)data forTLSFile: (NSString*)fileName
|
|
|
|
|
{
|
2013-09-23 11:46:23 +00:00
|
|
|
|
#if defined(HAVE_GNUTLS)
|
2013-09-18 12:49:07 +00:00
|
|
|
|
[GSTLSObject setData: data forTLSFile: fileName];
|
2013-09-23 11:46:23 +00:00
|
|
|
|
#else
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[NSFileHandle+setData:forTLSFile:] called for a copy of gnustep-base which had GNUTLS support explicitly disabled at configure time"];
|
|
|
|
|
#endif
|
2013-09-18 12:49:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-07 16:58:57 +00:00
|
|
|
|
/**
|
2012-09-23 12:26:28 +00:00
|
|
|
|
* returns the concrete class used to implement SSL/TLS connections.
|
2002-08-07 16:58:57 +00:00
|
|
|
|
*/
|
2002-06-06 14:02:59 +00:00
|
|
|
|
+ (Class) sslClass
|
|
|
|
|
{
|
|
|
|
|
return NSFileHandle_ssl_class;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-05 18:26:09 +00:00
|
|
|
|
- (BOOL) sslAccept
|
|
|
|
|
{
|
2012-09-23 12:26:28 +00:00
|
|
|
|
BOOL result = NO;
|
|
|
|
|
|
|
|
|
|
if (NO == [self sslHandshakeEstablished: &result outgoing: NO])
|
|
|
|
|
{
|
|
|
|
|
NSRunLoop *loop;
|
|
|
|
|
|
|
|
|
|
IF_NO_GC([self retain];) // Don't get destroyed during runloop
|
|
|
|
|
loop = [NSRunLoop currentRunLoop];
|
|
|
|
|
[loop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
|
|
|
|
|
if (NO == [self sslHandshakeEstablished: &result outgoing: NO])
|
|
|
|
|
{
|
|
|
|
|
NSDate *final;
|
|
|
|
|
NSDate *when;
|
|
|
|
|
NSTimeInterval last = 0.0;
|
|
|
|
|
NSTimeInterval limit = 0.1;
|
|
|
|
|
|
|
|
|
|
final = [[NSDate alloc] initWithTimeIntervalSinceNow: 30.0];
|
|
|
|
|
when = [NSDate alloc];
|
|
|
|
|
|
|
|
|
|
while (NO == [self sslHandshakeEstablished: &result outgoing: NO]
|
|
|
|
|
&& [final timeIntervalSinceNow] > 0.0)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval tmp = limit;
|
|
|
|
|
|
|
|
|
|
limit += last;
|
|
|
|
|
last = tmp;
|
|
|
|
|
if (limit > 0.5)
|
|
|
|
|
{
|
|
|
|
|
limit = 0.1;
|
|
|
|
|
last = 0.1;
|
|
|
|
|
}
|
|
|
|
|
when = [when initWithTimeIntervalSinceNow: limit];
|
|
|
|
|
[loop runUntilDate: when];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(when);
|
|
|
|
|
RELEASE(final);
|
|
|
|
|
}
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2003-11-05 18:26:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
- (BOOL) sslConnect
|
|
|
|
|
{
|
2012-09-23 12:26:28 +00:00
|
|
|
|
BOOL result = NO;
|
|
|
|
|
|
|
|
|
|
if (NO == [self sslHandshakeEstablished: &result outgoing: YES])
|
|
|
|
|
{
|
|
|
|
|
NSRunLoop *loop;
|
|
|
|
|
|
|
|
|
|
IF_NO_GC([self retain];) // Don't get destroyed during runloop
|
|
|
|
|
loop = [NSRunLoop currentRunLoop];
|
|
|
|
|
[loop runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
|
|
|
|
|
if (NO == [self sslHandshakeEstablished: &result outgoing: YES])
|
|
|
|
|
{
|
|
|
|
|
NSDate *final;
|
|
|
|
|
NSDate *when;
|
|
|
|
|
NSTimeInterval last = 0.0;
|
|
|
|
|
NSTimeInterval limit = 0.1;
|
|
|
|
|
|
|
|
|
|
final = [[NSDate alloc] initWithTimeIntervalSinceNow: 30.0];
|
|
|
|
|
when = [NSDate alloc];
|
|
|
|
|
|
|
|
|
|
while (NO == [self sslHandshakeEstablished: &result outgoing: YES]
|
|
|
|
|
&& [final timeIntervalSinceNow] > 0.0)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval tmp = limit;
|
|
|
|
|
|
|
|
|
|
limit += last;
|
|
|
|
|
last = tmp;
|
|
|
|
|
if (limit > 0.5)
|
|
|
|
|
{
|
|
|
|
|
limit = 0.1;
|
|
|
|
|
last = 0.1;
|
|
|
|
|
}
|
|
|
|
|
when = [when initWithTimeIntervalSinceNow: limit];
|
|
|
|
|
[loop runUntilDate: when];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(when);
|
|
|
|
|
RELEASE(final);
|
|
|
|
|
}
|
|
|
|
|
DESTROY(self);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2002-06-06 14:02:59 +00:00
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
- (void) sslDisconnect
|
|
|
|
|
{
|
2012-09-23 12:26:28 +00:00
|
|
|
|
return;
|
2002-06-06 14:02:59 +00:00
|
|
|
|
}
|
2002-08-07 16:58:57 +00:00
|
|
|
|
|
2011-10-17 10:59:02 +00:00
|
|
|
|
- (BOOL) sslHandshakeEstablished: (BOOL*)result outgoing: (BOOL)isOutgoing
|
2011-07-12 11:40:15 +00:00
|
|
|
|
{
|
2012-09-23 12:26:28 +00:00
|
|
|
|
if (0 != result)
|
|
|
|
|
{
|
|
|
|
|
*result = NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
2011-07-12 11:40:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 11:38:48 +00:00
|
|
|
|
- (NSString*) sslIssuer
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) sslOwner
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-06 14:02:59 +00:00
|
|
|
|
- (void) sslSetCertificate: (NSString*)certFile
|
|
|
|
|
privateKey: (NSString*)privateKey
|
|
|
|
|
PEMpasswd: (NSString*)PEMpasswd
|
|
|
|
|
{
|
2012-09-23 12:26:28 +00:00
|
|
|
|
NSMutableDictionary *opts;
|
|
|
|
|
NSString *err;
|
|
|
|
|
|
|
|
|
|
opts = [NSMutableDictionary dictionaryWithCapacity: 3];
|
|
|
|
|
if (nil != certFile)
|
|
|
|
|
{
|
2012-09-24 09:07:55 +00:00
|
|
|
|
[opts setObject: certFile forKey: GSTLSCertificateFile];
|
2012-09-23 12:26:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (nil != privateKey)
|
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
[opts setObject: privateKey forKey: GSTLSCertificateKeyFile];
|
2012-09-23 12:26:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (nil != PEMpasswd)
|
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
[opts setObject: PEMpasswd forKey: GSTLSCertificateKeyPassword];
|
2012-09-23 12:26:28 +00:00
|
|
|
|
}
|
|
|
|
|
err = [self sslSetOptions: opts];
|
|
|
|
|
if (nil != err)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"%@", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) sslSetOptions: (NSDictionary*)options
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
2002-06-06 14:02:59 +00:00
|
|
|
|
}
|
2011-07-12 11:40:15 +00:00
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2016-06-28 10:40:33 +00:00
|
|
|
|
#if defined(HAVE_GNUTLS) && !defined(_WIN32)
|
2012-09-24 09:07:55 +00:00
|
|
|
|
|
|
|
|
|
/* Callback to allow the TLS code to pull data from the remote system.
|
|
|
|
|
* If the operation fails, this sets the error number.
|
|
|
|
|
*/
|
|
|
|
|
static ssize_t
|
|
|
|
|
GSTLSHandlePull(gnutls_transport_ptr_t handle, void *buffer, size_t len)
|
|
|
|
|
{
|
|
|
|
|
ssize_t result = 0;
|
|
|
|
|
GSTLSHandle *tls = (GSTLSHandle*)handle;
|
|
|
|
|
int descriptor = [tls fileDescriptor];
|
|
|
|
|
|
|
|
|
|
result = read(descriptor, buffer, len);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_GNUTLS_TRANSPORT_SET_ERRNO
|
2013-11-30 08:57:20 +00:00
|
|
|
|
if (tls->session && tls->session->session)
|
|
|
|
|
{
|
|
|
|
|
gnutls_transport_set_errno (tls->session->session, errno);
|
|
|
|
|
}
|
2012-09-24 09:07:55 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Callback to allow the TLS code to push data to the remote system.
|
|
|
|
|
* If the operation fails, this sets the error number.
|
|
|
|
|
*/
|
|
|
|
|
static ssize_t
|
|
|
|
|
GSTLSHandlePush(gnutls_transport_ptr_t handle, const void *buffer, size_t len)
|
|
|
|
|
{
|
|
|
|
|
ssize_t result = 0;
|
|
|
|
|
GSTLSHandle *tls = (GSTLSHandle*)handle;
|
|
|
|
|
int descriptor = [tls fileDescriptor];
|
|
|
|
|
|
|
|
|
|
result = write(descriptor, buffer, len);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_GNUTLS_TRANSPORT_SET_ERRNO
|
2013-11-30 08:57:20 +00:00
|
|
|
|
if (tls->session && tls->session->session)
|
|
|
|
|
{
|
|
|
|
|
gnutls_transport_set_errno (tls->session->session, errno);
|
|
|
|
|
}
|
2012-09-24 09:07:55 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@implementation GSTLSHandle
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [GSTLSHandle class])
|
|
|
|
|
{
|
|
|
|
|
[GSTLSObject class]; // Force initialisation of gnu tls stuff
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) closeFile
|
|
|
|
|
{
|
2013-11-21 16:45:01 +00:00
|
|
|
|
[self sslDisconnect];
|
2012-09-24 09:07:55 +00:00
|
|
|
|
[super closeFile];
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 15:23:24 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2013-11-21 16:45:01 +00:00
|
|
|
|
// Don't DESTROY ivars below. First release them, then set nil, because
|
|
|
|
|
// `session' may need this back-reference during TLS teardown.
|
|
|
|
|
TEST_RELEASE(opts);
|
|
|
|
|
TEST_RELEASE(session);
|
|
|
|
|
opts = nil;
|
|
|
|
|
session = nil;
|
2012-09-26 15:23:24 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 16:45:01 +00:00
|
|
|
|
- (void) finalize
|
|
|
|
|
{
|
|
|
|
|
[self sslDisconnect];
|
|
|
|
|
[super finalize];
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 09:07:55 +00:00
|
|
|
|
- (NSInteger) read: (void*)buf length: (NSUInteger)len
|
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
if (YES == [session active])
|
2012-09-24 09:07:55 +00:00
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
return [session read: buf length: len];
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
return [super read: buf length: len];
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 09:58:02 +00:00
|
|
|
|
- (BOOL) sslAccept
|
|
|
|
|
{
|
|
|
|
|
/* If a server session is over five minutes old, destroy it so that
|
|
|
|
|
* we create a new one to accept the incoming connection. This is
|
|
|
|
|
* needed in case the certificate files associated with a long running
|
|
|
|
|
* server have been updated and we need to load/use the new certificate.
|
|
|
|
|
*/
|
|
|
|
|
if (session != nil && [session age] >= 300.0)
|
|
|
|
|
{
|
|
|
|
|
DESTROY(session);
|
|
|
|
|
}
|
|
|
|
|
return [super sslAccept];
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 09:07:55 +00:00
|
|
|
|
- (void) sslDisconnect
|
|
|
|
|
{
|
2013-11-30 08:57:20 +00:00
|
|
|
|
[self setNonBlocking: NO];
|
2013-11-27 12:26:33 +00:00
|
|
|
|
[session disconnect: NO];
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) sslHandshakeEstablished: (BOOL*)result outgoing: (BOOL)isOutgoing
|
|
|
|
|
{
|
|
|
|
|
NSAssert(0 != result, NSInvalidArgumentException);
|
|
|
|
|
|
2012-09-26 15:23:24 +00:00
|
|
|
|
if (YES == [session active])
|
2012-09-24 09:07:55 +00:00
|
|
|
|
{
|
2015-03-08 09:22:11 +00:00
|
|
|
|
*result = YES;
|
2012-09-24 09:07:55 +00:00
|
|
|
|
return YES; /* Already connected. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (YES == isStandardFile)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Attempt to perform ssl handshake with a standard file");
|
2015-03-08 09:22:11 +00:00
|
|
|
|
*result = NO;
|
2012-09-26 15:23:24 +00:00
|
|
|
|
return YES;
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the handshake direction so we know how to set up the connection.
|
|
|
|
|
*/
|
2012-09-26 15:23:24 +00:00
|
|
|
|
if (nil == session)
|
2012-09-24 09:07:55 +00:00
|
|
|
|
{
|
2012-09-26 16:57:17 +00:00
|
|
|
|
/* If No value is specified for GSTLSRemoteHosts, make a comma separated
|
|
|
|
|
* list of all known names for the remote host and use that.
|
|
|
|
|
*/
|
|
|
|
|
if (nil == [opts objectForKey: GSTLSRemoteHosts])
|
|
|
|
|
{
|
|
|
|
|
NSHost *host = [NSHost hostWithAddress: [self socketAddress]];
|
|
|
|
|
NSString *s = [[host names] description];
|
|
|
|
|
|
|
|
|
|
s = [s stringByReplacingString: @"\"" withString: @""];
|
|
|
|
|
if ([s length] > 1)
|
|
|
|
|
{
|
|
|
|
|
s = [s substringWithRange: NSMakeRange(1, [s length] - 2)];
|
|
|
|
|
}
|
|
|
|
|
if ([s length] > 0)
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *d = [opts mutableCopy];
|
|
|
|
|
|
|
|
|
|
[d setObject:s forKey: GSTLSRemoteHosts];
|
|
|
|
|
ASSIGNCOPY(opts, d);
|
|
|
|
|
[d release];
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-05 10:13:10 +00:00
|
|
|
|
[self setNonBlocking: YES];
|
2012-09-26 15:23:24 +00:00
|
|
|
|
session = [[GSTLSSession alloc] initWithOptions: opts
|
|
|
|
|
direction: isOutgoing
|
|
|
|
|
transport: (void*)self
|
|
|
|
|
push: GSTLSHandlePush
|
2012-09-26 16:57:17 +00:00
|
|
|
|
pull: GSTLSHandlePull];
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 15:23:24 +00:00
|
|
|
|
if (NO == [session handshake])
|
2012-09-24 09:07:55 +00:00
|
|
|
|
{
|
2015-03-08 09:22:11 +00:00
|
|
|
|
*result = NO;
|
|
|
|
|
if (nil == session)
|
|
|
|
|
{
|
|
|
|
|
return YES; // Unable to create session
|
|
|
|
|
}
|
2012-09-26 15:23:24 +00:00
|
|
|
|
return NO; // Need more.
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
*result = [session active];
|
|
|
|
|
return YES;
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 11:38:48 +00:00
|
|
|
|
- (NSString*) sslIssuer
|
|
|
|
|
{
|
|
|
|
|
return [session issuer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) sslOwner
|
|
|
|
|
{
|
|
|
|
|
return [session owner];
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 09:07:55 +00:00
|
|
|
|
- (NSString*) sslSetOptions: (NSDictionary*)options
|
|
|
|
|
{
|
|
|
|
|
if (isStandardFile == YES)
|
|
|
|
|
{
|
|
|
|
|
return @"Attempt to set ssl options for a standard file";
|
|
|
|
|
}
|
2012-09-26 15:23:24 +00:00
|
|
|
|
ASSIGNCOPY(opts, options);
|
2012-09-24 09:07:55 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger) write: (const void*)buf length: (NSUInteger)len
|
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
if (YES == [session active])
|
2012-09-24 09:07:55 +00:00
|
|
|
|
{
|
2012-09-26 15:23:24 +00:00
|
|
|
|
return [session write: buf length: len];
|
2012-09-24 09:07:55 +00:00
|
|
|
|
}
|
|
|
|
|
return [super write: buf length: len];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2016-06-28 10:40:33 +00:00
|
|
|
|
#endif /* defined(HAVE_GNUTLS) && !defined(_WIN32) */
|
2012-09-24 09:07:55 +00:00
|
|
|
|
|