|
Wired Networking
2.0
Objective-C implementation of the Wired 2.0 protocol
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2006-2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredNetworking/NSString-WINetworking.h> 00030 #import <WiredNetworking/WIAddress.h> 00031 #import <WiredNetworking/WIError.h> 00032 00033 @interface WIAddress(Private) 00034 00035 - (id)_initWithAddress:(wi_address_t *)address; 00036 00037 @end 00038 00039 00040 @implementation WIAddress(Private) 00041 00042 - (id)_initWithAddress:(wi_address_t *)address { 00043 self = [super init]; 00044 00045 _address = wi_retain(address); 00046 _string = [[NSString alloc] initWithWiredString:wi_address_string(_address)]; 00047 00048 return self; 00049 } 00050 00051 @end 00052 00053 00054 00055 @implementation WIAddress 00056 00057 + (WIAddress *)addressWildcardForFamily:(WIAddressFamily)family { 00058 return [[[self alloc] initWildcardForFamily:family] autorelease]; 00059 } 00060 00061 00062 00063 + (WIAddress *)addressWithString:(NSString *)address error:(WIError **)error { 00064 return [[[self alloc] initWithString:address error:error] autorelease]; 00065 } 00066 00067 00068 00069 + (WIAddress *)addressWithNetService:(NSNetService *)netService error:(WIError **)error { 00070 return [[[self alloc] initWithNetService:netService error:error] autorelease]; 00071 } 00072 00073 00074 00075 - (id)initWildcardForFamily:(WIAddressFamily)family { 00076 wi_pool_t *pool; 00077 wi_address_t *address; 00078 00079 pool = wi_pool_init(wi_pool_alloc()); 00080 address = wi_address_wildcard_for_family(family); 00081 00082 self = [self _initWithAddress:address]; 00083 00084 wi_retain(pool); 00085 00086 return self; 00087 } 00088 00089 00090 00091 - (id)initWithString:(NSString *)string error:(WIError **)error { 00092 wi_pool_t *pool; 00093 wi_address_t *address; 00094 00095 pool = wi_pool_init(wi_pool_alloc()); 00096 address = wi_host_address(wi_host_with_string([string wiredString])); 00097 00098 if(!address) { 00099 if(error) { 00100 *error = [WIError errorWithDomain:WIWiredNetworkingErrorDomain 00101 code:WIAddressLookupFailed 00102 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: 00103 [WIError errorWithDomain:WILibWiredErrorDomain], 00104 WILibWiredErrorKey, 00105 string, 00106 WIArgumentErrorKey, 00107 NULL]]; 00108 } 00109 00110 [self release]; 00111 00112 wi_release(pool); 00113 00114 return NULL; 00115 } 00116 00117 self = [self _initWithAddress:address]; 00118 00119 wi_release(pool); 00120 00121 return self; 00122 } 00123 00124 00125 00126 - (id)initWithNetService:(NSNetService *)netService error:(WIError **)error { 00127 NSArray *addresses; 00128 NSData *data; 00129 wi_pool_t *pool; 00130 wi_address_t *address; 00131 00132 self = [super init]; 00133 00134 addresses = [netService addresses]; 00135 00136 if([addresses count] == 0) { 00137 if(error) { 00138 *error = [WIError errorWithDomain:WIWiredNetworkingErrorDomain 00139 code:WIAddressNetServiceLookupFailed]; 00140 } 00141 00142 [self release]; 00143 00144 return NULL; 00145 } 00146 00147 data = [addresses objectAtIndex:0]; 00148 00149 pool = wi_pool_init(wi_pool_alloc()); 00150 address = wi_address_init_with_sa(wi_address_alloc(), (struct sockaddr *) [data bytes]); 00151 00152 self = [self _initWithAddress:address]; 00153 00154 wi_release(address); 00155 wi_release(pool); 00156 00157 return self; 00158 } 00159 00160 00161 00162 - (NSString *)description { 00163 return [NSSWF:@"<%@: %p>{address = %@}", [self class], self, [self string]]; 00164 } 00165 00166 00167 00168 - (void)dealloc { 00169 [_string release]; 00170 00171 wi_release(_address); 00172 00173 [super dealloc]; 00174 } 00175 00176 00177 00178 #pragma mark - 00179 00180 - (void)setPort:(NSUInteger)port { 00181 wi_address_set_port(_address, port); 00182 } 00183 00184 00185 00186 - (NSUInteger)port { 00187 return wi_address_port(_address); 00188 } 00189 00190 00191 00192 #pragma mark - 00193 00194 - (WIAddressFamily)family { 00195 return (WIAddressFamily) wi_address_family(_address); 00196 } 00197 00198 00199 00200 - (NSString *)string { 00201 return _string; 00202 } 00203 00204 00205 00206 - (NSString *)hostname { 00207 NSString *string; 00208 wi_pool_t *pool; 00209 00210 pool = wi_pool_init(wi_pool_alloc()); 00211 string = [NSString stringWithWiredString:wi_address_hostname(_address)]; 00212 wi_release(pool); 00213 00214 return string; 00215 } 00216 00217 00218 00219 - (struct sockaddr *)sockAddr { 00220 return wi_address_sa(_address); 00221 } 00222 00223 @end 00224 00225 00226 00227 @implementation WIAddress(WISocketAdditions) 00228 00229 - (id)initWithAddress:(wi_address_t *)address { 00230 return [self _initWithAddress:address]; 00231 } 00232 00233 00234 00235 - (wi_address_t *)address { 00236 return _address; 00237 } 00238 00239 @end