Ipc.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Created by miti on 21/07/19.
  3. //
  4. #include "Ipc.h"
  5. namespace Ipc {
  6. // Sets up a socket to bind and listen to the given port. Returns FD of the socket on success, -1 on failure (and prints a msg to stdout with the errno)
  7. int set_up_socket(int port, sockaddr_in *address) {
  8. int server_fd = 0;
  9. // Creating socket file descriptor for listening for attestation requests.
  10. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  11. if (server_fd == -1) {
  12. printf("Error in creating a socket - %d", errno);
  13. return -1;
  14. }
  15. // Preparing the address struct for binding
  16. address->sin_family = AF_INET;
  17. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  18. address->sin_port = htons(port);
  19. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  20. socklen_t addrlen = sizeof(*address);
  21. // Binding
  22. if (bind(server_fd, (sockaddr *) address, addrlen) < 0) {
  23. printf("Error in binding %d - port was %d - ", errno, port);
  24. return -1;
  25. }
  26. // Listening
  27. if (listen(server_fd, 128) < 0) {
  28. printf("Error in listening %d", errno);
  29. return -1;
  30. }
  31. return server_fd;
  32. }
  33. }