Blame tests/test_socket.c

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