socket.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <errno.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <netinet/in.h>
  22. #include <poll.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <arpa/inet.h>
  27. #include "iscsi.h"
  28. #include "iscsi-private.h"
  29. #include "dlinklist.h"
  30. static void set_nonblocking(int fd)
  31. {
  32. unsigned v;
  33. v = fcntl(fd, F_GETFL, 0);
  34. fcntl(fd, F_SETFL, v | O_NONBLOCK);
  35. }
  36. int iscsi_connect_async(struct iscsi_context *iscsi, const char *target, iscsi_command_cb cb, void *private_data)
  37. {
  38. int tpgt = -1;
  39. int port = 3260;
  40. char *str;
  41. char *addr;
  42. union {
  43. struct sockaddr sa;
  44. struct sockaddr_storage ss;
  45. struct sockaddr_in sin;
  46. } s;
  47. int socksize;
  48. if (iscsi == NULL) {
  49. printf("Trying to connect NULL context\n");
  50. return -1;
  51. }
  52. if (iscsi->fd != -1) {
  53. printf("Trying to connect but already connected\n");
  54. return -2;
  55. }
  56. addr = strdup(target);
  57. if (addr == NULL) {
  58. printf("failed to strdup target address\n");
  59. return -3;
  60. }
  61. /* check if we have a target portal group tag */
  62. if ((str = rindex(addr, ',')) != NULL) {
  63. tpgt = atoi(str+1);
  64. str[0] = 0;
  65. }
  66. /* XXX need handling for {ipv6 addresses} */
  67. /* for now, assume all is ipv4 */
  68. if ((str = rindex(addr, ':')) != NULL) {
  69. port = atoi(str+1);
  70. str[0] = 0;
  71. }
  72. s.sin.sin_family = AF_INET;
  73. s.sin.sin_port = htons(port);
  74. if (inet_pton(AF_INET, addr, &s.sin.sin_addr) != 1) {
  75. printf("failed to convert to ip address\n");
  76. free(addr);
  77. return -4;
  78. }
  79. free(addr);
  80. switch (s.ss.ss_family) {
  81. case AF_INET:
  82. iscsi->fd = socket(AF_INET, SOCK_STREAM, 0);
  83. socksize = sizeof(struct sockaddr_in);
  84. break;
  85. default:
  86. printf("Unknown family :%d\n", s.ss.ss_family);
  87. return -5;
  88. }
  89. if (iscsi->fd == -1) {
  90. printf("Failed to open socket\n");
  91. return -6;
  92. }
  93. iscsi->connect_cb = cb;
  94. iscsi->connect_data = private_data;
  95. set_nonblocking(iscsi->fd);
  96. if (connect(iscsi->fd, &s.sa, socksize) != 0 && errno != EINPROGRESS) {
  97. printf("Connect failed errno : %s (%d)\n", strerror(errno), errno);
  98. return -7;
  99. }
  100. return 0;
  101. }
  102. int iscsi_disconnect(struct iscsi_context *iscsi)
  103. {
  104. if (iscsi == NULL) {
  105. printf("Trying to disconnect NULL context\n");
  106. return -1;
  107. }
  108. if (iscsi->is_loggedin != 0) {
  109. printf("Trying to disconnect while logged in\n");
  110. return -2;
  111. }
  112. if (iscsi->fd == -1) {
  113. printf("Trying to disconnect but not connected\n");
  114. return -3;
  115. }
  116. close(iscsi->fd);
  117. iscsi->fd = -1;
  118. iscsi->is_connected = 0;
  119. return 0;
  120. }
  121. int iscsi_get_fd(struct iscsi_context *iscsi)
  122. {
  123. if (iscsi == NULL) {
  124. printf("Trying to get fd for NULL context\n");
  125. return -1;
  126. }
  127. return iscsi->fd;
  128. }
  129. int iscsi_which_events(struct iscsi_context *iscsi)
  130. {
  131. int events = POLLIN;
  132. if (iscsi->is_connected == 0) {
  133. events |= POLLOUT;
  134. }
  135. if (iscsi->outqueue) {
  136. events |= POLLOUT;
  137. }
  138. return events;
  139. }
  140. static int iscsi_read_from_socket(struct iscsi_context *iscsi)
  141. {
  142. int available;
  143. int size;
  144. unsigned char *buf;
  145. ssize_t count;
  146. if (ioctl(iscsi->fd, FIONREAD, &available) != 0) {
  147. printf("ioctl FIONREAD returned error : %d\n", errno);
  148. return -1;
  149. }
  150. if (available == 0) {
  151. printf("no data readable in socket, socket is closed\n");
  152. return -2;
  153. }
  154. size = iscsi->insize - iscsi->inpos + available;
  155. buf = malloc(size);
  156. if (buf == NULL) {
  157. printf("failed to allocate %d bytes for input buffer\n", size);
  158. return -3;
  159. }
  160. if (iscsi->insize > iscsi->inpos) {
  161. memcpy(buf, iscsi->inbuf + iscsi->inpos, iscsi->insize - iscsi->inpos);
  162. iscsi->insize -= iscsi->inpos;
  163. iscsi->inpos = 0;
  164. }
  165. count = read(iscsi->fd, buf + iscsi->insize, available);
  166. if (count == -1) {
  167. if (errno == EINTR) {
  168. free(buf);
  169. buf = NULL;
  170. return 0;
  171. }
  172. printf("read from socket failed, errno:%d\n", errno);
  173. free(buf);
  174. buf = NULL;
  175. return -4;
  176. }
  177. if (iscsi->inbuf != NULL) {
  178. free(iscsi->inbuf);
  179. }
  180. iscsi->inbuf = buf;
  181. iscsi->insize += count;
  182. while (1) {
  183. if (iscsi->insize - iscsi->inpos < 48) {
  184. return 0;
  185. }
  186. count = iscsi_get_pdu_size(iscsi->inbuf + iscsi->inpos);
  187. if (iscsi->insize + iscsi->inpos < count) {
  188. return 0;
  189. }
  190. if (iscsi_process_pdu(iscsi, iscsi->inbuf + iscsi->inpos, count) != 0) {
  191. printf("failed to process pdu\n");
  192. return -5;
  193. }
  194. iscsi->inpos += count;
  195. if (iscsi->inpos == iscsi->insize) {
  196. free(iscsi->inbuf);
  197. iscsi->inbuf = NULL;
  198. iscsi->insize = 0;
  199. iscsi->inpos = 0;
  200. }
  201. if (iscsi->inpos > iscsi->insize) {
  202. printf("inpos > insize. bug!\n");
  203. return -6;
  204. }
  205. }
  206. return 0;
  207. }
  208. static int iscsi_write_to_socket(struct iscsi_context *iscsi)
  209. {
  210. ssize_t count;
  211. if (iscsi == NULL) {
  212. printf("trying to write to socket for NULL context\n");
  213. return -1;
  214. }
  215. if (iscsi->fd == -1) {
  216. printf("trying to write but not connected\n");
  217. return -2;
  218. }
  219. while (iscsi->outqueue != NULL) {
  220. ssize_t total;
  221. total = iscsi->outqueue->outdata.size;
  222. total = (total +3) & 0xfffffffc;
  223. count = write(iscsi->fd, iscsi->outqueue->outdata.data + iscsi->outqueue->written, total - iscsi->outqueue->written);
  224. if (count == -1) {
  225. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  226. printf("socket would block, return from write to socket\n");
  227. return 0;
  228. }
  229. printf("Error when writing to socket :%d\n", errno);
  230. return -3;
  231. }
  232. iscsi->outqueue->written += count;
  233. if (iscsi->outqueue->written == total) {
  234. struct iscsi_pdu *pdu = iscsi->outqueue;
  235. DLIST_REMOVE(iscsi->outqueue, pdu);
  236. DLIST_ADD_END(iscsi->waitpdu, pdu, NULL);
  237. }
  238. }
  239. return 0;
  240. }
  241. int iscsi_service(struct iscsi_context *iscsi, int revents)
  242. {
  243. if (revents & POLLERR) {
  244. printf("iscsi_service: POLLERR, socket error\n");
  245. iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
  246. return -1;
  247. }
  248. if (revents & POLLHUP) {
  249. printf("iscsi_service: POLLHUP, socket error\n");
  250. iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
  251. return -2;
  252. }
  253. if (iscsi->is_connected == 0 && iscsi->fd != -1 && revents&POLLOUT) {
  254. iscsi->is_connected = 1;
  255. iscsi->connect_cb(iscsi, ISCSI_STATUS_GOOD, NULL, iscsi->connect_data);
  256. return 0;
  257. }
  258. if (revents & POLLOUT && iscsi->outqueue != NULL) {
  259. if (iscsi_write_to_socket(iscsi) != 0) {
  260. printf("write to socket failed\n");
  261. return -3;
  262. }
  263. }
  264. if (revents & POLLIN) {
  265. if (iscsi_read_from_socket(iscsi) != 0) {
  266. printf("read from socket failed\n");
  267. return -4;
  268. }
  269. }
  270. return 0;
  271. }
  272. int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
  273. {
  274. if (iscsi == NULL) {
  275. printf("trying to queue to NULL context\n");
  276. return -1;
  277. }
  278. if (pdu == NULL) {
  279. printf("trying to queue NULL pdu\n");
  280. return -2;
  281. }
  282. DLIST_ADD_END(iscsi->outqueue, pdu, NULL);
  283. return 0;
  284. }