~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viotest-ssl.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <my_global.h>
 
17
#ifdef HAVE_OPENSSL
 
18
#include <my_sys.h>
 
19
#include <m_string.h>
 
20
#include <m_ctype.h>
 
21
#include "mysql.h"
 
22
#include "errmsg.h"
 
23
#include <my_dir.h>
 
24
#include <my_getopt.h>
 
25
#include <signal.h>
 
26
#include <violite.h>
 
27
 
 
28
const char *VER="0.2";
 
29
 
 
30
 
 
31
#ifndef DBUG_OFF
 
32
const char *default_dbug_option="d:t:O,/tmp/viotest-ssl.trace";
 
33
#endif
 
34
 
 
35
void
 
36
fatal_error(const char *r)
 
37
{
 
38
  perror(r);
 
39
  exit(0);
 
40
}
 
41
 
 
42
void
 
43
print_usage()
 
44
{
 
45
  printf("viossl-test: testing SSL virtual IO. Usage:\n");
 
46
  printf("viossl-test server-key server-cert client-key client-cert [CAfile] [CApath]\n");
 
47
}
 
48
 
 
49
 
 
50
int main(int argc, char **argv)
 
51
{
 
52
  char* server_key = 0;
 
53
  char* server_cert = 0;
 
54
  char* client_key = 0;
 
55
  char* client_cert = 0;
 
56
  char* ca_file = 0;
 
57
  char* ca_path = 0;
 
58
  int   child_pid,sv[2];
 
59
  struct st_VioSSLAcceptorFd* ssl_acceptor=0;
 
60
  struct st_VioSSLConnectorFd* ssl_connector=0; 
 
61
  Vio* client_vio=0;
 
62
  Vio* server_vio=0;
 
63
  MY_INIT(argv[0]);
 
64
  DBUG_PROCESS(argv[0]);
 
65
  DBUG_PUSH(default_dbug_option);
 
66
 
 
67
  if (argc<5)
 
68
  {
 
69
    print_usage();
 
70
    return 1;
 
71
  }
 
72
 
 
73
  server_key = argv[1];
 
74
  server_cert = argv[2];
 
75
  client_key = argv[3];
 
76
  client_cert = argv[4];
 
77
  if (argc>5)
 
78
    ca_file = argv[5];
 
79
  if (argc>6)
 
80
    ca_path = argv[6];
 
81
  printf("Server key/cert : %s/%s\n", server_key, server_cert);
 
82
  printf("Client key/cert : %s/%s\n", client_key, client_cert);
 
83
  if (ca_file!=0)
 
84
    printf("CAfile          : %s\n", ca_file);
 
85
  if (ca_path!=0)
 
86
    printf("CApath          : %s\n", ca_path);
 
87
 
 
88
 
 
89
  if (socketpair(PF_UNIX, SOCK_STREAM, IPPROTO_IP, sv)==-1)
 
90
    fatal_error("socketpair");
 
91
 
 
92
  ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file,
 
93
                                      ca_path);
 
94
  ssl_connector = new_VioSSLConnectorFd(client_key, client_cert, ca_file,
 
95
                                        ca_path);
 
96
 
 
97
  client_vio = (Vio*)my_malloc(sizeof(struct st_vio),MYF(0));
 
98
  client_vio->sd = sv[0];
 
99
  sslconnect(ssl_connector,client_vio);
 
100
  server_vio = (Vio*)my_malloc(sizeof(struct st_vio),MYF(0));
 
101
  server_vio->sd = sv[1];
 
102
  sslaccept(ssl_acceptor,server_vio);
 
103
 
 
104
  printf("Socketpair: %d , %d\n", client_vio->sd, server_vio->sd);
 
105
 
 
106
  child_pid = fork();
 
107
  if (child_pid==-1)
 
108
  {
 
109
    my_free((uchar*)ssl_acceptor,MYF(0));
 
110
    my_free((uchar*)ssl_connector,MYF(0));
 
111
    fatal_error("fork");
 
112
  }
 
113
  if (child_pid==0)
 
114
  {
 
115
    /* child, therefore, client */
 
116
    char        xbuf[100];
 
117
    int r = vio_ssl_read(client_vio,xbuf, sizeof(xbuf));
 
118
    if (r<=0) {
 
119
      my_free((uchar*)ssl_acceptor,MYF(0));
 
120
      my_free((uchar*)ssl_connector,MYF(0));
 
121
      fatal_error("client:SSL_read");
 
122
    }
 
123
    xbuf[r] = 0;
 
124
    printf("client:got %s\n", xbuf);
 
125
    my_free((uchar*)client_vio,MYF(0));
 
126
    my_free((uchar*)ssl_acceptor,MYF(0));
 
127
    my_free((uchar*)ssl_connector,MYF(0));
 
128
    sleep(1);
 
129
  }
 
130
  else
 
131
  {
 
132
    const char* s = "Huhuhuh";
 
133
    int         r = vio_ssl_write(server_vio,(uchar*)s, strlen(s));
 
134
    if (r<=0) {
 
135
      my_free((uchar*)ssl_acceptor,MYF(0));
 
136
      my_free((uchar*)ssl_connector,MYF(0));
 
137
      fatal_error("server:SSL_write");
 
138
    }
 
139
    my_free((uchar*)server_vio,MYF(0));
 
140
    my_free((uchar*)ssl_acceptor,MYF(0));
 
141
    my_free((uchar*)ssl_connector,MYF(0));
 
142
    sleep(1);
 
143
  }
 
144
  return 0;
 
145
}
 
146
#else /* HAVE_OPENSSL */
 
147
 
 
148
int main() {
 
149
return 0;
 
150
}
 
151
#endif /* HAVE_OPENSSL */