pirserver.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <stdio.h>
  9. #include <time.h>
  10. #include "pirserver.h"
  11. #define PIRSERVER_LOG_FILE "pirserverlog"
  12. #define PIRSERVER_HDR_SIZE 13
  13. #define PIRSERVER_REQUEST_PARAMS 0x01
  14. #define PIRSERVER_REQUEST_STORE 0x02
  15. #define PIRSERVER_REQUEST_LOOKUP 0x03
  16. #define PIRSERVER_RESPONSE_PARAMS 0xFF
  17. #define PIRSERVER_RESPONSE_LOOKUP_SUCCESS 0xFE
  18. #define PIRSERVER_RESPONSE_LOOKUP_FAILURE 0xFD
  19. static int
  20. read_all(char *buf, size_t len)
  21. {
  22. int tot_read = 0;
  23. while(len > 0) {
  24. int res = read(0, buf, len);
  25. if (res <= 0) return res;
  26. buf += res;
  27. len -= res;
  28. tot_read += res;
  29. }
  30. return tot_read;
  31. }
  32. static int
  33. write_all(const char *buf, size_t len)
  34. {
  35. int tot_written = 0;
  36. while(len > 0) {
  37. int res = write(1, buf, len);
  38. if (res <= 0) return res;
  39. buf += res;
  40. len -= res;
  41. tot_written += res;
  42. }
  43. return tot_written;
  44. }
  45. void
  46. PIRServer::mainloop()
  47. {
  48. FILE *fptr = fopen(PIRSERVER_LOG_FILE, "a");
  49. clock_t start, end, cpu_time;
  50. char header[PIRSERVER_HDR_SIZE];
  51. size_t bodylen = 0;
  52. char *body = NULL;
  53. string query, response;
  54. size_t response_len;
  55. while(1) {
  56. // Read the request from stdin
  57. int res = read_all(header, PIRSERVER_HDR_SIZE);
  58. if (res <= 0) return; // stdin has reached EOF (or error); we
  59. // will terminate
  60. bodylen = ntohl(*(uint32_t*)(header+PIRSERVER_HDR_SIZE-4));
  61. if (bodylen > 0) {
  62. body = (char *)malloc(bodylen);
  63. res = read_all(body, bodylen);
  64. if (res <= 0) return;
  65. }
  66. // We have a complete request. Dispatch it.
  67. switch(header[8]) {
  68. case PIRSERVER_REQUEST_PARAMS:
  69. start = clock();
  70. get_params(response);
  71. response_len = response.length();
  72. header[8] = PIRSERVER_RESPONSE_PARAMS;
  73. *(uint32_t*)(header+PIRSERVER_HDR_SIZE-4) = htonl(response_len);
  74. res = write_all(header, PIRSERVER_HDR_SIZE);
  75. if (res <= 0) return;
  76. if (response_len > 0) {
  77. res = write_all(response.c_str(), response_len);
  78. if (res <= 0) return;
  79. }
  80. end = clock();
  81. fprintf(fptr, "REQUEST_PARAMS,");
  82. break;
  83. case PIRSERVER_REQUEST_STORE:
  84. if (bodylen >= 32) {
  85. start = clock();
  86. string key(body, 32);
  87. string value(body+32, bodylen-32);
  88. store(key, value);
  89. end=clock();
  90. fprintf(fptr, "STORE,");
  91. }
  92. break;
  93. case PIRSERVER_REQUEST_LOOKUP:
  94. start = clock();
  95. query.assign(body, bodylen);
  96. if (lookup(query, response)) {
  97. response_len = response.length();
  98. header[8] = PIRSERVER_RESPONSE_LOOKUP_SUCCESS;
  99. } else {
  100. response_len = 0;
  101. header[8] = PIRSERVER_RESPONSE_LOOKUP_FAILURE;
  102. }
  103. *(uint32_t*)(header+PIRSERVER_HDR_SIZE-4) = htonl(response_len);
  104. res = write_all(header, PIRSERVER_HDR_SIZE);
  105. if (res <= 0) return;
  106. if (response_len > 0) {
  107. res = write_all(response.c_str(), response_len);
  108. if (res <= 0) return;
  109. }
  110. end = clock();
  111. fprintf(fptr, "LOOKUP,");
  112. break;
  113. }
  114. cpu_time = end - start;
  115. //Time logged in ms
  116. fprintf(fptr,"%f\n", ((float)cpu_time/CLOCKS_PER_SEC) * 1000 );
  117. fflush(fptr);
  118. // Clean up for the next request.
  119. free(body);
  120. body = NULL;
  121. bodylen = 0;
  122. }
  123. }