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 <arpa/inet.h>
30 #include "iscsi-private.h"
31 #include "dlinklist.h"
33 static void set_nonblocking(int fd)
36 v = fcntl(fd, F_GETFL, 0);
37 fcntl(fd, F_SETFL, v | O_NONBLOCK);
40 int iscsi_connect_async(struct iscsi_context *iscsi, const char *target, iscsi_command_cb cb, void *private_data)
46 struct sockaddr_storage s;
47 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
51 printf("Trying to connect NULL context\n");
54 if (iscsi->fd != -1) {
55 printf("Trying to connect but already connected\n");
59 addr = strdup(target);
61 printf("failed to strdup target address\n");
65 /* check if we have a target portal group tag */
66 if ((str = rindex(addr, ',')) != NULL) {
71 /* XXX need handling for {ipv6 addresses} */
72 /* for now, assume all is ipv4 */
73 if ((str = rindex(addr, ':')) != NULL) {
78 sin->sin_family = AF_INET;
79 sin->sin_port = htons(port);
80 if (inet_pton(AF_INET, addr, &sin->sin_addr) != 1) {
81 printf("failed to convert to ip address\n");
87 switch (s.ss_family) {
89 iscsi->fd = socket(AF_INET, SOCK_STREAM, 0);
90 socksize = sizeof(struct sockaddr_in);
93 printf("Unknown family :%d\n", s.ss_family);
98 if (iscsi->fd == -1) {
99 printf("Failed to open socket\n");
104 iscsi->connect_cb = cb;
105 iscsi->connect_data = private_data;
107 set_nonblocking(iscsi->fd);
109 if (connect(iscsi->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
110 printf("Connect failed errno : %s (%d)\n", strerror(errno), errno);
117 int iscsi_disconnect(struct iscsi_context *iscsi)
120 printf("Trying to disconnect NULL context\n");
123 if (iscsi->is_loggedin != 0) {
124 printf("Trying to disconnect while logged in\n");
127 if (iscsi->fd == -1) {
128 printf("Trying to disconnect but not connected\n");
135 iscsi->is_connected = 0;
140 int iscsi_get_fd(struct iscsi_context *iscsi)
143 printf("Trying to get fd for NULL context\n");
150 int iscsi_which_events(struct iscsi_context *iscsi)
154 if (iscsi->is_connected == 0) {
158 if (iscsi->outqueue) {
164 static int iscsi_read_from_socket(struct iscsi_context *iscsi)
171 if (ioctl(iscsi->fd, FIONREAD, &available) != 0) {
172 printf("ioctl FIONREAD returned error : %d\n", errno);
175 if (available == 0) {
176 printf("no data readable in socket, socket is closed\n");
179 size = iscsi->insize - iscsi->inpos + available;
182 printf("failed to allocate %d bytes for input buffer\n", size);
185 if (iscsi->insize > iscsi->inpos) {
186 memcpy(buf, iscsi->inbuf + iscsi->inpos, iscsi->insize - iscsi->inpos);
187 iscsi->insize -= iscsi->inpos;
191 count = read(iscsi->fd, buf + iscsi->insize, available);
193 if (errno == EINTR) {
198 printf("read from socket failed, errno:%d\n", errno);
204 if (iscsi->inbuf != NULL) {
208 iscsi->insize += count;
211 if (iscsi->insize - iscsi->inpos < 48) {
214 count = iscsi_get_pdu_size(iscsi->inbuf + iscsi->inpos);
215 if (iscsi->insize + iscsi->inpos < count) {
218 if (iscsi_process_pdu(iscsi, iscsi->inbuf + iscsi->inpos, count) != 0) {
219 printf("failed to process pdu\n");
222 iscsi->inpos += count;
223 if (iscsi->inpos == iscsi->insize) {
229 if (iscsi->inpos > iscsi->insize) {
230 printf("inpos > insize. bug!\n");
238 static int iscsi_write_to_socket(struct iscsi_context *iscsi)
243 printf("trying to write to socket for NULL context\n");
246 if (iscsi->fd == -1) {
247 printf("trying to write but not connected\n");
251 while (iscsi->outqueue != NULL) {
254 total = iscsi->outqueue->outdata.size;
255 total = (total +3) & 0xfffffffc;
257 count = write(iscsi->fd, iscsi->outqueue->outdata.data + iscsi->outqueue->written, total - iscsi->outqueue->written);
259 if (errno == EAGAIN || errno == EWOULDBLOCK) {
260 printf("socket would block, return from write to socket\n");
263 printf("Error when writing to socket :%d\n", errno);
267 iscsi->outqueue->written += count;
268 if (iscsi->outqueue->written == total) {
269 struct iscsi_pdu *pdu = iscsi->outqueue;
271 DLIST_REMOVE(iscsi->outqueue, pdu);
272 DLIST_ADD_END(iscsi->waitpdu, pdu, NULL);
278 int iscsi_service(struct iscsi_context *iscsi, int revents)
280 if (revents & POLLERR) {
281 printf("iscsi_service: POLLERR, socket error\n");
282 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
285 if (revents & POLLHUP) {
286 printf("iscsi_service: POLLHUP, socket error\n");
287 iscsi->connect_cb(iscsi, ISCSI_STATUS_ERROR, NULL, iscsi->connect_data);
291 if (iscsi->is_connected == 0 && iscsi->fd != -1 && revents&POLLOUT) {
292 iscsi->is_connected = 1;
293 iscsi->connect_cb(iscsi, ISCSI_STATUS_GOOD, NULL, iscsi->connect_data);
297 if (revents & POLLOUT && iscsi->outqueue != NULL) {
298 if (iscsi_write_to_socket(iscsi) != 0) {
299 printf("write to socket failed\n");
303 if (revents & POLLIN) {
304 if (iscsi_read_from_socket(iscsi) != 0) {
305 printf("read from socket failed\n");
313 int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
316 printf("trying to queue to NULL context\n");
320 printf("trying to queue NULL pdu\n");
323 DLIST_ADD_END(iscsi->outqueue, pdu, NULL);