1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-31 00:03:07 +03:00

Added an option to set the port as string.

This commit is contained in:
Andreas Schneider
2009-10-15 17:00:56 +02:00
parent e736b1a40e
commit 2523ed0779
3 changed files with 121 additions and 4 deletions

View File

@ -14,9 +14,11 @@ clients must be made or how a client should react.
#include <libssh/libssh.h> #include <libssh/libssh.h>
#include <libssh/server.h> #include <libssh/server.h>
#include <unistd.h> #include <argp.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#ifndef KEYS_FOLDER #ifndef KEYS_FOLDER
#ifdef _WIN32 #ifdef _WIN32
#define KEYS_FOLDER #define KEYS_FOLDER
@ -33,6 +35,98 @@ static int auth_password(char *user, char *password){
return 1; // authenticated return 1; // authenticated
} }
const char *argp_program_version = "libssh server example "
SSH_STRINGIFY(LIBSSH_VERSION);
const char *argp_program_bug_address = "<libssh@libssh.org>";
/* Program documentation. */
static char doc[] = "libssh -- a Secure Shell protocol implementation";
/* A description of the arguments we accept. */
static char args_doc[] = "BINDADDR";
/* The options we understand. */
static struct argp_option options[] = {
{
.name = "port",
.key = 'p',
.arg = "PORT",
.flags = 0,
.doc = "Set the port to bind.",
.group = 0
},
{
.name = "hostkey",
.key = 'k',
.arg = "FILE",
.flags = 0,
.doc = "Set the host key.",
.group = 0
},
{
.name = "dsakey",
.key = 'd',
.arg = "FILE",
.flags = 0,
.doc = "Set the dsa key.",
.group = 0
},
{
.name = "rsakey",
.key = 'r',
.arg = "FILE",
.flags = 0,
.doc = "Set the rsa key.",
.group = 0
},
{NULL, 0, 0, 0, NULL, 0}
};
/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
int i;
/* Get the input argument from argp_parse, which we
* know is a pointer to our arguments structure.
*/
ssh_bind sshbind = state->input;
switch (key) {
case 'p':
i = atoi(arg);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &i);
break;
case 'd':
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, arg);
break;
case 'k':
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, arg);
break;
case 'r':
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, arg);
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1) {
/* Too many arguments. */
argp_usage (state);
}
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, arg);
break;
case ARGP_KEY_END:
if (state->arg_num < 1) {
/* Not enough arguments. */
argp_usage (state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
int main(int argc, char **argv){ int main(int argc, char **argv){
ssh_session session; ssh_session session;
ssh_bind sshbind; ssh_bind sshbind;
@ -46,10 +140,16 @@ int main(int argc, char **argv){
sshbind=ssh_bind_new(); sshbind=ssh_bind_new();
session=ssh_new(); session=ssh_new();
ssh_options_getopt(session,&argc,argv);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key"); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key"); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
/*
* Parse our arguments; every option seen by parse_opt will
* be reflected in arguments.
*/
argp_parse (&argp, argc, argv, 0, 0, sshbind);
if(ssh_bind_listen(sshbind)<0){ if(ssh_bind_listen(sshbind)<0){
printf("Error listening to socket: %s\n",ssh_get_error(sshbind)); printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
return 1; return 1;

View File

@ -39,6 +39,7 @@ extern "C" {
enum ssh_bind_options_e { enum ssh_bind_options_e {
SSH_BIND_OPTIONS_BINDADDR, SSH_BIND_OPTIONS_BINDADDR,
SSH_BIND_OPTIONS_BINDPORT, SSH_BIND_OPTIONS_BINDPORT,
SSH_BIND_OPTIONS_BINDPORT_STR,
SSH_BIND_OPTIONS_HOSTKEY, SSH_BIND_OPTIONS_HOSTKEY,
SSH_BIND_OPTIONS_DSAKEY, SSH_BIND_OPTIONS_DSAKEY,
SSH_BIND_OPTIONS_RSAKEY, SSH_BIND_OPTIONS_RSAKEY,

View File

@ -621,10 +621,8 @@ static int ssh_bind_options_set_algo(ssh_bind sshbind, int algo,
int ssh_bind_options_set(ssh_bind sshbind, enum ssh_bind_options_e type, int ssh_bind_options_set(ssh_bind sshbind, enum ssh_bind_options_e type,
const void *value) { const void *value) {
#if 0
char *p, *q; char *p, *q;
int i; int i;
#endif
if (sshbind == NULL) { if (sshbind == NULL) {
return -1; return -1;
@ -661,6 +659,24 @@ int ssh_bind_options_set(ssh_bind sshbind, enum ssh_bind_options_e type,
sshbind->bindport = *x & 0xffff; sshbind->bindport = *x & 0xffff;
} }
break; break;
case SSH_BIND_OPTIONS_BINDPORT_STR:
if (value == NULL) {
sshbind->bindport = 22 & 0xffff;
} else {
q = strdup(value);
if (q == NULL) {
ssh_set_error_oom(sshbind);
return -1;
}
i = strtol(q, &p, 10);
if (q == p) {
SAFE_FREE(q);
}
SAFE_FREE(q);
sshbind->bindport = i & 0xffff;
}
break;
case SSH_BIND_OPTIONS_DSAKEY: case SSH_BIND_OPTIONS_DSAKEY:
if (value == NULL) { if (value == NULL) {
ssh_set_error_invalid(sshbind, __FUNCTION__); ssh_set_error_invalid(sshbind, __FUNCTION__);