summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLSiphonServer.m
blob: dbb43342494a41a03840d3d60b131eccd86fb9c2 (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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//  SDLSiphonServer.m
//


/*******************************
 * Description:
 * This class opens a socket on port 7474.
 * The 'sendSiphonData' Function will
 * write to the socket that the SDL Relay Sniffer is
 * listening on.
 ******************************/

//#define ZERO_CONFIG //Uncomment when implementing zero-config.
//#define DEBUG_SIPHON //Uncomment to have output to NSLog.

#import "SDLSiphonServer.h"
#import "SDLDebugTool.h"
#include <CFNetwork/CFNetwork.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

typedef enum {
    fromApp = 0x01,
    fromSDL = 0x00,
    appLog = 0x02,
    formattedTrace = 0x03
} SiphonDataType;

const Byte siphonMsgVersionNumber = 1;
volatile int siphonSocket = 0;
NSObject *siphonLock = nil;
CFSocketRef _listeningSocket;
NSNetService *netService = nil;
volatile bool initStarted = false;
bool siphonServerEnabled = false;
bool sendingFormattedTrace = false;
NSDate *startTimeStamp;
int FIRST_PORT_TO_ATTEMPT_CONNECTION = 7474;

void _closeSiphonSocket();
bool _sendDataToSiphonSocket(int soc, const void *pData, int dataLength);
bool _sendSiphonData(const void *dataBytes, int dataBytesLength, SiphonDataType siphonDataType);
void _startServerOnPort(int port);
void _stopServer(NSString *reason);


@implementation SDLSiphonServer

// Model currently counts on init being called before any apps call _siphonNSLogData()
// The following is not thread safe (i.e. two threads could create siphon lock),
// but will assume for now that we won't have two transports created in the same proxy.
+ (void)init {
    if (initStarted) {
        return;
    } // end-if

    if (!siphonLock) {
        siphonLock = [NSData alloc];
    } // end-if

    @synchronized(siphonLock) {
        if (initStarted) {
            return;
        }

        initStarted = true;

        startTimeStamp = [NSDate date];

        _closeSiphonSocket();

        _startServerOnPort(FIRST_PORT_TO_ATTEMPT_CONNECTION);
    }
}

+ (void)enableSiphonDebug {
    siphonServerEnabled = true;
}

+ (void)disableSiphonDebug {
    siphonServerEnabled = false;
}

void _closeSiphonSocket() {
#ifdef DEBUG_SIPHON
    NSLog(@"siphon: Resetting siphon socket ...");
#endif
    if (siphonLock) {
        @synchronized(siphonLock) {
            if (siphonSocket) {
                close(siphonSocket);
                siphonSocket = 0;
            } // end-if
        } // end-lock
    } // end-if
#ifdef DEBUG_SIPHON
    NSLog(@"siphon: siphon socket reset complete");
#endif
} // end-method

+ (void)dealloc {
#ifdef ZERO_CONFIG
    _stopServer(@"Shutting Down");
#endif
}

+ (bool)_siphonIsActive {
    if (siphonSocket == 0) {
        return NO;
    }
    return YES;
}


+ (bool)_siphonFormattedTraceData:(NSString *)traceData {
    if ((traceData == NULL) || (traceData.length == 0)) {
        return NO;
    } // end-if

    NSData *traceBytes = [traceData dataUsingEncoding:NSUTF8StringEncoding];

    if (traceBytes == nil) {
        return NO;
    } // end-if

    bool dataSent = NO;

    sendingFormattedTrace = true;

    dataSent = _sendSiphonData(traceBytes.bytes, (int)traceBytes.length, formattedTrace);

    return dataSent;
} // end-method

+ (bool)_siphonNSLogData:(NSString *)textToLog {
    if ((textToLog == NULL) || (textToLog.length == 0)) {
        return NO;
    } // end-if

    NSData *textBytes = [textToLog dataUsingEncoding:NSUTF8StringEncoding];

    if (textBytes == nil) {
        return NO;
    } // end-if

    bool dataSent = NO;

    dataSent = _sendSiphonData(textBytes.bytes, (int)textBytes.length, appLog);

    return dataSent;
} // end-method

+ (bool)_siphonRawTransportDataFromApp:(const void *)msgBytes msgBytesLength:(int)msgBytesLength {
    if (sendingFormattedTrace) {
        return false;
    } // end-if

    if (msgBytes == NULL || msgBytesLength == 0) {
        return false;
    } // end-if

    return _sendSiphonData(msgBytes, msgBytesLength, fromApp);
}

+ (bool)_siphonRawTransportDataFromSDL:(const void *)msgBytes msgBytesLength:(int)msgBytesLength {
    if (sendingFormattedTrace) {
        return false;
    } // end-if

    if (msgBytes == NULL || msgBytesLength == 0) {
        return false;
    } // end-if

    return _sendSiphonData(msgBytes, msgBytesLength, fromSDL);
}

bool _sendSiphonData(const void *dataBytes, int dataBytesLength, SiphonDataType siphonDataType) {
    bool wasSent = NO;

    if (dataBytes == NULL || dataBytesLength == 0 || !siphonServerEnabled) {
        return false;
    } // end-if

    NSDate *currentTime = [NSDate date];
    NSTimeInterval deltaTimeMillis = ([currentTime timeIntervalSinceDate:startTimeStamp] * 1000.0);
    uint32_t integerDeltaTimeMillis = ((uint32_t)deltaTimeMillis);

    integerDeltaTimeMillis = htonl(integerDeltaTimeMillis);

    if (siphonLock) {
        @synchronized(siphonLock) {
            if (siphonSocket) {
                Byte sdt = (Byte)siphonDataType;
                sdt = (Byte)0x80 | sdt;
                uint32_t sizeBytes = htonl(dataBytesLength + sizeof(sdt) + sizeof(integerDeltaTimeMillis) + sizeof(siphonMsgVersionNumber));

                wasSent = _sendDataToSiphonSocket(siphonSocket, &sizeBytes, sizeof(sizeBytes));

                if (wasSent) {
                    wasSent = _sendDataToSiphonSocket(siphonSocket, &sdt, sizeof(sdt));
                }

                if (wasSent) {
                    wasSent = _sendDataToSiphonSocket(siphonSocket, &siphonMsgVersionNumber, sizeof(siphonMsgVersionNumber));
                }

                if (wasSent) {
                    wasSent = _sendDataToSiphonSocket(siphonSocket, &integerDeltaTimeMillis, sizeof(integerDeltaTimeMillis));
                }

                if (wasSent) {
                    wasSent = _sendDataToSiphonSocket(siphonSocket, dataBytes, dataBytesLength);
                }

                if (wasSent) {
                    return YES;
                } else {
#ifdef DEBUG_SIPHON
                    NSLog(@"siphon: failure sending to siphon socket");
#endif
                    _closeSiphonSocket();
                    return NO;
                } // end-if
            } else {
#ifdef DEBUG_SIPHON
                NSLog(@"siphon: siphon socket is NULL");
#endif
            } // end-if
        } //end  Synchronized
    } // end-if
    return NO;

} // end-method

bool _sendDataToSiphonSocket(int soc, const void *pData, int dataLength) {
    int bytesRemainingToSend = dataLength;
    ssize_t bytesSent = 0;
    const UInt8 *pd = pData;

    if (pData == NULL || dataLength == 0) {
        return false;
    } // end-if

    while (bytesRemainingToSend > 0) {
        if (soc) {
            bytesSent = send(soc, pd, bytesRemainingToSend, 0);

            if (bytesSent == -1) {
#ifdef DEBUG_SIPHON
                NSLog(@"siphon: got bytesSent==-1 on send(siphonSocket)");
#endif
                return NO;
            } // end-if

            bytesRemainingToSend -= bytesSent;
            pd += bytesSent;
        } // end-if

    } // end-while

    return YES;

} // end-method


void _serverDidStartOnPort(int port) {
#ifdef DEBUG_SIPHON
    NSLog(@"siphon: server started on port: %d", port);
#endif
}

#ifdef ZERO_CONFIG

#pragma mark
#pragma mark Server

- (void)_didSendData:(NSData *)data {
}

void _serverDidStopWithReason(NSString *reason) {
}

- (void)_updateStatus:(NSString *)statusString {
    NSLog(@"siphon: %@", statusString);
}

- (void)_SendDidStopWithStatus:(NSString *)statusString {
    NSLog(@"siphon: server configured for output");
}

- (BOOL)isStarted {
    return (netService != nil);
}
#endif


void _acceptConnection(int fd) {
    if (siphonLock) {
        @synchronized(siphonLock) {
            int socketOps = 1;

            _closeSiphonSocket();
#ifdef DEBUG_SIPHON
            NSLog(@"siphon: storing newly accepted siphon socket handle %08x ...", fd);
#endif
            siphonSocket = fd;

            setsockopt(siphonSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&socketOps, sizeof(int));
            [SDLDebugTool logInfo:@"Siphon connected." withType:SDLDebugType_Debug];

        } // end-lock
    } // end-if
    return;
}

static void AcceptCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
#ifdef DEBUG_SIPHON
    NSLog(@"siphon: accepted siphon connection ...");
#endif

#pragma unused(type)
    assert(type == kCFSocketAcceptCallBack);
#pragma unused(address)
    assert(data != NULL);

#pragma unused(s)
    assert(s == _listeningSocket);

    _acceptConnection(*(int *)data);
}


