pool-demo.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #define USE_CLIENT_POOL
  2. #include "server.h"
  3. /* Same as demo.c but instead of handling clients serially, a pool of clients
  4. * is handled in the same fashion. The slight difference may allow more
  5. * throughput at the cost of a tiny bit more memory and a tiny bit slowdown
  6. * due to the requirement to remember multiple client states and higher CPU
  7. * usage.
  8. *
  9. * [!] The attack surface of the interface is increased as well so an
  10. * adversarial client can now cause denial of service attacks by opening
  11. * and closing a connection making this demo needing to recalculate client
  12. * state. In addition if state is not calculated well, eg: the fractional time
  13. * resolution is not good enough, an adversary could either cause the client
  14. * pool to be always full or always empty. Yay!
  15. */
  16. int main(int argc, char *argv[]) {
  17. int sock;
  18. client pool[POOL_SIZE];
  19. #ifdef _WIN32
  20. // if compiling on Msft include the winsock librariy (ws2_32.lib)
  21. WSADATA wsaData;
  22. if(msftVendorLockInCode(&wsaData)) return 1;
  23. #endif
  24. // terminate if we cannot get a socket that can be used to accept clients
  25. if(!(sock = setup())) return 1;
  26. // initialize client pool
  27. for(uint32_t i = 0; i < POOL_SIZE; i++) pool[i].fd = -1;
  28. // server listen port is first socket file descriptor
  29. pool[0].fd = sock;
  30. while(1) {
  31. if(!faceItMetal(1000 /* ms */, alwaysGood, pool)) return softCuteBunnies(1);
  32. }
  33. return softCuteBunnies(0);
  34. }