1 /* Copyright (c) 2000 MySQL AB
2    Use is subject to license terms.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 /*
18 **  Virtual I/O library
19 **  Written by Andrei Errapart <andreie@no.spam.ee>
20 */
21 
22 #include	"all.h"
23 
24 #include	<sys/types.h>
25 #include	<sys/socket.h>
26 #include	<netinet/in.h>
27 #include	<stdio.h>
28 #include	<unistd.h>
29 
30 
31 void
fatal_error(const char * r)32 fatal_error(	const char*	r)
33 {
34 	perror(r);
35 	exit(0);
36 }
37 
38 void
print_usage()39 print_usage()
40 {
41 	printf("viotest-sslconnect: testing SSL virtual IO. Usage:\n");
42 	printf("viotest-sslconnect key cert\n");
43 }
44 
45 int
main(int argc,char ** argv)46 main(	int	argc,
47 	char**	argv)
48 {
49 	char*	key = 0;
50 	char*	cert = 0;
51 
52 	if (argc<3)
53 	{
54 		print_usage();
55 		return 1;
56 	}
57 
58 	char		ip[4] = {127, 0, 0, 1};
59 	unsigned long	addr = (unsigned long)
60 			((unsigned long)ip[0]<<24L)|
61 			((unsigned long)ip[1]<<16L)|
62 			((unsigned long)ip[2]<< 8L)|
63 			((unsigned long)ip[3]);
64 	int	fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
65 	if (fd<0)
66 		fatal_error("socket");
67 	struct sockaddr_in	sa;
68 	sa.sin_family = AF_INET;
69 	sa.sin_port=htons(4433);
70 	sa.sin_addr.s_addr=htonl(addr);
71 	int	sa_size = sizeof sa;
72 	if (connect(fd, reinterpret_cast<const sockaddr*>(&sa), sa_size)==-1)
73 		fatal_error("connect");
74 	key = argv[1];
75 	cert = argv[2];
76 	printf("Key  : %s\n", key);
77 	printf("Cert : %s\n", cert);
78 
79 	VIO_NS::VioSSLConnectorFd*	ssl_connector = new VIO_NS::VioSSLConnectorFd(cert, key,0,0);
80 
81 	VIO_NS::VioSSL*	vio = ssl_connector->connect(fd);
82 
83 	char	xbuf[100];
84 	int	r = vio->read(xbuf, sizeof(xbuf));
85 	if (r<=0) {
86 		delete ssl_connector;
87 		delete vio;
88 		fatal_error("client:SSL_read");
89 	}
90 	xbuf[r] = 0;
91 	printf("client:got %s\n", xbuf);
92 	delete vio;
93 	delete ssl_connector;
94 	return 0;
95 }
96