Commit e305b7546a33ea8ae07e276e89952303f0fac5b7
1 parent
98a5375a
Expand client identifying string, including address
Showing
5 changed files
with
58 additions
and
15 deletions
client.cpp
| ... | ... | @@ -24,8 +24,9 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>. |
| 24 | 24 | #include <chrono> |
| 25 | 25 | |
| 26 | 26 | #include "logger.h" |
| 27 | +#include "utils.h" | |
| 27 | 28 | |
| 28 | -Client::Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool websocket, std::shared_ptr<Settings> settings, bool fuzzMode) : | |
| 29 | +Client::Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool websocket, struct sockaddr *addr, std::shared_ptr<Settings> settings, bool fuzzMode) : | |
| 29 | 30 | fd(fd), |
| 30 | 31 | fuzzMode(fuzzMode), |
| 31 | 32 | initialBufferSize(settings->clientInitialBufferSize), // The client is constructed in the main thread, so we need to use its settings copy |
| ... | ... | @@ -37,6 +38,13 @@ Client::Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool we |
| 37 | 38 | { |
| 38 | 39 | int flags = fcntl(fd, F_GETFL); |
| 39 | 40 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 41 | + | |
| 42 | + this->address = sockaddrToString(addr); | |
| 43 | + | |
| 44 | + if (ssl) | |
| 45 | + transportStr = websocket ? "TCP/Websocket/MQTT/SSL" : "TCP/MQTT/SSL"; | |
| 46 | + else | |
| 47 | + transportStr = websocket ? "TCP/Websocket/MQTT/Non-SSL" : "TCP/MQTT/Non-SSL"; | |
| 40 | 48 | } |
| 41 | 49 | |
| 42 | 50 | Client::~Client() |
| ... | ... | @@ -255,10 +263,9 @@ bool Client::writeBufIntoFd() |
| 255 | 263 | |
| 256 | 264 | std::string Client::repr() |
| 257 | 265 | { |
| 258 | - std::ostringstream a; | |
| 259 | - a << "[Client=" << clientid << ", user=" << username << ", fd=" << fd << "]"; | |
| 260 | - a.flush(); | |
| 261 | - return a.str(); | |
| 266 | + std::string s = formatString("[ClientID='%s', username='%s', fd=%d, keepalive=%ds, transport='%s', address='%s']", | |
| 267 | + clientid.c_str(), username.c_str(), fd, keepalive, this->transportStr.c_str(), this->address.c_str()); | |
| 268 | + return s; | |
| 262 | 269 | } |
| 263 | 270 | |
| 264 | 271 | /** | ... | ... |
client.h
| ... | ... | @@ -40,7 +40,6 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>. |
| 40 | 40 | #define MQTT_HEADER_LENGH 2 |
| 41 | 41 | |
| 42 | 42 | |
| 43 | -// TODO: give accepted addr, for showing in logs | |
| 44 | 43 | class Client |
| 45 | 44 | { |
| 46 | 45 | friend class IoWrapper; |
| ... | ... | @@ -54,6 +53,8 @@ class Client |
| 54 | 53 | const size_t maxPacketSize = 0; |
| 55 | 54 | |
| 56 | 55 | IoWrapper ioWrapper; |
| 56 | + std::string transportStr; | |
| 57 | + std::string address; | |
| 57 | 58 | |
| 58 | 59 | CirBuf readbuf; |
| 59 | 60 | CirBuf writebuf; |
| ... | ... | @@ -88,7 +89,7 @@ class Client |
| 88 | 89 | void setReadyForReading(bool val); |
| 89 | 90 | |
| 90 | 91 | public: |
| 91 | - Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool websocket, std::shared_ptr<Settings> settings, bool fuzzMode=false); | |
| 92 | + Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool websocket, struct sockaddr *addr, std::shared_ptr<Settings> settings, bool fuzzMode=false); | |
| 92 | 93 | Client(const Client &other) = delete; |
| 93 | 94 | Client(Client &&other) = delete; |
| 94 | 95 | ~Client(); | ... | ... |
mainapp.cpp
| ... | ... | @@ -496,12 +496,12 @@ void MainApp::start() |
| 496 | 496 | |
| 497 | 497 | std::shared_ptr<ThreadData> threaddata(new ThreadData(0, subscriptionStore, settings)); |
| 498 | 498 | |
| 499 | - std::shared_ptr<Client> client(new Client(fd, threaddata, nullptr, fuzzWebsockets, settings, true)); | |
| 500 | - std::shared_ptr<Client> subscriber(new Client(fdnull, threaddata, nullptr, fuzzWebsockets, settings, true)); | |
| 499 | + std::shared_ptr<Client> client(new Client(fd, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true)); | |
| 500 | + std::shared_ptr<Client> subscriber(new Client(fdnull, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true)); | |
| 501 | 501 | subscriber->setClientProperties(ProtocolVersion::Mqtt311, "subscriber", "subuser", true, 60, true); |
| 502 | 502 | subscriber->setAuthenticated(true); |
| 503 | 503 | |
| 504 | - std::shared_ptr<Client> websocketsubscriber(new Client(fdnull2, threaddata, nullptr, true, settings, true)); | |
| 504 | + std::shared_ptr<Client> websocketsubscriber(new Client(fdnull2, threaddata, nullptr, true, nullptr, settings, true)); | |
| 505 | 505 | websocketsubscriber->setClientProperties(ProtocolVersion::Mqtt311, "websocketsubscriber", "websocksubuser", true, 60, true); |
| 506 | 506 | websocketsubscriber->setAuthenticated(true); |
| 507 | 507 | websocketsubscriber->setFakeUpgraded(); |
| ... | ... | @@ -573,10 +573,11 @@ void MainApp::start() |
| 573 | 573 | |
| 574 | 574 | logger->logf(LOG_INFO, "Accepting connection on thread %d on %s", thread_data->threadnr, listener->getProtocolName().c_str()); |
| 575 | 575 | |
| 576 | - struct sockaddr addr; | |
| 577 | - memset(&addr, 0, sizeof(struct sockaddr)); | |
| 578 | - socklen_t len = sizeof(struct sockaddr); | |
| 579 | - int fd = check<std::runtime_error>(accept(cur_fd, &addr, &len)); | |
| 576 | + struct sockaddr_in6 addrBiggest; | |
| 577 | + struct sockaddr *addr = reinterpret_cast<sockaddr*>(&addrBiggest); | |
| 578 | + socklen_t len = sizeof(struct sockaddr_in6); | |
| 579 | + memset(addr, 0, len); | |
| 580 | + int fd = check<std::runtime_error>(accept(cur_fd, addr, &len)); | |
| 580 | 581 | |
| 581 | 582 | SSL *clientSSL = nullptr; |
| 582 | 583 | if (listener->isSsl()) |
| ... | ... | @@ -593,7 +594,7 @@ void MainApp::start() |
| 593 | 594 | SSL_set_fd(clientSSL, fd); |
| 594 | 595 | } |
| 595 | 596 | |
| 596 | - std::shared_ptr<Client> client(new Client(fd, thread_data, clientSSL, listener->websocket, settings)); | |
| 597 | + std::shared_ptr<Client> client(new Client(fd, thread_data, clientSSL, listener->websocket, addr, settings)); | |
| 597 | 598 | thread_data->giveClient(client); |
| 598 | 599 | } |
| 599 | 600 | else | ... | ... |
utils.cpp
| ... | ... | @@ -600,3 +600,35 @@ ssize_t getFileSize(const std::string &path) |
| 600 | 600 | |
| 601 | 601 | return statbuf.st_size; |
| 602 | 602 | } |
| 603 | + | |
| 604 | +std::string sockaddrToString(sockaddr *addr) | |
| 605 | +{ | |
| 606 | + if (!addr) | |
| 607 | + return "[unknown address]"; | |
| 608 | + | |
| 609 | + char buf[INET6_ADDRSTRLEN]; | |
| 610 | + void *addr_in = nullptr; | |
| 611 | + | |
| 612 | + if (addr->sa_family == AF_INET) | |
| 613 | + { | |
| 614 | + struct sockaddr_in *ipv4sockAddr = reinterpret_cast<struct sockaddr_in*>(addr); | |
| 615 | + addr_in = &ipv4sockAddr->sin_addr; | |
| 616 | + } | |
| 617 | + else if (addr->sa_family == AF_INET6) | |
| 618 | + { | |
| 619 | + struct sockaddr_in6 *ipv6sockAddr = reinterpret_cast<struct sockaddr_in6*>(addr); | |
| 620 | + addr_in = &ipv6sockAddr->sin6_addr; | |
| 621 | + } | |
| 622 | + | |
| 623 | + if (addr_in) | |
| 624 | + { | |
| 625 | + const char *rc = inet_ntop(addr->sa_family, addr_in, buf, INET6_ADDRSTRLEN); | |
| 626 | + if (rc) | |
| 627 | + { | |
| 628 | + std::string remote_addr(rc); | |
| 629 | + return remote_addr; | |
| 630 | + } | |
| 631 | + } | |
| 632 | + | |
| 633 | + return "[unknown address]"; | |
| 634 | +} | ... | ... |