2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <netinet/in.h>
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include "iscsi-private.h"
33 #include "dlinklist.h"
35 static void set_nonblocking(int fd)
38 v = fcntl(fd, F_GETFL, 0);
39 fcntl(fd, F_SETFL, v | O_NONBLOCK);
42 int iscsi_connect_async(struct iscsi_context *iscsi, const char *target, iscsi_command_cb cb, void *private_data)
50 struct sockaddr_storage ss;
51 struct sockaddr_in sin;
56 printf("Trying to connect NULL context\n");
59 if (iscsi->fd != -1) {
60 printf("Trying to connect but already connected\n");
64 addr = strdup(target);
66 printf("failed to strdup target address\n");
70 /* check if we have a target portal group tag */
71 if ((str = rindex(addr, ',')) != NULL) {
76 /* XXX need handling for {ipv6 addresses} */
77 /* for now, assume all is ipv4 */
78 if ((str = rindex(addr, ':')) != NULL) {
83 s.sin.sin_family = AF_INET;
84 s.sin.sin_port = htons(port);
85 if (inet_pton(AF_INET, addr, &s.sin.sin_addr) != 1) {
86 printf("failed to convert to ip address\n");
92 switch (s.ss.ss_family) {
94 iscsi->fd = socket(AF_INET, SOCK_STREAM, 0);
95 socksize = sizeof(struct sockaddr_in);
98 printf("Unknown family :%d\n", s.ss.ss_family);
103 if (iscsi->fd == -1) {
104 printf("Failed to open socket\n");
109 iscsi->connect_cb = cb;
110 iscsi->connect_data = private_data;
112 set_nonblocking(iscsi->fd);
114 if (connect(iscsi->fd, &s.sa, socksize) != 0 && errno != EINPROGRESS) {
115 printf("Connect failed errno : %s (%d)\n", strerror(errno), errno);
122 int iscsi_disconnect(struct iscsi_context *iscsi)
125 printf("Trying to disconnect NULL context\n");
128 if (iscsi->is_loggedin != 0) {
129 printf("Trying to disconnect while logged in\n");
132 if (iscsi->fd == -1) {
133 printf("Trying to disconnect but not connected\n");
140 iscsi->is_connected = 0;
145 int iscsi_get_fd(struct iscsi_context *iscsi)
148 printf("Trying to get fd for NULL context\n");
155 int iscsi_which_events(struct iscsi_context *iscsi)
159 if (iscsi->is_connected == 0) {
163 if (iscsi->outqueue) {
169 static int iscsi_read_from_socket(struct iscsi_context *iscsi)
176 if (ioctl(iscsi->fd, FIONREAD, &available) != 0) {
177 printf("ioctl FIONREAD returned error : %d\n", errno);
180 if (available == 0) {
181 printf("no data readable in socket, socket is closed\n");
184 size = iscsi->insize - iscsi->inpos + available;
187 printf("failed to allocate %d bytes for input buffer\n", size);
190 if (iscsi->insize > iscsi->inpos) {
191 memcpy(buf, iscsi->inbuf + iscsi->inpos, iscsi->insize - iscsi->inpos);
192 iscsi->insize -= iscsi->inpos;
196 count = read(iscsi->fd, buf + iscsi->insize, available);
198 if (errno == EINTR) {
203 printf("read from socket failed, errno:%d\n", errno);
209 if (iscsi->inbuf != NULL) {
213 iscsi->insize += count;
216 if (iscsi->insize - iscsi->inpos < 48) {
219 count = iscsi_get_pdu_size(iscsi->inbuf + iscsi->inpos);
220 if (iscsi->insize + iscsi->inpos < count) {
223 if (iscsi_process_pdu(iscsi, iscsi->inbuf + iscsi->inpos, count) != 0) {
224 printf("failed to process pdu\n");
227 iscsi->inpos += count;
228 if (iscsi->inpos == iscsi->insize) {
234 if (iscsi->inpos > iscsi->insize) {
235 printf("inpos > insize. bug!\n");
243 static int iscsi_write_to_socket(struct iscsi_context *iscsi)
248 printf("trying to write to socket for NULL context\n");
251 if (iscsi->fd == -1) {
252 printf("trying to write but not connected\n");
256 while (iscsi->outqueue != NULL) {
259 total = iscsi->outqueue->outdata.size;
260 total = (total +3) & 0xfffffffc;
262 count = write(iscsi->fd, iscsi->outqueue->outdata.data + iscsi->outqueue->written, total - iscsi->outqueue->written);
264 if (errno == EAGAIN || errno == EWOULDBLOCK) {
265 printf("socket would block, return from write to socket\n");
268 printf("Error when writing to socket :%d\n", errno);
272 iscsi->outqueue->written += count;
273 if (iscsi->outqueue->written == total) {
274 struct iscsi_pdu *pdu = iscsi->outqueue;
276 DLIST_REMOVE(iscsi->outqueue, pdu);
277 DLIST_ADD_END(iscsi->waitpdu, pdu, NULL);
283 int iscsi_service(struct iscsi_context *iscsi, int revents)
285 if (revents & POLLERR) {
286 printf("iscsi_service: POLLERR, socket error\n");
287 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
290 if (revents & POLLHUP) {
291 printf("iscsi_service: POLLHUP, socket error\n");
292 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
296 if (iscsi->is_connected == 0 && iscsi->fd != -1 && revents&POLLOUT) {
297 iscsi->is_connected = 1;
298 iscsi->connect_cb(iscsi, ISCSI_STATUS_GOOD, NULL, iscsi->connect_data);
302 if (revents & POLLOUT && iscsi->outqueue != NULL) {
303 if (iscsi_write_to_socket(iscsi) != 0) {
304 printf("write to socket failed\n");
308 if (revents & POLLIN) {
309 if (iscsi_read_from_socket(iscsi) != 0) {
310 printf("read from socket failed\n");
318 int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
321 printf("trying to queue to NULL context\n");
325 printf("trying to queue NULL pdu\n");
328 DLIST_ADD_END(iscsi->outqueue, pdu, NULL);