2009-12-01 00:35:43 +03:00
|
|
|
/*
|
|
|
|
* This file is part of the SSH Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 by Aris Adamantiadis
|
|
|
|
*
|
|
|
|
* The SSH Library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SSH Library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with the SSH Library; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
* MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple test for the socket callbacks */
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
|
|
|
|
#include <libssh/callbacks.h>
|
|
|
|
#include <libssh/socket.h>
|
2009-12-01 01:44:40 +03:00
|
|
|
#include <libssh/poll.h>
|
2009-12-01 00:35:43 +03:00
|
|
|
|
2009-12-02 01:34:55 +03:00
|
|
|
int stop=0;
|
2009-12-07 00:48:56 +03:00
|
|
|
ssh_socket s;
|
2009-12-02 01:34:55 +03:00
|
|
|
|
2009-12-01 00:35:43 +03:00
|
|
|
static int data_rcv(const void *data, size_t len, void *user){
|
|
|
|
printf("Received data: '");
|
|
|
|
fwrite(data,1,len,stdout);
|
|
|
|
printf("'\n");
|
2009-12-02 01:34:55 +03:00
|
|
|
ssh_socket_write(s,"Hello you !\n",12);
|
|
|
|
ssh_socket_nonblocking_flush(s);
|
2009-12-01 00:35:43 +03:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-12-01 01:44:40 +03:00
|
|
|
static void controlflow(int code,void *user){
|
2009-12-01 00:35:43 +03:00
|
|
|
printf("Control flow: %x\n",code);
|
|
|
|
}
|
|
|
|
|
2009-12-01 01:44:40 +03:00
|
|
|
static void exception(int code, int errno_code,void *user){
|
2009-12-01 00:35:43 +03:00
|
|
|
printf("Exception: %d (%d)\n",code,errno_code);
|
2009-12-02 01:34:55 +03:00
|
|
|
stop=1;
|
2009-12-01 00:35:43 +03:00
|
|
|
}
|
|
|
|
|
2009-12-01 01:44:40 +03:00
|
|
|
static void connected(int code, int errno_code,void *user){
|
2009-12-02 01:34:55 +03:00
|
|
|
if(code == SSH_SOCKET_CONNECTED_OK)
|
|
|
|
printf("Connected: %d (%d)\n",code, errno_code);
|
|
|
|
else {
|
|
|
|
printf("Error while connecting:(%d, %d:%s)\n",code,errno_code,strerror(errno_code));
|
|
|
|
stop=1;
|
|
|
|
}
|
2009-12-01 00:35:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ssh_socket_callbacks_struct callbacks={
|
|
|
|
data_rcv,
|
|
|
|
controlflow,
|
|
|
|
exception,
|
|
|
|
connected,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int main(int argc, char **argv){
|
|
|
|
ssh_session session;
|
|
|
|
ssh_poll_ctx ctx;
|
2009-12-01 01:56:21 +03:00
|
|
|
int verbosity=SSH_LOG_FUNCTIONS;
|
2009-12-01 00:35:43 +03:00
|
|
|
if(argc < 3){
|
|
|
|
printf("Usage : %s host port\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
session=ssh_new();
|
2009-12-01 01:56:21 +03:00
|
|
|
ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity);
|
2009-12-01 01:44:40 +03:00
|
|
|
ssh_init();
|
2009-12-01 00:35:43 +03:00
|
|
|
s=ssh_socket_new(session);
|
|
|
|
ctx=ssh_poll_ctx_new(2);
|
|
|
|
ssh_socket_set_callbacks(s, &callbacks);
|
|
|
|
ssh_poll_ctx_add_socket(ctx,s);
|
2009-12-02 01:34:55 +03:00
|
|
|
if(ssh_socket_connect(s,argv[1],atoi(argv[2]),NULL) != SSH_OK){
|
2009-12-01 01:44:40 +03:00
|
|
|
printf("ssh_socket_connect: %s\n",ssh_get_error(session));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2009-12-02 01:34:55 +03:00
|
|
|
while(!stop)
|
|
|
|
ssh_poll_ctx_dopoll(ctx,-1);
|
|
|
|
printf("finished\n");
|
2009-12-01 00:35:43 +03:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|