LCOV - code coverage report
Current view: top level - libcli/smb - read_smb.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 35 45 77.8 %
Date: 2024-02-29 22:57:05 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Infrastructure for async SMB client requests
       4             :    Copyright (C) Volker Lendecke 2008
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "system/network.h"
      22             : #include "lib/async_req/async_sock.h"
      23             : #include "read_smb.h"
      24             : #include "lib/util/tevent_unix.h"
      25             : #include "libcli/smb/smb_constants.h"
      26             : 
      27             : /*
      28             :  * Read an smb packet asynchronously, discard keepalives
      29             :  */
      30             : 
      31             : struct read_smb_state {
      32             :         struct tevent_context *ev;
      33             :         int fd;
      34             :         uint8_t *buf;
      35             : };
      36             : 
      37             : static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data);
      38             : static void read_smb_done(struct tevent_req *subreq);
      39             : 
      40     3028108 : struct tevent_req *read_smb_send(TALLOC_CTX *mem_ctx,
      41             :                                  struct tevent_context *ev,
      42             :                                  int fd)
      43             : {
      44       23782 :         struct tevent_req *result, *subreq;
      45       23782 :         struct read_smb_state *state;
      46             : 
      47     3028108 :         result = tevent_req_create(mem_ctx, &state, struct read_smb_state);
      48     3028108 :         if (result == NULL) {
      49           0 :                 return NULL;
      50             :         }
      51     3028108 :         state->ev = ev;
      52     3028108 :         state->fd = fd;
      53             : 
      54     3028108 :         subreq = read_packet_send(state, ev, fd, 4, read_smb_more, NULL);
      55     3028108 :         if (subreq == NULL) {
      56           0 :                 goto fail;
      57             :         }
      58     3028108 :         tevent_req_set_callback(subreq, read_smb_done, result);
      59     3028108 :         return result;
      60           0 :  fail:
      61           0 :         TALLOC_FREE(result);
      62           0 :         return NULL;
      63             : }
      64             : 
      65     5863130 : static ssize_t read_smb_more(uint8_t *buf, size_t buflen, void *private_data)
      66             : {
      67     5863130 :         if (buflen > 4) {
      68     2908915 :                 return 0;       /* We've been here, we're done */
      69             :         }
      70     2932105 :         return smb_len_tcp(buf);
      71             : }
      72             : 
      73     2932343 : static void read_smb_done(struct tevent_req *subreq)
      74             : {
      75     2932343 :         struct tevent_req *req = tevent_req_callback_data(
      76             :                 subreq, struct tevent_req);
      77     2932343 :         struct read_smb_state *state = tevent_req_data(
      78             :                 req, struct read_smb_state);
      79       22110 :         ssize_t len;
      80       22110 :         int err;
      81             : 
      82     2932343 :         len = read_packet_recv(subreq, state, &state->buf, &err);
      83     2932343 :         TALLOC_FREE(subreq);
      84     2932343 :         if (len == -1) {
      85         242 :                 tevent_req_error(req, err);
      86         242 :                 return;
      87             :         }
      88             : 
      89     2932101 :         if (CVAL(state->buf, 0) == NBSSkeepalive) {
      90           0 :                 subreq = read_packet_send(state, state->ev, state->fd, 4,
      91             :                                           read_smb_more, NULL);
      92           0 :                 if (tevent_req_nomem(subreq, req)) {
      93           0 :                         return;
      94             :                 }
      95           0 :                 tevent_req_set_callback(subreq, read_smb_done, req);
      96           0 :                 return;
      97             :         }
      98     2932101 :         tevent_req_done(req);
      99             : }
     100             : 
     101     2932343 : ssize_t read_smb_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
     102             :                       uint8_t **pbuf, int *perrno)
     103             : {
     104     2932343 :         struct read_smb_state *state = tevent_req_data(
     105             :                 req, struct read_smb_state);
     106             : 
     107     2932343 :         if (tevent_req_is_unix_error(req, perrno)) {
     108         242 :                 tevent_req_received(req);
     109         242 :                 return -1;
     110             :         }
     111     2932101 :         *pbuf = talloc_move(mem_ctx, &state->buf);
     112     2932101 :         tevent_req_received(req);
     113     2932101 :         return talloc_get_size(*pbuf);
     114             : }

Generated by: LCOV version 1.14