diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2015-09-06 14:18:35 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2015-09-06 14:18:35 +0200 |
commit | 42dece6021fa1ab8fdc4e8d8a729e5f7aa17fd27 (patch) | |
tree | dfeb3429e5be21049ed5e4e32fb57ea04c95a876 | |
parent | 85e3c2b1a065f7bab008a824aeb981caf10e192b (diff) | |
download | cssh-42dece6021fa1ab8fdc4e8d8a729e5f7aa17fd27.tar.gz cssh-42dece6021fa1ab8fdc4e8d8a729e5f7aa17fd27.tar.bz2 |
code cleanup, introduced a scp_data_t structure
-rw-r--r-- | src/cssh.c | 242 |
1 files changed, 117 insertions, 125 deletions
@@ -62,6 +62,15 @@ typedef struct scp_dir_stack_t { struct scp_dir_stack_entry_t *head; } scp_dir_stack_t; +typedef struct scp_data_t { + ssh_scp scp; + scp_read_state_e read_state; + scp_dir_stack_t dir_stack; + char *buf; + uint64_t size; + uint64_t bytesReceived; +} scp_data_t; + static int push_dir_stack( scp_dir_stack_t *stack, const char *dir ) { struct scp_dir_stack_entry_t *e = (struct scp_dir_stack_entry_t *)malloc( sizeof( scp_dir_stack_entry_t ) ); @@ -406,7 +415,7 @@ static int read_hosts_file( const char *hosts_file, unsigned short default_port, return 0; } -static void cleanup_sessions( ssh_session **session, ssh_channel **channel, ssh_scp **scp, scp_read_state_e **read_state, char ***buf, scp_dir_stack_t **dir_stack, char **host, unsigned short *port, const int nof_sessions, bool verbose ) +static void cleanup_sessions( ssh_session **session, ssh_channel **channel, scp_data_t **scp_data, char **host, unsigned short *port, const int nof_sessions, bool verbose ) { for( unsigned int i = 0; i < nof_sessions; i++ ) { if( ssh_is_connected( (*session)[i] ) ) { @@ -420,15 +429,24 @@ static void cleanup_sessions( ssh_session **session, ssh_channel **channel, ssh_ ssh_channel_free( (*channel)[i] ); } } - if( scp != NULL ) { - ssh_scp_close( (*scp)[i] ); - ssh_scp_free( (*scp)[i] ); - } - if( buf != NULL ) { - free( (*buf)[i] ); - } - if( dir_stack != NULL ) { - free_dir_stack( &(*dir_stack)[i] ); + if( scp_data != NULL ) { + ssh_scp *scp = &(*scp_data)[i].scp; + if( scp != NULL ) { + ssh_scp_close( *scp ); + ssh_scp_free( *scp ); + scp = NULL; + } + char *buf = (*scp_data)[i].buf; + if( buf != NULL ) { + free( buf ); + buf = NULL; + } + scp_dir_stack_t *dir_stack = &(*scp_data)[i].dir_stack; + if( dir_stack != NULL ) { + free_dir_stack( dir_stack ); + dir_stack = NULL; + } + } ssh_disconnect( (*session)[i] ); if( verbose ) { @@ -440,17 +458,8 @@ static void cleanup_sessions( ssh_session **session, ssh_channel **channel, ssh_ if( channel != NULL ) { free( *channel ); } - if( scp != NULL ) { - free( *scp ); - } - if( read_state != NULL ) { - free( *read_state ); - } - if( buf != NULL ) { - free( *buf ); - } - if( dir_stack != NULL ) { - free( *dir_stack ); + if( scp_data != NULL ) { + free( *scp_data ); } free( *session ); free( *host ); @@ -689,7 +698,7 @@ int main( int argc, char *argv[] ) bool *is_connected = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) ); if( is_connected == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for 'is_connected'" ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_SUCCESS ); } memset( is_connected, false, ( nof_sessions + 1 ) * sizeof( bool ) ); @@ -708,7 +717,7 @@ int main( int argc, char *argv[] ) } else { fprintf( stderr, "ERROR: error connecting to '%s', port '%d': %s\n", host[i], port[i], ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -730,7 +739,7 @@ int main( int argc, char *argv[] ) auth_state_e *auth_state = (auth_state_e *)malloc( ( nof_sessions + 1 ) * sizeof( auth_state_e ) ); if( auth_state == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for 'auth_state'" ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } memset( auth_state, CSSH_AUTH_INIT, ( nof_sessions + 1 ) * sizeof( auth_state_e ) ); @@ -741,7 +750,7 @@ int main( int argc, char *argv[] ) if( verify_knownhost( session[i] ) < 0 ) { fprintf( stderr, "ERROR: closing connection to '%s', port '%d' due to security reasons\n", host[i], port[i] ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } auth_state[i] = CSSH_AUTH_VERIFY_HOST_DONE; @@ -762,7 +771,7 @@ int main( int argc, char *argv[] ) } else { fprintf( stderr, "ERROR: ssh_userauth_none to '%s', port '%d' failed: %s\n", host[i], port[i], ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } break; @@ -787,7 +796,7 @@ int main( int argc, char *argv[] ) } else if( rc == SSH_AUTH_DENIED ) { auth_state[i] = CSSH_AUTH_PUBKEY_DONE_FAILED; } else { - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } break; @@ -806,7 +815,7 @@ int main( int argc, char *argv[] ) } else if( rc == SSH_AUTH_DENIED ) { auth_state[i] = CSSH_AUTH_PASSWORD_DONE_FAILED; } else { - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } break; @@ -826,7 +835,7 @@ int main( int argc, char *argv[] ) case CSSH_AUTH_DONE_FAILED: // one authentication failed, bail out for now fprintf( stderr, "ERROR: authentication failed for one host ('%s'), aborting now", host[i] ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -851,7 +860,7 @@ int main( int argc, char *argv[] ) memset( channel, 0, nof_sessions + 1 ); if( channel == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for ssh_channels" ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } for( unsigned int i = 0; i < nof_sessions; i++ ) { @@ -859,7 +868,7 @@ int main( int argc, char *argv[] ) if( channel[i] == NULL ) { fprintf( stderr, "ERROR: Unable to open SSH channel: %s\n", ssh_get_error( session[i] ) ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, &channel, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -867,7 +876,7 @@ int main( int argc, char *argv[] ) for( unsigned int i = 0; i < nof_sessions; i++ ) { rc = ssh_channel_open_session( channel[i] ); if( rc != SSH_OK ) { - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -878,7 +887,7 @@ int main( int argc, char *argv[] ) if( rc != SSH_OK ) { fprintf( stderr, "ERROR: Executing SSH command '%s' failed: %s\n", cmd, ssh_get_error( session[i] ) ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -887,7 +896,7 @@ int main( int argc, char *argv[] ) bool *eof_sent = (bool *)malloc( ( nof_sessions + 1 ) * sizeof( bool ) ); if( eof_sent == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for 'eof_sent'" ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } memset( eof_sent, false, ( nof_sessions + 1 ) * sizeof( bool ) ); @@ -895,7 +904,7 @@ int main( int argc, char *argv[] ) if( is_eof == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for 'is_eof'" ); free( eof_sent ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } memset( is_eof, false, ( nof_sessions + 1 ) * sizeof( bool ) ); @@ -926,7 +935,7 @@ int main( int argc, char *argv[] ) ssh_get_error( session[i] ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -937,7 +946,7 @@ int main( int argc, char *argv[] ) ssh_get_error( session[i] ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -952,7 +961,7 @@ int main( int argc, char *argv[] ) strerror( errno ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -961,7 +970,7 @@ int main( int argc, char *argv[] ) wrc, nread ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -973,7 +982,7 @@ int main( int argc, char *argv[] ) ssh_get_error( session[i] ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -984,7 +993,7 @@ int main( int argc, char *argv[] ) ssh_get_error( session[i] ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -995,7 +1004,7 @@ int main( int argc, char *argv[] ) strerror( errno ) ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -1004,7 +1013,7 @@ int main( int argc, char *argv[] ) wrc, nread ); free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -1020,91 +1029,74 @@ int main( int argc, char *argv[] ) free( eof_sent ); free( is_eof ); - cleanup_sessions( &session, &channel, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); } break; case CSSH_EXECUTE_AS_SCP: { - ssh_scp *scp = (ssh_scp *)malloc( ( nof_sessions + 1 ) * sizeof( ssh_scp ) ); - memset( scp, 0, nof_sessions + 1 ); - if( scp == NULL ) { - fprintf( stderr, "ERROR: Memory allocation failed for ssh_scp" ); - cleanup_sessions( &session, NULL, NULL, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + scp_data_t *scp_data = (scp_data_t *)malloc( ( nof_sessions + 1 ) * sizeof( scp_data_t ) ); + if( scp_data == NULL ) { + fprintf( stderr, "ERROR: Memory allocation failed for ssh_data array" ); + cleanup_sessions( &session, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } + memset( scp_data, 0, ( nof_sessions + 1 ) * sizeof( scp_data_t ) ); for( unsigned int i = 0; i < nof_sessions; i++ ) { - scp[i] = ssh_scp_new( session[i], + scp_data[i].read_state = CSSH_SCP_READ_STATE_IDLE; + } + + for( unsigned int i = 0; i < nof_sessions; i++ ) { + scp_data[i].scp = ssh_scp_new( session[i], ( ( copy_direction == CSSH_COPY_DIRECTION_UPLOAD ) ? SSH_SCP_WRITE : SSH_SCP_READ ) | ( ( args_info.recursive_given > 0 ) ? SSH_SCP_RECURSIVE : 0 ), remote_directory ); - if( scp[i] == NULL ) { + if( scp_data[i].scp == NULL ) { fprintf( stderr, "ERROR: Unable to open SCP channel: %s\n", ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } for( unsigned int i = 0; i < nof_sessions; i++ ) { - rc = ssh_scp_init( scp[i] ); + rc = ssh_scp_init( scp_data[i].scp ); if( rc != SSH_OK ) { fprintf( stderr, "ERROR: Unable to initialize SCP sessions: %s\n", ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } - char **buf = (char **)malloc( ( nof_sessions + 1 ) * sizeof( char * ) ); - if( buf == NULL ) { - fprintf( stderr, "ERROR: Memory allocation failed for receiving buffers of ssh_scp" ); - cleanup_sessions( &session, NULL, &scp, NULL, NULL, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); - exit( EXIT_FAILURE ); - } - memset( buf, 0, ( nof_sessions + 1 ) * sizeof( char * ) ); for( unsigned int i = 0; i < nof_sessions; i++ ) { - buf[i] = (char *)malloc( BUFSIZE ); - if( buf[i] == NULL ) { + scp_data[i].buf = (char *)malloc( BUFSIZE ); + if( scp_data[i].buf == NULL ) { fprintf( stderr, "ERROR: Memory allocation failed for receiving buffers of ssh_scp" ); - cleanup_sessions( &session, NULL, &scp, NULL, &buf, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } bool all_eof = false; - scp_read_state_e *read_state = (scp_read_state_e *)malloc( ( nof_sessions + 1 ) * sizeof( scp_read_state_e ) ); - if( read_state == NULL ) { - fprintf( stderr, "ERROR: Memory allocation failed for 'is_eof'" ); - cleanup_sessions( &session, NULL, &scp, NULL, &buf, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); - exit( EXIT_FAILURE ); - } - memset( read_state, CSSH_SCP_READ_STATE_IDLE, ( nof_sessions + 1 ) * sizeof( scp_read_state_e ) ); - scp_dir_stack_t *dir_stack = (scp_dir_stack_t *)malloc( ( nof_sessions + 1 ) * sizeof( scp_dir_stack_t ) ); - if( dir_stack == NULL ) { - fprintf( stderr, "ERROR: Memory allocation failed for 'dir_stack'" ); - cleanup_sessions( &session, NULL, &scp, NULL, &buf, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); - exit( EXIT_FAILURE ); - } - memset( dir_stack, 0, ( nof_sessions + 1 ) * sizeof( scp_dir_stack_t ) ); for( unsigned int i = 0; i < nof_sessions; i++ ) { - create_dir_stack( &dir_stack[i] ); + create_dir_stack( &scp_data[i].dir_stack ); char *pwd = getcwd( NULL, 0 ); if( pwd == NULL ) { fprintf( stderr, "ERROR: failed to determine working directory for SCP for host '%s': %s\n", host[i], strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, NULL, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } - rc = push_dir_stack( &dir_stack[i], pwd ); + rc = push_dir_stack( &scp_data[i].dir_stack, pwd ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed to push current directory to stack for host '%s'\n", host[i] ); free( pwd ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -1115,17 +1107,17 @@ int main( int argc, char *argv[] ) fprintf( stderr, "ERROR: Memory allocation failed for full path of base directory for host '%s'\n", host[i] ); free( pwd ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } snprintf( full_path, len, "%s/%s", pwd, host[i] ); - rc = push_dir_stack( &dir_stack[i], full_path ); + rc = push_dir_stack( &scp_data[i].dir_stack, full_path ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed remember initial base directory '%s' in directory stack\n", pwd ); free( pwd ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } free( pwd ); @@ -1134,7 +1126,7 @@ int main( int argc, char *argv[] ) if( rc < 0 ) { fprintf( stderr, "ERROR: failed to create base directory '%s' for host '%s': %s\n", full_path, host[i], strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -1145,36 +1137,36 @@ int main( int argc, char *argv[] ) switch( copy_direction ) { case CSSH_COPY_DIRECTION_UPLOAD: { fprintf( stderr, "ERROR: SCP upload has not been implemented yet.\n" ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } break; case CSSH_COPY_DIRECTION_DOWNLOAD: { for( unsigned int i = 0; i < nof_sessions; i++ ) { - switch( read_state[i] ) { + switch( scp_data[i].read_state ) { case CSSH_SCP_READ_STATE_IDLE: - rc = ssh_scp_pull_request( scp[i] ); + rc = ssh_scp_pull_request( scp_data[i].scp ); switch( rc ) { case SSH_SCP_REQUEST_NEWDIR: { - char *dir = top_dir_stack( &dir_stack[i] ); + char *dir = top_dir_stack( &scp_data[i].dir_stack ); rc = chdir( dir ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed to change to directory '%s': %s\n", dir, strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } - const char *filename = ssh_scp_request_get_filename( scp[i] ); - int mode = ssh_scp_request_get_permissions( scp[i] ); + const char *filename = ssh_scp_request_get_filename( scp_data[i].scp ); + int mode = ssh_scp_request_get_permissions( scp_data[i].scp ); fprintf( stderr, "Receiving directory '%s' with permissions '0%o'\n", filename, mode ); - rc = ssh_scp_accept_request( scp[i] ); + rc = ssh_scp_accept_request( scp_data[i].scp ); if( rc != SSH_OK ) { fprintf( stderr, "ERROR: accepting request for directory '%s' failed: %s\n", filename, ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -1182,14 +1174,14 @@ int main( int argc, char *argv[] ) if( rc < 0 ) { fprintf( stderr, "ERROR: failed to create directory '%s': %s\n", filename, strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } char *pwd = getcwd( NULL, 0 ); if( pwd == NULL ) { fprintf( stderr, "ERROR: failed to determine working directory for SCP for host '%s': %s\n", host[i], strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } size_t len = strlen( pwd ) + 2 + strlen( filename ); @@ -1198,47 +1190,47 @@ int main( int argc, char *argv[] ) fprintf( stderr, "ERROR: Memory allocation failed for full path of directory '%s'\n", filename ); free( pwd ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } snprintf( full_path, len, "%s/%s", pwd, filename ); - rc = push_dir_stack( &dir_stack[i], full_path ); + rc = push_dir_stack( &scp_data[i].dir_stack, full_path ); free( pwd ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed remember directory '%s' in directory stack\n", full_path ); free( full_path ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } free( full_path ); } break; case SSH_SCP_REQUEST_ENDDIR: - pop_dir_stack( &dir_stack[i] ); + pop_dir_stack( &scp_data[i].dir_stack ); break; case SSH_SCP_REQUEST_NEWFILE: { - const char *filename = ssh_scp_request_get_filename( scp[i] ); - int mode = ssh_scp_request_get_permissions( scp[i] ); - uint64_t size = ssh_scp_request_get_size64( scp[i] ); + const char *filename = ssh_scp_request_get_filename( scp_data[i].scp ); + int mode = ssh_scp_request_get_permissions( scp_data[i].scp ); + scp_data[i].size = ssh_scp_request_get_size64( scp_data[i].scp ); fprintf( stderr, "Receiving file '%s' with permissions '0%o' of size '%"PRIu64"'\n", - filename, mode, size ); - rc = ssh_scp_accept_request( scp[i] ); + filename, mode, scp_data[i].size ); + rc = ssh_scp_accept_request( scp_data[i].scp ); if( rc != SSH_OK ) { fprintf( stderr, "ERROR: accepting request for file '%s' failed: %s\n", filename, ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } - char *dir = top_dir_stack( &dir_stack[i] ); + char *dir = top_dir_stack( &scp_data[i].dir_stack ); rc = chdir( dir ); if( rc < 0 ) { fprintf( stderr, "ERROR: failed to change to directory '%s': %s\n", dir, strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } @@ -1248,7 +1240,7 @@ int main( int argc, char *argv[] ) fprintf( stderr, "ERROR: Memory allocation failed for full path of directory '%s'\n", filename ); free( dir ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } snprintf( full_path, len, "%s/%s", dir, filename ); @@ -1257,7 +1249,7 @@ int main( int argc, char *argv[] ) fprintf( stderr, "ERROR: Unable to open file '%s': %s\n", full_path, strerror( errno ) ); free( full_path ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } free( full_path ); @@ -1269,22 +1261,22 @@ int main( int argc, char *argv[] ) // TODO: slurping it all for one connection is not fair to other connections, // we should read a buffer and then check the states of the other and hit the // loop again, this needs a state machine.. - uint64_t bytesReceived = 0; - while( bytesReceived < size ) { - rc = ssh_scp_read( scp[i], buf[i], BUFSIZE ); + scp_data[i].bytesReceived = 0; + while( scp_data[i].bytesReceived < scp_data[i].size ) { + rc = ssh_scp_read( scp_data[i].scp, scp_data[i].buf, BUFSIZE ); if( rc == SSH_ERROR ) { fprintf( stderr, "ERROR: reading data for file '%s' failed: %s\n", filename, ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } - bytesReceived += rc; + scp_data[i].bytesReceived += rc; if( rc > 0 ) { - ssize_t r = write( fd, buf[i], rc ); + ssize_t r = write( fd, scp_data[i].buf, rc ); if( r < 0 ) { fprintf( stderr, "ERROR: writing data to file '%s' failed: %s\n", filename, strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } @@ -1294,16 +1286,16 @@ int main( int argc, char *argv[] ) if( rc < 0 ) { fprintf( stderr, "ERROR: Unable to close file '%s': %s\n", filename, strerror( errno ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); } } break; case SSH_SCP_REQUEST_EOF: - read_state[i] = CSSH_SCP_READ_STATE_EOF; + scp_data[i].read_state = CSSH_SCP_READ_STATE_EOF; all_eof = true; for( unsigned int i = 0; i < nof_sessions; i++ ) { - if( read_state[i] != CSSH_SCP_READ_STATE_EOF ) { + if( scp_data[i].read_state != CSSH_SCP_READ_STATE_EOF ) { all_eof = false; break; } @@ -1313,14 +1305,14 @@ int main( int argc, char *argv[] ) case SSH_ERROR: fprintf( stderr, "ERROR: error from remote host '%s': %s\n", host[i], ssh_get_error( session[i] ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); break; case SSH_SCP_REQUEST_WARNING: fprintf( stderr, "WARNING: remote host '%s': %s\n", - host[i], ssh_scp_request_get_warning( scp[i] ) ); - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + host[i], ssh_scp_request_get_warning( scp_data[i].scp ) ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); exit( EXIT_FAILURE ); break; } @@ -1337,7 +1329,7 @@ int main( int argc, char *argv[] ) } } - cleanup_sessions( &session, NULL, &scp, &read_state, &buf, &dir_stack, host, port, nof_sessions, args_info.verbose_given > 0 ); + cleanup_sessions( &session, NULL, &scp_data, host, port, nof_sessions, args_info.verbose_given > 0 ); } break; |