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_write_request(struct smb2_context *smb2,
51 struct smb2_write_request *req)
55 struct smb2_iovec *iov;
57 len = SMB2_WRITE_REQUEST_SIZE & 0xfffffffe;
58 buf = calloc(len, sizeof(uint8_t));
60 smb2_set_error(smb2, "Failed to allocate write 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;
69 smb2_set_uint16(iov, 0, SMB2_WRITE_REQUEST_SIZE);
70 smb2_set_uint16(iov, 2, SMB2_HEADER_SIZE + 48);
71 smb2_set_uint32(iov, 4, req->length);
72 smb2_set_uint64(iov, 8, req->offset);
73 memcpy(iov->buf + 16, req->file_id, SMB2_FD_SIZE);
74 smb2_set_uint32(iov, 32, req->channel);
75 smb2_set_uint32(iov, 36, req->remaining_bytes);
76 smb2_set_uint16(iov, 42, req->write_channel_info_length);
77 smb2_set_uint32(iov, 44, req->flags);
79 if (req->write_channel_info_length > 0 ||
80 req->write_channel_info != NULL) {
81 smb2_set_error(smb2, "ChannelInfo not yet implemented");
89 smb2_cmd_write_async(struct smb2_context *smb2,
90 struct smb2_write_request *req,
91 smb2_command_cb cb, void *cb_data)
95 pdu = smb2_allocate_pdu(smb2, SMB2_WRITE, cb, cb_data);
100 if (smb2_encode_write_request(smb2, pdu, req)) {
101 smb2_free_pdu(smb2, pdu);
105 smb2_add_iovector(smb2, &pdu->out, req->buf,
108 if (smb2_pad_to_64bit(smb2, &pdu->out) != 0) {
109 smb2_free_pdu(smb2, pdu);
113 /* Adjust credit charge for large payloads */
114 if (smb2->supports_multi_credit) {
115 pdu->header.credit_charge = (req->length - 1) / 65536 + 1; // 3.1.5.2 of [MS-SMB2]
122 smb2_process_write_fixed(struct smb2_context *smb2,
123 struct smb2_pdu *pdu)
125 struct smb2_write_reply *rep;
126 struct smb2_iovec *iov = &smb2->in.iov[smb2->in.niov - 1];
127 uint16_t struct_size;
129 rep = malloc(sizeof(*rep));
131 smb2_set_error(smb2, "Failed to allocate write reply");
136 smb2_get_uint16(iov, 0, &struct_size);
137 if (struct_size != SMB2_WRITE_REPLY_SIZE ||
138 (struct_size & 0xfffe) != iov->len) {
139 smb2_set_error(smb2, "Unexpected size of Write "
140 "reply. Expected %d, got %d",
141 SMB2_WRITE_REPLY_SIZE,
146 smb2_get_uint32(iov, 4, &rep->count);
147 smb2_get_uint32(iov, 8, &rep->remaining);