Improve ssh_options_set_host().

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@347 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-02 10:13:12 +00:00
parent a9ef024f10
commit 6026de4648
3 changed files with 48 additions and 19 deletions

View File

@@ -311,7 +311,7 @@ int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
void ssh_options_set_username(SSH_OPTIONS *opt, const char *username); void ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
void ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port); void ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv); int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv);
void ssh_options_set_host(SSH_OPTIONS *opt, const char *host); int ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
void ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd); void ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd);
void ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port); void ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port);
void ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity); void ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity);

View File

@@ -211,24 +211,50 @@ void ssh_options_free(SSH_OPTIONS *opt) {
SAFE_FREE(opt); SAFE_FREE(opt);
} }
/** \brief set destination hostname /**
* \param opt option structure * @brief Set destination hostname
* \param hostname host name to connect *
* @param opt The option structure to use.
*
* @param hostname The host name to connect.
*
* @return 0 on succes, < 0 on error.
*/ */
void ssh_options_set_host(SSH_OPTIONS *opt, const char *hostname){ int ssh_options_set_host(SSH_OPTIONS *opt, const char *hostname){
char *ptr=strdup(hostname); char *h;
char *ptr2=strchr(ptr,'@'); char *p;
if(opt->host) // don't leak memory
free(opt->host); h = strdup(hostname);
if(ptr2){ if (h == NULL) {
*ptr2=0; return -1;
opt->host=strdup(ptr2+1); }
if(opt->username) p = strchr(h, '@');
free(opt->username);
opt->username=strdup(ptr); if (opt->host) {
free(ptr); SAFE_FREE(opt->host);
} else }
opt->host=ptr;
if (p) {
*p = '\0';
opt->host = strdup(p + 1);
if (opt->host == NULL) {
SAFE_FREE(h);
return -1;
}
if (opt->username) {
SAFE_FREE(opt->username);
}
opt->username = strdup(h);
SAFE_FREE(h);
if (opt->username == NULL) {
return -1;
}
} else {
opt->host = h;
}
return 0;
} }
/** \brief set username for authentication /** \brief set username for authentication

View File

@@ -422,7 +422,10 @@ int main(int argc, char **argv){
signal(SIGTERM, do_exit); signal(SIGTERM, do_exit);
if(user) if(user)
ssh_options_set_username(options,user); ssh_options_set_username(options,user);
ssh_options_set_host(options,host); if (ssh_options_set_host(options,host) < 0) {
ssh_options_free(options);
return 1;
}
session=ssh_new(); session=ssh_new();
ssh_set_options(session,options); ssh_set_options(session,options);
if(ssh_connect(session)){ if(ssh_connect(session)){