pirserver.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Implementation of the main loop of the pirserver, responsible for the
  2. // communication with the tor process. All the actual private lookup
  3. // work is done by an appropriate subclass of PIRServer.
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <arpa/inet.h>
  7. #include <string>
  8. #include "pirserver.h"
  9. #define PIRSERVER_HDR_SIZE 13
  10. #define PIRSERVER_REQUEST_PARAMS 0x01
  11. #define PIRSERVER_REQUEST_STORE 0x02
  12. #define PIRSERVER_REQUEST_LOOKUP 0x03
  13. #define PIRSERVER_RESPONSE_PARAMS 0xFF
  14. #define PIRSERVER_RESPONSE_LOOKUP_SUCCESS 0xFE
  15. #define PIRSERVER_RESPONSE_LOOKUP_FAILURE 0xFD
  16. static int
  17. read_all(char *buf, size_t len)
  18. {
  19. int tot_read = 0;
  20. while(len > 0) {
  21. int res = read(0, buf, len);
  22. if (res <= 0) return res;
  23. buf += res;
  24. len -= res;
  25. tot_read += res;
  26. }
  27. return tot_read;
  28. }
  29. static int
  30. write_all(const char *buf, size_t len)
  31. {
  32. int tot_written = 0;
  33. while(len > 0) {
  34. int res = write(1, buf, len);
  35. if (res <= 0) return res;
  36. buf += res;
  37. len -= res;
  38. tot_written += res;
  39. }
  40. return tot_written;
  41. }
  42. void
  43. PIRServer::mainloop()
  44. {
  45. char header[PIRSERVER_HDR_SIZE];
  46. size_t bodylen = 0;
  47. char *body = NULL;
  48. string query, response;
  49. size_t response_len;
  50. while(1) {
  51. // Read the request from stdin
  52. int res = read_all(header, PIRSERVER_HDR_SIZE);
  53. if (res <= 0) return; // stdin has reached EOF (or error); we
  54. // will terminate
  55. bodylen = ntohl(*(uint32_t*)(header+PIRSERVER_HDR_SIZE-4));
  56. if (bodylen > 0) {
  57. body = (char *)malloc(bodylen);
  58. res = read_all(body, bodylen);
  59. if (res <= 0) return;
  60. }
  61. // We have a complete request. Dispatch it.
  62. switch(header[8]) {
  63. case PIRSERVER_REQUEST_PARAMS:
  64. get_params(response);
  65. response_len = response.length();
  66. header[8] = PIRSERVER_RESPONSE_PARAMS;
  67. *(uint32_t*)(header+PIRSERVER_HDR_SIZE-4) = htonl(response_len);
  68. res = write_all(header, PIRSERVER_HDR_SIZE);
  69. if (res <= 0) return;
  70. if (response_len > 0) {
  71. res = write_all(response.c_str(), response_len);
  72. if (res <= 0) return;
  73. }
  74. break;
  75. case PIRSERVER_REQUEST_STORE:
  76. if (bodylen >= 32) {
  77. string key(body, 32);
  78. string value(body+32, bodylen-32);
  79. store(key, value);
  80. }
  81. break;
  82. case PIRSERVER_REQUEST_LOOKUP:
  83. query.assign(body, bodylen);
  84. if (lookup(query, response)) {
  85. response_len = response.length();
  86. header[8] = PIRSERVER_RESPONSE_LOOKUP_SUCCESS;
  87. } else {
  88. response_len = 0;
  89. header[8] = PIRSERVER_RESPONSE_LOOKUP_FAILURE;
  90. }
  91. *(uint32_t*)(header+PIRSERVER_HDR_SIZE-4) = htonl(response_len);
  92. res = write_all(header, PIRSERVER_HDR_SIZE);
  93. if (res <= 0) return;
  94. if (response_len > 0) {
  95. res = write_all(response.c_str(), response_len);
  96. if (res <= 0) return;
  97. }
  98. break;
  99. }
  100. // Clean up for the next request.
  101. free(body);
  102. body = NULL;
  103. bodylen = 0;
  104. }
  105. }