LCOV - code coverage report
Current view: top level - src - gittransport.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 117 149 78.5 %
Date: 2024-11-15 09:04:49 Functions: 13 25 52.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #include "gittransport.h"
      18             : 
      19             : #include "manager.h"
      20             : 
      21             : #include <dhtnet/multiplexed_socket.h>
      22             : #include <dhtnet/connectionmanager.h>
      23             : 
      24             : using namespace std::string_view_literals;
      25             : 
      26             : // NOTE: THIS MUST BE IN THE ROOT NAMESPACE FOR LIBGIT2
      27             : 
      28             : int
      29        2276 : generateRequest(git_buf* request, const std::string& cmd, const std::string_view& url)
      30             : {
      31        2276 :     if (cmd.empty()) {
      32           0 :         giterr_set_str(GITERR_NET, "empty command");
      33           0 :         return -1;
      34             :     }
      35             :     // url format = deviceId/conversationId
      36        2276 :     auto delim = url.find('/');
      37        2276 :     if (delim == std::string::npos) {
      38           0 :         giterr_set_str(GITERR_NET, "malformed URL");
      39           0 :         return -1;
      40             :     }
      41             : 
      42        2276 :     auto deviceId = url.substr(0, delim);
      43        2276 :     auto conversationId = url.substr(delim, url.size());
      44             : 
      45        2276 :     auto nullSeparator = "\0"sv;
      46             :     auto total = 4                                   /* 4 bytes for the len len */
      47        2276 :                  + cmd.size()                        /* followed by the command */
      48             :                  + 1                                 /* space */
      49        2276 :                  + conversationId.size()             /* conversation */
      50             :                  + 1                                 /* \0 */
      51        2276 :                  + HOST_TAG.size() + deviceId.size() /* device */
      52        2276 :                  + nullSeparator.size() /* \0 */;
      53             : 
      54        2276 :     std::ostringstream streamed;
      55        2276 :     streamed << std::setw(4) << std::setfill('0') << std::hex << (total & 0x0FFFF) << cmd;
      56        2276 :     streamed << " " << conversationId;
      57        2276 :     streamed << nullSeparator << HOST_TAG << deviceId << nullSeparator;
      58        2276 :     auto str = streamed.str();
      59        2276 :     git_buf_set(request, str.c_str(), str.size());
      60        2276 :     return 0;
      61        2276 : }
      62             : 
      63             : int
      64        2276 : sendCmd(P2PStream* s)
      65             : {
      66        2276 :     auto res = 0;
      67        2276 :     git_buf request = {};
      68        2276 :     if ((res = generateRequest(&request, s->cmd, s->url)) < 0) {
      69           0 :         git_buf_dispose(&request);
      70           0 :         return res;
      71             :     }
      72             : 
      73        2276 :     std::error_code ec;
      74        2276 :     auto sock = s->socket.lock();
      75        2276 :     if (!sock) {
      76           0 :         git_buf_dispose(&request);
      77           0 :         return -1;
      78             :     }
      79        2276 :     if ((res = sock->write(reinterpret_cast<const unsigned char*>(request.ptr), request.size, ec))) {
      80        2276 :         s->sent_command = 1;
      81        2276 :         git_buf_dispose(&request);
      82        2276 :         return res;
      83             :     }
      84             : 
      85           0 :     s->sent_command = 1;
      86           0 :     git_buf_dispose(&request);
      87           0 :     return res;
      88        2276 : }
      89             : 
      90             : int
      91        5784 : P2PStreamRead(git_smart_subtransport_stream* stream, char* buffer, size_t buflen, size_t* read)
      92             : {
      93        5784 :     *read = 0;
      94        5784 :     auto* fs = reinterpret_cast<P2PStream*>(stream);
      95        5784 :     auto sock = fs->socket.lock();
      96        5784 :     if (!sock) {
      97           0 :         giterr_set_str(GITERR_NET, "unavailable socket");
      98           0 :         return -1;
      99             :     }
     100             : 
     101        5784 :     int res = 0;
     102             :     // If it's the first read, we need to send
     103             :     // the upload-pack command
     104        5784 :     if (!fs->sent_command && (res = sendCmd(fs)) < 0)
     105           2 :         return res;
     106             : 
     107        5782 :     std::error_code ec;
     108             :     // TODO ChannelSocket needs a blocking read operation
     109        5782 :     size_t datalen = sock->waitForData(std::chrono::milliseconds(3600 * 1000 * 24), ec);
     110        5782 :     if (datalen > 0)
     111        5782 :         *read = sock->read(reinterpret_cast<unsigned char*>(buffer), std::min(datalen, buflen), ec);
     112             : 
     113        5782 :     return res;
     114        5784 : }
     115             : 
     116             : int
     117        3943 : P2PStreamWrite(git_smart_subtransport_stream* stream, const char* buffer, size_t len)
     118             : {
     119        3943 :     auto* fs = reinterpret_cast<P2PStream*>(stream);
     120        3943 :     auto sock = fs->socket.lock();
     121        3943 :     if (!sock) {
     122           0 :         giterr_set_str(GITERR_NET, "unavailable socket");
     123           0 :         return -1;
     124             :     }
     125        3943 :     std::error_code ec;
     126        3943 :     sock->write(reinterpret_cast<const unsigned char*>(buffer), len, ec);
     127        3943 :     if (ec) {
     128           0 :         giterr_set_str(GITERR_NET, ec.message().c_str());
     129           0 :         return -1;
     130             :     }
     131        3942 :     return 0;
     132        3942 : }
     133             : 
     134             : void
     135        2276 : P2PStreamFree(git_smart_subtransport_stream*)
     136        2276 : {}
     137             : 
     138             : int
     139        6225 : P2PSubTransportAction(git_smart_subtransport_stream** out,
     140             :                       git_smart_subtransport* transport,
     141             :                       const char* url,
     142             :                       git_smart_service_t action)
     143             : {
     144        6225 :     auto* sub = reinterpret_cast<P2PSubTransport*>(transport);
     145        6225 :     if (!sub || !sub->remote) {
     146           0 :         JAMI_ERROR("Invalid subtransport");
     147           0 :         return -1;
     148             :     }
     149             : 
     150        6225 :     auto repo = git_remote_owner(sub->remote);
     151        6225 :     if (!repo) {
     152           0 :         JAMI_ERROR("No repository linked to the transport");
     153           0 :         return -1;
     154             :     }
     155             : 
     156        6225 :     const auto* workdir = git_repository_workdir(repo);
     157        6225 :     if (!workdir) {
     158           0 :         JAMI_ERROR("No working linked to the repository");
     159           0 :         return -1;
     160             :     }
     161        6225 :     std::string_view path = workdir;
     162        6225 :     auto delimConv = path.rfind("/conversations");
     163        6225 :     if (delimConv == std::string::npos) {
     164           0 :         JAMI_ERROR("No conversation id found");
     165           0 :         return -1;
     166             :     }
     167        6225 :     auto delimAccount = path.rfind('/', delimConv - 1);
     168        6225 :     if (delimAccount == std::string::npos && delimConv - 1 - delimAccount == 16) {
     169           0 :         JAMI_ERROR("No account id found");
     170           0 :         return -1;
     171             :     }
     172        6225 :     auto accountId = path.substr(delimAccount + 1, delimConv - 1 - delimAccount);
     173        6225 :     std::string_view gitUrl = url + ("git://"sv).size();
     174        6225 :     auto delim = gitUrl.find('/');
     175        6225 :     if (delim == std::string::npos) {
     176           0 :         JAMI_ERROR("Incorrect url {:s}", gitUrl);
     177           0 :         return -1;
     178             :     }
     179        6225 :     auto deviceId = gitUrl.substr(0, delim);
     180        6225 :     auto conversationId = gitUrl.substr(delim + 1, gitUrl.size());
     181             : 
     182        6225 :     if (action == GIT_SERVICE_UPLOADPACK_LS) {
     183        2282 :         auto gitSocket = jami::Manager::instance().gitSocket(accountId, deviceId, conversationId);
     184        2282 :         if (!gitSocket) {
     185          18 :             JAMI_ERROR("Unable to find related socket for {:s}, {:s}, {:s}",
     186             :                        accountId,
     187             :                        deviceId,
     188             :                        conversationId);
     189           6 :             return -1;
     190             :         }
     191        2276 :         auto stream = std::make_unique<P2PStream>();
     192        2276 :         stream->socket = gitSocket;
     193        2276 :         stream->base.read = P2PStreamRead;
     194        2276 :         stream->base.write = P2PStreamWrite;
     195        2276 :         stream->base.free = P2PStreamFree;
     196        2276 :         stream->cmd = UPLOAD_PACK_CMD;
     197        2276 :         stream->url = gitUrl;
     198        2276 :         sub->stream = std::move(stream);
     199        2276 :         *out = &sub->stream->base;
     200        2276 :         return 0;
     201        6225 :     } else if (action == GIT_SERVICE_UPLOADPACK) {
     202        3943 :         if (sub->stream) {
     203        3943 :             *out = &sub->stream->base;
     204        3943 :             return 0;
     205             :         }
     206           0 :         return -1;
     207             :     }
     208           0 :     return 0;
     209             : }
     210             : 
     211             : int
     212        6838 : P2PSubTransportClose(git_smart_subtransport*)
     213             : {
     214        6838 :     return 0;
     215             : }
     216             : 
     217             : void
     218        2282 : P2PSubTransportFree(git_smart_subtransport* transport)
     219             : {
     220        2282 :     jami::Manager::instance().eraseGitTransport(transport);
     221        2282 : }
     222             : 
     223             : int
     224        2282 : P2PSubTransportNew(P2PSubTransport** out, git_transport*, void* payload)
     225             : {
     226        2282 :     auto sub = std::make_unique<P2PSubTransport>();
     227        2282 :     sub->remote = reinterpret_cast<git_remote*>(payload);
     228        2282 :     auto* base = &sub->base;
     229        2282 :     base->action = P2PSubTransportAction;
     230        2282 :     base->close = P2PSubTransportClose;
     231        2282 :     base->free = P2PSubTransportFree;
     232        2282 :     *out = sub.get();
     233        2282 :     jami::Manager::instance().insertGitTransport(base, std::move(sub));
     234        2282 :     return 0;
     235        2282 : }
     236             : 
     237             : int
     238        2282 : p2p_subtransport_cb(git_smart_subtransport** out, git_transport* owner, void* payload)
     239             : {
     240             :     P2PSubTransport* sub;
     241             : 
     242        2282 :     if (P2PSubTransportNew(&sub, owner, payload) < 0)
     243           0 :         return -1;
     244             : 
     245        2282 :     *out = &sub->base;
     246        2282 :     return 0;
     247             : }
     248             : 
     249             : int
     250        2282 : p2p_transport_cb(git_transport** out, git_remote* owner, void*)
     251             : {
     252        2282 :     git_smart_subtransport_definition def
     253             :         = {p2p_subtransport_cb,
     254             :            0, /* Because we use an already existing channel socket, we use a permanent transport */
     255        2282 :            reinterpret_cast<void*>(owner)};
     256        4564 :     return git_transport_smart(out, owner, &def);
     257             : }

Generated by: LCOV version 1.14