diff --git a/core/src/lib/bnet.cc b/core/src/lib/bnet.cc index a56fcef9739..d29ed9c5587 100644 --- a/core/src/lib/bnet.cc +++ b/core/src/lib/bnet.cc @@ -589,3 +589,44 @@ const char *BnetSigToAscii(BareosSocket * bs) return buf; } } + +uint32_t ReadoutCommandIdFromString(std::string message) +{ + size_t pos = message.find(' '); + if (pos == std::string::npos) { + return kProtokollError; + } + std::string id_string; + id_string = message.substr(0,pos); + + uint32_t id; + try { + id = std::stoul(id_string); + } catch (const std::exception &e) { + id = kProtokollError; + } + return id; +} + +uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message_output) +{ + int recv_return_value = bsock->recv(); + bsock->StopTimer(); + + if (recv_return_value <= 0) { + return kReceiveError; + } + + Dmsg1(10, "msg); + + std::string message(bsock->msg); + uint32_t id = ReadoutCommandIdFromString(message); + +// if (!bstrncmp(bsock->msg, OKhello, sizeof(OKhello) - 1)) { +// Bsnprintf(response, response_len, _("bsockector at \"%s:%d\" rejected Hello command\n"), bsock->host(), +// bsock->port()); +// return false; +// } else { +// Bsnprintf(response, response_len, "%s", bsock->msg); +// } +} diff --git a/core/src/lib/bnet.h b/core/src/lib/bnet.h index abc2df6512f..c3a4f8a71a0 100644 --- a/core/src/lib/bnet.h +++ b/core/src/lib/bnet.h @@ -49,4 +49,17 @@ int NetConnect(int port); BareosSocket *BnetBind(int port); BareosSocket *BnetAccept(BareosSocket *bsock, char *who); +enum : uint32_t { + kUnknown = 0, + kProtokollError = 1, + kReceiveError = 2, + kOk = 1000, + kPamRequired = 1001, + kPamInteractive = 4001, + kPamUserCredentials = 4002 +}; + +uint32_t ReadoutCommandIdFromString(std::string message); +uint32_t ReceiveAndEvaluateResponse(BareosSocket *bsock, std::string &message); + #endif // BAREOS_LIB_BNET_H_ diff --git a/core/src/tests/CMakeLists.txt b/core/src/tests/CMakeLists.txt index 3aa53595b67..06eeb72249f 100644 --- a/core/src/tests/CMakeLists.txt +++ b/core/src/tests/CMakeLists.txt @@ -26,6 +26,7 @@ set (TEST_SRC dlist_test.cc htable_test.cc qualified_resource_name_type_converter_test.cc + lib_tests.cc ) diff --git a/core/src/tests/lib_tests.cc b/core/src/tests/lib_tests.cc new file mode 100644 index 00000000000..e85ace70412 --- /dev/null +++ b/core/src/tests/lib_tests.cc @@ -0,0 +1,36 @@ +/** + BAREOSĀ® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2018-2018 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "gtest/gtest.h" +#include "include/bareos.h" +#include "lib/bnet.h" + +TEST(ReadoutCommandIdFromStringTest, BNet) +{ + int id; + std::string message1 {"1000 OK: Version: "}; + id = ReadoutCommandIdFromString(message1); + EXPECT_EQ(id, 1000); + + std::string message2 {"1001 OK: Version: "}; + id = ReadoutCommandIdFromString(message2); + EXPECT_NE(id, 1000); +}