ISCSI-CONTEXT change dynamic string allocations to statics
This commit is contained in:
@@ -61,15 +61,21 @@ enum iscsi_immediate_data {
|
||||
ISCSI_IMMEDIATE_DATA_YES = 1
|
||||
};
|
||||
|
||||
struct iscsi_context {
|
||||
const char *initiator_name;
|
||||
const char *target_name;
|
||||
const char *target_address; /* If a redirect */
|
||||
const char *connected_portal;
|
||||
const char *alias;
|
||||
#define MAX_STRING_SIZE (255)
|
||||
|
||||
const char *user;
|
||||
const char *passwd;
|
||||
struct iscsi_context {
|
||||
char initiator_name[MAX_STRING_SIZE+1];
|
||||
char target_name[MAX_STRING_SIZE+1];
|
||||
char target_address[MAX_STRING_SIZE+1]; /* If a redirect */
|
||||
char connected_portal[MAX_STRING_SIZE+1];
|
||||
char portal[MAX_STRING_SIZE+1];
|
||||
char alias[MAX_STRING_SIZE+1];
|
||||
|
||||
char user[MAX_STRING_SIZE+1];
|
||||
char passwd[MAX_STRING_SIZE+1];
|
||||
char chap_c[MAX_STRING_SIZE+1];
|
||||
|
||||
char error_string[MAX_STRING_SIZE+1];
|
||||
|
||||
enum iscsi_session_type session_type;
|
||||
unsigned char isid[6];
|
||||
@@ -80,8 +86,6 @@ struct iscsi_context {
|
||||
enum iscsi_header_digest want_header_digest;
|
||||
enum iscsi_header_digest header_digest;
|
||||
|
||||
char *error_string;
|
||||
|
||||
int fd;
|
||||
int is_connected;
|
||||
|
||||
@@ -103,7 +107,6 @@ struct iscsi_context {
|
||||
|
||||
int chap_a;
|
||||
int chap_i;
|
||||
char *chap_c;
|
||||
|
||||
iscsi_command_cb socket_status_cb;
|
||||
void *connect_data;
|
||||
@@ -124,7 +127,6 @@ struct iscsi_context {
|
||||
enum iscsi_immediate_data use_immediate_data;
|
||||
|
||||
int lun;
|
||||
const char *portal;
|
||||
int no_auto_reconnect;
|
||||
int reconnect_deferred;
|
||||
int debug;
|
||||
|
||||
@@ -971,7 +971,7 @@ iscsi_scsi_cancel_all_tasks(struct iscsi_context *iscsi);
|
||||
if ((iscsi)->debug >= level) { \
|
||||
fprintf(stderr,"libiscsi: "); \
|
||||
fprintf(stderr, (fmt), ##args); \
|
||||
if (iscsi->target_name) { \
|
||||
if (iscsi->target_name[0]) { \
|
||||
fprintf(stderr," [%s]",iscsi->target_name); \
|
||||
} \
|
||||
fprintf(stderr,"\n"); \
|
||||
|
||||
@@ -89,7 +89,7 @@ iscsi_login_cb(struct iscsi_context *iscsi, int status, void *command_data _U_,
|
||||
{
|
||||
struct connect_task *ct = private_data;
|
||||
|
||||
if (status == SCSI_STATUS_REDIRECT && iscsi->target_address) {
|
||||
if (status == SCSI_STATUS_REDIRECT && iscsi->target_address[0]) {
|
||||
iscsi_disconnect(iscsi);
|
||||
if (iscsi_connect_async(iscsi, iscsi->target_address, iscsi_connect_cb, iscsi->connect_data) != 0) {
|
||||
free(ct);
|
||||
@@ -140,7 +140,7 @@ iscsi_full_connect_async(struct iscsi_context *iscsi, const char *portal,
|
||||
struct connect_task *ct;
|
||||
|
||||
iscsi->lun = lun;
|
||||
iscsi->portal = strdup(portal);
|
||||
strncpy(iscsi->portal,portal,MAX_STRING_SIZE);
|
||||
|
||||
ct = malloc(sizeof(struct connect_task));
|
||||
if (ct == NULL) {
|
||||
@@ -227,7 +227,7 @@ try_again:
|
||||
|
||||
iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest);
|
||||
|
||||
if (old_iscsi->user != NULL) {
|
||||
if (old_iscsi->user[0]) {
|
||||
iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd);
|
||||
}
|
||||
|
||||
@@ -235,7 +235,7 @@ try_again:
|
||||
|
||||
iscsi->lun = old_iscsi->lun;
|
||||
|
||||
iscsi->portal = strdup(old_iscsi->portal);
|
||||
strncpy(iscsi->portal,old_iscsi->portal,MAX_STRING_SIZE);
|
||||
|
||||
iscsi->debug = old_iscsi->debug;
|
||||
|
||||
@@ -317,26 +317,12 @@ try_again:
|
||||
goto try_again;
|
||||
}
|
||||
|
||||
|
||||
free(discard_const(old_iscsi->initiator_name));
|
||||
free(discard_const(old_iscsi->target_name));
|
||||
free(discard_const(old_iscsi->target_address));
|
||||
free(discard_const(old_iscsi->alias));
|
||||
free(discard_const(old_iscsi->portal));
|
||||
if (old_iscsi->incoming != NULL) {
|
||||
iscsi_free_iscsi_in_pdu(old_iscsi->incoming);
|
||||
}
|
||||
if (old_iscsi->inqueue != NULL) {
|
||||
iscsi_free_iscsi_inqueue(old_iscsi->inqueue);
|
||||
}
|
||||
free(old_iscsi->error_string);
|
||||
free(discard_const(old_iscsi->user));
|
||||
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;
|
||||
|
||||
84
lib/init.c
84
lib/init.c
@@ -45,8 +45,8 @@ iscsi_create_context(const char *initiator_name)
|
||||
|
||||
memset(iscsi, 0, sizeof(struct iscsi_context));
|
||||
|
||||
iscsi->initiator_name = strdup(initiator_name);
|
||||
if (iscsi->initiator_name == NULL) {
|
||||
strncpy(iscsi->initiator_name,initiator_name,MAX_STRING_SIZE);
|
||||
if (!iscsi->initiator_name[0]) {
|
||||
free(iscsi);
|
||||
return NULL;
|
||||
}
|
||||
@@ -169,14 +169,7 @@ iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(discard_const(iscsi->alias));
|
||||
|
||||
iscsi->alias = strdup(alias);
|
||||
if (iscsi->alias == NULL) {
|
||||
iscsi_set_error(iscsi, "Failed to allocate alias name");
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(iscsi->alias,alias,MAX_STRING_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -189,13 +182,7 @@ iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(discard_const(iscsi->target_name));
|
||||
|
||||
iscsi->target_name = strdup(target_name);
|
||||
if (iscsi->target_name == NULL) {
|
||||
iscsi_set_error(iscsi, "Failed to allocate target name");
|
||||
return -1;
|
||||
}
|
||||
strncpy(iscsi->target_name,target_name,MAX_STRING_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -238,21 +225,6 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
|
||||
iscsi_free_pdu(iscsi, pdu);
|
||||
}
|
||||
|
||||
free(discard_const(iscsi->initiator_name));
|
||||
iscsi->initiator_name = NULL;
|
||||
|
||||
free(discard_const(iscsi->target_name));
|
||||
iscsi->target_name = NULL;
|
||||
|
||||
free(discard_const(iscsi->target_address));
|
||||
iscsi->target_address = NULL;
|
||||
|
||||
free(discard_const(iscsi->alias));
|
||||
iscsi->alias = NULL;
|
||||
|
||||
free(discard_const(iscsi->portal));
|
||||
iscsi->portal = NULL;
|
||||
|
||||
if (iscsi->incoming != NULL) {
|
||||
iscsi_free_iscsi_in_pdu(iscsi->incoming);
|
||||
}
|
||||
@@ -260,23 +232,6 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
|
||||
iscsi_free_iscsi_inqueue(iscsi->inqueue);
|
||||
}
|
||||
|
||||
free(iscsi->error_string);
|
||||
iscsi->error_string = NULL;
|
||||
|
||||
free(discard_const(iscsi->user));
|
||||
iscsi->user = NULL;
|
||||
|
||||
free(discard_const(iscsi->passwd));
|
||||
iscsi->passwd = NULL;
|
||||
|
||||
free(discard_const(iscsi->chap_c));
|
||||
iscsi->chap_c = NULL;
|
||||
|
||||
if (iscsi->connected_portal != NULL) {
|
||||
free(discard_const(iscsi->connected_portal));
|
||||
iscsi->connected_portal = NULL;
|
||||
}
|
||||
|
||||
iscsi->connect_data = NULL;
|
||||
|
||||
free(iscsi);
|
||||
@@ -288,23 +243,14 @@ void
|
||||
iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *str;
|
||||
|
||||
va_start(ap, error_string);
|
||||
str = malloc(1024);
|
||||
if (vsnprintf(str, 1024, error_string, ap) < 0) {
|
||||
/* not much we can do here */
|
||||
free(str);
|
||||
str = NULL;
|
||||
if (vsnprintf(iscsi->error_string, MAX_STRING_SIZE, error_string, ap) < 0) {
|
||||
strncpy(iscsi->error_string,"could not format error string!",MAX_STRING_SIZE);
|
||||
}
|
||||
|
||||
free(iscsi->error_string);
|
||||
|
||||
iscsi->error_string = str;
|
||||
|
||||
va_end(ap);
|
||||
|
||||
DPRINTF(iscsi,1,"%s",str);
|
||||
DPRINTF(iscsi,1,"%s",iscsi->error_string);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -589,19 +535,7 @@ int
|
||||
iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi,
|
||||
const char *user, const char *passwd)
|
||||
{
|
||||
free(discard_const(iscsi->user));
|
||||
iscsi->user = strdup(user);
|
||||
if (iscsi->user == NULL) {
|
||||
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup username");
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(discard_const(iscsi->passwd));
|
||||
iscsi->passwd = strdup(passwd);
|
||||
if (iscsi->passwd == NULL) {
|
||||
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup password");
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(iscsi->user,user,MAX_STRING_SIZE);
|
||||
strncpy(iscsi->passwd,passwd,MAX_STRING_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
24
lib/login.c
24
lib/login.c
@@ -106,7 +106,7 @@ iscsi_login_add_targetname(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (iscsi->target_name == NULL) {
|
||||
if (!iscsi->target_name[0]) {
|
||||
iscsi_set_error(iscsi, "Trying normal connect but "
|
||||
"target name not set.");
|
||||
return -1;
|
||||
@@ -663,7 +663,7 @@ iscsi_login_add_chap_response(struct iscsi_context *iscsi, struct iscsi_pdu *pdu
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (iscsi->chap_c == NULL) {
|
||||
if (!iscsi->chap_c[0]) {
|
||||
iscsi_set_error(iscsi, "No CHAP challenge found");
|
||||
return -1;
|
||||
}
|
||||
@@ -748,7 +748,7 @@ iscsi_login_async(struct iscsi_context *iscsi, iscsi_command_cb cb,
|
||||
/* login request */
|
||||
iscsi_pdu_set_immediate(pdu);
|
||||
|
||||
if (iscsi->user == NULL) {
|
||||
if (!iscsi->user[0]) {
|
||||
iscsi->current_phase = ISCSI_PDU_LOGIN_CSG_OPNEG;
|
||||
}
|
||||
|
||||
@@ -1008,15 +1008,7 @@ iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
|
||||
|
||||
/* parse the strings */
|
||||
if (!strncmp(ptr, "TargetAddress=", 14)) {
|
||||
free(discard_const(iscsi->target_address));
|
||||
iscsi->target_address = strdup(ptr+14);
|
||||
if (iscsi->target_address == NULL) {
|
||||
iscsi_set_error(iscsi, "Failed to allocate"
|
||||
" target address");
|
||||
pdu->callback(iscsi, SCSI_STATUS_ERROR, NULL,
|
||||
pdu->private_data);
|
||||
return -1;
|
||||
}
|
||||
strncpy(iscsi->target_address,ptr+14,MAX_STRING_SIZE);
|
||||
}
|
||||
|
||||
if (!strncmp(ptr, "HeaderDigest=", 13)) {
|
||||
@@ -1074,13 +1066,7 @@ iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
|
||||
}
|
||||
|
||||
if (!strncmp(ptr, "CHAP_C=0x", 9)) {
|
||||
free(iscsi->chap_c);
|
||||
iscsi->chap_c = strdup(ptr+9);
|
||||
if (iscsi->chap_c == NULL) {
|
||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup CHAP challenge.");
|
||||
pdu->callback(iscsi, SCSI_STATUS_ERROR, NULL, pdu->private_data);
|
||||
return -1;
|
||||
}
|
||||
strncpy(iscsi->chap_c,ptr+9,MAX_STRING_SIZE);
|
||||
iscsi->secneg_phase = ISCSI_LOGIN_SECNEG_PHASE_SEND_RESPONSE;
|
||||
}
|
||||
|
||||
|
||||
@@ -228,8 +228,7 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
|
||||
|
||||
freeaddrinfo(ai);
|
||||
|
||||
if (iscsi->connected_portal) free(discard_const(iscsi->connected_portal));
|
||||
iscsi->connected_portal=strdup(portal);
|
||||
strncpy(iscsi->connected_portal,portal,MAX_STRING_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -245,7 +244,7 @@ iscsi_disconnect(struct iscsi_context *iscsi)
|
||||
|
||||
close(iscsi->fd);
|
||||
|
||||
if (iscsi->connected_portal)
|
||||
if (iscsi->connected_portal[0])
|
||||
DPRINTF(iscsi,2,"disconnected from portal %s",iscsi->connected_portal);
|
||||
|
||||
iscsi->fd = -1;
|
||||
|
||||
Reference in New Issue
Block a user