#ifdef ZERO_CONFIG
- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict {
#pragma unused(sender)
    assert(sender == netService);
#pragma unused(errorDict)

    _stopServer(@"Registration failed");
}
#endif

void _startServerOnPort(int port) {
    BOOL success;
    int err;
    int fd;
    struct sockaddr_in addr;
    int const retryLimit = 1000;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    success = (fd != -1);

    if (success) {
        memset(&addr, 0, sizeof(addr));
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;

        addr.sin_addr.s_addr = INADDR_ANY;

        bool openPortFound = false;
        short bindPort = (short)port;
        success = false;
        for (int retryCount = 0; retryCount < retryLimit && !openPortFound; retryCount++) {
            addr.sin_port = htons(bindPort);
            err = bind(fd, (const struct sockaddr *)&addr, sizeof(addr));
            if (err == 0) {
                openPortFound = true;
                success = (err == 0);
                port = bindPort;
            } else {
                bindPort++;
            }
        } // end-for
    }
    if (success) {
        err = listen(fd, 5);
        success = (err == 0);
    }
    if (success) {
        socklen_t addrLen;

        addrLen = sizeof(addr);
        err = getsockname(fd, (struct sockaddr *)&addr, &addrLen);
        success = (err == 0);

        if (success) {
            assert(addrLen == sizeof(addr));
            port = ntohs(addr.sin_port);
        }
#ifdef DEBUG_SIPHON
        NSLog(@"siphon: my port is %d ", port);
#endif
    }
    if (success) {
        _listeningSocket = CFSocketCreateWithNative(
            NULL,
            fd,
            kCFSocketAcceptCallBack,
            AcceptCallback,
            NULL);
        success = (_listeningSocket != NULL);

        if (success) {
            CFRunLoopSourceRef rls;
            fd = -1;
            rls = CFSocketCreateRunLoopSource(NULL, _listeningSocket, 0);
            assert(rls != NULL);
            CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
            CFRelease(rls);
        }
    }

#ifdef ZERO_CONFIG

    if (success) {
        UIDevice *device = [UIDevice currentDevice];
        ;
        NSString *serviceName = [NSString stringWithFormat:@"%@_%d ", device.name, port];
        netService = [[[NSNetService alloc] initWithDomain:@"local." type:@"_sync._tcp." name:serviceName port:port] autorelease];
        success = (netService != nil);
    }
    if (success) {
        [netService publishWithOptions:NSNetServiceNoAutoRename];
    }

    if (success) {
        assert(port != 0);
        _serverDidStartOnPort(port);
    } else {
        _stopServer(@"Start failed");
        if (fd != -1) {
            assert(startFailed == 0);
        }
    }

#endif
}

void _stopServer(NSString *reason) {
#ifdef ZERO_CONFIG
    if (netService != nil) {
        [netService stop];
        netService = nil;
    }
    _serverDidStopWithReason(reason);
#endif
}

@end