socket.c 7.3 KB

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