-
Notifications
You must be signed in to change notification settings - Fork 83
/
rocketd.c
70 lines (65 loc) · 1.88 KB
/
rocketd.c
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
#import <notify.h>
#import <CoreFoundation/CFLogUtilities.h>
#define LIGHTMESSAGING_USE_ROCKETBOOTSTRAP 0
#import "rocketbootstrap_internal.h"
static CFMutableSetRef allowedNames;
static const uint32_t one = 1;
static void machPortCallback(CFMachPortRef port, void *bytes, CFIndex size, void *info)
{
LMMessage *request = bytes;
if (!LMDataWithSizeIsValidMessage(bytes, size)) {
LMSendReply(request->head.msgh_remote_port, NULL, 0);
LMResponseBufferFree(bytes);
return;
}
// Send Response
const void *data = LMMessageGetData(request);
size_t length = LMMessageGetDataLength(request);
const void *reply_data = NULL;
uint32_t reply_length = 0;
if (length) {
CFStringRef name = CFStringCreateWithBytes(kCFAllocatorDefault, data, length, kCFStringEncodingUTF8, false);
if (name) {
switch (request->head.msgh_id) {
case 0: // Register
#ifdef DEBUG
CFLog(kCFLogLevelWarning, CFSTR("Unlocking %@"), name);
#endif
CFSetAddValue(allowedNames, name);
break;
case 1: // Query
if (CFSetContainsValue(allowedNames, name)) {
reply_data = &one;
reply_length = sizeof one;
#ifdef DEBUG
CFLog(kCFLogLevelWarning, CFSTR("Queried %@, is unlocked"), name);
#endif
} else {
#ifdef DEBUG
CFLog(kCFLogLevelWarning, CFSTR("Queried %@, is locked!"), name);
#endif
}
break;
case 2:
// Good morning, still awake
reply_data = &one;
reply_length = sizeof one;
break;
}
CFRelease(name);
}
}
LMSendReply(request->head.msgh_remote_port, reply_data, reply_length);
LMResponseBufferFree(bytes);
}
int main(int argc, char *argv[])
{
if (rocketbootstrap_is_passthrough()) {
return 0;
}
allowedNames = CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks);
LMCheckInService(connection.serverName, CFRunLoopGetCurrent(), machPortCallback, NULL);
notify_post("com.rpetrich.rocketd.started");
CFRunLoopRun();
return 0;
}