Adding functionality to get the IP address of the remote host and then using it in...
[macgdbp.git] / Source / SocketWrapper.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "SocketWrapper.h"
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <unistd.h>
23
24 NSString *sockNotificationDebuggerConnection = @"DebuggerConnection";
25 NSString *sockNotificationReceiver = @"SEL-del-SocketWrapper_dataReceived";
26 NSString *NsockError = @"SocketWrapper_Error";
27 NSString *NsockDidAccept = @"SocketWrapper_DidAccept";
28 NSString *NsockDataReceived = @"SocketWrapper_DataReceived";
29 NSString *NsockDataSent = @"SocketWrapper_DataSent";
30
31 @implementation SocketWrapper
32
33 /**
34 * Initializes the socket wrapper with a host and port
35 */
36 - (id)initWithPort: (int)port
37 {
38 if (self = [super init])
39 {
40 _port = port;
41
42 // the delegate notifications work funky because of threads. we register ourselves as the
43 // observer and then pass up the messages that are actually from this object (as we can't only observe self due to threads)
44 // to our delegate, and not to all delegates
45 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_sendMessageToDelegate:) name: nil object: nil];
46 }
47 return self;
48 }
49
50 /**
51 * Close our socket and clean up anything else
52 */
53 - (void)dealloc
54 {
55 close(_socket);
56
57 [super dealloc];
58 }
59
60 /**
61 * Returns the delegate
62 */
63 - (id)delegate
64 {
65 return _delegate;
66 }
67
68 /**
69 * Sets the delegate but does *not* retain it
70 */
71 - (void)setDelegate: (id)delegate
72 {
73 _delegate = delegate;
74 }
75
76 /**
77 * Returns the name of the host to whom we are currently connected.
78 */
79 - (NSString *)remoteHost
80 {
81 struct sockaddr_in addr;
82 socklen_t addrLength;
83
84 if (getpeername(_socket, (struct sockaddr *)&addr, &addrLength) < 0)
85 {
86 [self _postNotification: NsockError withObject: [NSError errorWithDomain: @"Could not get remote hostname." code: -1 userInfo: nil]];
87 }
88
89 char *name = inet_ntoa(addr.sin_addr);
90
91 return [NSString stringWithUTF8String: name];
92 }
93
94 /**
95 * This is the notification listener for all types of notifications. If the notifications are from a SocketWrapper
96 * class, it checks that the value of _delegate in the NSNotification's userInfo matches that of this object. If it does,
97 * then the notification was sent from the same object in another thread and it passes the message along to the object's
98 * delegate. Complicated enough?
99 */
100 - (void)_sendMessageToDelegate: (NSNotification *)notif
101 {
102 // this isn't us, so there's no point in continuing
103 if ([[notif userInfo] objectForKey: sockNotificationDebuggerConnection] != _delegate)
104 {
105 return;
106 }
107
108 NSString *name = [notif name];
109
110 if (name == NsockDidAccept)
111 {
112 [_delegate socketDidAccept];
113 }
114 else if (name == NsockDataReceived)
115 {
116 [_delegate dataReceived: [notif object] deliverTo: NSSelectorFromString([[notif userInfo] objectForKey: sockNotificationReceiver])];
117 }
118 else if (name == NsockDataSent)
119 {
120 [_delegate dataSent];
121 }
122 else if (name == NsockError)
123 {
124 [_delegate errorEncountered: [NSError errorWithDomain: [notif object] code: -1 userInfo: nil]];
125 }
126 }
127
128 /**
129 * Connects to a socket on the port specified during init. This will dispatch another thread to do the
130 * actual waiting. Delegate notifications are posted along the way to let the client know what is going on.
131 */
132 - (void)connect
133 {
134 [NSThread detachNewThreadSelector: @selector(_connect:) toTarget: self withObject: nil];
135 }
136
137 /**
138 * This does the actual dirty work (in a separate thread) of connecting to a socket
139 */
140 - (void)_connect: (id)obj
141 {
142 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
143
144 // create an INET socket that we'll be listen()ing on
145 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
146
147 // create our address given the port
148 struct sockaddr_in address;
149 address.sin_family = AF_INET;
150 address.sin_port = htons(_port);
151 address.sin_addr.s_addr = htonl(INADDR_ANY);
152 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
153
154 // bind the socket... and don't give up until we've tried for a while
155 int tries = 0;
156 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
157 {
158 if (tries >= 5)
159 {
160 close(socketOpen);
161 [self _postNotification: NsockError withObject: @"Could not bind to socket"];
162 return;
163 }
164 NSLog(@"couldn't bind to the socket... trying again in 5");
165 sleep(5);
166 tries++;
167 }
168
169 // now we just have to keep our ears open
170 if (listen(socketOpen, 0) == -1)
171 {
172 [self _postNotification: NsockError withObject: @"Could not use bound socket for listening"];
173 }
174
175 // accept a connection
176 struct sockaddr_in remoteAddress;
177 socklen_t remoteAddressLen = sizeof(remoteAddress);
178 _socket = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
179 if (_socket < 0)
180 {
181 close(socketOpen);
182 [self _postNotification: NsockError withObject: @"Client failed to accept remote socket"];
183 return;
184 }
185
186 // we're done listening now that we have a connection
187 close(socketOpen);
188
189 [self _postNotification: NsockDidAccept withObject: nil];
190
191 [pool release];
192 }
193
194 /**
195 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
196 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
197 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
198 * will return something (which we almost always do).
199 *
200 * The paramater is an optional selector which the delegate method dataReceived:deliverTo: should forward to
201 */
202 - (void)receive: (SEL)selector
203 {
204 // create a buffer
205 char buffer[1024];
206
207 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
208 int recvd = recv(_socket, &buffer, sizeof(buffer), 0);
209
210 // take the received data and put it into an NSData
211 NSMutableData *data = [NSMutableData data];
212
213 // strip the length from the packet, and clear the null byte then add it to the NSData
214 char packetLength[8];
215 int i = 0;
216 while (buffer[i] != '\0')
217 {
218 packetLength[i] = buffer[i];
219 i++;
220 }
221
222 // we also want the null byte, so move us up 1
223 i++;
224
225 // the total length of the full transmission
226 int length = atoi(packetLength);
227
228 // move the packet part of the received data into it's own char[]
229 char packet[sizeof(buffer)];
230 memmove(packet, &buffer[i], recvd - i);
231
232 // convert bytes to NSData
233 [data appendBytes: packet length: recvd];
234
235 // check if we have a partial packet
236 if (length + i > sizeof(buffer))
237 {
238 while (recvd < length)
239 {
240 int latest = recv(_socket, &buffer, sizeof(buffer), 0);
241 if (latest < 1)
242 {
243 [self _postNotification: NsockError withObject: @"Socket closed or could not be read"];
244 return;
245 }
246 [data appendBytes: buffer length: latest];
247 recvd += latest;
248 }
249 }
250
251 // convert the NSData into a NSString
252 NSString *string = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
253
254 [self _postNotification: NsockDataReceived
255 withObject: string
256 withDict: [NSMutableDictionary dictionaryWithObject: NSStringFromSelector(selector) forKey: sockNotificationReceiver]];
257 }
258
259 /**
260 * Sends a given NSString over the socket
261 */
262 - (void)send: (NSString *)data
263 {
264 data = [NSString stringWithFormat: @"%@\0", data];
265 int sent = send(_socket, [data UTF8String], [data length], 0);
266 if (sent < 0)
267 {
268 [self _postNotification: NsockError withObject: @"Failed to write data to socket"];
269 return;
270 }
271 if (sent < [data length])
272 {
273 // TODO - do we really need to worry about partial sends with the lenght of our commands?
274 NSLog(@"FAIL: only partial packet was sent; sent %d bytes", sent);
275 }
276
277 [self _postNotification: NsockDataSent withObject: nil];
278 }
279
280 /**
281 * Helper method to simply post a notification to the default notification center with a given name and object
282 */
283 - (void)_postNotification: (NSString *)name withObject: (id)obj
284 {
285 [self _postNotification: name withObject: obj withDict: [NSMutableDictionary dictionary]];
286 }
287
288 /**
289 * Another helper method to aid in the posting of notifications. This one should be used if you have additional
290 * things for the userInfo. This automatically adds the sockNotificationDebuggerConnection key.
291 */
292 - (void)_postNotification: (NSString *)name withObject: (id)obj withDict: (NSMutableDictionary *)dict
293 {
294 [dict setValue: _delegate forKey: sockNotificationDebuggerConnection];
295 [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj userInfo: dict];
296 }
297
298 @end