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/>.
22 static void cli_state_handler(struct event_context *event_ctx,
23 struct fd_event *event, uint16 flags, void *p);
26 * Fetch an error out of a NBT packet
27 * @param[in] buf The SMB packet
28 * @retval The error, converted to NTSTATUS
31 NTSTATUS cli_pull_error(char *buf)
33 uint32_t flags2 = SVAL(buf, smb_flg2);
35 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
36 return NT_STATUS(IVAL(buf, smb_rcls));
39 /* if the client uses dos errors, but there is no error,
40 we should return no error here, otherwise it looks
41 like an unknown bad NT_STATUS. jmcd */
42 if (CVAL(buf, smb_rcls) == 0)
45 return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
49 * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
50 * @param[in] cli The client connection that just received an error
51 * @param[in] status The error to set on "cli"
54 void cli_set_error(struct cli_state *cli, NTSTATUS status)
56 uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
58 if (NT_STATUS_IS_DOS(status)) {
59 SSVAL(cli->inbuf, smb_flg2,
60 flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
61 SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
62 SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
66 SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
67 SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
73 * @param[in] cli The client connection
74 * @retval The new, unused mid
77 static uint16_t cli_new_mid(struct cli_state *cli)
80 struct cli_request *req;
88 for (req = cli->outstanding_requests; req; req = req->next) {
89 if (result == req->mid) {
101 * Print an async req that happens to be a cli_request
102 * @param[in] mem_ctx The TALLOC_CTX to put the result on
103 * @param[in] req The request to print
104 * @retval The string representation of "req"
107 static char *cli_request_print(TALLOC_CTX *mem_ctx, struct async_req *req)
109 char *result = async_req_print(mem_ctx, req);
110 struct cli_request *cli_req = talloc_get_type_abort(
111 req->private_data, struct cli_request);
113 if (result == NULL) {
117 return talloc_asprintf_append_buffer(
118 result, "mid=%d\n", cli_req->mid);
122 * Destroy a cli_request
123 * @param[in] req The cli_request to kill
127 static int cli_request_destructor(struct cli_request *req)
129 if (req->enc_state != NULL) {
130 common_free_enc_buffer(req->enc_state, req->outbuf);
132 DLIST_REMOVE(req->cli->outstanding_requests, req);
133 if (req->cli->outstanding_requests == NULL) {
134 TALLOC_FREE(req->cli->fd_event);
140 * Are there already requests waiting in the chain_accumulator?
141 * @param[in] cli The cli_state we want to check
145 bool cli_in_chain(struct cli_state *cli)
147 if (cli->chain_accumulator == NULL) {
151 return (cli->chain_accumulator->num_async != 0);
155 * Is the SMB command able to hold an AND_X successor
156 * @param[in] cmd The SMB command in question
157 * @retval Can we add a chained request after "cmd"?
160 static bool is_andx_req(uint8_t cmd)
181 * @brief Find the smb_cmd offset of the last command pushed
182 * @param[in] buf The buffer we're building up
183 * @retval Where can we put our next andx cmd?
185 * While chaining requests, the "next" request we're looking at needs to put
186 * its SMB_Command before the data the previous request already built up added
187 * to the chain. Find the offset to the place where we have to put our cmd.
190 static bool find_andx_cmd_ofs(char *buf, size_t *pofs)
195 cmd = CVAL(buf, smb_com);
197 SMB_ASSERT(is_andx_req(cmd));
201 while (CVAL(buf, ofs) != 0xff) {
203 if (!is_andx_req(CVAL(buf, ofs))) {
208 * ofs is from start of smb header, so add the 4 length
209 * bytes. The next cmd is right after the wct field.
211 ofs = SVAL(buf, ofs+2) + 4 + 1;
213 SMB_ASSERT(ofs+4 < talloc_get_size(buf));
221 * @brief Do the smb chaining at a buffer level
222 * @param[in] poutbuf Pointer to the talloc'ed buffer to be modified
223 * @param[in] smb_command The command that we want to issue
224 * @param[in] wct How many words?
225 * @param[in] vwv The words, already in network order
226 * @param[in] num_bytes How many bytes?
227 * @param[in] bytes The data the request ships
229 * smb_splice_chain() adds the vwv and bytes to the request already present in
233 bool smb_splice_chain(char **poutbuf, uint8_t smb_command,
234 uint8_t wct, const uint16_t *vwv,
235 uint16_t num_bytes, const uint8_t *bytes)
238 size_t old_size, new_size;
243 old_size = talloc_get_size(*poutbuf);
246 * old_size == smb_wct means we're pushing the first request in for
250 first_request = (old_size == smb_wct);
252 if (!first_request && ((old_size % 4) != 0)) {
254 * Align subsequent requests to a 4-byte boundary
256 padding = 4 - (old_size % 4);
260 * We need space for the wct field, the words, the byte count field
261 * and the bytes themselves.
263 new_size = old_size + padding
264 + 1 + wct * sizeof(uint16_t) + 2 + num_bytes;
266 if (new_size > 0xffff) {
267 DEBUG(1, ("splice_chain: %u bytes won't fit\n",
268 (unsigned)new_size));
272 outbuf = TALLOC_REALLOC_ARRAY(NULL, *poutbuf, char, new_size);
273 if (outbuf == NULL) {
274 DEBUG(0, ("talloc failed\n"));
280 SCVAL(outbuf, smb_com, smb_command);
284 if (!find_andx_cmd_ofs(outbuf, &andx_cmd_ofs)) {
285 DEBUG(1, ("invalid command chain\n"));
286 *poutbuf = TALLOC_REALLOC_ARRAY(
287 NULL, *poutbuf, char, old_size);
292 memset(outbuf + old_size, 0, padding);
296 SCVAL(outbuf, andx_cmd_ofs, smb_command);
297 SSVAL(outbuf, andx_cmd_ofs + 2, old_size - 4);
302 SCVAL(outbuf, ofs, wct);
305 memcpy(outbuf + ofs, vwv, sizeof(uint16_t) * wct);
306 ofs += sizeof(uint16_t) * wct;
308 SSVAL(outbuf, ofs, num_bytes);
309 ofs += sizeof(uint16_t);
311 memcpy(outbuf + ofs, bytes, num_bytes);
317 * @brief Destroy an async_req that is the visible part of a cli_request
318 * @param[in] req The request to kill
319 * @retval Return 0 to make talloc happy
321 * This destructor is a bit tricky: Because a cli_request can host more than
322 * one async_req for chained requests, we need to make sure that the
323 * "cli_request" that we were part of is correctly destroyed at the right
324 * time. This is done by NULLing out ourself from the "async" member of our
325 * "cli_request". If there is none left, then also TALLOC_FREE() the
326 * cli_request, which was a talloc child of the client connection cli_state.
329 static int cli_async_req_destructor(struct async_req *req)
331 struct cli_request *cli_req = talloc_get_type_abort(
332 req->private_data, struct cli_request);
338 for (i=0; i<cli_req->num_async; i++) {
339 if (cli_req->async[i] == req) {
340 cli_req->async[i] = NULL;
343 if (cli_req->async[i] != NULL) {
351 TALLOC_FREE(cli_req);
358 * @brief Chain up a request
359 * @param[in] mem_ctx The TALLOC_CTX for the result
360 * @param[in] ev The event context that will call us back
361 * @param[in] cli The cli_state we queue the request up for
362 * @param[in] smb_command The command that we want to issue
363 * @param[in] additional_flags open_and_x wants to add oplock header flags
364 * @param[in] wct How many words?
365 * @param[in] vwv The words, already in network order
366 * @param[in] num_bytes How many bytes?
367 * @param[in] bytes The data the request ships
369 * cli_request_chain() is the core of the SMB request marshalling routine. It
370 * will create a new async_req structure in the cli->chain_accumulator->async
371 * array and marshall the smb_cmd, the vwv array and the bytes into
372 * cli->chain_accumulator->outbuf.
375 static struct async_req *cli_request_chain(TALLOC_CTX *mem_ctx,
376 struct event_context *ev,
377 struct cli_state *cli,
379 uint8_t additional_flags,
380 uint8_t wct, const uint16_t *vwv,
382 const uint8_t *bytes)
384 struct async_req **tmp_reqs;
385 struct cli_request *req;
387 req = cli->chain_accumulator;
389 tmp_reqs = TALLOC_REALLOC_ARRAY(req, req->async, struct async_req *,
391 if (tmp_reqs == NULL) {
392 DEBUG(0, ("talloc failed\n"));
395 req->async = tmp_reqs;
398 req->async[req->num_async-1] = async_req_new(mem_ctx, ev);
399 if (req->async[req->num_async-1] == NULL) {
400 DEBUG(0, ("async_req_new failed\n"));
404 req->async[req->num_async-1]->private_data = req;
405 req->async[req->num_async-1]->print = cli_request_print;
406 talloc_set_destructor(req->async[req->num_async-1],
407 cli_async_req_destructor);
409 if (!smb_splice_chain(&req->outbuf, smb_command, wct, vwv,
414 return req->async[req->num_async-1];
417 TALLOC_FREE(req->async[req->num_async-1]);
423 * @brief prepare a cli_state to accept a chain of requests
424 * @param[in] cli The cli_state we want to queue up in
425 * @param[in] ev The event_context that will call us back for the socket
426 * @param[in] size_hint How many bytes are expected, just an optimization
427 * @retval Did we have enough memory?
429 * cli_chain_cork() sets up a new cli_request in cli->chain_accumulator. If
430 * cli is used in an async fashion, i.e. if we have outstanding requests, then
431 * we do not have to create a fd event. If cli is used only with the sync
432 * helpers, we need to create the fd_event here.
434 * If you want to issue a chained request to the server, do a
435 * cli_chain_cork(), then do you cli_open_send(), cli_read_and_x_send(),
436 * cli_close_send() and so on. The async requests that come out of
437 * cli_xxx_send() are normal async requests with the difference that they
438 * won't be shipped individually. But the event_context will still trigger the
439 * req->async.fn to be called on every single request.
441 * You have to take care yourself that you only issue chainable requests in
442 * the middle of the chain.
445 bool cli_chain_cork(struct cli_state *cli, struct event_context *ev,
448 struct cli_request *req = NULL;
450 SMB_ASSERT(cli->chain_accumulator == NULL);
453 DEBUG(10, ("cli->fd closed\n"));
457 if (cli->fd_event == NULL) {
458 SMB_ASSERT(cli->outstanding_requests == NULL);
459 cli->fd_event = event_add_fd(ev, cli, cli->fd,
461 cli_state_handler, cli);
462 if (cli->fd_event == NULL) {
467 req = talloc(cli, struct cli_request);
473 if (size_hint == 0) {
476 req->outbuf = talloc_array(req, char, smb_wct + size_hint);
477 if (req->outbuf == NULL) {
480 req->outbuf = TALLOC_REALLOC_ARRAY(NULL, req->outbuf, char, smb_wct);
485 req->enc_state = NULL;
486 req->recv_helper.fn = NULL;
488 SSVAL(req->outbuf, smb_tid, cli->cnum);
489 cli_setup_packet_buf(cli, req->outbuf);
491 req->mid = cli_new_mid(cli);
493 cli->chain_accumulator = req;
495 DEBUG(10, ("cli_chain_cork: mid=%d\n", req->mid));
500 if (cli->outstanding_requests == NULL) {
501 TALLOC_FREE(cli->fd_event);
507 * Ship a request queued up via cli_request_chain()
508 * @param[in] cl The connection
511 void cli_chain_uncork(struct cli_state *cli)
513 struct cli_request *req = cli->chain_accumulator;
515 SMB_ASSERT(req != NULL);
517 DLIST_ADD_END(cli->outstanding_requests, req, struct cli_request *);
518 talloc_set_destructor(req, cli_request_destructor);
520 cli->chain_accumulator = NULL;
522 SSVAL(req->outbuf, smb_mid, req->mid);
523 smb_setlen(req->outbuf, talloc_get_size(req->outbuf) - 4);
525 cli_calculate_sign_mac(cli, req->outbuf);
527 if (cli_encryption_on(cli)) {
531 status = cli_encrypt_message(cli, req->outbuf, &enc_buf);
532 if (!NT_STATUS_IS_OK(status)) {
533 DEBUG(0, ("Error in encrypting client message. "
534 "Error %s\n", nt_errstr(status)));
538 req->outbuf = enc_buf;
539 req->enc_state = cli->trans_enc_state;
544 event_fd_set_writeable(cli->fd_event);
548 * @brief Send a request to the server
549 * @param[in] mem_ctx The TALLOC_CTX for the result
550 * @param[in] ev The event context that will call us back
551 * @param[in] cli The cli_state we queue the request up for
552 * @param[in] smb_command The command that we want to issue
553 * @param[in] additional_flags open_and_x wants to add oplock header flags
554 * @param[in] wct How many words?
555 * @param[in] vwv The words, already in network order
556 * @param[in] num_bytes How many bytes?
557 * @param[in] bytes The data the request ships
559 * This is the generic routine to be used by the cli_xxx_send routines.
562 struct async_req *cli_request_send(TALLOC_CTX *mem_ctx,
563 struct event_context *ev,
564 struct cli_state *cli,
566 uint8_t additional_flags,
567 uint8_t wct, const uint16_t *vwv,
568 uint16_t num_bytes, const uint8_t *bytes)
570 struct async_req *result;
573 if (cli->chain_accumulator == NULL) {
574 if (!cli_chain_cork(cli, ev,
575 wct * sizeof(uint16_t) + num_bytes + 3)) {
576 DEBUG(1, ("cli_chain_cork failed\n"));
582 result = cli_request_chain(mem_ctx, ev, cli, smb_command,
583 additional_flags, wct, vwv,
586 if (result == NULL) {
587 DEBUG(1, ("cli_request_chain failed\n"));
591 cli_chain_uncork(cli);
598 * Figure out if there is an andx command behind the current one
599 * @param[in] buf The smb buffer to look at
600 * @param[in] ofs The offset to the wct field that is followed by the cmd
601 * @retval Is there a command following?
604 static bool have_andx_command(const char *buf, uint16_t ofs)
607 size_t buflen = talloc_get_size(buf);
609 if ((ofs == buflen-1) || (ofs == buflen)) {
613 wct = CVAL(buf, ofs);
616 * Not enough space for the command and a following pointer
620 return (CVAL(buf, ofs+1) != 0xff);
624 * @brief Pull reply data out of a request
625 * @param[in] req The request that we just received a reply for
626 * @param[out] pwct How many words did the server send?
627 * @param[out] pvwv The words themselves
628 * @param[out] pnum_bytes How many bytes did the server send?
629 * @param[out] pbytes The bytes themselves
630 * @retval Was the reply formally correct?
633 NTSTATUS cli_pull_reply(struct async_req *req,
634 uint8_t *pwct, uint16_t **pvwv,
635 uint16_t *pnum_bytes, uint8_t **pbytes)
637 struct cli_request *cli_req = talloc_get_type_abort(
638 req->private_data, struct cli_request);
641 size_t wct_ofs, bytes_offset;
645 for (i = 0; i < cli_req->num_async; i++) {
646 if (req == cli_req->async[i]) {
651 if (i == cli_req->num_async) {
652 cli_set_error(cli_req->cli, NT_STATUS_INVALID_PARAMETER);
653 return NT_STATUS_INVALID_PARAMETER;
657 * The status we pull here is only relevant for the last reply in the
661 status = cli_pull_error(cli_req->inbuf);
664 if (NT_STATUS_IS_ERR(status)
665 && !have_andx_command(cli_req->inbuf, smb_wct)) {
666 cli_set_error(cli_req->cli, status);
673 cmd = CVAL(cli_req->inbuf, smb_com);
676 for (j = 0; j < i; j++) {
679 return NT_STATUS_REQUEST_ABORTED;
681 if (!is_andx_req(cmd)) {
682 return NT_STATUS_INVALID_NETWORK_RESPONSE;
686 if (!have_andx_command(cli_req->inbuf, wct_ofs)) {
688 * This request was not completed because a previous
689 * request in the chain had received an error.
691 return NT_STATUS_REQUEST_ABORTED;
694 wct_ofs = SVAL(cli_req->inbuf, wct_ofs + 3);
697 * Skip the all-present length field. No overflow, we've just
698 * put a 16-bit value into a size_t.
702 if (wct_ofs+2 > talloc_get_size(cli_req->inbuf)) {
703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
706 cmd = CVAL(cli_req->inbuf, wct_ofs + 1);
709 if (!have_andx_command(cli_req->inbuf, wct_ofs)
710 && NT_STATUS_IS_ERR(status)) {
712 * The last command takes the error code. All further commands
713 * down the requested chain will get a
714 * NT_STATUS_REQUEST_ABORTED.
720 wct = CVAL(cli_req->inbuf, wct_ofs);
722 bytes_offset = wct_ofs + 1 + wct * sizeof(uint16_t);
723 num_bytes = SVAL(cli_req->inbuf, bytes_offset);
726 * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
727 * is a 16-bit value. So bytes_offset being size_t should be far from
731 if ((bytes_offset + 2 > talloc_get_size(cli_req->inbuf))
732 || (bytes_offset > 0xffff)) {
733 return NT_STATUS_INVALID_NETWORK_RESPONSE;
737 *pvwv = (uint16_t *)(cli_req->inbuf + wct_ofs + 1);
738 *pnum_bytes = num_bytes;
739 *pbytes = (uint8_t *)cli_req->inbuf + bytes_offset + 2;
745 * Decrypt a PDU, check the signature
746 * @param[in] cli The cli_state that received something
747 * @param[in] pdu The incoming bytes
752 static NTSTATUS validate_smb_crypto(struct cli_state *cli, char *pdu)
756 if ((IVAL(pdu, 4) != 0x424d53ff) /* 0xFF"SMB" */
757 && (SVAL(pdu, 4) != 0x45ff)) /* 0xFF"E" */ {
758 DEBUG(10, ("Got non-SMB PDU\n"));
759 return NT_STATUS_INVALID_NETWORK_RESPONSE;
762 if (cli_encryption_on(cli) && CVAL(pdu, 0) == 0) {
763 uint16_t enc_ctx_num;
765 status = get_enc_ctx_num((uint8_t *)pdu, &enc_ctx_num);
766 if (!NT_STATUS_IS_OK(status)) {
767 DEBUG(10, ("get_enc_ctx_num returned %s\n",
772 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
773 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
775 cli->trans_enc_state->enc_ctx_num));
776 return NT_STATUS_INVALID_HANDLE;
779 status = common_decrypt_buffer(cli->trans_enc_state, pdu);
780 if (!NT_STATUS_IS_OK(status)) {
781 DEBUG(10, ("common_decrypt_buffer returned %s\n",
787 if (!cli_check_sign_mac(cli, pdu)) {
788 DEBUG(10, ("cli_check_sign_mac failed\n"));
789 return NT_STATUS_ACCESS_DENIED;
796 * A PDU has arrived on cli->evt_inbuf
797 * @param[in] cli The cli_state that received something
800 static void handle_incoming_pdu(struct cli_state *cli)
802 struct cli_request *req;
804 size_t raw_pdu_len, buf_len, pdu_len, rest_len;
812 * The encrypted PDU len might differ from the unencrypted one
814 raw_pdu_len = smb_len(cli->evt_inbuf) + 4;
815 buf_len = talloc_get_size(cli->evt_inbuf);
816 rest_len = buf_len - raw_pdu_len;
818 if (buf_len == raw_pdu_len) {
820 * Optimal case: Exactly one PDU was in the socket buffer
822 pdu = cli->evt_inbuf;
823 cli->evt_inbuf = NULL;
826 DEBUG(11, ("buf_len = %d, raw_pdu_len = %d, splitting "
827 "buffer\n", (int)buf_len, (int)raw_pdu_len));
829 if (raw_pdu_len < rest_len) {
831 * The PDU is shorter, talloc_memdup that one.
833 pdu = (char *)talloc_memdup(
834 cli, cli->evt_inbuf, raw_pdu_len);
836 memmove(cli->evt_inbuf, cli->evt_inbuf + raw_pdu_len,
837 buf_len - raw_pdu_len);
839 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(
840 NULL, cli->evt_inbuf, char, rest_len);
843 status = NT_STATUS_NO_MEMORY;
844 goto invalidate_requests;
849 * The PDU is larger than the rest, talloc_memdup the
852 pdu = cli->evt_inbuf;
854 cli->evt_inbuf = (char *)talloc_memdup(
855 cli, pdu + raw_pdu_len, rest_len);
857 if (cli->evt_inbuf == NULL) {
858 status = NT_STATUS_NO_MEMORY;
859 goto invalidate_requests;
864 status = validate_smb_crypto(cli, pdu);
865 if (!NT_STATUS_IS_OK(status)) {
866 goto invalidate_requests;
869 mid = SVAL(pdu, smb_mid);
871 DEBUG(10, ("handle_incoming_pdu: got mid %d\n", mid));
873 for (req = cli->outstanding_requests; req; req = req->next) {
874 if (req->mid == mid) {
879 pdu_len = smb_len(pdu) + 4;
882 DEBUG(3, ("Request for mid %d not found, dumping PDU\n", mid));
888 req->inbuf = talloc_move(req, &pdu);
891 * Freeing the last async_req will free the req (see
892 * cli_async_req_destructor). So make a copy of req->num_async, we
893 * can't reference it in the last round.
896 num_async = req->num_async;
898 for (i=0; i<num_async; i++) {
900 * A request might have been talloc_free()'ed before we arrive
901 * here. It will have removed itself from req->async via its
902 * destructor cli_async_req_destructor().
904 if (req->async[i] != NULL) {
905 if (req->recv_helper.fn != NULL) {
906 req->recv_helper.fn(req->async[i]);
908 async_req_done(req->async[i]);
916 DEBUG(10, ("handle_incoming_pdu: Aborting with %s\n",
919 for (req = cli->outstanding_requests; req; req = req->next) {
920 async_req_error(req->async[0], status);
926 * fd event callback. This is the basic connection to the socket
927 * @param[in] event_ctx The event context that called us
928 * @param[in] event The event that fired
929 * @param[in] flags EVENT_FD_READ | EVENT_FD_WRITE
930 * @param[in] p private_data, in this case the cli_state
933 static void cli_state_handler(struct event_context *event_ctx,
934 struct fd_event *event, uint16 flags, void *p)
936 struct cli_state *cli = (struct cli_state *)p;
937 struct cli_request *req;
940 DEBUG(11, ("cli_state_handler called with flags %d\n", flags));
942 if (flags & EVENT_FD_READ) {
944 size_t old_size, new_size;
947 res = ioctl(cli->fd, FIONREAD, &available);
949 DEBUG(10, ("ioctl(FIONREAD) failed: %s\n",
951 status = map_nt_error_from_unix(errno);
955 if (available == 0) {
957 status = NT_STATUS_END_OF_FILE;
961 old_size = talloc_get_size(cli->evt_inbuf);
962 new_size = old_size + available;
964 if (new_size < old_size) {
966 status = NT_STATUS_UNEXPECTED_IO_ERROR;
970 tmp = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf, char,
974 status = NT_STATUS_NO_MEMORY;
977 cli->evt_inbuf = tmp;
979 res = sys_recv(cli->fd, cli->evt_inbuf + old_size, available, 0);
981 DEBUG(10, ("recv failed: %s\n", strerror(errno)));
982 status = map_nt_error_from_unix(errno);
986 DEBUG(11, ("cli_state_handler: received %d bytes, "
987 "smb_len(evt_inbuf) = %d\n", (int)res,
988 smb_len(cli->evt_inbuf)));
990 /* recv *might* have returned less than announced */
991 new_size = old_size + res;
993 /* shrink, so I don't expect errors here */
994 cli->evt_inbuf = TALLOC_REALLOC_ARRAY(cli, cli->evt_inbuf,
997 while ((cli->evt_inbuf != NULL)
998 && ((smb_len(cli->evt_inbuf) + 4) <= new_size)) {
1000 * we've got a complete NBT level PDU in evt_inbuf
1002 handle_incoming_pdu(cli);
1003 new_size = talloc_get_size(cli->evt_inbuf);
1007 if (flags & EVENT_FD_WRITE) {
1011 for (req = cli->outstanding_requests; req; req = req->next) {
1012 to_send = smb_len(req->outbuf)+4;
1013 if (to_send > req->sent) {
1019 if (cli->fd_event != NULL) {
1020 event_fd_set_not_writeable(cli->fd_event);
1025 sent = sys_send(cli->fd, req->outbuf + req->sent,
1026 to_send - req->sent, 0);
1029 status = map_nt_error_from_unix(errno);
1035 if (req->sent == to_send) {
1042 for (req = cli->outstanding_requests; req; req = req->next) {
1044 for (i=0; i<req->num_async; i++) {
1045 async_req_error(req->async[i], status);
1048 TALLOC_FREE(cli->fd_event);