summaryrefslogtreecommitdiffabout
path: root/pumpkin/ARequest.m
blob: 439366e5228d11ebc1ff7ad9f997d0fe563c6247 (plain)
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

#import "pumpkin.h"
#import "ARequest.h"
#import "ReceiveXFer.h"
#import "SendXFer.h"

static void cbHost(CFHostRef h,CFHostInfoType hi,const CFStreamError *e,void *i) {
    [(ARequest*)i hostCallbackWithHost:h info:hi andError:e];
}


@implementation ARequest
@synthesize requestIsGet;
@synthesize doTouchMe;
@synthesize statusLabel;
@synthesize errorLabel;

@synthesize localFile;
@synthesize remoteHost;
@synthesize remotePort;
@synthesize remoteFile;
@synthesize xferType;
@synthesize blockSize;
@synthesize timeout;

@synthesize remoteHostBox;

-(void)unhost {
    if(!cfhost) return;
    CFHostCancelInfoResolution(cfhost, kCFHostAddresses);
    CFHostUnscheduleFromRunLoop(cfhost, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    CFRelease(cfhost);
    cfhost = nil;
}
-(void)loadDefaults {
    id d = [NSUserDefaultsController.sharedUserDefaultsController values];
    self.remotePort = [d valueForKey:@"remotePort"];
    self.blockSize = [d valueForKey:@"blockSize"];
    self.xferType = [d valueForKey:@"xferType"];
    self.remoteHost = [d valueForKey:@"remoteHost"];
    self.timeout = [d valueForKey:@"timeout"];
    
    self.localFile = [[d valueForKey:@"tftpRoot"] stringByAppendingString:@"/"];
}
-(void)saveDefaults {
    NSUserDefaultsController *dc = [NSUserDefaultsController sharedUserDefaultsController];
    id d = dc.values;
    [d setValue:self.remotePort forKey:@"remotePort"];
    [d setValue:self.remoteHost forKey:@"remoteHost"];
    [d setValue:self.blockSize forKey:@"blockSize"];
    [d setValue:self.xferType forKey:@"xferType"];
    [d setValue:self.timeout forKey:@"timeout"];
    [dc save:self];
}


- (IBAction)startXfer:(id)sender {
    if(!(cfhost = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)remoteHost))) {
	self.errorLabel = @"failed to even try to resolve.";
	return;
    }
    struct CFHostClientContext hc;
    hc.version=0; hc.info=self; hc.retain=0;hc.release=0;
    hc.copyDescription=0;
    CFHostSetClient(cfhost, cbHost, &hc);
    CFHostScheduleWithRunLoop(cfhost, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    CFStreamError e;
    if(!CFHostStartInfoResolution(cfhost, kCFHostAddresses, &e)) {
	self.errorLabel = @"failed to start host resolution.";
	[self unhost];
	return;
    }
    self.statusLabel = @"resolving remote host…";
    self.doTouchMe = NO;
}

-(void)hostCallbackWithHost:(CFHostRef)h info:(CFHostInfoType)hi andError:(const CFStreamError *)e {
    NSString *el = nil;
    CFArrayRef aa = nil;
    __block struct sockaddr_in peer;
    do {
	if(e && (e->domain || e->error)) {
	    el=@"failed to resolve remote address"; break;
	}
	Boolean hbr;
	aa = CFHostGetAddressing(cfhost, &hbr);
	if(!(hbr && aa && CFArrayGetCount(aa))) {
	    el=@"failed to find remote address"; break;
	}
	peer.sin_addr.s_addr=INADDR_NONE; [(NSArray*)aa enumerateObjectsUsingBlock:^(NSData *o,NSUInteger i,BOOL *s) {
	    const struct sockaddr_in *sin = o.bytes;
	    if(sin->sin_family!=AF_INET) return;
	    memmove(&peer,sin,sizeof(peer));
	    *s = YES;
	}];
	if(peer.sin_addr.s_addr==INADDR_NONE) {
	    el=@"found no ipv4 address"; break;
	}
	peer.sin_port = htons([remotePort unsignedIntValue]);
    }while(false);
    [self unhost];
    if(el) {
	self.errorLabel = el; self.doTouchMe = YES; return;
    }
    [self saveDefaults];
    [[[requestIsGet?ReceiveXFer.class:SendXFer.class alloc]
      initWithLocalFile:localFile peerAddress:&peer remoteFile:remoteFile xferType:xferType blockSize:blockSize.unsignedIntValue andTimeout:timeout.intValue]
     autorelease];
    [self.window performClose:nil];
}

- (IBAction)pickFile:(id)sender {
    NSSavePanel *p = nil;
    if(requestIsGet) {
	p = [NSSavePanel savePanel];
	p.canCreateDirectories = YES;
    }else{
	NSOpenPanel *pp = [NSOpenPanel openPanel];
	pp.canChooseDirectories = NO;
	pp.canChooseFiles = YES;
	pp.allowsMultipleSelection = NO;
	p = pp;
    }
    p.prompt = @"Pick the local file";
    if([p runModal]!=NSFileHandlingPanelOKButton) return;
    self.localFile = p.URL.path;
}

- (ARequest*) initWithGet:(BOOL)gr {
    if(!(self = [super initWithWindowNibName:@"ARequest"])) return self;
    self.doTouchMe = YES;
    cfhost = nil;
    requestIsGet = gr;
    if(requestIsGet) {
	self.window.title = @"Get file from remote TFTP server";
	self.window.initialFirstResponder = remoteHostBox;
    }else{
	self.window.title = @"Put file to remote TFTP server";
    }
    [self loadDefaults];
    [self addObserver:self forKeyPath:@"localFile" options:0 context:0];
    [self addObserver:self forKeyPath:@"remoteFile" options:0 context:0];
    return [self retain];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if(requestIsGet) {
	if([keyPath isEqualToString:@"remoteFile"]) {
	    if(self.remoteFile.length) {
		self.localFile= [([self.localFile hasSuffix:@"/"]
				   ?self.localFile
				   :[self.localFile stringByDeletingLastPathComponent])
				  stringByAppendingPathComponent:self.remoteFile.lastPathComponent];
	    }else
		self.localFile=[[self.localFile stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
	}
    }else{
	if([keyPath isEqualToString:@"localFile"]) {
	    self.remoteFile=[self.localFile hasSuffix:@"/"]
		?@"":self.localFile.lastPathComponent;
	}
    }
}

+ (ARequest*) aRequestWithGet:(BOOL)gr {
    return [[[ARequest alloc] initWithGet:gr] autorelease];
}

static void popMeUp(BOOL g) {
    [[ARequest aRequestWithGet:g].window makeKeyAndOrderFront:nil];
}
+ (void)getFile { popMeUp(YES); }
+ (void)putFile { popMeUp(NO); }

- (void)windowDidLoad {
}

- (void)windowWillClose:(NSNotification*)n {
    [self unhost];
    [self removeObserver:self forKeyPath:@"localFile" context:0];
    [self removeObserver:self forKeyPath:@"remoteFile" context:0];
    [self release];
}

@end