ISCSI_URL change strings from dynamic to static

This commit is contained in:
Peter Lieven
2012-10-30 11:41:51 +01:00
parent e10a5a97be
commit 774ede1f46
4 changed files with 21 additions and 93 deletions

View File

@@ -61,8 +61,6 @@ enum iscsi_immediate_data {
ISCSI_IMMEDIATE_DATA_YES = 1
};
#define MAX_STRING_SIZE (255)
struct iscsi_context {
char initiator_name[MAX_STRING_SIZE+1];
char target_name[MAX_STRING_SIZE+1];

View File

@@ -32,6 +32,8 @@ extern "C" {
struct iscsi_context;
struct sockaddr;
#define MAX_STRING_SIZE (255)
/*
* Syntax for normal and portal/discovery URLs.
*/
@@ -69,13 +71,11 @@ EXTERN int iscsi_queue_length(struct iscsi_context *iscsi);
*/
int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval);
struct iscsi_url {
const char *portal;
const char *target;
const char *user;
const char *passwd;
char portal[MAX_STRING_SIZE+1];
char target[MAX_STRING_SIZE+1];
char user[MAX_STRING_SIZE+1];
char passwd[MAX_STRING_SIZE+1];
int lun;
};

View File

@@ -302,7 +302,7 @@ struct iscsi_url *
iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
{
struct iscsi_url *iscsi_url;
char *str;
char str[MAX_STRING_SIZE+1];
char *portal;
char *user = NULL;
char *passwd = NULL;
@@ -319,11 +319,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
return NULL;
}
str = strdup(url + 8);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
strncpy(str,url + 8,MAX_STRING_SIZE);
portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME");
@@ -353,7 +349,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
"form: %s",
url,
ISCSI_URL_SYNTAX);
free(str);
return NULL;
}
*target++ = 0;
@@ -364,7 +359,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
"iSCSI URL must be of the form: %s",
url,
ISCSI_URL_SYNTAX);
free(str);
return NULL;
}
@@ -374,7 +368,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
"iSCSI URL must be of the form: %s",
url,
ISCSI_URL_SYNTAX);
free(str);
return NULL;
}
*lun++ = 0;
@@ -385,54 +378,25 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
"iSCSI URL must be of the form: %s",
url,
ISCSI_URL_SYNTAX);
free(str);
return NULL;
}
iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL;
}
memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal);
if (iscsi_url->portal == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
iscsi_url->target = strdup(target);
if (iscsi_url->target == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup target string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
strncpy(iscsi_url->target,target,MAX_STRING_SIZE);
if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user);
if (iscsi_url->user == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
iscsi_url->passwd = strdup(passwd);
if (iscsi_url->passwd == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE);
}
iscsi_url->lun = l;
free(str);
return iscsi_url;
}
@@ -440,7 +404,7 @@ struct iscsi_url *
iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
{
struct iscsi_url *iscsi_url;
char *str;
char str[MAX_STRING_SIZE+1];
char *portal;
char *user = NULL;
char *passwd = NULL;
@@ -454,11 +418,7 @@ iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
return NULL;
}
str = strdup(url + 8);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
strncpy(str,url + 8,MAX_STRING_SIZE);
portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME");
@@ -477,56 +437,26 @@ iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
}
}
iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL;
}
memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal);
if (iscsi_url->portal == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user);
if (iscsi_url->user == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
iscsi_url->passwd = strdup(passwd);
if (iscsi_url->passwd == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string");
iscsi_destroy_url(iscsi_url);
free(str);
return NULL;
}
strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE);
}
free(str);
return iscsi_url;
}
void
iscsi_destroy_url(struct iscsi_url *iscsi_url)
{
if (iscsi_url == NULL) {
return;
}
free(discard_const(iscsi_url->portal));
free(discard_const(iscsi_url->target));
free(discard_const(iscsi_url->user));
free(discard_const(iscsi_url->passwd));
free(iscsi_url);
}

View File

@@ -475,9 +475,9 @@ int main(int argc, const char *argv[])
printf("\n");
}
free(skipname);
free(testname);
free(url);
free(skipname);
free(testname);
free(url);
return num_failed ? num_failed : num_skipped ? 77 : 0;
}