nop.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include "iscsi.h"
  16. #include "iscsi-private.h"
  17. int iscsi_nop_out_async(struct iscsi_context *iscsi, iscsi_command_cb cb, unsigned char *data, int len, void *private_data)
  18. {
  19. struct iscsi_pdu *pdu;
  20. if (iscsi == NULL) {
  21. printf("trying to send nop-out on NULL context\n");
  22. return -1;
  23. }
  24. if (iscsi->is_loggedin == 0) {
  25. printf("trying send nop-out while not logged in\n");
  26. return -2;
  27. }
  28. pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_NOP_OUT, ISCSI_PDU_NOP_IN);
  29. if (pdu == NULL) {
  30. printf("Failed to allocate nop-out pdu\n");
  31. return -3;
  32. }
  33. /* immediate flag */
  34. iscsi_pdu_set_immediate(pdu);
  35. /* flags */
  36. iscsi_pdu_set_pduflags(pdu, 0x80);
  37. /* ttt */
  38. iscsi_pdu_set_ttt(pdu, 0xffffffff);
  39. /* lun */
  40. iscsi_pdu_set_lun(pdu, 2);
  41. /* cmdsn is not increased if Immediate delivery*/
  42. iscsi_pdu_set_cmdsn(pdu, iscsi->cmdsn);
  43. pdu->cmdsn = iscsi->cmdsn;
  44. // iscsi->cmdsn++;
  45. pdu->callback = cb;
  46. pdu->private_data = private_data;
  47. if (iscsi_pdu_add_data(iscsi, pdu, data, len) != 0) {
  48. printf("Failed to add outdata to nop-out\n");
  49. iscsi_free_pdu(iscsi, pdu);
  50. return -4;
  51. }
  52. if (iscsi_queue_pdu(iscsi, pdu) != 0) {
  53. printf("failed to queue iscsi nop-out pdu\n");
  54. iscsi_free_pdu(iscsi, pdu);
  55. return -5;
  56. }
  57. return 0;
  58. }
  59. int iscsi_process_nop_out_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
  60. {
  61. struct iscsi_data data;
  62. data.data = NULL;
  63. data.size = 0;
  64. if (size > ISCSI_HEADER_SIZE) {
  65. data.data = discard_const(&hdr[ISCSI_HEADER_SIZE]);
  66. data.size = size - ISCSI_HEADER_SIZE;
  67. }
  68. pdu->callback(iscsi, ISCSI_STATUS_GOOD, &data, pdu->private_data);
  69. return 0;
  70. }