1 /* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
3 Copyright (C) 2016 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
46 #include "libsmb2-private.h"
49 smb2_encode_read_request(struct smb2_context *smb2,
51 struct smb2_read_request *req)
55 struct smb2_iovec *iov;
57 len = SMB2_READ_REQUEST_SIZE & 0xfffffffe;
58 buf = calloc(len, sizeof(uint8_t));
60 smb2_set_error(smb2, "Failed to allocate read buffer");
64 iov = smb2_add_iovector(smb2, &pdu->out, buf, len, free);
66 if (!smb2->supports_multi_credit && req->length > 64 * 1024) {
67 req->length = 64 * 1024;
68 req->minimum_count = 0;
70 smb2_set_uint16(iov, 0, SMB2_READ_REQUEST_SIZE);
71 smb2_set_uint8(iov, 3, req->flags);
72 smb2_set_uint32(iov, 4, req->length);
73 smb2_set_uint64(iov, 8, req->offset);
74 memcpy(iov->buf + 16, req->file_id, SMB2_FD_SIZE);
75 smb2_set_uint32(iov, 32, req->minimum_count);
76 smb2_set_uint32(iov, 36, req->channel);
77 smb2_set_uint32(iov, 40, req->remaining_bytes);
78 smb2_set_uint16(iov, 46, req->read_channel_info_length);
80 if (req->read_channel_info_length > 0 ||
81 req->read_channel_info != NULL) {
82 smb2_set_error(smb2, "ChannelInfo not yet implemented");
86 /* The buffer must contain at least one byte, even if we do not
87 * have any read channel info.
89 if (req->read_channel_info == NULL) {
92 smb2_add_iovector(smb2, &pdu->out, &zero, 1, NULL);
99 smb2_cmd_read_async(struct smb2_context *smb2,
100 struct smb2_read_request *req,
101 smb2_command_cb cb, void *cb_data)
103 struct smb2_pdu *pdu;
105 pdu = smb2_allocate_pdu(smb2, SMB2_READ, cb, cb_data);
110 if (smb2_encode_read_request(smb2, pdu, req)) {
111 smb2_free_pdu(smb2, pdu);
115 /* Add a vector for the buffer that the application gave us */
116 smb2_add_iovector(smb2, &pdu->in, req->buf,
119 if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) {
120 smb2_free_pdu(smb2, pdu);
124 /* Adjust credit charge for large payloads */
125 if (smb2->supports_multi_credit) {
126 pdu->header.credit_charge = (req->length - 1) / 65536 + 1; // 3.1.5.2 of [MS-SMB2]
133 smb2_process_read_fixed(struct smb2_context *smb2,
134 struct smb2_pdu *pdu)
136 struct smb2_read_reply *rep;
137 struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1];
138 uint16_t struct_size;
140 rep = malloc(sizeof(*rep));
142 smb2_set_error(smb2, "Failed to allocate read reply");
147 smb2_get_uint16(iov, 0, &struct_size);
148 if (struct_size > SMB2_READ_REPLY_SIZE) {
149 smb2_set_error(smb2, "Unexpected size of Read "
150 "reply. Expected %d, got %d",
151 SMB2_READ_REPLY_SIZE,
156 smb2_get_uint8(iov, 2, &rep->data_offset);
157 smb2_get_uint32(iov, 4, &rep->data_length);
158 smb2_get_uint32(iov, 8, &rep->data_remaining);
160 if (rep->data_length == 0) {
164 if (rep->data_offset != SMB2_HEADER_SIZE + 16) {
165 smb2_set_error(smb2, "Unexpected data offset in Read reply. "
166 "Expected %d, got %d",
167 16, rep->data_offset);
171 return rep->data_length;