rocketd.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #import <notify.h>
  2. #import <CoreFoundation/CFLogUtilities.h>
  3. #define LIGHTMESSAGING_USE_ROCKETBOOTSTRAP 0
  4. #import "rocketbootstrap_internal.h"
  5. static CFMutableSetRef allowedNames;
  6. static const uint32_t one = 1;
  7. static void machPortCallback(CFMachPortRef port, void *bytes, CFIndex size, void *info)
  8. {
  9. LMMessage *request = bytes;
  10. if (!LMDataWithSizeIsValidMessage(bytes, size)) {
  11. LMSendReply(request->head.msgh_remote_port, NULL, 0);
  12. LMResponseBufferFree(bytes);
  13. return;
  14. }
  15. // Send Response
  16. const void *data = LMMessageGetData(request);
  17. size_t length = LMMessageGetDataLength(request);
  18. const void *reply_data = NULL;
  19. uint32_t reply_length = 0;
  20. if (length) {
  21. CFStringRef name = CFStringCreateWithBytes(kCFAllocatorDefault, data, length, kCFStringEncodingUTF8, false);
  22. if (name) {
  23. switch (request->head.msgh_id) {
  24. case 0: // Register
  25. #ifdef DEBUG
  26. CFLog(kCFLogLevelWarning, CFSTR("Unlocking %@"), name);
  27. #endif
  28. CFSetAddValue(allowedNames, name);
  29. break;
  30. case 1: // Query
  31. if (CFSetContainsValue(allowedNames, name)) {
  32. reply_data = &one;
  33. reply_length = sizeof one;
  34. #ifdef DEBUG
  35. CFLog(kCFLogLevelWarning, CFSTR("Queried %@, is unlocked"), name);
  36. #endif
  37. } else {
  38. #ifdef DEBUG
  39. CFLog(kCFLogLevelWarning, CFSTR("Queried %@, is locked!"), name);
  40. #endif
  41. }
  42. break;
  43. case 2:
  44. // Good morning, still awake
  45. reply_data = &one;
  46. reply_length = sizeof one;
  47. break;
  48. }
  49. CFRelease(name);
  50. }
  51. }
  52. LMSendReply(request->head.msgh_remote_port, reply_data, reply_length);
  53. LMResponseBufferFree(bytes);
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. allowedNames = CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks);
  58. LMCheckInService(connection.serverName, CFRunLoopGetCurrent(), machPortCallback, NULL);
  59. notify_post("com.rpetrich.rocketd.started");
  60. CFRunLoopRun();
  61. return 0;
  62. }