-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGAsyncHelper.m
307 lines (267 loc) · 11.9 KB
/
GPGAsyncHelper.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// GPGAsyncHelper.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Mon Apr 12 2004.
//
//
// Copyright (C) 2001-2006 Mac GPG Project.
//
// This code is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This code is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, visit <http://www.gnu.org/> or write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// More info at <http://macgpg.sourceforge.net/>
//
#include "GPGAsyncHelper.h"
#include <MacGPGME/GPGContext.h>
#include <MacGPGME/GPGInternals.h>
#include <MacGPGME/GPGTrustItem.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <gpgme.h>
static gpgme_error_t addCallback(void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag);
static void removeCallback(void *tag);
static void eventCallback(void *data, gpgme_event_io_t type, void *type_data);
@interface GPGAsyncHelper(Private)
- (gpgme_error_t) addCallbackForContext:(GPGContext *)context fileDescriptor:(int)fd direction:(int)dir function:(void *)fnc functionData:(void *)fnc_data;
- (void) removeCallbacksForFileDescriptor:(int)fd;
- (void) eventOfType:(gpgme_event_io_t)type forContext:(GPGContext *)context eventData:(void *)type_data;
@end
@implementation GPGAsyncHelper
+ (GPGAsyncHelper *) sharedInstance
{
static GPGAsyncHelper *_sharedInstance = nil;
if(_sharedInstance == nil)
_sharedInstance = [[self alloc] init];
return _sharedInstance;
}
- (id) init
{
if(self = [super init]){
NSZone *aZone = [self zone];
_dataLock = [[NSLock allocWithZone:aZone] init];
_runSemaphore = [[NSConditionLock allocWithZone:aZone] initWithCondition:0];
#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
_paramsPerFd = NSCreateMapTableWithZone(NSIntegerMapKeyCallBacks, NSObjectMapValueCallBacks, 10, aZone);
#else
_paramsPerFd = NSCreateMapTableWithZone(NSIntMapKeyCallBacks, NSObjectMapValueCallBacks, 10, aZone);
#endif
_contexts = [[NSMutableSet allocWithZone:aZone] initWithCapacity:3];
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
}
return self;
}
- (void) dealloc
{
[_dataLock release];
[_runSemaphore release];
if(_paramsPerFd != NULL)
NSFreeMapTable(_paramsPerFd);
[_contexts release];
[super dealloc];
}
- (void) prepareAsyncOperationInContext:(GPGContext *)context
{
[_dataLock lock];
NS_DURING
gpgme_io_cbs_t callbacks;
NSParameterAssert(context != nil && ![_contexts containsObject:context]); // TODO: allow queueing operations for same context? How to?
callbacks = (gpgme_io_cbs_t)NSZoneMalloc(NSDefaultMallocZone(), sizeof(struct gpgme_io_cbs));
callbacks->add = addCallback;
callbacks->add_priv = context;
callbacks->remove = removeCallback;
callbacks->event = eventCallback;
callbacks->event_priv = context;
gpgme_set_io_cbs([context gpgmeContext], callbacks);
[_contexts addObject:context];
NSZoneFree(NSDefaultMallocZone(), callbacks);
NS_HANDLER
[_dataLock unlock];
[localException raise];
NS_ENDHANDLER
[_dataLock unlock];
}
- (void) run
{
// We should use a semaphore to show when running or not; set after adding/removing fds
while(YES){
int nfds = 0;
fd_set aReadFdSet;
fd_set aWriteFdSet;
fd_set allFdSet;
struct timeval timeout = {0L, 100000L}; // 0.1s
NSMapEnumerator anEnum;
void *aKey;
void *aValue;
NSAutoreleasePool *localAP = [[NSAutoreleasePool alloc] init];
// [_runSemaphore lockWhenCondition:1];
FD_ZERO(&aReadFdSet);
FD_ZERO(&aWriteFdSet);
FD_ZERO(&allFdSet);
[_dataLock lock];
anEnum = NSEnumerateMapTable(_paramsPerFd);
while(NSNextMapEnumeratorPair(&anEnum, &aKey, &aValue)){
int aFd = (int)aKey;
NSDictionary *aDict = (NSDictionary *)aValue;
nfds = MAX(aFd, nfds);
if([[aDict objectForKey:@"dir"] intValue] == 0)
FD_SET(aFd, &aWriteFdSet);
else
FD_SET(aFd, &aReadFdSet);
FD_SET(aFd, &allFdSet);
NSLog(@"Checking %d", aFd);
}
NSEndMapTableEnumeration(&anEnum);
[_dataLock unlock];
if(nfds > 0){
int aResult = select(nfds, &aReadFdSet, &aWriteFdSet, &allFdSet, &timeout);
switch(aResult){
case 0: // timeout
break;
case -1: // error
NSLog(@"### Error when surveying fds; errno = %d ###", errno);
break;
default: // something to read/write/exception
[_dataLock lock];
anEnum = NSEnumerateMapTable(_paramsPerFd);
while(NSNextMapEnumeratorPair(&anEnum, &aKey, &aValue)){
int aFd = (int)aKey;
NSDictionary *aDict = (NSDictionary *)aValue;
if(aFd <= nfds){
#warning Should we unlock _dataLock during function evaluation?
if(FD_ISSET(aFd, &aReadFdSet)){
NSLog(@"Reading %d", aFd);
(void)(*((gpgme_io_cb_t)[[aDict objectForKey:@"fnc"] pointerValue]))([[aDict objectForKey:@"fnc_data"] pointerValue], aFd); // We don't care (yet) about the result; it should always be 0
}
else if(FD_ISSET(aFd, &aWriteFdSet)){
NSLog(@"Writing %d", aFd);
(void)(*((gpgme_io_cb_t)[[aDict objectForKey:@"fnc"] pointerValue]))([[aDict objectForKey:@"fnc_data"] pointerValue], aFd); // We don't care (yet) about the result; it should always be 0
}
else if(FD_ISSET(aFd, &allFdSet)){
NSLog(@"### Exception on fd %d when surveying fds ###", aFd);
}
}
}
NSEndMapTableEnumeration(&anEnum);
[_dataLock unlock];
}
}
else
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
// [_runSemaphore unlock];
[localAP drain];
}
}
static gpgme_error_t addCallback(void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag)
{
gpgme_error_t anError = [[GPGAsyncHelper sharedInstance] addCallbackForContext:data fileDescriptor:fd direction:dir function:fnc functionData:fnc_data];
*tag = (void *)fd;
return anError;
}
- (gpgme_error_t) addCallbackForContext:(GPGContext *)context fileDescriptor:(int)fd direction:(int)dir function:(void *)fnc functionData:(void *)fnc_data
{
gpgme_error_t result;
NSValue *fncValue = [NSValue valueWithPointer:fnc];
NSValue *fnc_dataValue = [NSValue valueWithPointer:fnc_data];
NSNumber *dirNumber = [NSNumber numberWithInt:dir];
[_dataLock lock];
NS_DURING
NSDictionary *newParams = [[NSDictionary alloc] initWithObjectsAndKeys:fncValue, @"fnc", fnc_dataValue, @"fnc_data", dirNumber, @"dir", nil];
NSMapInsertKnownAbsent(_paramsPerFd, (void *)fd, newParams);
[newParams release];
NSLog(@"Added callback %d(%d)", fd, dir);
result = GPG_ERR_NO_ERROR;
NS_HANDLER
NSLog(@"### Error when adding async callback: %@", localException);
result = gpg_err_make(GPG_MacGPGMEFrameworkErrorSource, GPG_ERR_GENERAL);
NS_ENDHANDLER
[_dataLock unlock];
if([_runSemaphore tryLockWhenCondition:0])
[_runSemaphore unlockWithCondition:1];
return result;
}
static void removeCallback(void *tag)
{
[[GPGAsyncHelper sharedInstance] removeCallbacksForFileDescriptor:(int)tag];
}
- (void) removeCallbacksForFileDescriptor:(int)fd
{
[_dataLock lock];
NSMapRemove(_paramsPerFd, (void *)fd);
NSLog(@"Removed callback %d", fd);
if(NSCountMapTable(_paramsPerFd) == 0){
[_dataLock unlock];
if([_runSemaphore tryLockWhenCondition:1])
[_runSemaphore unlockWithCondition:0];
}
else
[_dataLock unlock];
}
static void eventCallback(void *data, gpgme_event_io_t type, void *type_data)
{
[[GPGAsyncHelper sharedInstance] eventOfType:type forContext:data eventData:type_data];
}
- (void) postNotificationInMainThread:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
- (void) eventOfType:(gpgme_event_io_t)type forContext:(GPGContext *)context eventData:(void *)type_data
{
switch(type){
case GPGME_EVENT_START:
#warning Add context fds to select FD_SET
NSLog(@"eventCallback: GPGME_EVENT_START");
break;
case GPGME_EVENT_DONE:{
GPGError anError = *((GPGError *)type_data);
NSNotification *aNotification = [NSNotification notificationWithName:GPGAsynchronousOperationDidTerminateNotification object:context userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:anError] forKey:GPGErrorKey]];
[_dataLock lock];
gpgme_set_io_cbs([context gpgmeContext], NULL);
[_contexts removeObject:context];
[_dataLock unlock];
[self performSelectorOnMainThread:@selector(postNotificationInMainThread:) withObject:aNotification waitUntilDone:NO];
// Post notification in main thread
NSLog(@"eventCallback: GPGME_EVENT_DONE");
NSLog(@"Termination status: %@ (%d)", GPGErrorDescription(anError), anError);
break;
}
case GPGME_EVENT_NEXT_KEY:{
GPGKey *aKey = [[GPGKey alloc] initWithInternalRepresentation:((gpgme_key_t)type_data)];
NSNotification *aNotification = [NSNotification notificationWithName:GPGNextKeyNotification object:context userInfo:[NSDictionary dictionaryWithObject:aKey forKey:GPGNextKeyKey]];
gpgme_key_unref((gpgme_key_t)type_data);
[self performSelectorOnMainThread:@selector(postNotificationInMainThread:) withObject:aNotification waitUntilDone:NO];
// Post notification in main thread
NSLog(@"eventCallback: GPGME_EVENT_NEXT_KEY");
NSLog(@"Next key: %@", [aKey userID]);
[aKey release];
break;
}
case GPGME_EVENT_NEXT_TRUSTITEM:{
GPGTrustItem *aTrustItem = [[GPGTrustItem alloc] initWithInternalRepresentation:((gpgme_trust_item_t)type_data)];
NSNotification *aNotification = [NSNotification notificationWithName:GPGNextTrustItemNotification object:context userInfo:[NSDictionary dictionaryWithObject:aTrustItem forKey:GPGNextTrustItemKey]];
gpgme_trust_item_unref((gpgme_trust_item_t)type_data);
[self performSelectorOnMainThread:@selector(postNotificationInMainThread:) withObject:aNotification waitUntilDone:NO];
// Post notification in main thread
NSLog(@"eventCallback: GPGME_EVENT_NEXT_TRUSTITEM");
NSLog(@"Next trustItem: %@", aTrustItem);
[aTrustItem release];
break;
}
default:
NSLog(@"-[%@ %@]: unknown event type %d; ignored", [self class], NSStringFromSelector(_cmd), type);
}
}
@end