diff --git a/Wired Server/WPExportManager.m b/Wired Server/WPExportManager.m index 2d52aaa..a9e35dc 100644 --- a/Wired Server/WPExportManager.m +++ b/Wired Server/WPExportManager.m @@ -161,7 +161,7 @@ - (BOOL)importFromFile:(NSString *)file error:(WPError **)error { } files = [NSDictionary dictionaryWithObjectsAndKeys: - @"etc/wired.conf", WPExportManagerConfig, + @"etc/wired.conf", WPExportManagerConfig, @"banner.png", WPExportManagerBanner, @"database.sqlite3", WPExportManagerDatabase, NULL]; @@ -178,22 +178,24 @@ - (BOOL)importFromFile:(NSString *)file error:(WPError **)error { return NO; } } - + data = [dictionary objectForKey:key]; - + if(!data) continue; zipfile = [NSFileManager temporaryPathWithPrefix:@"WiredSettings"]; - + if(![data writeToFile:zipfile options:0 error:error]) return NO; task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" arguments:[NSArray arrayWithObjects: - @"-o", - zipfile, - NULL]]; + @"-o", + zipfile, + @"-d", + [_wiredManager rootPath], + NULL]]; [task waitUntilExit]; diff --git a/Wired Server/WSSettingsController.m b/Wired Server/WSSettingsController.m index eecce3f..c6576ab 100644 --- a/Wired Server/WSSettingsController.m +++ b/Wired Server/WSSettingsController.m @@ -205,7 +205,7 @@ - (void)windowDidLoad { url = [[NSBundle mainBundle] URLForResource:@"Wired Server Helper" withExtension:@"app"]; if([[WISettings settings] boolForKey:WPEnableMenuItem]) { - if(![WIStatusMenuManager isHelperRunning]) { + if(![WIStatusMenuManager isHelperRunning:WPHelperBundleID]) { [WIStatusMenuManager startHelper:url]; } } @@ -283,9 +283,9 @@ - (void)awakeFromNib { #pragma mark - - (void)wiredStatusDidChange:(NSNotification *)notification { - [self _updateSettings]; [self _updateRunningStatus]; + if([_wiredManager isRunning]) { if ([[_hostTextField stringValue] length]==0){ [self loadInfo]; @@ -302,18 +302,19 @@ - (void)wiredStatusDidChange:(NSNotification *)notification { } - (void)saveInfo { - NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; - if ([[_hostTextField stringValue] length] ==0) { + + if ([[_hostTextField stringValue] length] == 0) { [_hostTextField setStringValue:@"127.0.0.1"]; } + [prefs setObject:[_hostTextField stringValue] forKey:@"Host"]; [prefs synchronize]; } - (void)loadInfo { - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + if ([[defaults objectForKey:@"Host"] length]!=0) [_hostTextField setStringValue:[defaults stringForKey:@"Host"]]; else @@ -695,14 +696,17 @@ - (IBAction)exportSettings:(id)sender { [_activityProgressIndicator startAnimation:self]; [_activityTextField setStringValue:@"Export Settings..."]; - NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ - NSLog(@"exportSettings : %@", [[savePanel URL] path]); - - [self _exportToFile:[[savePanel URL] path]]; - [NSThread sleepForTimeInterval:1.0]; - [self.window performSelectorOnMainThread:@selector(endSheet:) withObject:_activityWindow]; - }]; - [_queue addOperation:operation]; + NSString *path = [[savePanel URL] path]; + + dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ + [self _exportToFile:path]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [NSThread sleepForTimeInterval:1.0]; + [_activityWindow orderOut:self]; + [self.window endSheet:_activityWindow]; + }); + }); } }]; } @@ -739,14 +743,17 @@ - (IBAction)importSettings:(id)sender { [_activityProgressIndicator startAnimation:self]; [_activityTextField setStringValue:@"Import Settings..."]; - NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ - [self _importFromFile:[[openPanel URL] path]]; - [NSThread sleepForTimeInterval:1.0]; - [self.window performSelectorOnMainThread:@selector(endSheet:) withObject:_activityWindow]; - - }]; + NSString *path = [[openPanel URL] path]; - [_queue addOperation:operation]; + dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ + [self _importFromFile:path]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [NSThread sleepForTimeInterval:1.0]; + [_activityWindow orderOut:self]; + [self.window endSheet:_activityWindow]; + }); + }); } }]; } else { @@ -1257,9 +1264,13 @@ - (void)_importFromFile:(NSString *)file { WPError *error; if([_exportManager importFromFile:file error:&error]) - [self _updateSettings]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self _updateSettings]; + }); else - [[error alert] beginSheetModalForWindow:[_importSettingsButton window]]; + dispatch_async(dispatch_get_main_queue(), ^{ + [[error alert] beginSheetModalForWindow:[_importSettingsButton window]]; + }); } diff --git a/WiredFrameworks/WiredAppKit/WIStatusMenuManager.h b/WiredFrameworks/WiredAppKit/WIStatusMenuManager.h index aac9023..8e61f66 100755 --- a/WiredFrameworks/WiredAppKit/WIStatusMenuManager.h +++ b/WiredFrameworks/WiredAppKit/WIStatusMenuManager.h @@ -16,6 +16,6 @@ + (void) startHelper:(NSURL *)itemURL; + (void) stopHelper: (NSURL *)itemURL; -+ (BOOL) isHelperRunning; ++ (BOOL) isHelperRunning:(NSString *)bundleID; @end diff --git a/WiredFrameworks/WiredAppKit/WIStatusMenuManager.m b/WiredFrameworks/WiredAppKit/WIStatusMenuManager.m index 4d5ddc5..09c1780 100755 --- a/WiredFrameworks/WiredAppKit/WIStatusMenuManager.m +++ b/WiredFrameworks/WiredAppKit/WIStatusMenuManager.m @@ -19,10 +19,9 @@ + (void)stopHelper:(NSURL *)itemURL { system("killall 'Wired Server Helper'"); } -+ (BOOL)isHelperRunning -{ ++ (BOOL)isHelperRunning:(NSString *)bundleID { BOOL result = NO; - NSArray *running = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"fr.read-write.Wired-Server-Helper"]; + NSArray *running = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID]; if ([running count] > 0) { result = YES;; } @@ -32,6 +31,6 @@ + (BOOL)isHelperRunning + (void) setStartAtLogin:(NSString *)bundleID enabled:(BOOL)enabled { SMLoginItemSetEnabled((__bridge CFStringRef)bundleID, enabled); -} +} @end diff --git a/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.c b/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.c index ed999ae..b4c9456 100644 --- a/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.c +++ b/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.c @@ -84,19 +84,19 @@ int wi_p7_socket_dummy = 0; #define _WI_P7_CHECKSUM_SHA512 2 #define _WI_P7_COMPRESSION_ENUM_TO_OPTIONS(flag) \ - ((flag) == _WI_P7_COMPRESSION_DEFLATE ? \ + ((flag) == _WI_P7_COMPRESSION_DEFLATE ? \ WI_P7_COMPRESSION_DEFLATE : -1) -#define _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag) \ - ((flag) == _WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ - WI_P7_ENCRYPTION_RSA_AES128_SHA1 : \ - (flag) == _WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ - WI_P7_ENCRYPTION_RSA_AES192_SHA1 : \ - (flag) == _WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ - WI_P7_ENCRYPTION_RSA_AES256_SHA1 : \ - (flag) == _WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ - WI_P7_ENCRYPTION_RSA_BF128_SHA1 : \ - (flag) == _WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ +#define _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag) \ + ((flag) == _WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + WI_P7_ENCRYPTION_RSA_AES128_SHA1 : \ + (flag) == _WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + WI_P7_ENCRYPTION_RSA_AES192_SHA1 : \ + (flag) == _WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + WI_P7_ENCRYPTION_RSA_AES256_SHA1 : \ + (flag) == _WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + WI_P7_ENCRYPTION_RSA_BF128_SHA1 : \ + (flag) == _WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ WI_P7_ENCRYPTION_RSA_3DES192_SHA1 : \ (flag) == _WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ WI_P7_ENCRYPTION_RSA_AES128_SHA256 : \ @@ -107,7 +107,7 @@ int wi_p7_socket_dummy = 0; (flag) == _WI_P7_ENCRYPTION_RSA_BF128_SHA256 ? \ WI_P7_ENCRYPTION_RSA_BF128_SHA256 : \ (flag) == _WI_P7_ENCRYPTION_RSA_3DES192_SHA256 ? \ - WI_P7_ENCRYPTION_RSA_3DES192_SHA256 : \ + WI_P7_ENCRYPTION_RSA_3DES192_SHA256 : \ (flag) == _WI_P7_ENCRYPTION_RSA_AES128_SHA512 ? \ WI_P7_ENCRYPTION_RSA_AES128_SHA512 : \ (flag) == _WI_P7_ENCRYPTION_RSA_AES192_SHA512 ? \ @@ -119,27 +119,27 @@ int wi_p7_socket_dummy = 0; (flag) == _WI_P7_ENCRYPTION_RSA_3DES192_SHA512 ? \ WI_P7_ENCRYPTION_RSA_3DES192_SHA512 : -1) -#define _WI_P7_CHECKSUM_ENUM_TO_OPTIONS(flag) \ - ((flag) == _WI_P7_CHECKSUM_SHA1 ? \ +#define _WI_P7_CHECKSUM_ENUM_TO_OPTIONS(flag) \ + ((flag) == _WI_P7_CHECKSUM_SHA1 ? \ WI_P7_CHECKSUM_SHA1 : \ (flag) == _WI_P7_CHECKSUM_SHA256 ? \ WI_P7_CHECKSUM_SHA256 : \ (flag) == _WI_P7_CHECKSUM_SHA512 ? \ WI_P7_CHECKSUM_SHA512 : -1) -#define _WI_P7_COMPRESSION_OPTIONS_TO_ENUM(options) \ +#define _WI_P7_COMPRESSION_OPTIONS_TO_ENUM(options) \ ((options) & WI_P7_COMPRESSION_DEFLATE ? \ _WI_P7_COMPRESSION_DEFLATE : -1) -#define _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(options) \ - ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ - _WI_P7_ENCRYPTION_RSA_AES128_SHA1 : \ - (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ - _WI_P7_ENCRYPTION_RSA_AES192_SHA1 : \ - (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ - _WI_P7_ENCRYPTION_RSA_AES256_SHA1 : \ - (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ - _WI_P7_ENCRYPTION_RSA_BF128_SHA1 : \ +#define _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(options) \ + ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + _WI_P7_ENCRYPTION_RSA_AES128_SHA1 : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + _WI_P7_ENCRYPTION_RSA_AES192_SHA1 : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + _WI_P7_ENCRYPTION_RSA_AES256_SHA1 : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + _WI_P7_ENCRYPTION_RSA_BF128_SHA1 : \ (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ _WI_P7_ENCRYPTION_RSA_3DES192_SHA1 : \ (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ @@ -164,7 +164,7 @@ int wi_p7_socket_dummy = 0; _WI_P7_ENCRYPTION_RSA_3DES192_SHA512 : -1) #define _WI_P7_CHECKSUM_OPTIONS_TO_ENUM(options) \ - ((options) & WI_P7_CHECKSUM_SHA1 ? \ + ((options) & WI_P7_CHECKSUM_SHA1 ? \ _WI_P7_CHECKSUM_SHA1 : \ (options) & WI_P7_CHECKSUM_SHA256 ? \ _WI_P7_CHECKSUM_SHA256 : \ @@ -212,14 +212,14 @@ int wi_p7_socket_dummy = 0; _WI_P7_CHECKSUM_SHA512 : -1) #define _WI_P7_ENCRYPTION_OPTIONS_TO_CIPHER(options) \ - ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ - WI_CIPHER_AES128 : \ - (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ - WI_CIPHER_AES192 : \ - (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ - WI_CIPHER_AES256 : \ - (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ - WI_CIPHER_BF128 : \ + ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + WI_CIPHER_AES128 : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + WI_CIPHER_AES192 : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + WI_CIPHER_AES256 : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + WI_CIPHER_BF128 : \ (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ WI_CIPHER_3DES192 : \ (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ @@ -245,56 +245,56 @@ int wi_p7_socket_dummy = 0; struct _wi_p7_socket { - wi_runtime_base_t base; - - wi_socket_t *socket; - wi_p7_spec_t *spec; - wi_p7_spec_t *merged_spec; - - wi_string_t *remote_name; - wi_string_t *remote_version; - - wi_string_t *user_name; - - wi_p7_serialization_t serialization; - wi_uinteger_t options; - - uint32_t message_binary_size; - + wi_runtime_base_t base; + + wi_socket_t *socket; + wi_p7_spec_t *spec; + wi_p7_spec_t *merged_spec; + + wi_string_t *remote_name; + wi_string_t *remote_version; + + wi_string_t *user_name; + + wi_p7_serialization_t serialization; + wi_uinteger_t options; + + uint32_t message_binary_size; + #ifdef WI_RSA wi_boolean_t encryption_enabled; wi_rsa_t *private_key; wi_rsa_t *public_key; - wi_cipher_t *cipher; + wi_cipher_t *cipher; #endif - - wi_boolean_t compression_enabled; - z_stream deflate_stream; - z_stream inflate_stream; - - wi_boolean_t checksum_enabled; - wi_uinteger_t checksum_length; - - wi_p7_boolean_t local_compatibility_check; - wi_p7_boolean_t remote_compatibility_check; - - void *compression_buffer; - wi_uinteger_t compression_buffer_length; - void *encryption_buffer; - wi_uinteger_t encryption_buffer_length; - void *decryption_buffer; - wi_uinteger_t decryption_buffer_length; - void *oobdata_read_buffer; - wi_uinteger_t oobdata_read_buffer_length; - - wi_p7_socket_message_callback_func_t *read_message_callback; - void *read_message_context; - - wi_p7_socket_message_callback_func_t *wrote_message_callback; - void *wrote_message_context; - - uint64_t read_raw_bytes, read_processed_bytes; - uint64_t sent_raw_bytes, sent_processed_bytes; + + wi_boolean_t compression_enabled; + z_stream deflate_stream; + z_stream inflate_stream; + + wi_boolean_t checksum_enabled; + wi_uinteger_t checksum_length; + + wi_p7_boolean_t local_compatibility_check; + wi_p7_boolean_t remote_compatibility_check; + + void *compression_buffer; + wi_uinteger_t compression_buffer_length; + void *encryption_buffer; + wi_uinteger_t encryption_buffer_length; + void *decryption_buffer; + wi_uinteger_t decryption_buffer_length; + void *oobdata_read_buffer; + wi_uinteger_t oobdata_read_buffer_length; + + wi_p7_socket_message_callback_func_t *read_message_callback; + void *read_message_context; + + wi_p7_socket_message_callback_func_t *wrote_message_callback; + void *wrote_message_context; + + uint64_t read_raw_bytes, read_processed_bytes; + uint64_t sent_raw_bytes, sent_processed_bytes; }; @@ -302,43 +302,43 @@ enum _wi_p7_socket_compression { _WI_P7_SOCKET_COMPRESS, _WI_P7_SOCKET_DECOMPRESS, }; -typedef enum _wi_p7_socket_compression _wi_p7_socket_compression_t; +typedef enum _wi_p7_socket_compression _wi_p7_socket_compression_t; -static void _wi_p7_socket_dealloc(wi_runtime_instance_t *); -static wi_string_t * _wi_p7_socket_description(wi_runtime_instance_t *); +static void _wi_p7_socket_dealloc(wi_runtime_instance_t *); +static wi_string_t * _wi_p7_socket_description(wi_runtime_instance_t *); -static wi_boolean_t _wi_p7_socket_connect_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_uinteger_t); -static wi_boolean_t _wi_p7_socket_accept_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_uinteger_t); +static wi_boolean_t _wi_p7_socket_connect_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_uinteger_t); +static wi_boolean_t _wi_p7_socket_accept_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_uinteger_t); #ifdef WI_RSA -static wi_boolean_t _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *, wi_string_t *); -static wi_boolean_t _wi_p7_socket_accept_key_exchange(wi_p7_socket_t *, wi_time_interval_t); -static wi_boolean_t _wi_p7_password_is_equal(wi_string_t *, wi_string_t *); +static wi_boolean_t _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *, wi_string_t *); +static wi_boolean_t _wi_p7_socket_accept_key_exchange(wi_p7_socket_t *, wi_time_interval_t); +static wi_boolean_t _wi_p7_password_is_equal(wi_string_t *, wi_string_t *); #endif -static wi_boolean_t _wi_p7_socket_send_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); -static wi_boolean_t _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); +static wi_boolean_t _wi_p7_socket_send_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); +static wi_boolean_t _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); -static wi_boolean_t _wi_p7_socket_write_binary_message(wi_p7_socket_t *, wi_time_interval_t, wi_p7_message_t *); -static wi_boolean_t _wi_p7_socket_write_xml_message(wi_p7_socket_t *, wi_time_interval_t, wi_p7_message_t *); +static wi_boolean_t _wi_p7_socket_write_binary_message(wi_p7_socket_t *, wi_time_interval_t, wi_p7_message_t *); +static wi_boolean_t _wi_p7_socket_write_xml_message(wi_p7_socket_t *, wi_time_interval_t, wi_p7_message_t *); static wi_p7_message_t * _wi_p7_socket_read_binary_message(wi_p7_socket_t *, wi_time_interval_t, uint32_t); static wi_p7_message_t * _wi_p7_socket_read_xml_message(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *); -static wi_boolean_t _wi_p7_socket_configure_compression(wi_p7_socket_t *); -static wi_integer_t _wi_p7_socket_deflate(wi_p7_socket_t *, const void *, uint32_t); -static wi_integer_t _wi_p7_socket_inflate(wi_p7_socket_t *, const void *, uint32_t); +static wi_boolean_t _wi_p7_socket_configure_compression(wi_p7_socket_t *); +static wi_integer_t _wi_p7_socket_deflate(wi_p7_socket_t *, const void *, uint32_t); +static wi_integer_t _wi_p7_socket_inflate(wi_p7_socket_t *, const void *, uint32_t); -static void _wi_p7_socket_configure_checksum(wi_p7_socket_t *); -static void _wi_p7_socket_checksum_binary_message(wi_p7_socket_t *, wi_p7_message_t *, void *); -static void _wi_p7_socket_checksum_buffer(wi_p7_socket_t *, const void *, uint32_t, void *); +static void _wi_p7_socket_configure_checksum(wi_p7_socket_t *); +static void _wi_p7_socket_checksum_binary_message(wi_p7_socket_t *, wi_p7_message_t *, void *); +static void _wi_p7_socket_checksum_buffer(wi_p7_socket_t *, const void *, uint32_t, void *); wi_boolean_t wi_p7_socket_debug = false; -wi_p7_socket_password_provider_func_t *wi_p7_socket_password_provider = NULL; +wi_p7_socket_password_provider_func_t *wi_p7_socket_password_provider = NULL; -static wi_runtime_id_t _wi_p7_socket_runtime_id = WI_RUNTIME_ID_NULL; -static wi_runtime_class_t _wi_p7_socket_runtime_class = { +static wi_runtime_id_t _wi_p7_socket_runtime_id = WI_RUNTIME_ID_NULL; +static wi_runtime_class_t _wi_p7_socket_runtime_class = { "wi_p7_socket_t", _wi_p7_socket_dealloc, NULL, @@ -356,15 +356,15 @@ void wi_p7_socket_register(void) { void wi_p7_socket_initialize(void) { - char *env; - - env = getenv("wi_p7_socket_debug"); - - if(env) { - wi_p7_socket_debug = (strcmp(env, "0") != 0); - - printf("*** wi_p7_socket_initialize(): wi_p7_socket_debug = %u\n", wi_p7_socket_debug); - } + char *env; + + env = getenv("wi_p7_socket_debug"); + + if(env) { + wi_p7_socket_debug = (strcmp(env, "0") != 0); + + printf("*** wi_p7_socket_initialize(): wi_p7_socket_debug = %u\n", wi_p7_socket_debug); + } } @@ -386,60 +386,60 @@ wi_p7_socket_t * wi_p7_socket_alloc(void) { wi_p7_socket_t * wi_p7_socket_init_with_descriptor(wi_p7_socket_t *p7_socket, int sd, wi_p7_spec_t *p7_spec) { - p7_socket->socket = wi_socket_init_with_descriptor(wi_socket_alloc(), sd); - p7_socket->spec = wi_retain(p7_spec); + p7_socket->socket = wi_socket_init_with_descriptor(wi_socket_alloc(), sd); + p7_socket->spec = wi_retain(p7_spec); - return p7_socket; + return p7_socket; } wi_p7_socket_t * wi_p7_socket_init_with_socket(wi_p7_socket_t *p7_socket, wi_socket_t *socket, wi_p7_spec_t *p7_spec) { - p7_socket->socket = wi_retain(socket); - p7_socket->spec = wi_retain(p7_spec); - - return p7_socket; + p7_socket->socket = wi_retain(socket); + p7_socket->spec = wi_retain(p7_spec); + + return p7_socket; } static void _wi_p7_socket_dealloc(wi_runtime_instance_t *instance) { - wi_p7_socket_t *p7_socket = instance; - - if(p7_socket->compression_enabled) { - deflateEnd(&p7_socket->deflate_stream); - inflateEnd(&p7_socket->inflate_stream); - } - - wi_free(p7_socket->compression_buffer); - wi_free(p7_socket->encryption_buffer); - wi_free(p7_socket->decryption_buffer); - wi_free(p7_socket->oobdata_read_buffer); - - wi_release(p7_socket->socket); - wi_release(p7_socket->spec); - wi_release(p7_socket->merged_spec); - wi_release(p7_socket->remote_name); - wi_release(p7_socket->remote_version); - wi_release(p7_socket->user_name); - + wi_p7_socket_t *p7_socket = instance; + + if(p7_socket->compression_enabled) { + deflateEnd(&p7_socket->deflate_stream); + inflateEnd(&p7_socket->inflate_stream); + } + + wi_free(p7_socket->compression_buffer); + wi_free(p7_socket->encryption_buffer); + wi_free(p7_socket->decryption_buffer); + wi_free(p7_socket->oobdata_read_buffer); + + wi_release(p7_socket->socket); + wi_release(p7_socket->spec); + wi_release(p7_socket->merged_spec); + wi_release(p7_socket->remote_name); + wi_release(p7_socket->remote_version); + wi_release(p7_socket->user_name); + #ifdef WI_RSA - wi_release(p7_socket->private_key); - wi_release(p7_socket->public_key); - wi_release(p7_socket->cipher); + wi_release(p7_socket->private_key); + wi_release(p7_socket->public_key); + wi_release(p7_socket->cipher); #endif } static wi_string_t * _wi_p7_socket_description(wi_runtime_instance_t *instance) { - wi_p7_socket_t *p7_socket = instance; + wi_p7_socket_t *p7_socket = instance; - return wi_string_with_format(WI_STR("<%@ %p>{options = 0x%X, socket = %@}"), - wi_runtime_class_name(p7_socket), - p7_socket, - p7_socket->options, - p7_socket->socket); + return wi_string_with_format(WI_STR("<%@ %p>{options = 0x%X, socket = %@}"), + wi_runtime_class_name(p7_socket), + p7_socket, + p7_socket->options, + p7_socket->socket); } @@ -449,21 +449,21 @@ static wi_string_t * _wi_p7_socket_description(wi_runtime_instance_t *instance) #ifdef WI_RSA void wi_p7_socket_set_private_key(wi_p7_socket_t *p7_socket, wi_rsa_t *rsa) { - wi_release(p7_socket->private_key); - - p7_socket->private_key = wi_copy(rsa); + wi_release(p7_socket->private_key); + + p7_socket->private_key = wi_copy(rsa); } wi_rsa_t * wi_p7_socket_private_key(wi_p7_socket_t *p7_socket) { - return p7_socket->private_key; + return p7_socket->private_key; } wi_rsa_t * wi_p7_socket_public_key(wi_p7_socket_t *p7_socket) { - return p7_socket->public_key; + return p7_socket->public_key; } #endif @@ -471,15 +471,15 @@ wi_rsa_t * wi_p7_socket_public_key(wi_p7_socket_t *p7_socket) { void wi_p7_socket_set_read_message_callback(wi_p7_socket_t *p7_socket, wi_p7_socket_message_callback_func_t *callback, void *context) { - p7_socket->read_message_callback = callback; - p7_socket->read_message_context = context; + p7_socket->read_message_callback = callback; + p7_socket->read_message_context = context; } void wi_p7_socket_set_wrote_message_callback(wi_p7_socket_t *p7_socket, wi_p7_socket_message_callback_func_t *callback, void *context) { - p7_socket->wrote_message_callback = callback; - p7_socket->wrote_message_context = context; + p7_socket->wrote_message_callback = callback; + p7_socket->wrote_message_context = context; } @@ -487,13 +487,13 @@ void wi_p7_socket_set_wrote_message_callback(wi_p7_socket_t *p7_socket, wi_p7_so #pragma mark - wi_socket_t * wi_p7_socket_socket(wi_p7_socket_t *p7_socket) { - return p7_socket->socket; + return p7_socket->socket; } wi_p7_spec_t * wi_p7_socket_spec(wi_p7_socket_t *p7_socket) { - return p7_socket->spec; + return p7_socket->spec; } @@ -501,7 +501,7 @@ wi_p7_spec_t * wi_p7_socket_spec(wi_p7_socket_t *p7_socket) { #ifdef WI_RSA wi_cipher_t * wi_p7_socket_cipher(wi_p7_socket_t *p7_socket) { - return p7_socket->cipher; + return p7_socket->cipher; } #endif @@ -509,38 +509,38 @@ wi_cipher_t * wi_p7_socket_cipher(wi_p7_socket_t *p7_socket) { wi_uinteger_t wi_p7_socket_options(wi_p7_socket_t *p7_socket) { - return p7_socket->options; + return p7_socket->options; } wi_p7_serialization_t wi_p7_socket_serialization(wi_p7_socket_t *p7_socket) { - return p7_socket->serialization; + return p7_socket->serialization; } wi_string_t * wi_p7_socket_remote_protocol_name(wi_p7_socket_t *p7_socket) { - return p7_socket->remote_name; + return p7_socket->remote_name; } wi_string_t * wi_p7_socket_remote_protocol_version(wi_p7_socket_t *p7_socket) { - return p7_socket->remote_version; + return p7_socket->remote_version; } wi_string_t * wi_p7_socket_user_name(wi_p7_socket_t *p7_socket) { - return p7_socket->user_name; + return p7_socket->user_name; } double wi_p7_socket_compression_ratio(wi_p7_socket_t *p7_socket) { - return ((double) (p7_socket->sent_raw_bytes + p7_socket->read_processed_bytes) / - (double) (p7_socket->sent_processed_bytes + p7_socket->read_raw_bytes)); + return ((double) (p7_socket->sent_raw_bytes + p7_socket->read_processed_bytes) / + (double) (p7_socket->sent_processed_bytes + p7_socket->read_raw_bytes)); } @@ -548,207 +548,211 @@ double wi_p7_socket_compression_ratio(wi_p7_socket_t *p7_socket) { #pragma mark - static wi_boolean_t _wi_p7_socket_connect_handshake(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_uinteger_t options) { - wi_string_t *version; - wi_p7_message_t *p7_message; - wi_p7_enum_t flag; - - p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.client_handshake"), wi_p7_socket_spec(p7_socket)); - - if(!p7_message) - return false; - - if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(wi_p7_spec_builtin_spec()), WI_STR("p7.handshake.version"))) - return false; - - if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_name(p7_socket->spec), WI_STR("p7.handshake.protocol.name"))) - return false; - - if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(p7_socket->spec), WI_STR("p7.handshake.protocol.version"))) - return false; - - if(p7_socket->serialization == WI_P7_BINARY) { - if(WI_P7_COMPRESSION_ENABLED(options)) { - if(!wi_p7_message_set_enum_for_name(p7_message, - _WI_P7_COMPRESSION_OPTIONS_TO_ENUM(options), - WI_STR("p7.handshake.compression"))) { - return false; - } - } - - if(WI_P7_ENCRYPTION_ENABLED(options)) { + wi_string_t *version; + wi_p7_message_t *p7_message; + wi_p7_enum_t flag; + + p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.client_handshake"), wi_p7_socket_spec(p7_socket)); + + if(!p7_message) + return false; + + if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(wi_p7_spec_builtin_spec()), WI_STR("p7.handshake.version"))) + return false; + + if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_name(p7_socket->spec), WI_STR("p7.handshake.protocol.name"))) + return false; + + if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(p7_socket->spec), WI_STR("p7.handshake.protocol.version"))) + return false; + + if(p7_socket->serialization == WI_P7_BINARY) { + if(WI_P7_COMPRESSION_ENABLED(options)) { + if(!wi_p7_message_set_enum_for_name(p7_message, + _WI_P7_COMPRESSION_OPTIONS_TO_ENUM(options), + WI_STR("p7.handshake.compression"))) { + return false; + } + } + + if(WI_P7_ENCRYPTION_ENABLED(options)) { #ifdef WI_RSA - if(!wi_p7_message_set_enum_for_name(p7_message, - _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(options), - WI_STR("p7.handshake.encryption"))) { - return false; - } + if(!wi_p7_message_set_enum_for_name(p7_message, + _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(options), + WI_STR("p7.handshake.encryption"))) { + return false; + } #else - wi_error_set_libwired_error(WI_ERROR_P7_RSANOTSUPP); - - return false; + wi_error_set_libwired_error(WI_ERROR_P7_RSANOTSUPP); + + return false; #endif - } - - if(WI_P7_CHECKSUM_ENABLED(options)) { - if(!wi_p7_message_set_enum_for_name(p7_message, - _WI_P7_CHECKSUM_OPTIONS_TO_ENUM(options), - WI_STR("p7.handshake.checksum"))) { - return false; - } - } - } - - if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) - return false; - - p7_message = wi_p7_socket_read_message(p7_socket, timeout); - - if(!p7_message) - return false; - - if(!wi_is_equal(p7_message->name, WI_STR("p7.handshake.server_handshake"))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message should be \"p7.handshake.server_handshake\", not \"%@\""), - p7_message->name); - - return false; - } - - version = wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.version")); - - if(!version) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.version\" field")); - - return false; - } - - if(!wi_is_equal(version, wi_p7_spec_version(wi_p7_spec_builtin_spec()))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Remote P7 protocol %.1f is not compatible"), - version); - - return false; - } - - p7_socket->remote_name = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.name"))); - - if(!p7_socket->remote_name) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.protocol.name\" field")); - - return false; - } - - p7_socket->remote_version = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.version"))); - - if(!p7_socket->remote_version) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.protocol.version\" field")); - - return false; - } - - p7_socket->local_compatibility_check = !wi_p7_spec_is_compatible_with_protocol(p7_socket->spec, p7_socket->remote_name, p7_socket->remote_version); - - if(p7_socket->serialization == WI_P7_BINARY) { - if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.compression"))) - p7_socket->options |= _WI_P7_COMPRESSION_ENUM_TO_OPTIONS(flag); - - if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.encryption"))) - p7_socket->options |= _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag); - - if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.checksum"))) - p7_socket->options |= _WI_P7_CHECKSUM_ENUM_TO_OPTIONS(flag); - } - - if(!wi_p7_message_get_bool_for_name(p7_message, &p7_socket->remote_compatibility_check, WI_STR("p7.handshake.compatibility_check"))) - p7_socket->remote_compatibility_check = false; - - p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.acknowledge"), wi_p7_socket_spec(p7_socket)); - - if(!p7_message) - return false; - - if(p7_socket->local_compatibility_check) { - if(!wi_p7_message_set_bool_for_name(p7_message, true, WI_STR("p7.handshake.compatibility_check"))) - return false; - } - - if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) - return false; - - return true; + } + + if(WI_P7_CHECKSUM_ENABLED(options)) { + if(!wi_p7_message_set_enum_for_name(p7_message, + _WI_P7_CHECKSUM_OPTIONS_TO_ENUM(options), + WI_STR("p7.handshake.checksum"))) { + return false; + } + } + } + + if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) + return false; + + p7_message = wi_p7_socket_read_message(p7_socket, timeout); + + if(!p7_message) + return false; + + if(!wi_is_equal(p7_message->name, WI_STR("p7.handshake.server_handshake"))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message should be \"p7.handshake.server_handshake\", not \"%@\""), + p7_message->name); + + return false; + } + + version = wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.version")); + + if(!version) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.version\" field")); + + return false; + } + + if(!wi_is_equal(version, wi_p7_spec_version(wi_p7_spec_builtin_spec()))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Remote P7 protocol %.1f is not compatible"), + version); + + return false; + } + + p7_socket->remote_name = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.name"))); + + if(!p7_socket->remote_name) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.protocol.name\" field")); + + return false; + } + + p7_socket->remote_version = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.version"))); + + if(!p7_socket->remote_version) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.protocol.version\" field")); + + return false; + } + + p7_socket->local_compatibility_check = !wi_p7_spec_is_compatible_with_protocol(p7_socket->spec, p7_socket->remote_name, p7_socket->remote_version); + + if(p7_socket->serialization == WI_P7_BINARY) { + if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.compression"))) + p7_socket->options |= _WI_P7_COMPRESSION_ENUM_TO_OPTIONS(flag); + + if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.encryption"))) + p7_socket->options |= _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag); + + if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.checksum"))) + p7_socket->options |= _WI_P7_CHECKSUM_ENUM_TO_OPTIONS(flag); + } + + if(WI_P7_DEPRECATED_ENCRYPTION_CIPHER(p7_socket->options)) { + wi_log_warn(WI_STR("Connected using deprecated cipher: %d"), _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(p7_socket->options)); + } + + if(!wi_p7_message_get_bool_for_name(p7_message, &p7_socket->remote_compatibility_check, WI_STR("p7.handshake.compatibility_check"))) + p7_socket->remote_compatibility_check = false; + + p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.acknowledge"), wi_p7_socket_spec(p7_socket)); + + if(!p7_message) + return false; + + if(p7_socket->local_compatibility_check) { + if(!wi_p7_message_set_bool_for_name(p7_message, true, WI_STR("p7.handshake.compatibility_check"))) + return false; + } + + if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) + return false; + + return true; } static wi_boolean_t _wi_p7_socket_accept_handshake(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_uinteger_t options) { - wi_string_t *version; - wi_p7_message_t *p7_message; - wi_p7_enum_t flag; - wi_uinteger_t client_options; - - p7_message = wi_p7_socket_read_message(p7_socket, timeout); - - if(!p7_message) - return false; - - if(!wi_is_equal(p7_message->name, WI_STR("p7.handshake.client_handshake"))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message should be \"p7.handshake.client_handshake\", not \"%@\""), - p7_message->name); - - return false; - } - - version = wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.version")); - - if(!version) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.version\" field")); - - return false; - } - - if(!wi_is_equal(version, wi_p7_spec_version(wi_p7_spec_builtin_spec()))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Remote P7 protocol %.1f is not compatible"), - version); - - return false; - } - - p7_socket->remote_name = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.name"))); - - if(!p7_socket->remote_name) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.protocol.name\" field")); - - return false; - } - - p7_socket->remote_version = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.version"))); - - if(!p7_socket->remote_version) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.handshake.protocol.version\" field")); - - return false; - } - - p7_socket->local_compatibility_check = !wi_p7_spec_is_compatible_with_protocol(p7_socket->spec, p7_socket->remote_name, p7_socket->remote_version); - - if(p7_socket->serialization == WI_P7_BINARY) { - if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.compression"))) { - client_options = _WI_P7_COMPRESSION_ENUM_TO_OPTIONS(flag); - - if(options & client_options) - p7_socket->options |= client_options; - } - - if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.encryption"))) { - client_options = _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag); + wi_string_t *version; + wi_p7_message_t *p7_message; + wi_p7_enum_t flag; + wi_uinteger_t client_options; + + p7_message = wi_p7_socket_read_message(p7_socket, timeout); + + if(!p7_message) + return false; + + if(!wi_is_equal(p7_message->name, WI_STR("p7.handshake.client_handshake"))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message should be \"p7.handshake.client_handshake\", not \"%@\""), + p7_message->name); + + return false; + } + + version = wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.version")); + + if(!version) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.version\" field")); + + return false; + } + + if(!wi_is_equal(version, wi_p7_spec_version(wi_p7_spec_builtin_spec()))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Remote P7 protocol %.1f is not compatible"), + version); + + return false; + } + + p7_socket->remote_name = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.name"))); + + if(!p7_socket->remote_name) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.protocol.name\" field")); + + return false; + } + + p7_socket->remote_version = wi_retain(wi_p7_message_string_for_name(p7_message, WI_STR("p7.handshake.protocol.version"))); + + if(!p7_socket->remote_version) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.handshake.protocol.version\" field")); + + return false; + } + + p7_socket->local_compatibility_check = !wi_p7_spec_is_compatible_with_protocol(p7_socket->spec, p7_socket->remote_name, p7_socket->remote_version); + + if(p7_socket->serialization == WI_P7_BINARY) { + if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.compression"))) { + client_options = _WI_P7_COMPRESSION_ENUM_TO_OPTIONS(flag); + + if(options & client_options) + p7_socket->options |= client_options; + } + + if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.encryption"))) { + client_options = _WI_P7_ENCRYPTION_ENUM_TO_OPTIONS(flag); #ifdef WI_RSA if(options & client_options) @@ -778,6 +782,10 @@ static wi_boolean_t _wi_p7_socket_accept_handshake(wi_p7_socket_t *p7_socket, wi if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(p7_socket->spec), WI_STR("p7.handshake.protocol.version"))) return false; + if(WI_P7_DEPRECATED_ENCRYPTION_CIPHER(p7_socket->options)) { + wi_log_warn(WI_STR("Connected using deprecated cipher: %d"), _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(p7_socket->options)); + } + if(p7_socket->serialization == WI_P7_BINARY) { if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { if(!wi_p7_message_set_enum_for_name(p7_message, @@ -1160,26 +1168,26 @@ static wi_boolean_t _wi_p7_socket_accept_key_exchange(wi_p7_socket_t *p7_socket, static wi_boolean_t _wi_p7_password_is_equal(wi_string_t *password1, wi_string_t *password2) { - const char *cstring1, *cstring2; - wi_uinteger_t length1, length2, i; - wi_boolean_t result; - - length1 = wi_string_length(password1); - length2 = wi_string_length(password2); - - if(length1 != length2) - return false; - - cstring1 = wi_string_cstring(password1); - cstring2 = wi_string_cstring(password2); - result = true; - - for(i = 0; i < length1; i++) { - if(cstring1[i] != cstring2[i]) - result = false; - } - - return result; + const char *cstring1, *cstring2; + wi_uinteger_t length1, length2, i; + wi_boolean_t result; + + length1 = wi_string_length(password1); + length2 = wi_string_length(password2); + + if(length1 != length2) + return false; + + cstring1 = wi_string_cstring(password1); + cstring2 = wi_string_cstring(password2); + result = true; + + for(i = 0; i < length1; i++) { + if(cstring1[i] != cstring2[i]) + result = false; + } + + return result; } #endif @@ -1187,115 +1195,115 @@ static wi_boolean_t _wi_p7_password_is_equal(wi_string_t *password1, wi_string_t static wi_boolean_t _wi_p7_socket_send_compatibility_check(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout) { - wi_p7_message_t *p7_message; - wi_p7_boolean_t status; - - p7_message = wi_p7_message_with_name(WI_STR("p7.compatibility_check.specification"), wi_p7_socket_spec(p7_socket)); - - if(!p7_message) - return false; - - if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_xml(p7_socket->spec), WI_STR("p7.compatibility_check.specification"))) - return false; - - if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) - return false; - - p7_message = wi_p7_socket_read_message(p7_socket, timeout); - - if(!p7_message) - return false; - - if(!wi_is_equal(p7_message->name, WI_STR("p7.compatibility_check.status"))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message should be \"p7.compatibility_check.status\", not \"%@\""), - p7_message->name); - - return false; - } - - if(!wi_p7_message_get_bool_for_name(p7_message, &status, WI_STR("p7.compatibility_check.status"))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.compatibility_check.status\" field")); - - return false; - } - - if(!status) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_INCOMPATIBLESPEC, - WI_STR("Remote protocol %@ %@ is not compatible with local protocol %@ %@"), - p7_socket->remote_name, - p7_socket->remote_version, - wi_p7_spec_name(p7_socket->spec), - wi_p7_spec_version(p7_socket->spec)); - } - - return status; + wi_p7_message_t *p7_message; + wi_p7_boolean_t status; + + p7_message = wi_p7_message_with_name(WI_STR("p7.compatibility_check.specification"), wi_p7_socket_spec(p7_socket)); + + if(!p7_message) + return false; + + if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_xml(p7_socket->spec), WI_STR("p7.compatibility_check.specification"))) + return false; + + if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) + return false; + + p7_message = wi_p7_socket_read_message(p7_socket, timeout); + + if(!p7_message) + return false; + + if(!wi_is_equal(p7_message->name, WI_STR("p7.compatibility_check.status"))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message should be \"p7.compatibility_check.status\", not \"%@\""), + p7_message->name); + + return false; + } + + if(!wi_p7_message_get_bool_for_name(p7_message, &status, WI_STR("p7.compatibility_check.status"))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.compatibility_check.status\" field")); + + return false; + } + + if(!status) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_INCOMPATIBLESPEC, + WI_STR("Remote protocol %@ %@ is not compatible with local protocol %@ %@"), + p7_socket->remote_name, + p7_socket->remote_version, + wi_p7_spec_name(p7_socket->spec), + wi_p7_spec_version(p7_socket->spec)); + } + + return status; } static wi_boolean_t _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout) { - wi_string_t *string; - wi_p7_message_t *p7_message; - wi_p7_spec_t *p7_spec; - wi_boolean_t compatible; - - p7_message = wi_p7_socket_read_message(p7_socket, timeout); - - if(!p7_message) - return false; - - if(!wi_is_equal(p7_message->name, WI_STR("p7.compatibility_check.specification"))) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message should be \"p7.compatibility_check.specification\", not \"%@\""), - p7_message->name); - - return false; - } - - string = wi_p7_message_string_for_name(p7_message, WI_STR("p7.compatibility_check.specification")); - - if(!string) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Message has no \"p7.compatibility_check.specification\" field")); - - return false; - } - - p7_spec = wi_autorelease(wi_p7_spec_init_with_string(wi_p7_spec_alloc(), string, - wi_p7_spec_opposite_originator(wi_p7_spec_originator(p7_socket->spec)))); - - if(!p7_spec) - return false; - - compatible = wi_p7_spec_is_compatible_with_spec(p7_socket->spec, p7_spec); - - if(compatible) { - p7_socket->merged_spec = wi_copy(p7_socket->spec); - - wi_p7_spec_merge_with_spec(p7_socket->merged_spec, p7_spec); - } else { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_INCOMPATIBLESPEC, - WI_STR("Remote protocol %@ %@ is not compatible with local protocol %@ %@: %m"), - p7_socket->remote_name, - p7_socket->remote_version, - wi_p7_spec_name(p7_socket->spec), - wi_p7_spec_version(p7_socket->spec)); - } - - p7_message = wi_p7_message_with_name(WI_STR("p7.compatibility_check.status"), wi_p7_socket_spec(p7_socket)); - - if(!p7_message) - return false; - - if(!wi_p7_message_set_bool_for_name(p7_message, compatible, WI_STR("p7.compatibility_check.status"))) - return false; - - if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) - return false; - - return compatible; + wi_string_t *string; + wi_p7_message_t *p7_message; + wi_p7_spec_t *p7_spec; + wi_boolean_t compatible; + + p7_message = wi_p7_socket_read_message(p7_socket, timeout); + + if(!p7_message) + return false; + + if(!wi_is_equal(p7_message->name, WI_STR("p7.compatibility_check.specification"))) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message should be \"p7.compatibility_check.specification\", not \"%@\""), + p7_message->name); + + return false; + } + + string = wi_p7_message_string_for_name(p7_message, WI_STR("p7.compatibility_check.specification")); + + if(!string) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Message has no \"p7.compatibility_check.specification\" field")); + + return false; + } + + p7_spec = wi_autorelease(wi_p7_spec_init_with_string(wi_p7_spec_alloc(), string, + wi_p7_spec_opposite_originator(wi_p7_spec_originator(p7_socket->spec)))); + + if(!p7_spec) + return false; + + compatible = wi_p7_spec_is_compatible_with_spec(p7_socket->spec, p7_spec); + + if(compatible) { + p7_socket->merged_spec = wi_copy(p7_socket->spec); + + wi_p7_spec_merge_with_spec(p7_socket->merged_spec, p7_spec); + } else { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_INCOMPATIBLESPEC, + WI_STR("Remote protocol %@ %@ is not compatible with local protocol %@ %@: %m"), + p7_socket->remote_name, + p7_socket->remote_version, + wi_p7_spec_name(p7_socket->spec), + wi_p7_spec_version(p7_socket->spec)); + } + + p7_message = wi_p7_message_with_name(WI_STR("p7.compatibility_check.status"), wi_p7_socket_spec(p7_socket)); + + if(!p7_message) + return false; + + if(!wi_p7_message_set_bool_for_name(p7_message, compatible, WI_STR("p7.compatibility_check.status"))) + return false; + + if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) + return false; + + return compatible; } @@ -1303,215 +1311,215 @@ static wi_boolean_t _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *p7 #pragma mark - static wi_boolean_t _wi_p7_socket_write_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_p7_message_t *p7_message) { - const void *send_buffer; - char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; - unsigned char checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - wi_integer_t compressed_size; + const void *send_buffer; + char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; + unsigned char checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + wi_integer_t compressed_size; #ifdef WI_RSA - wi_integer_t encrypted_size; + wi_integer_t encrypted_size; #endif - uint32_t send_size; - - send_size = p7_message->binary_size; - send_buffer = p7_message->binary_buffer; - - p7_socket->sent_raw_bytes += send_size; - - if(p7_socket->compression_enabled) { - compressed_size = _wi_p7_socket_deflate(p7_socket, send_buffer, send_size); - - if(compressed_size < 0) - return false; - - send_size = compressed_size; - send_buffer = p7_socket->compression_buffer; - } - + uint32_t send_size; + + send_size = p7_message->binary_size; + send_buffer = p7_message->binary_buffer; + + p7_socket->sent_raw_bytes += send_size; + + if(p7_socket->compression_enabled) { + compressed_size = _wi_p7_socket_deflate(p7_socket, send_buffer, send_size); + + if(compressed_size < 0) + return false; + + send_size = compressed_size; + send_buffer = p7_socket->compression_buffer; + } + #ifdef WI_RSA - if(p7_socket->encryption_enabled) { - encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); - - if(!p7_socket->encryption_buffer) { - p7_socket->encryption_buffer_length = encrypted_size; - p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); - } - else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { - p7_socket->encryption_buffer_length = encrypted_size * 2; - p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); - } - - encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, - send_buffer, - send_size, - p7_socket->encryption_buffer); - - if(encrypted_size < 0) - return false; - - send_size = encrypted_size; - send_buffer = p7_socket->encryption_buffer; - } + if(p7_socket->encryption_enabled) { + encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); + + if(!p7_socket->encryption_buffer) { + p7_socket->encryption_buffer_length = encrypted_size; + p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); + } + else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { + p7_socket->encryption_buffer_length = encrypted_size * 2; + p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); + } + + encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, + send_buffer, + send_size, + p7_socket->encryption_buffer); + + if(encrypted_size < 0) + return false; + + send_size = encrypted_size; + send_buffer = p7_socket->encryption_buffer; + } #endif - p7_socket->sent_processed_bytes += send_size; - - wi_write_swap_host_to_big_int32(length_buffer, 0, send_size); - - if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) - return false; - - if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) - return false; - - if(p7_socket->checksum_enabled) { - _wi_p7_socket_checksum_binary_message(p7_socket, p7_message, checksum_buffer); - - if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) - return false; - } - - return true; + p7_socket->sent_processed_bytes += send_size; + + wi_write_swap_host_to_big_int32(length_buffer, 0, send_size); + + if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) + return false; + + if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) + return false; + + if(p7_socket->checksum_enabled) { + _wi_p7_socket_checksum_binary_message(p7_socket, p7_message, checksum_buffer); + + if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) + return false; + } + + return true; } static wi_boolean_t _wi_p7_socket_write_xml_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_p7_message_t *p7_message) { - if(wi_socket_write_format(p7_socket->socket, timeout, WI_STR("%s\r\n"), p7_message->xml_buffer) < 0) - return false; - - p7_socket->sent_raw_bytes += p7_message->xml_length; - p7_socket->sent_processed_bytes += p7_message->xml_length; - - return true; + if(wi_socket_write_format(p7_socket->socket, timeout, WI_STR("%s\r\n"), p7_message->xml_buffer) < 0) + return false; + + p7_socket->sent_raw_bytes += p7_message->xml_length; + p7_socket->sent_processed_bytes += p7_message->xml_length; + + return true; } static wi_p7_message_t * _wi_p7_socket_read_binary_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, uint32_t message_size) { - wi_p7_message_t *p7_message; - unsigned char local_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - unsigned char remote_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - wi_integer_t decompressed_size; + wi_p7_message_t *p7_message; + unsigned char local_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + unsigned char remote_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + wi_integer_t decompressed_size; #ifdef WI_RSA - wi_integer_t decrypted_size; + wi_integer_t decrypted_size; #endif - int32_t length; - - if(message_size > _WI_P7_SOCKET_MAX_BINARY_SIZE) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_MESSAGETOOLARGE, - WI_STR("%u bytes"), message_size); - - return NULL; - } - - p7_message = wi_autorelease(wi_p7_message_init(wi_p7_message_alloc(), p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec)); - p7_message->binary_capacity = message_size; - p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); - - length = wi_socket_read_buffer(p7_socket->socket, timeout, p7_message->binary_buffer, message_size); - - if(length <= 0) - return NULL; - - p7_message->binary_size = length; - p7_socket->message_binary_size = 0; - p7_socket->read_raw_bytes += p7_message->binary_size; + int32_t length; + + if(message_size > _WI_P7_SOCKET_MAX_BINARY_SIZE) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_MESSAGETOOLARGE, + WI_STR("%u bytes"), message_size); + + return NULL; + } + + p7_message = wi_autorelease(wi_p7_message_init(wi_p7_message_alloc(), p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec)); + p7_message->binary_capacity = message_size; + p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); + + length = wi_socket_read_buffer(p7_socket->socket, timeout, p7_message->binary_buffer, message_size); + + if(length <= 0) + return NULL; + + p7_message->binary_size = length; + p7_socket->message_binary_size = 0; + p7_socket->read_raw_bytes += p7_message->binary_size; #ifdef WI_RSA - if(p7_socket->encryption_enabled) { - decrypted_size = p7_message->binary_size + wi_cipher_block_size(p7_socket->cipher); - - if(!p7_socket->decryption_buffer) { - p7_socket->decryption_buffer_length = decrypted_size; - p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); - } - else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { - p7_socket->decryption_buffer_length = decrypted_size * 2; - p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); - } - - decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, - p7_message->binary_buffer, - p7_message->binary_size, - p7_socket->decryption_buffer); - - if(decrypted_size < 0) - return NULL; - - if((wi_uinteger_t) decrypted_size > p7_message->binary_capacity) { - p7_message->binary_capacity = decrypted_size; - p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); - } - - memcpy(p7_message->binary_buffer, p7_socket->decryption_buffer, decrypted_size); - - p7_message->binary_size = decrypted_size; - } + if(p7_socket->encryption_enabled) { + decrypted_size = p7_message->binary_size + wi_cipher_block_size(p7_socket->cipher); + + if(!p7_socket->decryption_buffer) { + p7_socket->decryption_buffer_length = decrypted_size; + p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); + } + else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { + p7_socket->decryption_buffer_length = decrypted_size * 2; + p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); + } + + decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, + p7_message->binary_buffer, + p7_message->binary_size, + p7_socket->decryption_buffer); + + if(decrypted_size < 0) + return NULL; + + if((wi_uinteger_t) decrypted_size > p7_message->binary_capacity) { + p7_message->binary_capacity = decrypted_size; + p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); + } + + memcpy(p7_message->binary_buffer, p7_socket->decryption_buffer, decrypted_size); + + p7_message->binary_size = decrypted_size; + } #endif - - if(p7_socket->compression_enabled) { - decompressed_size = _wi_p7_socket_inflate(p7_socket, p7_message->binary_buffer, p7_message->binary_size); - - if(decompressed_size < 0) - return NULL; - - if((wi_uinteger_t) decompressed_size > p7_message->binary_capacity) { - p7_message->binary_capacity = decompressed_size; - p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); - } - - memcpy(p7_message->binary_buffer, p7_socket->compression_buffer, decompressed_size); - - p7_message->binary_size = decompressed_size; - } - - p7_socket->read_processed_bytes += p7_message->binary_size; - - if(p7_socket->checksum_enabled) { - length = wi_socket_read_buffer(p7_socket->socket, timeout, remote_checksum_buffer, p7_socket->checksum_length); - - if(length <= 0) - return NULL; - - _wi_p7_socket_checksum_binary_message(p7_socket, p7_message, local_checksum_buffer); - - if(memcmp(remote_checksum_buffer, local_checksum_buffer, p7_socket->checksum_length) != 0) { - wi_error_set_libwired_error(WI_ERROR_P7_CHECKSUMMISMATCH); - - return NULL; - } - } - - return p7_message; + + if(p7_socket->compression_enabled) { + decompressed_size = _wi_p7_socket_inflate(p7_socket, p7_message->binary_buffer, p7_message->binary_size); + + if(decompressed_size < 0) + return NULL; + + if((wi_uinteger_t) decompressed_size > p7_message->binary_capacity) { + p7_message->binary_capacity = decompressed_size; + p7_message->binary_buffer = wi_realloc(p7_message->binary_buffer, p7_message->binary_capacity); + } + + memcpy(p7_message->binary_buffer, p7_socket->compression_buffer, decompressed_size); + + p7_message->binary_size = decompressed_size; + } + + p7_socket->read_processed_bytes += p7_message->binary_size; + + if(p7_socket->checksum_enabled) { + length = wi_socket_read_buffer(p7_socket->socket, timeout, remote_checksum_buffer, p7_socket->checksum_length); + + if(length <= 0) + return NULL; + + _wi_p7_socket_checksum_binary_message(p7_socket, p7_message, local_checksum_buffer); + + if(memcmp(remote_checksum_buffer, local_checksum_buffer, p7_socket->checksum_length) != 0) { + wi_error_set_libwired_error(WI_ERROR_P7_CHECKSUMMISMATCH); + + return NULL; + } + } + + return p7_message; } static wi_p7_message_t * _wi_p7_socket_read_xml_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_string_t *prefix) { - wi_string_t *string; - wi_p7_message_t *p7_message; - wi_uinteger_t length; - - p7_message = wi_autorelease(wi_p7_message_init(wi_p7_message_alloc(), p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec)); - - string = wi_socket_read_to_string(p7_socket->socket, timeout, WI_STR("\r\n")); - - if(!string || wi_string_length(string) == 0) - return NULL; - - p7_message->xml_string = wi_mutable_copy(wi_string_by_deleting_surrounding_whitespace(string)); - - if(prefix) - wi_mutable_string_insert_string_at_index(p7_message->xml_string, prefix, 0); - - length = wi_string_length(p7_message->xml_string); - - p7_socket->read_raw_bytes += length; - p7_socket->read_processed_bytes += length; - - wi_mutable_string_delete_surrounding_whitespace(p7_message->xml_string); - - return p7_message; + wi_string_t *string; + wi_p7_message_t *p7_message; + wi_uinteger_t length; + + p7_message = wi_autorelease(wi_p7_message_init(wi_p7_message_alloc(), p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec)); + + string = wi_socket_read_to_string(p7_socket->socket, timeout, WI_STR("\r\n")); + + if(!string || wi_string_length(string) == 0) + return NULL; + + p7_message->xml_string = wi_mutable_copy(wi_string_by_deleting_surrounding_whitespace(string)); + + if(prefix) + wi_mutable_string_insert_string_at_index(p7_message->xml_string, prefix, 0); + + length = wi_string_length(p7_message->xml_string); + + p7_socket->read_raw_bytes += length; + p7_socket->read_processed_bytes += length; + + wi_mutable_string_delete_surrounding_whitespace(p7_message->xml_string); + + return p7_message; } @@ -1519,104 +1527,104 @@ static wi_p7_message_t * _wi_p7_socket_read_xml_message(wi_p7_socket_t *p7_socke #pragma mark - static wi_boolean_t _wi_p7_socket_configure_compression(wi_p7_socket_t *p7_socket) { - int err; - - p7_socket->deflate_stream.data_type = Z_UNKNOWN; - - err = deflateInit(&p7_socket->deflate_stream, Z_DEFAULT_COMPRESSION); - - if(err != Z_OK) { - wi_error_set_zlib_error(err); - - return false; - } - - err = inflateInit(&p7_socket->inflate_stream); - - if(err != Z_OK) { - wi_error_set_zlib_error(err); - - return false; - } - - p7_socket->compression_enabled = true; - - return true; + int err; + + p7_socket->deflate_stream.data_type = Z_UNKNOWN; + + err = deflateInit(&p7_socket->deflate_stream, Z_DEFAULT_COMPRESSION); + + if(err != Z_OK) { + wi_error_set_zlib_error(err); + + return false; + } + + err = inflateInit(&p7_socket->inflate_stream); + + if(err != Z_OK) { + wi_error_set_zlib_error(err); + + return false; + } + + p7_socket->compression_enabled = true; + + return true; } static wi_integer_t _wi_p7_socket_deflate(wi_p7_socket_t *p7_socket, const void *in_buffer, uint32_t in_size) { - wi_integer_t bytes; - size_t length; - int err, enderr; - - length = (in_size * 2) + 16; - - if(!p7_socket->compression_buffer) { - p7_socket->compression_buffer = wi_malloc(length); - p7_socket->compression_buffer_length = length; - } - else if(p7_socket->compression_buffer_length < length) { - p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, length); - p7_socket->compression_buffer_length = length; - } - - p7_socket->deflate_stream.next_in = (unsigned char *) in_buffer; - p7_socket->deflate_stream.avail_in = in_size; - p7_socket->deflate_stream.next_out = p7_socket->compression_buffer; - p7_socket->deflate_stream.avail_out = p7_socket->compression_buffer_length; - - err = deflate(&p7_socket->deflate_stream, Z_FINISH); - bytes = p7_socket->deflate_stream.total_out; - enderr = deflateReset(&p7_socket->deflate_stream); - - if(err != Z_STREAM_END) { - if(err == Z_OK) - wi_error_set_zlib_error(Z_BUF_ERROR); - else - wi_error_set_zlib_error(err); - - return -1; - } - - if(enderr != Z_OK) { - wi_error_set_zlib_error(err); - - return -1; - } - - return bytes; + wi_integer_t bytes; + size_t length; + int err, enderr; + + length = (in_size * 2) + 16; + + if(!p7_socket->compression_buffer) { + p7_socket->compression_buffer = wi_malloc(length); + p7_socket->compression_buffer_length = length; + } + else if(p7_socket->compression_buffer_length < length) { + p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, length); + p7_socket->compression_buffer_length = length; + } + + p7_socket->deflate_stream.next_in = (unsigned char *) in_buffer; + p7_socket->deflate_stream.avail_in = in_size; + p7_socket->deflate_stream.next_out = p7_socket->compression_buffer; + p7_socket->deflate_stream.avail_out = p7_socket->compression_buffer_length; + + err = deflate(&p7_socket->deflate_stream, Z_FINISH); + bytes = p7_socket->deflate_stream.total_out; + enderr = deflateReset(&p7_socket->deflate_stream); + + if(err != Z_STREAM_END) { + if(err == Z_OK) + wi_error_set_zlib_error(Z_BUF_ERROR); + else + wi_error_set_zlib_error(err); + + return -1; + } + + if(enderr != Z_OK) { + wi_error_set_zlib_error(err); + + return -1; + } + + return bytes; } static wi_integer_t _wi_p7_socket_inflate(wi_p7_socket_t *p7_socket, const void *in_buffer, uint32_t in_size) { - wi_uinteger_t multiple, bytes; - int err, enderr; - - for(multiple = 2; multiple < 16; multiple++) { - p7_socket->compression_buffer_length = in_size * (1 << multiple); - - if(!p7_socket->compression_buffer) - p7_socket->compression_buffer = wi_malloc(p7_socket->compression_buffer_length); - else - p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); - - p7_socket->inflate_stream.next_in = (unsigned char *) in_buffer; - p7_socket->inflate_stream.avail_in = in_size; - p7_socket->inflate_stream.next_out = (unsigned char *) p7_socket->compression_buffer; - p7_socket->inflate_stream.avail_out = p7_socket->compression_buffer_length; - - err = inflate(&p7_socket->inflate_stream, Z_FINISH); - bytes = p7_socket->inflate_stream.total_out; - enderr = inflateReset(&p7_socket->inflate_stream); - - if(err == Z_STREAM_END && enderr != Z_BUF_ERROR) - break; - } - - return bytes; + wi_uinteger_t multiple, bytes; + int err, enderr; + + for(multiple = 2; multiple < 16; multiple++) { + p7_socket->compression_buffer_length = in_size * (1 << multiple); + + if(!p7_socket->compression_buffer) + p7_socket->compression_buffer = wi_malloc(p7_socket->compression_buffer_length); + else + p7_socket->compression_buffer = wi_realloc(p7_socket->compression_buffer, p7_socket->compression_buffer_length); + + p7_socket->inflate_stream.next_in = (unsigned char *) in_buffer; + p7_socket->inflate_stream.avail_in = in_size; + p7_socket->inflate_stream.next_out = (unsigned char *) p7_socket->compression_buffer; + p7_socket->inflate_stream.avail_out = p7_socket->compression_buffer_length; + + err = inflate(&p7_socket->inflate_stream, Z_FINISH); + bytes = p7_socket->inflate_stream.total_out; + enderr = inflateReset(&p7_socket->inflate_stream); + + if(err == Z_STREAM_END && enderr != Z_BUF_ERROR) + break; + } + + return bytes; } @@ -1626,20 +1634,20 @@ static wi_integer_t _wi_p7_socket_inflate(wi_p7_socket_t *p7_socket, const void static void _wi_p7_socket_configure_checksum(wi_p7_socket_t *p7_socket) { if(p7_socket->options & WI_P7_CHECKSUM_SHA1) p7_socket->checksum_length = WI_SHA1_DIGEST_LENGTH; - + else if(p7_socket->options & WI_P7_CHECKSUM_SHA256) p7_socket->checksum_length = WI_SHA256_DIGEST_LENGTH; - + else if(p7_socket->options & WI_P7_CHECKSUM_SHA512) p7_socket->checksum_length = WI_SHA512_DIGEST_LENGTH; - + p7_socket->checksum_enabled = true; } static void _wi_p7_socket_checksum_binary_message(wi_p7_socket_t *p7_socket, wi_p7_message_t *p7_message, void *out_buffer) { - _wi_p7_socket_checksum_buffer(p7_socket, p7_message->binary_buffer, p7_message->binary_size, out_buffer); + _wi_p7_socket_checksum_buffer(p7_socket, p7_message->binary_buffer, p7_message->binary_size, out_buffer); } @@ -1647,8 +1655,10 @@ static void _wi_p7_socket_checksum_binary_message(wi_p7_socket_t *p7_socket, wi_ static void _wi_p7_socket_checksum_buffer(wi_p7_socket_t *p7_socket, const void *buffer, uint32_t size, void *out_buffer) { if(p7_socket->options & WI_P7_CHECKSUM_SHA1) wi_sha1_digest(buffer, size, out_buffer); + else if(p7_socket->options & WI_P7_CHECKSUM_SHA256) wi_sha256_digest(buffer, size, out_buffer); + else if(p7_socket->options & WI_P7_CHECKSUM_SHA512) wi_sha512_digest(buffer, size, out_buffer); } @@ -1658,7 +1668,7 @@ static void _wi_p7_socket_checksum_buffer(wi_p7_socket_t *p7_socket, const void #pragma mark - wi_boolean_t wi_p7_socket_verify_message(wi_p7_socket_t *p7_socket, wi_p7_message_t *p7_message) { - return wi_p7_spec_verify_message(p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec, p7_message); + return wi_p7_spec_verify_message(p7_socket->merged_spec ? p7_socket->merged_spec : p7_socket->spec, p7_message); } @@ -1666,24 +1676,25 @@ wi_boolean_t wi_p7_socket_verify_message(wi_p7_socket_t *p7_socket, wi_p7_messag #pragma mark - wi_boolean_t wi_p7_socket_connect(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_uinteger_t options, wi_p7_serialization_t serialization, wi_string_t *username, wi_string_t *password) { - p7_socket->serialization = serialization; - - if(!_wi_p7_socket_connect_handshake(p7_socket, timeout, options)) - return false; - - if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { - if(!_wi_p7_socket_configure_compression(p7_socket)) - return false; - } - - if(WI_P7_CHECKSUM_ENABLED(p7_socket->options)) - _wi_p7_socket_configure_checksum(p7_socket); - + p7_socket->serialization = serialization; + + if(!_wi_p7_socket_connect_handshake(p7_socket, timeout, options)) + return false; + + if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { + if(!_wi_p7_socket_configure_compression(p7_socket)) + return false; + } + + if(WI_P7_CHECKSUM_ENABLED(p7_socket->options)) + _wi_p7_socket_configure_checksum(p7_socket); + + #ifdef WI_RSA - if(WI_P7_ENCRYPTION_ENABLED(p7_socket->options)) { - if(!_wi_p7_socket_connect_key_exchange(p7_socket, timeout, username, password)) - return false; - } + if(WI_P7_ENCRYPTION_ENABLED(p7_socket->options)) { + if(!_wi_p7_socket_connect_key_exchange(p7_socket, timeout, username, password)) + return false; + } #endif if(p7_socket->remote_compatibility_check) { @@ -1702,22 +1713,22 @@ wi_boolean_t wi_p7_socket_connect(wi_p7_socket_t *p7_socket, wi_time_interval_t wi_boolean_t wi_p7_socket_accept(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_uinteger_t options) { - if(!_wi_p7_socket_accept_handshake(p7_socket, timeout, options)) - return false; - - if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { - if(!_wi_p7_socket_configure_compression(p7_socket)) - return false; - } - - if(WI_P7_CHECKSUM_ENABLED(p7_socket->options)) - _wi_p7_socket_configure_checksum(p7_socket); - + if(!_wi_p7_socket_accept_handshake(p7_socket, timeout, options)) + return false; + + if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { + if(!_wi_p7_socket_configure_compression(p7_socket)) + return false; + } + + if(WI_P7_CHECKSUM_ENABLED(p7_socket->options)) + _wi_p7_socket_configure_checksum(p7_socket); + #ifdef WI_RSA - if(WI_P7_ENCRYPTION_ENABLED(p7_socket->options)) { - if(!_wi_p7_socket_accept_key_exchange(p7_socket, timeout)) - return false; - } + if(WI_P7_ENCRYPTION_ENABLED(p7_socket->options)) { + if(!_wi_p7_socket_accept_key_exchange(p7_socket, timeout)) + return false; + } #endif if(p7_socket->local_compatibility_check) { @@ -1743,267 +1754,267 @@ void wi_p7_socket_close(wi_p7_socket_t *p7_socket) { #pragma mark - wi_boolean_t wi_p7_socket_write_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_p7_message_t *p7_message) { - wi_boolean_t result; - - wi_p7_message_serialize(p7_message, wi_p7_socket_serialization(p7_socket)); - - if(wi_p7_socket_debug) - wi_log_debug(WI_STR("Sending %@"), p7_message); - - if(p7_socket->serialization == WI_P7_BINARY) - result = _wi_p7_socket_write_binary_message(p7_socket, timeout, p7_message); - else - result = _wi_p7_socket_write_xml_message(p7_socket, timeout, p7_message); - - if(!result) - return false; - - if(wi_p7_socket_debug) { - wi_log_debug(WI_STR("Sent %llu processed bytes, %llu raw bytes, compressed to %.2f%%"), - p7_socket->sent_processed_bytes, - p7_socket->sent_raw_bytes, - ((double) p7_socket->sent_processed_bytes / (double) p7_socket->sent_raw_bytes) * 100.0); - } - - if(p7_socket->wrote_message_callback) - (*p7_socket->wrote_message_callback)(p7_socket, p7_message, p7_socket->wrote_message_context); - - return true; + wi_boolean_t result; + + wi_p7_message_serialize(p7_message, wi_p7_socket_serialization(p7_socket)); + + if(wi_p7_socket_debug) + wi_log_debug(WI_STR("Sending %@"), p7_message); + + if(p7_socket->serialization == WI_P7_BINARY) + result = _wi_p7_socket_write_binary_message(p7_socket, timeout, p7_message); + else + result = _wi_p7_socket_write_xml_message(p7_socket, timeout, p7_message); + + if(!result) + return false; + + if(wi_p7_socket_debug) { + wi_log_debug(WI_STR("Sent %llu processed bytes, %llu raw bytes, compressed to %.2f%%"), + p7_socket->sent_processed_bytes, + p7_socket->sent_raw_bytes, + ((double) p7_socket->sent_processed_bytes / (double) p7_socket->sent_raw_bytes) * 100.0); + } + + if(p7_socket->wrote_message_callback) + (*p7_socket->wrote_message_callback)(p7_socket, p7_message, p7_socket->wrote_message_context); + + return true; } wi_p7_message_t * wi_p7_socket_read_message(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout) { - wi_p7_message_t *p7_message; - wi_string_t *prefix = NULL; - char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; - - if(p7_socket->serialization == WI_P7_UNKNOWN || p7_socket->serialization == WI_P7_BINARY) { - if(p7_socket->message_binary_size == 0) { - if(wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) <= 0) - return NULL; - - p7_socket->message_binary_size = wi_read_swap_big_to_host_int32(length_buffer, 0); - } - - if(p7_socket->serialization == WI_P7_UNKNOWN) { - if(p7_socket->message_binary_size == _WI_P7_SOCKET_XML_MAGIC) { - p7_socket->serialization = WI_P7_XML; - prefix = WI_STR("message_binary_size < _WI_P7_SOCKET_MAX_BINARY_SIZE) { - p7_socket->serialization = WI_P7_BINARY; - } - } - } - - if(p7_socket->serialization == WI_P7_BINARY) - p7_message = _wi_p7_socket_read_binary_message(p7_socket, timeout, p7_socket->message_binary_size); - else if(p7_socket->serialization == WI_P7_XML) - p7_message = _wi_p7_socket_read_xml_message(p7_socket, timeout, prefix); - else { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, - WI_STR("Invalid data from remote host (%u doesn't look like a header)"), - p7_socket->message_binary_size); - - return NULL; - } - - if(!p7_message) - return NULL; - - if(p7_socket->serialization == WI_P7_BINARY && p7_message->binary_size == 0) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_INVALIDMESSAGE, - WI_STR("Invalid data from remote host (%u doesn't look like a header)"), - p7_socket->message_binary_size); - - return NULL; - } - - wi_p7_message_deserialize(p7_message, p7_socket->serialization); - - if(wi_p7_socket_debug) { - wi_log_debug(WI_STR("Received %@"), p7_message); - - wi_log_debug(WI_STR("Received %llu raw bytes, %llu processed bytes, compressed to %.2f%%"), - p7_socket->read_raw_bytes, - p7_socket->read_processed_bytes, - ((double) p7_socket->read_raw_bytes / (double) p7_socket->read_processed_bytes) * 100.0); - } - - if(p7_socket->read_message_callback) - (*p7_socket->read_message_callback)(p7_socket, p7_message, p7_socket->read_message_context); - - return p7_message; + wi_p7_message_t *p7_message; + wi_string_t *prefix = NULL; + char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; + + if(p7_socket->serialization == WI_P7_UNKNOWN || p7_socket->serialization == WI_P7_BINARY) { + if(p7_socket->message_binary_size == 0) { + if(wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) <= 0) + return NULL; + + p7_socket->message_binary_size = wi_read_swap_big_to_host_int32(length_buffer, 0); + } + + if(p7_socket->serialization == WI_P7_UNKNOWN) { + if(p7_socket->message_binary_size == _WI_P7_SOCKET_XML_MAGIC) { + p7_socket->serialization = WI_P7_XML; + prefix = WI_STR("message_binary_size < _WI_P7_SOCKET_MAX_BINARY_SIZE) { + p7_socket->serialization = WI_P7_BINARY; + } + } + } + + if(p7_socket->serialization == WI_P7_BINARY) + p7_message = _wi_p7_socket_read_binary_message(p7_socket, timeout, p7_socket->message_binary_size); + else if(p7_socket->serialization == WI_P7_XML) + p7_message = _wi_p7_socket_read_xml_message(p7_socket, timeout, prefix); + else { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_HANDSHAKEFAILED, + WI_STR("Invalid data from remote host (%u doesn't look like a header)"), + p7_socket->message_binary_size); + + return NULL; + } + + if(!p7_message) + return NULL; + + if(p7_socket->serialization == WI_P7_BINARY && p7_message->binary_size == 0) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_INVALIDMESSAGE, + WI_STR("Invalid data from remote host (%u doesn't look like a header)"), + p7_socket->message_binary_size); + + return NULL; + } + + wi_p7_message_deserialize(p7_message, p7_socket->serialization); + + if(wi_p7_socket_debug) { + wi_log_debug(WI_STR("Received %@"), p7_message); + + wi_log_debug(WI_STR("Received %llu raw bytes, %llu processed bytes, compressed to %.2f%%"), + p7_socket->read_raw_bytes, + p7_socket->read_processed_bytes, + ((double) p7_socket->read_raw_bytes / (double) p7_socket->read_processed_bytes) * 100.0); + } + + if(p7_socket->read_message_callback) + (*p7_socket->read_message_callback)(p7_socket, p7_message, p7_socket->read_message_context); + + return p7_message; } wi_boolean_t wi_p7_socket_write_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, const void *buffer, uint32_t size) { - const void *send_buffer; - char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; - unsigned char checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - wi_integer_t compressed_size; + const void *send_buffer; + char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; + unsigned char checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + wi_integer_t compressed_size; #ifdef WI_RSA - wi_integer_t encrypted_size; + wi_integer_t encrypted_size; #endif - uint32_t send_size; - - send_size = size; - send_buffer = buffer; - - if(p7_socket->checksum_enabled) - _wi_p7_socket_checksum_buffer(p7_socket, send_buffer, send_size, checksum_buffer); - - if(p7_socket->compression_enabled) { - compressed_size = _wi_p7_socket_deflate(p7_socket, send_buffer, send_size); - - if(compressed_size < 0) - return false; - - send_size = compressed_size; - send_buffer = p7_socket->compression_buffer; - } - + uint32_t send_size; + + send_size = size; + send_buffer = buffer; + + if(p7_socket->checksum_enabled) + _wi_p7_socket_checksum_buffer(p7_socket, send_buffer, send_size, checksum_buffer); + + if(p7_socket->compression_enabled) { + compressed_size = _wi_p7_socket_deflate(p7_socket, send_buffer, send_size); + + if(compressed_size < 0) + return false; + + send_size = compressed_size; + send_buffer = p7_socket->compression_buffer; + } + #ifdef WI_RSA - if(p7_socket->encryption_enabled) { - encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); - - if(!p7_socket->encryption_buffer) { - p7_socket->encryption_buffer_length = encrypted_size; - p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); - } - else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { - p7_socket->encryption_buffer_length = encrypted_size * 2; - p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); - } - - encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, - send_buffer, - send_size, - p7_socket->encryption_buffer); - - if(encrypted_size < 0) - return false; - - send_size = encrypted_size; - send_buffer = p7_socket->encryption_buffer; - } + if(p7_socket->encryption_enabled) { + encrypted_size = send_size + wi_cipher_block_size(p7_socket->cipher); + + if(!p7_socket->encryption_buffer) { + p7_socket->encryption_buffer_length = encrypted_size; + p7_socket->encryption_buffer = wi_malloc(p7_socket->encryption_buffer_length); + } + else if((wi_uinteger_t) encrypted_size > p7_socket->encryption_buffer_length) { + p7_socket->encryption_buffer_length = encrypted_size * 2; + p7_socket->encryption_buffer = wi_realloc(p7_socket->encryption_buffer, p7_socket->encryption_buffer_length); + } + + encrypted_size = wi_cipher_encrypt_bytes(p7_socket->cipher, + send_buffer, + send_size, + p7_socket->encryption_buffer); + + if(encrypted_size < 0) + return false; + + send_size = encrypted_size; + send_buffer = p7_socket->encryption_buffer; + } #endif - wi_write_swap_host_to_big_int32(length_buffer, 0, send_size); + wi_write_swap_host_to_big_int32(length_buffer, 0, send_size); - if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) - return false; + if(wi_socket_write_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)) < 0) + return false; - if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) - return false; + if(wi_socket_write_buffer(p7_socket->socket, timeout, send_buffer, send_size) < 0) + return false; - if(p7_socket->checksum_enabled) { - if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) - return false; - } - - return true; + if(p7_socket->checksum_enabled) { + if(wi_socket_write_buffer(p7_socket->socket, timeout, checksum_buffer, p7_socket->checksum_length) < 0) + return false; + } + + return true; } wi_integer_t wi_p7_socket_read_oobdata(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, void **out_buffer) { - void *receive_buffer; - char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; - unsigned char local_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - unsigned char remote_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; - wi_integer_t result, decompressed_size; + void *receive_buffer; + char length_buffer[_WI_P7_SOCKET_LENGTH_SIZE]; + unsigned char local_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + unsigned char remote_checksum_buffer[_WI_P7_CHECKSUM_OPTIONS_TO_LENGTH(p7_socket->options)]; + wi_integer_t result, decompressed_size; #ifdef WI_RSA - wi_integer_t decrypted_size; + wi_integer_t decrypted_size; #endif - uint32_t receive_size; - - result = wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)); - - if(result <= 0) - return result; - - receive_size = wi_read_swap_big_to_host_int32(length_buffer, 0); - - if(receive_size > _WI_P7_SOCKET_MAX_BINARY_SIZE) { - wi_error_set_libwired_error_with_format(WI_ERROR_P7_MESSAGETOOLARGE, - WI_STR("%u bytes"), receive_size); - - return -1; - } - - if(!p7_socket->oobdata_read_buffer) { - p7_socket->oobdata_read_buffer_length = receive_size * 2; - p7_socket->oobdata_read_buffer = wi_malloc(p7_socket->oobdata_read_buffer_length); - } - else if(receive_size > p7_socket->oobdata_read_buffer_length) { - p7_socket->oobdata_read_buffer_length = receive_size * 2; - p7_socket->oobdata_read_buffer = wi_realloc(p7_socket->oobdata_read_buffer, p7_socket->oobdata_read_buffer_length); - } - - receive_buffer = p7_socket->oobdata_read_buffer; - - result = wi_socket_read_buffer(p7_socket->socket, timeout, receive_buffer, receive_size); - - if(result <= 0) - return false; - + uint32_t receive_size; + + result = wi_socket_read_buffer(p7_socket->socket, timeout, length_buffer, sizeof(length_buffer)); + + if(result <= 0) + return result; + + receive_size = wi_read_swap_big_to_host_int32(length_buffer, 0); + + if(receive_size > _WI_P7_SOCKET_MAX_BINARY_SIZE) { + wi_error_set_libwired_error_with_format(WI_ERROR_P7_MESSAGETOOLARGE, + WI_STR("%u bytes"), receive_size); + + return -1; + } + + if(!p7_socket->oobdata_read_buffer) { + p7_socket->oobdata_read_buffer_length = receive_size * 2; + p7_socket->oobdata_read_buffer = wi_malloc(p7_socket->oobdata_read_buffer_length); + } + else if(receive_size > p7_socket->oobdata_read_buffer_length) { + p7_socket->oobdata_read_buffer_length = receive_size * 2; + p7_socket->oobdata_read_buffer = wi_realloc(p7_socket->oobdata_read_buffer, p7_socket->oobdata_read_buffer_length); + } + + receive_buffer = p7_socket->oobdata_read_buffer; + + result = wi_socket_read_buffer(p7_socket->socket, timeout, receive_buffer, receive_size); + + if(result <= 0) + return false; + #ifdef WI_RSA - if(p7_socket->encryption_enabled) { - decrypted_size = receive_size + wi_cipher_block_size(p7_socket->cipher); - - if(!p7_socket->decryption_buffer) { - p7_socket->decryption_buffer_length = decrypted_size; - p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); - } - else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { - p7_socket->decryption_buffer_length = decrypted_size * 2; - p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); - } - - decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, - receive_buffer, - receive_size, - p7_socket->decryption_buffer); - - if(decrypted_size < 0) - return -1; - - receive_size = decrypted_size; - receive_buffer = p7_socket->decryption_buffer; - } + if(p7_socket->encryption_enabled) { + decrypted_size = receive_size + wi_cipher_block_size(p7_socket->cipher); + + if(!p7_socket->decryption_buffer) { + p7_socket->decryption_buffer_length = decrypted_size; + p7_socket->decryption_buffer = wi_malloc(p7_socket->decryption_buffer_length); + } + else if((wi_uinteger_t) decrypted_size > p7_socket->decryption_buffer_length) { + p7_socket->decryption_buffer_length = decrypted_size * 2; + p7_socket->decryption_buffer = wi_realloc(p7_socket->decryption_buffer, p7_socket->decryption_buffer_length); + } + + decrypted_size = wi_cipher_decrypt_bytes(p7_socket->cipher, + receive_buffer, + receive_size, + p7_socket->decryption_buffer); + + if(decrypted_size < 0) + return -1; + + receive_size = decrypted_size; + receive_buffer = p7_socket->decryption_buffer; + } #endif - - if(p7_socket->compression_enabled) { - decompressed_size = _wi_p7_socket_inflate(p7_socket, receive_buffer, receive_size); - - if(decompressed_size < 0) - return -1; - - receive_size = decompressed_size; - receive_buffer = p7_socket->compression_buffer; - } - - if(p7_socket->checksum_enabled) { - result = wi_socket_read_buffer(p7_socket->socket, timeout, remote_checksum_buffer, p7_socket->checksum_length); - - if(result <= 0) - return result; - - _wi_p7_socket_checksum_buffer(p7_socket, receive_buffer, receive_size, local_checksum_buffer); - - if(memcmp(remote_checksum_buffer, local_checksum_buffer, p7_socket->checksum_length) != 0) { - wi_error_set_libwired_error(WI_ERROR_P7_CHECKSUMMISMATCH); - - return -1; - } - } - - *out_buffer = receive_buffer; - - return receive_size; + + if(p7_socket->compression_enabled) { + decompressed_size = _wi_p7_socket_inflate(p7_socket, receive_buffer, receive_size); + + if(decompressed_size < 0) + return -1; + + receive_size = decompressed_size; + receive_buffer = p7_socket->compression_buffer; + } + + if(p7_socket->checksum_enabled) { + result = wi_socket_read_buffer(p7_socket->socket, timeout, remote_checksum_buffer, p7_socket->checksum_length); + + if(result <= 0) + return result; + + _wi_p7_socket_checksum_buffer(p7_socket, receive_buffer, receive_size, local_checksum_buffer); + + if(memcmp(remote_checksum_buffer, local_checksum_buffer, p7_socket->checksum_length) != 0) { + wi_error_set_libwired_error(WI_ERROR_P7_CHECKSUMMISMATCH); + + return -1; + } + } + + *out_buffer = receive_buffer; + + return receive_size; } #endif diff --git a/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.h b/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.h index 12a184b..608f9df 100644 --- a/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.h +++ b/WiredFrameworks/libwired/libwired/p7/wi-p7-socket.h @@ -60,6 +60,38 @@ ((options) & WI_P7_CHECKSUM_SHA256) || \ ((options) & WI_P7_CHECKSUM_SHA512)) +#define WI_P7_DEPRECATED_ENCRYPTION_CIPHER(options) \ + ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA512 ? \ + true : true) + enum _wi_p7_options { WI_P7_COMPRESSION_DEFLATE = (1 << 0), diff --git a/WiredServer.xcodeproj/project.pbxproj b/WiredServer.xcodeproj/project.pbxproj index abd4abf..3dbfd50 100644 --- a/WiredServer.xcodeproj/project.pbxproj +++ b/WiredServer.xcodeproj/project.pbxproj @@ -103,7 +103,7 @@ remoteGlobalIDString = 4C233C881521022800245506; remoteInfo = "Wired Server Helper"; }; - 4C52C886245A214300BFB5AE /* PBXContainerItemProxy */ = { + 4C61710A246AB43400D24A9D /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 4C8D334B151E2D8C001A6BF7 /* Project object */; proxyType = 1; @@ -985,7 +985,7 @@ buildRules = ( ); dependencies = ( - 4C52C887245A214300BFB5AE /* PBXTargetDependency */, + 4C61710B246AB43400D24A9D /* PBXTargetDependency */, 4C8865D821A1BE56002A7F8A /* PBXTargetDependency */, 4C789002170B35D6004CE617 /* PBXTargetDependency */, 4C789004170B35D6004CE617 /* PBXTargetDependency */, @@ -1249,10 +1249,10 @@ target = 4C233C881521022800245506 /* Wired Server Helper */; targetProxy = 4C233CBA15211F9C00245506 /* PBXContainerItemProxy */; }; - 4C52C887245A214300BFB5AE /* PBXTargetDependency */ = { + 4C61710B246AB43400D24A9D /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 4C8A853E21B05BB2001154BC /* wired */; - targetProxy = 4C52C886245A214300BFB5AE /* PBXContainerItemProxy */; + targetProxy = 4C61710A246AB43400D24A9D /* PBXContainerItemProxy */; }; 4C789002170B35D6004CE617 /* PBXTargetDependency */ = { isa = PBXTargetDependency; @@ -1371,7 +1371,7 @@ CODE_SIGN_ENTITLEMENTS = "Wired Server Helper/Wired Server Helper.entitlements"; CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 17; + CURRENT_PROJECT_VERSION = 19; DEVELOPMENT_TEAM = 7KTXA5Y36K; ENABLE_HARDENED_RUNTIME = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; @@ -1396,7 +1396,7 @@ CODE_SIGN_ENTITLEMENTS = "Wired Server Helper/Wired Server Helper.entitlements"; CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 17; + CURRENT_PROJECT_VERSION = 19; DEVELOPMENT_TEAM = 7KTXA5Y36K; ENABLE_HARDENED_RUNTIME = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; @@ -1574,7 +1574,7 @@ CODE_SIGN_ENTITLEMENTS = "Wired Server/Wired Server.entitlements"; CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 18; + CURRENT_PROJECT_VERSION = 19; DEVELOPMENT_TEAM = 7KTXA5Y36K; ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( @@ -1616,7 +1616,7 @@ CODE_SIGN_ENTITLEMENTS = "Wired Server/Wired Server.entitlements"; CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 18; + CURRENT_PROJECT_VERSION = 19; DEVELOPMENT_TEAM = 7KTXA5Y36K; ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( diff --git a/WiredServer.xcworkspace/xcuserdata/nark.xcuserdatad/UserInterfaceState.xcuserstate b/WiredServer.xcworkspace/xcuserdata/nark.xcuserdatad/UserInterfaceState.xcuserstate index a4d23d0..ac4539a 100644 Binary files a/WiredServer.xcworkspace/xcuserdata/nark.xcuserdatad/UserInterfaceState.xcuserstate and b/WiredServer.xcworkspace/xcuserdata/nark.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/wired/Dockerfile b/wired/Dockerfile index 6fc1823..8cf3d59 100644 --- a/wired/Dockerfile +++ b/wired/Dockerfile @@ -3,8 +3,8 @@ FROM ubuntu:18.04 RUN apt-get update RUN apt-get install -y build-essential git libsqlite3-dev libxml2-dev libssl-dev zlib1g-dev autoconf -RUN mkdir /files RUN mkdir /wired + ADD . /wired/ WORKDIR /wired @@ -12,7 +12,6 @@ RUN git submodule update --init --remote RUN bash /wired/libwired/bootstrap RUN ./configure - RUN make RUN make install diff --git a/wired/README.md b/wired/README.md index 5378993..c2e8aeb 100755 --- a/wired/README.md +++ b/wired/README.md @@ -17,32 +17,6 @@ These are usually distributed with operating systems. #### Howto install on: -**Docker** - -Build the image: - - cd wired/ - docker build --tag wired:2.5 . - -Run the container (first time): - - docker run -p 4871:4871 \ - -v /path/to/your/files:/files \ - -v /path/to/your/database.sqlite3:/usr/local/wired/database.sqlite3 \ - --name wired wired:2.5 - -* the `-p` option maps the container port (`4871`) to whatever the port you want to use on your host machine. If you want `wired` to be available on another you can use: `10000:4871` and Docker will translate it. -* the `-v` option maps items of the container file system with your local file system. You can change the first value of the pair to adjust to your server files configuration. - -Start/stop the container: - - docker start wired - docker stop wired - -Remove the container: - - docker rm wired - **Debian/Ubuntu** sudo apt-get install -y build-essential autoconf git libsqlite3-dev libxml2-dev libssl-dev zlib1g-dev @@ -130,6 +104,51 @@ To start an installed Wired server, run: By default a user with the login "admin" and no password is created. Use Wired Client or Wire to connect to your newly installed Wired Server. +### Running on Docker + +Pull and run the container: + + docker run --name wired -d -p 4871:4871 -v /path/to/yours/files:/files wired2/wired:2.5 + +* the `-p` option maps the container port (`4871`) to whatever the port you want to use on your host machine. If you want `wired` to be available on another you can use: `10000:4871` and Docker will translate it. +* the `-v` option maps items of the container file system with your local file system. You can change the first value of the pair to adjust to your server files configuration. Wired Server will look into the `/files` path of the container to index and server your files. + +If you want Docker to start the container automatically for you, add the `--restart always` argument as follow: + + docker run --name wired --restart always -d -p 4871:4871 -v /path/to/yours/files:/files wired2/wired:2.5 + +Start/stop the container: + + docker stop wired + docker start wired + +Remove the container: + + docker rm wired + +It could be useful to backup and restore container files such as `database.sqlite3` or `wired.conf`. You can do that do using the `docker cp` command. + +From container to host: + + docker cp wired:/user/local/wired/database.sqlite3 /path/to/your/database.sqlite3 + +From host to container: + + docker cp /path/to/your/database.sqlite3 wired:/user/local/wired/database.sqlite3 + +Build the image locally: + + cd wired/ + docker build --tag wired:2.5 . + +Tag the image: + + docker tag wired2/wired:2.5 + +Push to Docker Hub: + + docker push wired2/wired:2.5 + ### Get More If you are interested in the Wired project, check the Website at [http://wired.read-write.fr/](http://wired.read-write.fr) diff --git a/wired/config.h b/wired/config.h index 92f3663..d1c0c16 100644 --- a/wired/config.h +++ b/wired/config.h @@ -95,7 +95,7 @@ #define WD_VERSION "2.5" /* Subversion revision */ -#define WI_REVISION "2.5.1" +#define WI_REVISION "2.5.2" /* SQLite does not support backup */ #define WI_SQLITE_SUPPORTS_BACKUP 1 diff --git a/wired/libwired/libwired/p7/wi-p7-socket.c b/wired/libwired/libwired/p7/wi-p7-socket.c index 9e6d7ad..b8d1945 100644 --- a/wired/libwired/libwired/p7/wi-p7-socket.c +++ b/wired/libwired/libwired/p7/wi-p7-socket.c @@ -661,6 +661,10 @@ static wi_boolean_t _wi_p7_socket_connect_handshake(wi_p7_socket_t *p7_socket, w if(wi_p7_message_get_enum_for_name(p7_message, &flag, WI_STR("p7.handshake.checksum"))) p7_socket->options |= _WI_P7_CHECKSUM_ENUM_TO_OPTIONS(flag); } + + if(WI_P7_DEPRECATED_ENCRYPTION_CIPHER(p7_socket->options)) { + wi_log_warn(WI_STR("Connected using deprecated cipher: %d"), _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(p7_socket->options)); + } if(!wi_p7_message_get_bool_for_name(p7_message, &p7_socket->remote_compatibility_check, WI_STR("p7.handshake.compatibility_check"))) p7_socket->remote_compatibility_check = false; @@ -778,6 +782,10 @@ static wi_boolean_t _wi_p7_socket_accept_handshake(wi_p7_socket_t *p7_socket, wi if(!wi_p7_message_set_string_for_name(p7_message, wi_p7_spec_version(p7_socket->spec), WI_STR("p7.handshake.protocol.version"))) return false; + if(WI_P7_DEPRECATED_ENCRYPTION_CIPHER(p7_socket->options)) { + wi_log_warn(WI_STR("Connected using deprecated encryption cipher: %d"), _WI_P7_ENCRYPTION_OPTIONS_TO_ENUM(p7_socket->options)); + } + if(p7_socket->serialization == WI_P7_BINARY) { if(WI_P7_COMPRESSION_ENABLED(p7_socket->options)) { if(!wi_p7_message_set_enum_for_name(p7_message, diff --git a/wired/libwired/libwired/p7/wi-p7-socket.h b/wired/libwired/libwired/p7/wi-p7-socket.h index 12a184b..608f9df 100644 --- a/wired/libwired/libwired/p7/wi-p7-socket.h +++ b/wired/libwired/libwired/p7/wi-p7-socket.h @@ -60,6 +60,38 @@ ((options) & WI_P7_CHECKSUM_SHA256) || \ ((options) & WI_P7_CHECKSUM_SHA512)) +#define WI_P7_DEPRECATED_ENCRYPTION_CIPHER(options) \ + ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA512 ? \ + true : true) + enum _wi_p7_options { WI_P7_COMPRESSION_DEFLATE = (1 << 0), diff --git a/wired/libwired/version b/wired/libwired/version new file mode 100644 index 0000000..21b159d --- /dev/null +++ b/wired/libwired/version @@ -0,0 +1 @@ +2.5.2 \ No newline at end of file diff --git a/wired/run/etc/wired.conf b/wired/run/etc/wired.conf deleted file mode 100644 index 4bc40d7..0000000 --- a/wired/run/etc/wired.conf +++ /dev/null @@ -1,133 +0,0 @@ -# wired.conf - -### SERVER ############################################################ - -# Name of the server. -# (default "Wired Server") -name = Wired Server - -# A short description of the server. -# (default "Wired Server") -description = Wired Server - -# A specific address to bind to, instead of binding to all available -# addresses. Can be specified more than once, for each address the -# server should listen on. -# (no default) -#address = localhost - -# Port number to listen on. -# (default 4871) -port = 4871 - -# Automatically map port using NAT-PMP or UPnP if available. -# (default "no") -map port = no - -# Name or id of the user that the server should operate as. -# (default "wired") -user = nark - -# Name or id of the group that the server should operate as. -# (default "daemon") -group = daemon - -# Ask the server to accept or not incoming connections -# that do not use an SSL encryption cipher. -# (default yes) -force encryption = yes - - -### DATABASE ############################################################# - -# If set, snapshots database every 'snapshot time'. -# (default yes) -snapshots = yes - -# If set, snapshots database after this many seconds. Without it, snapshots -# will use the default value. -# (default 86400) -snapshot time = 86400 - - -### EVENTS ############################################################# - -# Older events will be automatically deleted after 'events time'. -# Accepted values for 'events time' are: none, daily, weekly, monthly, -# yearly. -# (default "none") -events time = none - - -### FILES ############################################################# - -# Path to the files directory. -# (default "files") -files = files - -# If set, indexes files after this many seconds. Without it, no -# automatic indexing takes place. -# (default 14400) -# index time = 14400 - - -### TRANSFERS ######################################################### - -# Maximum number of downloads across all clients. -# (default 10) -total downloads = 10 - -# Maximum number of uploads across all clients. -# (default 10) -total uploads = 10 - -# Maximum total speed of downloads in bytes/sec. -# (no default) -#total download speed = 100000 - -# Maximum total speed of uploads in bytes/sec. -# (no default) -#total upload speed = 50000 - - -### TRACKERS ########################################################## - -# If set, registers with all configured trackers. -# (default "no") -register = no - -# A URL of a tracker to register with. The tracker is connected to at -# startup, and then periodically informed with status updates. The -# path of the URL is the category to register as. Can be specified more -# than once, for each tracker that the server should register with. -# (default "wired://wired.read-write.fr/") -tracker = wired://wired.read-write.fr/ - -# IP address to send to the tracker. If this is not set, the tracker -# will automatically register the server as the originating IP address. -# Set this to your external address if you're on the same subnet as the -# tracker, and you're using private addresses behind a firewall. -# (no default) -# ip = 127.0.0.1 - -# If set, enables the tracker. -# (default "no") -enable tracker = no - -# Categories that servers can register with on this tracker. Can be -# specified more than once, for each category. Use "/" to separate -# subcategories. -# (no default) -# category = Category -# category = Category/Subcategory - - -### PATHS ############################################################# - -# Paths that do not begin with a / are taken to be relative the root -# path (-d flag). - -# Path to an image file that will be sent to clients as the banner. -# PNG file format is recommended. Maximum image size is 200x32 pixels. -# (no default) -banner = banner.png diff --git a/wired/run/libwired/include/wired/wi-p7-socket.h b/wired/run/libwired/include/wired/wi-p7-socket.h index 12a184b..608f9df 100644 --- a/wired/run/libwired/include/wired/wi-p7-socket.h +++ b/wired/run/libwired/include/wired/wi-p7-socket.h @@ -60,6 +60,38 @@ ((options) & WI_P7_CHECKSUM_SHA256) || \ ((options) & WI_P7_CHECKSUM_SHA512)) +#define WI_P7_DEPRECATED_ENCRYPTION_CIPHER(options) \ + ((options) & WI_P7_ENCRYPTION_RSA_AES128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA1 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA256 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA256 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_AES192_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_AES256_SHA512 ? \ + false : \ + (options) & WI_P7_ENCRYPTION_RSA_BF128_SHA512 ? \ + true : \ + (options) & WI_P7_ENCRYPTION_RSA_3DES192_SHA512 ? \ + true : true) + enum _wi_p7_options { WI_P7_COMPRESSION_DEFLATE = (1 << 0), diff --git a/wired/run/libwired/lib/libwired.a b/wired/run/libwired/lib/libwired.a deleted file mode 100644 index c21b51d..0000000 Binary files a/wired/run/libwired/lib/libwired.a and /dev/null differ diff --git a/wired/run/wired b/wired/run/wired deleted file mode 100755 index 8a0a2b7..0000000 Binary files a/wired/run/wired and /dev/null differ diff --git a/wired/run/wiredctl b/wired/run/wiredctl deleted file mode 100755 index f9e0b30..0000000 --- a/wired/run/wiredctl +++ /dev/null @@ -1,247 +0,0 @@ -#!/bin/sh - -# The path to your config file -CONFIGFILE="/Library/Wired/etc/wired.conf" - -# The path to your pid file -PIDFILE="/Library/Wired/wired.pid" - -# The path to your status file -STATUSFILE="/Library/Wired/wired.status" - -# The path to your wired binary -WIRED="/Library/Wired/wired" - -# Flags to pass to wired -WIREDFLAGS="-d /Library/Wired" - -# Begin script -PROG=$(basename $0) -CMD=$1 - -checkpid() { - RUNNING=0 - - if [ -f $PIDFILE ]; then - PID=`cat $PIDFILE` - - if [ "x$PID" != "x" ]; then - if kill -0 $PID 2>/dev/null ; then - RUNNING=1 - fi - fi - fi -} - -checkrunning() { - checkpid - - if [ $RUNNING -eq 0 ]; then - echo "$PROG: $CMD: wired is not running" - exit 1 - fi -} - -case $CMD in - start) - checkpid - - if [ $RUNNING -eq 1 ]; then - echo "$PROG: $CMD: wired (pid $PID) already running" - exit 1 - fi - - if $WIRED $WIREDFLAGS; then - echo "$PROG: $CMD: wired started" - else - echo "$PROG: $CMD: wired could not be started" - fi - ;; - - stop) - checkrunning - - if kill $PID; then - echo "$PROG: $CMD: wired stopped" - else - echo "$PROG: $CMD: wired could not be stopped" - exit 1 - fi - ;; - - restart) - checkpid - - if [ $RUNNING -eq 1 ]; then - if kill $PID; then - echo "$PROG: $CMD: wired stopped" - else - echo "$PROG: $CMD: wired could not be stopped" - exit 1 - fi - fi - - if $WIRED $WIREDFLAGS; then - echo "$PROG: $CMD: wired started" - else - echo "$PROG: $CMD: wired could not be started" - fi - ;; - - reload) - checkrunning - - if kill -HUP $PID; then - echo "$PROG: $CMD: wired reloaded" - else - echo "$PROG: $CMD: wired could not be reloaded" - fi - ;; - - register) - checkrunning - - if kill -USR1 $PID; then - echo "$PROG: $CMD: wired registering with trackers" - else - echo "$PROG: $CMD: wired could not register with trackers" - fi - ;; - - index) - checkrunning - - if kill -USR2 $PID; then - echo "$PROG: $CMD: wired indexing" - else - echo "$PROG: $CMD: wired could not be indexed" - fi - ;; - - clean) - checkrunning - - if kill -PROF $PID; then - echo "$PROG: $CMD: wired cleaning events" - else - echo "$PROG: $CMD: wired could not clean events" - fi - ;; - - config) - grep -v "^#" $CONFIGFILE | grep -v "^$" | sort - ;; - - configtest) - $WIRED -t - ;; - - debug) - echo "run -D $WIREDFLAGS" | gdb $WIRED - ;; - - status) - if [ -f $STATUSFILE ]; then - $WIRED -v - awk ' - function pad(number) { - if(number < 10) - return "0" number - else - return number - } - - function ftime(time) { - days = int(time / 86400) - time = time % 86400 - - hours = int(time / 3600) - time = time % 3600 - - minutes = int(time / 60) - time = time % 60 - - seconds = time - - if(days > 0) - return pad(days) ":" pad(hours) ":" \ - pad(minutes) ":" pad(seconds) \ - " days" - else if(hours > 0) - return pad(hours) ":" pad(minutes) ":" \ - pad(seconds) " hours" - else if(minutes > 0) - return pad(minutes) ":" pad(seconds) \ - " minutes" - else - return seconds " seconds" - } - - function fbytes(bytes) { - power = 0 - - while(bytes > (1024 ^ ++power)) - ; - - value = sprintf("%.2f", bytes / (1024 ^ --power)) - - if(power == 4) - return value " TB" - else if(power == 3) - return value " GB" - else if(power == 2) - return value " MB" - else if(power == 1) - return value " KB" - else if(power == 0) - if(bytes == 1) - return bytes " byte" - else - return bytes " bytes" - } - - { - "date +%s" | getline now - print "Up " ftime(now - $1) - print "" - print "Current users: " $2 - print "Total users: " $3 - print "Current downloads: " $4 - print "Total downloads: " $5 - print "Current uploads: " $6 - print "Total uploads: " $7 - print "Downloads traffic: " fbytes($8) - print "Uploads traffic: " fbytes($9) - print "Total traffic: " fbytes($8 + $9) - print "Current tracker servers: " $10 - print "Current tracker users: " $11 - print "Current tracker files: " $12 - print "Current tracker size: " fbytes($13) - } - ' $STATUSFILE - else - echo "$PROG: $CMD: $STATUSFILE could not be found" - fi - ;; - - *) - cat < -EOF - ;; -esac diff --git a/wired/version b/wired/version index 4fd0fe3..21b159d 100644 --- a/wired/version +++ b/wired/version @@ -1 +1 @@ -2.5.1 \ No newline at end of file +2.5.2 \ No newline at end of file diff --git a/wired/wired/server.c b/wired/wired/server.c index 96b79ce..e659910 100644 --- a/wired/wired/server.c +++ b/wired/wired/server.c @@ -638,7 +638,9 @@ static void wd_server_accept_thread(wi_runtime_instance_t *argument) { wi_socket_t *socket = argument; wi_string_t *ip; wd_user_t *user; - + wi_integer_t cipher; + wi_p7_options_t options; + pool = wi_pool_init(wi_pool_alloc()); ip = wi_address_string(wi_socket_address(socket)); @@ -651,9 +653,16 @@ static void wd_server_accept_thread(wi_runtime_instance_t *argument) { p7_socket = wi_autorelease(wi_p7_socket_init_with_socket(wi_p7_socket_alloc(), socket, wd_p7_spec)); wi_p7_socket_set_private_key(p7_socket, wd_rsa); + options = WI_P7_ALL; user = wd_user_with_p7_socket(p7_socket); - - if(wi_p7_socket_accept(p7_socket, 30.0, WI_P7_ALL)) { + cipher = wi_config_integer_for_name(wd_config, WI_STR("preferred cipher")); + + if(cipher != -1) { + //wi_log_debug(WI_STR("Enforce preferred encryption cipher: %ld"), cipher); + options = (WI_P7_COMPRESSION_DEFLATE | (1 << (cipher + 1)) | WI_P7_CHECKSUM_SHA1 | WI_P7_CHECKSUM_SHA256 | WI_P7_CHECKSUM_SHA512); + } + + if(wi_p7_socket_accept(p7_socket, 30.0, options)) { if(wi_config_bool_for_name(wd_config, WI_STR("force encryption"))) { #ifdef WI_RSA if(!WI_P7_ENCRYPTION_ENABLED(wi_p7_socket_options(p7_socket))) { diff --git a/wired/wired/settings.c b/wired/wired/settings.c index 515164d..3e3994f 100644 --- a/wired/wired/settings.c +++ b/wired/wired/settings.c @@ -52,6 +52,7 @@ void wd_settings_initialize(void) { WI_INT32(WI_CONFIG_BOOL), WI_STR("enable tracker"), WI_INT32(WI_CONFIG_PATH), WI_STR("files"), WI_INT32(WI_CONFIG_BOOL), WI_STR("force encryption"), + WI_INT32(WI_CONFIG_INTEGER), WI_STR("preferred cipher"), WI_INT32(WI_CONFIG_GROUP), WI_STR("group"), WI_INT32(WI_CONFIG_BOOL), WI_STR("snapshots"), WI_INT32(WI_CONFIG_TIME_INTERVAL), WI_STR("snapshot time"), @@ -78,6 +79,7 @@ void wd_settings_initialize(void) { wi_number_with_bool(false), WI_STR("enable tracker"), WI_STR("files"), WI_STR("files"), wi_number_with_bool(true), WI_STR("force encryption"), + WI_INT32(-1), WI_STR("preferred cipher"), WI_STR("daemon"), WI_STR("group"), wi_number_with_bool(true), WI_STR("snapshots"), WI_INT32(86400), WI_STR("snapshot time"), diff --git a/wired/wired/wired.conf.in b/wired/wired/wired.conf.in index 4bc40d7..8223920 100644 --- a/wired/wired/wired.conf.in +++ b/wired/wired/wired.conf.in @@ -37,6 +37,31 @@ group = daemon # (default yes) force encryption = yes +# Ask the server to only accept connections from +# the preferred cipher. If -1 is selected, all encryption +# ciphers are accepted. +# +# Supported ciphers: +# +# ALL = -1 +# RSA-AES128-SHA1 = 0 (deprecated) +# RSA-AES192-SHA1 = 1 (deprecated) +# RSA-AES256-SHA1 = 2 (legacy) +# RSA-BF128-SHA1 = 3 (deprecated) +# RSA-3DES192-SHA1 = 4 (deprecated) +# RSA-AES128-SHA256 = 5 (deprecated) +# RSA-AES192-SHA256 = 6 (recommended) +# RSA-AES256-SHA256 = 7 (recommended) +# RSA-BF128-SHA256 = 8 (deprecated) +# RSA-3DES192-SHA256 = 9 (deprecated) +# RSA-AES128-SHA512 = 10 (deprecated) +# RSA-AES192-SHA512 = 11 (recommended) +# RSA-AES256-SHA512 = 12 (recommended) +# RSA-BF128-SHA512 = 13 (deprecated) +# RSA-3DES192-SHA512 = 14 (deprecated) +# +# (default -1) +preferred cipher = -1 ### DATABASE #############################################################