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/>.
26 #include <netinet/in.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
33 #include "iscsi-private.h"
34 #include "dlinklist.h"
36 #include <sys/filio.h>
39 static void set_nonblocking(int fd)
42 v = fcntl(fd, F_GETFL, 0);
43 fcntl(fd, F_SETFL, v | O_NONBLOCK);
46 int iscsi_connect_async(struct iscsi_context *iscsi, const char *target, iscsi_command_cb cb, void *private_data)
54 struct sockaddr_storage ss;
55 struct sockaddr_in sin;
60 printf("Trying to connect NULL context\n");
63 if (iscsi->fd != -1) {
64 printf("Trying to connect but already connected\n");
68 addr = strdup(target);
70 printf("failed to strdup target address\n");
74 /* check if we have a target portal group tag */
75 if ((str = rindex(addr, ',')) != NULL) {
80 /* XXX need handling for {ipv6 addresses} */
81 /* for now, assume all is ipv4 */
82 if ((str = rindex(addr, ':')) != NULL) {
87 s.sin.sin_family = AF_INET;
88 s.sin.sin_port = htons(port);
89 if (inet_pton(AF_INET, addr, &s.sin.sin_addr) != 1) {
90 printf("failed to convert to ip address\n");
96 switch (s.ss.ss_family) {
98 iscsi->fd = socket(AF_INET, SOCK_STREAM, 0);
99 socksize = sizeof(struct sockaddr_in);
102 printf("Unknown family :%d\n", s.ss.ss_family);
107 if (iscsi->fd == -1) {
108 printf("Failed to open socket\n");
113 iscsi->connect_cb = cb;
114 iscsi->connect_data = private_data;
116 set_nonblocking(iscsi->fd);
118 if (connect(iscsi->fd, &s.sa, socksize) != 0 && errno != EINPROGRESS) {
119 printf("Connect failed errno : %s (%d)\n", strerror(errno), errno);
126 int iscsi_disconnect(struct iscsi_context *iscsi)
129 printf("Trying to disconnect NULL context\n");
132 if (iscsi->is_loggedin != 0) {
133 printf("Trying to disconnect while logged in\n");
136 if (iscsi->fd == -1) {
137 printf("Trying to disconnect but not connected\n");
144 iscsi->is_connected = 0;
149 int iscsi_get_fd(struct iscsi_context *iscsi)
152 printf("Trying to get fd for NULL context\n");
159 int iscsi_which_events(struct iscsi_context *iscsi)
163 if (iscsi->is_connected == 0) {
167 if (iscsi->outqueue) {
173 static int iscsi_read_from_socket(struct iscsi_context *iscsi)
180 if (ioctl(iscsi->fd, FIONREAD, &available) != 0) {
181 printf("ioctl FIONREAD returned error : %d\n", errno);
184 if (available == 0) {
185 printf("no data readable in socket, socket is closed\n");
188 size = iscsi->insize - iscsi->inpos + available;
191 printf("failed to allocate %d bytes for input buffer\n", size);
194 if (iscsi->insize > iscsi->inpos) {
195 memcpy(buf, iscsi->inbuf + iscsi->inpos, iscsi->insize - iscsi->inpos);
196 iscsi->insize -= iscsi->inpos;
200 count = read(iscsi->fd, buf + iscsi->insize, available);
202 if (errno == EINTR) {
207 printf("read from socket failed, errno:%d\n", errno);
213 if (iscsi->inbuf != NULL) {
217 iscsi->insize += count;
220 if (iscsi->insize - iscsi->inpos < 48) {
223 count = iscsi_get_pdu_size(iscsi->inbuf + iscsi->inpos);
224 if (iscsi->insize + iscsi->inpos < count) {
227 if (iscsi_process_pdu(iscsi, iscsi->inbuf + iscsi->inpos, count) != 0) {
228 printf("failed to process pdu\n");
231 iscsi->inpos += count;
232 if (iscsi->inpos == iscsi->insize) {
238 if (iscsi->inpos > iscsi->insize) {
239 printf("inpos > insize. bug!\n");
247 static int iscsi_write_to_socket(struct iscsi_context *iscsi)
252 printf("trying to write to socket for NULL context\n");
255 if (iscsi->fd == -1) {
256 printf("trying to write but not connected\n");
260 while (iscsi->outqueue != NULL) {
263 total = iscsi->outqueue->outdata.size;
264 total = (total +3) & 0xfffffffc;
266 count = write(iscsi->fd, iscsi->outqueue->outdata.data + iscsi->outqueue->written, total - iscsi->outqueue->written);
268 if (errno == EAGAIN || errno == EWOULDBLOCK) {
269 printf("socket would block, return from write to socket\n");
272 printf("Error when writing to socket :%d\n", errno);
276 iscsi->outqueue->written += count;
277 if (iscsi->outqueue->written == total) {
278 struct iscsi_pdu *pdu = iscsi->outqueue;
280 DLIST_REMOVE(iscsi->outqueue, pdu);
281 DLIST_ADD_END(iscsi->waitpdu, pdu, NULL);
287 int iscsi_service(struct iscsi_context *iscsi, int revents)
289 if (revents & POLLERR) {
290 printf("iscsi_service: POLLERR, socket error\n");
291 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
294 if (revents & POLLHUP) {
295 printf("iscsi_service: POLLHUP, socket error\n");
296 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
300 if (iscsi->is_connected == 0 && iscsi->fd != -1 && revents&POLLOUT) {
301 iscsi->is_connected = 1;
302 iscsi->connect_cb(iscsi, ISCSI_STATUS_GOOD, NULL, iscsi->connect_data);
306 if (revents & POLLOUT && iscsi->outqueue != NULL) {
307 if (iscsi_write_to_socket(iscsi) != 0) {
308 printf("write to socket failed\n");
312 if (revents & POLLIN) {
313 if (iscsi_read_from_socket(iscsi) != 0) {
314 printf("read from socket failed\n");
322 int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
325 printf("trying to queue to NULL context\n");
329 printf("trying to queue NULL pdu\n");
332 DLIST_ADD_END(iscsi->outqueue, pdu, NULL);