connection.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. \file connection.cpp
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Connection Implementation
  17. */
  18. #include "connection.h"
  19. #include "constants.h"
  20. #include "socket.h"
  21. #include "utils.h"
  22. #include <cassert>
  23. #include <iostream>
  24. #include <limits>
  25. bool Connect(const std::string& address, uint16_t port,
  26. std::vector<std::unique_ptr<CSocket>> &sockets, uint32_t id) {
  27. #ifndef BATCH
  28. std::cout << "Connecting party "<< id <<": " << address << ", " << port << std::endl;
  29. #endif
  30. assert(sockets.size() <= std::numeric_limits<uint32_t>::max());
  31. for (size_t j = 0; j < sockets.size(); j++) {
  32. sockets[j] = Connect(address, port);
  33. if (sockets[j]) {
  34. // handshake
  35. sockets[j]->Send(&id, sizeof(id));
  36. uint32_t index = static_cast<uint32_t>(j);
  37. sockets[j]->Send(&index, sizeof(index));
  38. }
  39. else {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. bool Listen(const std::string& address, uint16_t port,
  46. std::vector<std::vector<std::unique_ptr<CSocket>>> &sockets,
  47. size_t numConnections, uint32_t myID) {
  48. auto listen_socket = std::make_unique<CSocket>();
  49. if (!listen_socket->Bind(address, port)) {
  50. std::cerr << "Error: a socket could not be bound\n";
  51. return false;
  52. }
  53. if (!listen_socket->Listen()) {
  54. std::cerr << "Error: could not listen on the socket \n";
  55. return false;
  56. }
  57. for (size_t i = 0; i < numConnections; i++)
  58. {
  59. auto sock = listen_socket->Accept();
  60. if (!sock) {
  61. std::cerr << "Error: could not accept connection\n";
  62. return false;
  63. }
  64. // receive initial pid when connected
  65. uint32_t nID;
  66. uint32_t conID; //a mix of threadID and role - depends on the application
  67. sock->Receive(&nID, sizeof(nID));
  68. sock->Receive(&conID, sizeof(conID));
  69. if (nID >= sockets.size()) //Not more than two parties currently allowed
  70. {
  71. sock->Close();
  72. i--; // try same index again
  73. continue;
  74. }
  75. if (conID >= sockets[myID].size()) {
  76. sock->Close();
  77. i--; // try same index again
  78. continue;
  79. }
  80. // locate the socket appropriately
  81. sockets[nID][conID] = std::move(sock);
  82. }
  83. #ifndef BATCH
  84. std::cout << "Listening finished" << std::endl;
  85. #endif
  86. return true;
  87. }
  88. std::unique_ptr<CSocket> Connect(const std::string& address, uint16_t port) {
  89. auto socket = std::make_unique<CSocket>();
  90. for (int i = 0; i < RETRY_CONNECT; i++) {
  91. if (socket->Connect(address, port))
  92. return socket;
  93. SleepMiliSec(10);
  94. }
  95. std::cerr << "Connect failed due to timeout!\n";
  96. return nullptr;
  97. }
  98. std::unique_ptr<CSocket> Listen(const std::string& address, uint16_t port) {
  99. auto listen_socket = std::make_unique<CSocket>();
  100. if (!listen_socket->Bind(address, port)) {
  101. return nullptr;
  102. }
  103. if (!listen_socket->Listen()) {
  104. return nullptr;
  105. }
  106. return listen_socket->Accept();
  107. }