init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <stdlib.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include <stdlib.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <time.h>
  22. #include "iscsi.h"
  23. #include "iscsi-private.h"
  24. #include "dlinklist.h"
  25. struct iscsi_context *iscsi_create_context(const char *initiator_name)
  26. {
  27. struct iscsi_context *iscsi;
  28. iscsi = malloc(sizeof(struct iscsi_context));
  29. if (iscsi == NULL) {
  30. printf("Failed to allocate iscsi context\n");
  31. return NULL;
  32. }
  33. bzero(iscsi, sizeof(struct iscsi_context));
  34. iscsi->initiator_name = strdup(initiator_name);
  35. if (iscsi->initiator_name == NULL) {
  36. printf("Failed to allocate initiator name context\n");
  37. free(iscsi);
  38. return NULL;
  39. }
  40. iscsi->fd = -1;
  41. /* use a "random" isid */
  42. srandom(getpid() ^ time(NULL));
  43. iscsi_set_random_isid(iscsi);
  44. return iscsi;
  45. }
  46. int iscsi_set_random_isid(struct iscsi_context *iscsi)
  47. {
  48. iscsi->isid[0] = 0x80;
  49. iscsi->isid[1] = random()&0xff;
  50. iscsi->isid[2] = random()&0xff;
  51. iscsi->isid[3] = random()&0xff;
  52. iscsi->isid[4] = 0;
  53. iscsi->isid[5] = 0;
  54. return 0;
  55. }
  56. int iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
  57. {
  58. if (iscsi == NULL) {
  59. printf("Context is NULL when adding alias\n");
  60. return -1;
  61. }
  62. if (iscsi->is_loggedin != 0) {
  63. printf("Already logged in when adding alias\n");
  64. return -2;
  65. }
  66. if (iscsi->alias != NULL) {
  67. free(discard_const(iscsi->alias));
  68. iscsi->alias = NULL;
  69. }
  70. iscsi->alias = strdup(alias);
  71. if (iscsi->alias == NULL) {
  72. printf("Failed to allocate alias name\n");
  73. return -3;
  74. }
  75. return 0;
  76. }
  77. int iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
  78. {
  79. if (iscsi == NULL) {
  80. printf("Context is NULL when adding targetname\n");
  81. return -1;
  82. }
  83. if (iscsi->is_loggedin != 0) {
  84. printf("Already logged in when adding targetname\n");
  85. return -2;
  86. }
  87. if (iscsi->target_name != NULL) {
  88. free(discard_const(iscsi->target_name));
  89. iscsi->target_name= NULL;
  90. }
  91. iscsi->target_name = strdup(target_name);
  92. if (iscsi->target_name == NULL) {
  93. printf("Failed to allocate target name\n");
  94. return -3;
  95. }
  96. return 0;
  97. }
  98. int iscsi_destroy_context(struct iscsi_context *iscsi)
  99. {
  100. struct iscsi_pdu *pdu;
  101. if (iscsi == NULL) {
  102. return 0;
  103. }
  104. if (iscsi->initiator_name != NULL) {
  105. free(discard_const(iscsi->initiator_name));
  106. iscsi->initiator_name = NULL;
  107. }
  108. if (iscsi->alias != NULL) {
  109. free(discard_const(iscsi->alias));
  110. iscsi->alias = NULL;
  111. }
  112. if (iscsi->is_loggedin != 0) {
  113. printf("deswtroying context while logged in\n");
  114. }
  115. if (iscsi->fd != -1) {
  116. iscsi_disconnect(iscsi);
  117. }
  118. if (iscsi->inbuf != NULL) {
  119. free(iscsi->inbuf);
  120. iscsi->inbuf = NULL;
  121. iscsi->insize = 0;
  122. iscsi->inpos = 0;
  123. }
  124. while ((pdu = iscsi->outqueue)) {
  125. DLIST_REMOVE(iscsi->outqueue, pdu);
  126. pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
  127. iscsi_free_pdu(iscsi, pdu);
  128. }
  129. while ((pdu = iscsi->waitpdu)) {
  130. DLIST_REMOVE(iscsi->waitpdu, pdu);
  131. pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
  132. iscsi_free_pdu(iscsi, pdu);
  133. }
  134. free(iscsi);
  135. return 0;
  136. }