2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
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.
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.
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/>.
21 #include "system/network.h"
22 #include "lib/async_req/async_sock.h"
24 #include "lib/util/tevent_unix.h"
25 #include "libcli/smb/smb_constants.h"
28 * Read an smb packet asynchronously, discard keepalives
31 struct read_smb_state
{
32 struct tevent_context
*ev
;
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
);
40 struct tevent_req
*read_smb_send(TALLOC_CTX
*mem_ctx
,
41 struct tevent_context
*ev
,
44 struct tevent_req
*result
, *subreq
;
45 struct read_smb_state
*state
;
47 result
= tevent_req_create(mem_ctx
, &state
, struct read_smb_state
);
54 subreq
= read_packet_send(state
, ev
, fd
, 4, read_smb_more
, NULL
);
58 tevent_req_set_callback(subreq
, read_smb_done
, result
);
65 static ssize_t
read_smb_more(uint8_t *buf
, size_t buflen
, void *private_data
)
68 return 0; /* We've been here, we're done */
70 return smb_len_tcp(buf
);
73 static void read_smb_done(struct tevent_req
*subreq
)
75 struct tevent_req
*req
= tevent_req_callback_data(
76 subreq
, struct tevent_req
);
77 struct read_smb_state
*state
= tevent_req_data(
78 req
, struct read_smb_state
);
82 len
= read_packet_recv(subreq
, state
, &state
->buf
, &err
);
85 tevent_req_error(req
, err
);
89 if (CVAL(state
->buf
, 0) == NBSSkeepalive
) {
90 subreq
= read_packet_send(state
, state
->ev
, state
->fd
, 4,
92 if (tevent_req_nomem(subreq
, req
)) {
95 tevent_req_set_callback(subreq
, read_smb_done
, req
);
101 ssize_t
read_smb_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
102 uint8_t **pbuf
, int *perrno
)
104 struct read_smb_state
*state
= tevent_req_data(
105 req
, struct read_smb_state
);
107 if (tevent_req_is_unix_error(req
, perrno
)) {
110 *pbuf
= talloc_move(mem_ctx
, &state
->buf
);
111 return talloc_get_size(*pbuf
);