William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againadium / Plugins / Purple Service / adiumPurpleDnsRequest.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | //
// adiumPurpleDnsRequest.m
// Adium
//
// Created by Graham Booker on 2/24/07.
//
#import "adiumPurpleDnsRequest.h"
#import <libpurple/internal.h>
#import <sys/socket.h>
#import <netdb.h>
@interface AdiumPurpleDnsRequest : NSObject {
PurpleDnsQueryData *query_data;
PurpleDnsQueryResolvedCallback resolved_cb;
PurpleDnsQueryFailedCallback failed_cb;
CFHostRef host;
BOOL finished_lookup;
}
+ (AdiumPurpleDnsRequest *)lookupRequestForData:(PurpleDnsQueryData *)query_data;
- (id)initWithData:(PurpleDnsQueryData *)data resolvedCB:(PurpleDnsQueryResolvedCallback)resolved failedCB:(PurpleDnsQueryFailedCallback)failed;
- (BOOL)startLookup;
- (void)lookupFailedWithError:(const CFStreamError *)streamError;
- (void)lookupSucceededWithAddresses:(NSArray *)addresses;
- (void)cancel;
@end
@implementation AdiumPurpleDnsRequest
static NSMutableDictionary *lookupRequestsByQueryData = nil;
+ (void)initialize
{
if (self == [AdiumPurpleDnsRequest class]) {
lookupRequestsByQueryData = [[NSMutableDictionary alloc] init];
}
}
+ (AdiumPurpleDnsRequest *)lookupRequestForData:(PurpleDnsQueryData *)query_data
{
return [lookupRequestsByQueryData objectForKey:[NSValue valueWithPointer:query_data]];
}
/*!
* @brief Perform a DNS lookup request
*
* @result YES if the DNS lookup began successfully. It was asynchronously return success or failure at a later time.
*/
+ (BOOL)performDnsRequestWithData:(PurpleDnsQueryData *)inData resolvedCB:(PurpleDnsQueryResolvedCallback)inResolved failedCB:(PurpleDnsQueryFailedCallback)inFailed
{
return [[[[self alloc] initWithData:inData resolvedCB:inResolved failedCB:inFailed] autorelease] startLookup];
}
- (id)initWithData:(PurpleDnsQueryData *)data resolvedCB:(PurpleDnsQueryResolvedCallback)resolved failedCB:(PurpleDnsQueryFailedCallback)failed
{
self = [super init];
if(self == nil)
return nil;
query_data = data;
resolved_cb = resolved;
failed_cb = failed;
finished_lookup = NO;
[lookupRequestsByQueryData setObject:self forKey:[NSValue valueWithPointer:query_data]];
//Released in finishDnsRequest
[self retain];
return self;
}
- (void)dealloc
{
if (host)
CFRelease(host);
[super dealloc];
}
- (PurpleDnsQueryData *)queryData
{
return query_data;
}
/*!
* @brief Called by CFHost when the resolve is complete or fails
*/
static void host_client_cb(CFHostRef theHost, CFHostInfoType typeInfo,
const CFStreamError *streamError,
void *info)
{
AdiumPurpleDnsRequest *self = (AdiumPurpleDnsRequest *)info;
if (streamError && (streamError->error != 0)) {
[self lookupFailedWithError:streamError];
} else {
Boolean hasBeenResolved;
/* CFHostGetAddressing retrieves the known addresses from the given host. Returns a
CFArrayRef of addresses. Each address is a CFDataRef wrapping a struct sockaddr. */
CFArrayRef addresses = CFHostGetAddressing(theHost, &hasBeenResolved);
if (hasBeenResolved) {
[self lookupSucceededWithAddresses:(NSArray *)addresses];
} else {
[self lookupFailedWithError:NULL];
}
}
}
/*!
* @brief Lookup succeeded
*
* This will call the resolved callback provided by libpurple
*
* @param addresses An NSArray of NSData objects, each wrapping a struct sockaddr
*/
- (void)lookupSucceededWithAddresses:(NSArray *)addresses
{
//Success! Build a list of our results and pass it to the resolved callback
AILog(@"DNS resolve complete for %s:%d",
purple_dnsquery_get_host(query_data),
purple_dnsquery_get_port(query_data));
NSData *address;
GSList *returnAddresses = NULL;
unsigned short port = purple_dnsquery_get_port(query_data);
for (address in addresses) {
struct sockaddr *addr = (struct sockaddr *)[address bytes];
struct sockaddr_in *addr_to_return = g_malloc(addr->sa_len);
memcpy(addr_to_return, addr, addr->sa_len);
addr_to_return->sin_port = htons(port);
returnAddresses = g_slist_append(returnAddresses, GINT_TO_POINTER((NSInteger)(addr_to_return->sin_len)));
returnAddresses = g_slist_append(returnAddresses, addr_to_return);
}
finished_lookup = YES;
resolved_cb(query_data, returnAddresses);
}
/*!
* @brief Report that the lookup failed
*
* This will call the failed callback provided by libpurple
*/
- (void)lookupFailedWithError:(const CFStreamError *)streamError
{
AILogWithSignature(@"Failed lookup for %s. Error domain %i, error %i",
purple_dnsquery_get_host([self queryData]),
(streamError ? streamError->domain : 0),
(streamError ? streamError->error : 0));
finished_lookup = YES;
//Failure :( Send an error message to the failed callback
char message[1024];
g_snprintf(message, sizeof(message), _("Error resolving %s:\n%s"),
purple_dnsquery_get_host(query_data), (streamError ? gai_strerror(streamError->error) : _("Unknown")));
failed_cb(query_data, message);
}
/*!
* @brief Begin an asynchronous lookup
*
* @result YES if the lookup started successfully
*/
- (BOOL)startLookup
{
CFStreamError streamError;
Boolean success;
CFHostClientContext context = { /* Version */ 0, /* info */ self, CFRetain, CFRelease, NULL};
AILogWithSignature(@"Performing DNS resolve: %s:%d",
purple_dnsquery_get_host(query_data),
purple_dnsquery_get_port(query_data));
host = CFHostCreateWithName(kCFAllocatorDefault,
(CFStringRef)[NSString stringWithUTF8String:purple_dnsquery_get_host(query_data)]);
success = CFHostSetClient(host, host_client_cb, &context);
if (!success) {
[self lookupFailedWithError:NULL];
return FALSE;
}
CFHostScheduleWithRunLoop(host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
success = CFHostStartInfoResolution(host, kCFHostAddresses, &streamError);
if (!success)
[self lookupFailedWithError:&streamError];
return success;
}
/*!
* @brief Clean up after a DNS request (whether it succeeded or failed)
*
* Must be called only once. Should only be called by -[self cancel].
*/
- (void)_finishDnsRequest
{
if (host) {
CFHostUnscheduleFromRunLoop(host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFHostSetClient(host, /* callback */ NULL, /* context */ NULL);
}
/* Immediately remove ourselves from the global lookup dictionary so we won't be double-released */
if (query_data) {
[lookupRequestsByQueryData removeObjectForKey:[NSValue valueWithPointer:query_data]];
query_data = NULL;
}
//Release our retain in init...
[self autorelease];
}
/*!
* @brief Cancel
*/
- (void)cancel
{
if (!finished_lookup && host)
CFHostCancelInfoResolution(host, kCFHostAddresses);
[self _finishDnsRequest];
}
@end
gboolean adiumPurpleDnsRequestResolve(PurpleDnsQueryData *query_data, PurpleDnsQueryResolvedCallback resolved_cb, PurpleDnsQueryFailedCallback failed_cb)
{
return [AdiumPurpleDnsRequest performDnsRequestWithData:query_data resolvedCB:resolved_cb failedCB:failed_cb];
}
/*!
* @brief Libpurple is done with a query_data
*
* This will be called after we call the resolved_cb or the failed_cb or when libpurple wants us to cancel
*/
void adiumPurpleDnsRequestDestroy(PurpleDnsQueryData *query_data)
{
[[AdiumPurpleDnsRequest lookupRequestForData:query_data] cancel];
}
static PurpleDnsQueryUiOps adiumPurpleDnsRequestOps = {
adiumPurpleDnsRequestResolve,
adiumPurpleDnsRequestDestroy
};
PurpleDnsQueryUiOps *adium_purple_dns_request_get_ui_ops(void)
{
return &adiumPurpleDnsRequestOps;
}
|