From 2df2f4f12ed929833756c4016250bbdc0ae26c71 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 08:39:37 +0200 Subject: [PATCH 1/6] ISCSI_READCAPACITY16 add switch to print only target size --- src/iscsi-readcapacity16.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/iscsi-readcapacity16.c b/src/iscsi-readcapacity16.c index 5300ea6..5f76de0 100644 --- a/src/iscsi-readcapacity16.c +++ b/src/iscsi-readcapacity16.c @@ -30,13 +30,14 @@ const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-readcapa void print_usage(void) { - fprintf(stderr, "Usage: iscsi-readcapacity16 [-?] [-?|--help] [--usage] [-i|--initiator-name=iqn-name] \n"); + fprintf(stderr, "Usage: iscsi-readcapacity16 [-?] [-?|--help] [--usage] [-i|--initiator-name=iqn-name] [-s] \n"); } void print_help(void) { fprintf(stderr, "Usage: iscsi_readcapacity16 [OPTION...] \n"); fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n"); + fprintf(stderr, " -s, --size print target size only\n"); fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n"); fprintf(stderr, "\n"); fprintf(stderr, "Help options:\n"); @@ -59,7 +60,7 @@ int main(int argc, const char *argv[]) int extra_argc = 0; const char *url = NULL; struct iscsi_url *iscsi_url = NULL; - int show_help = 0, show_usage = 0, debug = 0; + int show_help = 0, show_usage = 0, debug = 0, size_only=0; int res; struct scsi_task *task; struct scsi_readcapacity16 *rc16; @@ -68,6 +69,7 @@ int main(int argc, const char *argv[]) { "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL }, { "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL }, { "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" }, + { "size", 's', POPT_ARG_NONE, &size_only, 0, "Print target size only", NULL }, { "debug", 'd', POPT_ARG_INT, &debug, 0, "Debugging level", "integer" }, POPT_TABLEEND }; @@ -155,16 +157,22 @@ int main(int argc, const char *argv[]) iscsi_destroy_context(iscsi); exit(10); } + + if (!size_only) { + printf("RETURNED LOGICAL BLOCK ADDRESS:%" PRIu64 "\n", rc16->returned_lba); + printf("LOGICAL BLOCK LENGTH IN BYTES:%u\n", rc16->block_length); + printf("P_TYPE:%d PROT_EN:%d\n", rc16->p_type, rc16->prot_en); + printf("P_I_EXPONENT:%d LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT:%d\n", rc16->p_i_exp, rc16->lbppbe); + printf("LBPME:%d LBPRZ:%d\n", rc16->lbpme, rc16->lbprz); + printf("LOWEST ALIGNED LOGICAL BLOCK ADDRESS:%d\n", rc16->lalba); - printf("RETURNED LOGICAL BLOCK ADDRESS:%" PRIu64 "\n", rc16->returned_lba); - printf("LOGICAL BLOCK LENGTH IN BYTES:%u\n", rc16->block_length); - printf("P_TYPE:%d PROT_EN:%d\n", rc16->p_type, rc16->prot_en); - printf("P_I_EXPONENT:%d LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT:%d\n", rc16->p_i_exp, rc16->lbppbe); - printf("LBPME:%d LBPRZ:%d\n", rc16->lbpme, rc16->lbprz); - printf("LOWEST ALIGNED LOGICAL BLOCK ADDRESS:%d\n", rc16->lalba); - - printf("Total size:%" PRIu64 "\n", rc16->block_length * (rc16->returned_lba + 1)); - + printf("Total size:%" PRIu64 "\n", rc16->block_length * (rc16->returned_lba + 1)); + } + else + { + printf("%" PRIu64 "\n", rc16->block_length * (rc16->returned_lba + 1)); + } + iscsi_destroy_url(iscsi_url); iscsi_logout_sync(iscsi); From 4e7bd8767b17d4dc00551c03657993012d43f1e2 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 08:43:47 +0200 Subject: [PATCH 2/6] DPRINTF explain debug levels --- include/iscsi.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/iscsi.h b/include/iscsi.h index bbbca61..8453e5d 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -979,7 +979,14 @@ iscsi_scsi_cancel_all_tasks(struct iscsi_context *iscsi); } while (0); /* - * This function is to set the debugging level (0=disabled). + * This function is to set the debugging level where level is + * + * 0 = disabled (default) + * 1 = errors only + * 2 = connection related info + * 3 = user set variables + * 4 = function calls + * 5 = ... */ EXTERN void iscsi_set_debug(struct iscsi_context *iscsi, int level); From 89e918e9d738de25eacb03d8e7b2f78c097c7e7b Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 17:12:07 +0200 Subject: [PATCH 3/6] SOCKET validate data_size in in_pdu header --- lib/socket.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/socket.c b/lib/socket.c index 5e50954..1376527 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -333,6 +333,10 @@ iscsi_read_from_socket(struct iscsi_context *iscsi) } data_size = iscsi_get_pdu_data_size(&in->hdr[0]); + if (data_size < 0 || data_size > iscsi->initiator_max_recv_data_segment_length) { + iscsi_set_error(iscsi, "Invalid data size received from target (%d)", (int)data_size); + return -1; + } if (data_size != 0) { unsigned char *buf = NULL; From 48e55669e34c535e1f8d907e6eb22f562d072358 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 18:10:26 +0200 Subject: [PATCH 4/6] SLIST add function to debug length --- include/slist.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/slist.h b/include/slist.h index 0631b17..0f287c6 100644 --- a/include/slist.h +++ b/include/slist.h @@ -49,4 +49,15 @@ (*list) = head; \ } +#define SLIST_LENGTH(list,length) \ + do { \ + (length) = 0; \ + void *head = (*list); \ + while ((*list)) { \ + (*list) = (*list)->next; \ + (length)++; \ + } \ + (*list) = head; \ + } while (0); + #endif /* __iscsi_slist_h__ */ From 0b4424cca08e48b2f5ef1bb7c19c441e4a8d3bcf Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 20:47:15 +0200 Subject: [PATCH 5/6] CONNECT Fix memory leak in iscsi_reconnect --- lib/connect.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/connect.c b/lib/connect.c index 9d0d8f8..d7e519c 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -329,6 +329,10 @@ try_again: free(discard_const(old_iscsi->passwd)); free(discard_const(old_iscsi->chap_c)); + if (old_iscsi->connected_portal != NULL) { + free(discard_const(old_iscsi->connected_portal)); + } + close(iscsi->fd); iscsi->fd = old_iscsi->fd; memcpy(old_iscsi, iscsi, sizeof(struct iscsi_context)); From 4785dd9933a450483ce5edc5bb5544dcea411ee7 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Fri, 26 Oct 2012 20:47:35 +0200 Subject: [PATCH 6/6] CONNECT do not reseed RNG in iscsi_reconnect --- lib/connect.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/connect.c b/lib/connect.c index d7e519c..7550162 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -211,7 +211,6 @@ int iscsi_reconnect(struct iscsi_context *old_iscsi) } int retry = 0; - srand (time(NULL)^getpid()); try_again: