~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* 
2
**  Virtual I/O library
3
**  Written by Andrei Errapart <andreie@no.spam.ee>
4
*/
5
6
#include	"all.h"
7
8
#include	<sys/types.h>
9
#include	<sys/socket.h>
10
#include	<netinet/in.h>
11
#include	<stdio.h>
12
#include	<unistd.h>
13
14
15
void
16
fatal_error(	const char*	r)
17
{
18
	perror(r);
19
	exit(0);
20
}
21
22
void
23
print_usage()
24
{
25
	printf("viotest-sslconnect: testing SSL virtual IO. Usage:\n");
26
	printf("viotest-sslconnect key cert\n");
27
}
28
29
int
30
main(	int	argc,
31
	char**	argv)
32
{
33
	char*	key = 0;
34
	char*	cert = 0;
35
36
	if (argc<3)
37
	{
38
		print_usage();
39
		return 1;
40
	}
41
42
	char		ip[4] = {127, 0, 0, 1};
43
	unsigned long	addr = (unsigned long)
44
			((unsigned long)ip[0]<<24L)|
45
			((unsigned long)ip[1]<<16L)|
46
			((unsigned long)ip[2]<< 8L)|
47
			((unsigned long)ip[3]);
48
	int	fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
49
	if (fd<0)
50
		fatal_error("socket");
51
	struct sockaddr_in	sa;
52
	sa.sin_family = AF_INET;
53
	sa.sin_port=htons(4433);
54
	sa.sin_addr.s_addr=htonl(addr);
55
	int	sa_size = sizeof sa;
56
	if (connect(fd, reinterpret_cast<const sockaddr*>(&sa), sa_size)==-1)
57
		fatal_error("connect");
58
	key = argv[1];
59
	cert = argv[2];
60
	printf("Key  : %s\n", key);
61
	printf("Cert : %s\n", cert);
62
63
	VIO_NS::VioSSLConnectorFd*	ssl_connector = new VIO_NS::VioSSLConnectorFd(cert, key,0,0);
64
65
	VIO_NS::VioSSL*	vio = ssl_connector->connect(fd);
66
67
	char	xbuf[100];
68
	int	r = vio->read(xbuf, sizeof(xbuf));
69
	if (r<=0) {
70
		delete ssl_connector;
71
		delete vio;
72
		fatal_error("client:SSL_read");
73
	}
74
	xbuf[r] = 0;
75
	printf("client:got %s\n", xbuf);
76
	delete vio;
77
	delete ssl_connector;
78
	return 0;
79
}
80