MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
client.cpp
1 /*
2  Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
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; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* client.cpp */
20 
21 #include "../../testsuite/test.hpp"
22 
23 //#define TEST_RESUME
24 
25 
26 void ClientError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& sockfd, const char* msg)
27 {
28  SSL_CTX_free(ctx);
29  SSL_free(ssl);
30  tcp_close(sockfd);
31  err_sys(msg);
32 }
33 
34 
35 #ifdef NON_BLOCKING
36  void NonBlockingSSL_Connect(SSL* ssl, SSL_CTX* ctx, SOCKET_T& sockfd)
37  {
38  int ret = SSL_connect(ssl);
39  int err = SSL_get_error(ssl, 0);
40  while (ret != SSL_SUCCESS && (err == SSL_ERROR_WANT_READ ||
41  err == SSL_ERROR_WANT_WRITE)) {
42  if (err == SSL_ERROR_WANT_READ)
43  printf("... client would read block\n");
44  else
45  printf("... client would write block\n");
46  #ifdef _WIN32
47  Sleep(1000);
48  #else
49  sleep(1);
50  #endif
51  ret = SSL_connect(ssl);
52  err = SSL_get_error(ssl, 0);
53  }
54  if (ret != SSL_SUCCESS)
55  ClientError(ctx, ssl, sockfd, "SSL_connect failed");
56  }
57 #endif
58 
59 
60 void client_test(void* args)
61 {
62 #ifdef _WIN32
63  WSADATA wsd;
64  WSAStartup(0x0002, &wsd);
65 #endif
66 
67  SOCKET_T sockfd = 0;
68  int argc = 0;
69  char** argv = 0;
70 
71  set_args(argc, argv, *static_cast<func_args*>(args));
72  tcp_connect(sockfd);
73 #ifdef NON_BLOCKING
74  tcp_set_nonblocking(sockfd);
75 #endif
76 
77  SSL_METHOD* method = TLSv1_client_method();
78  SSL_CTX* ctx = SSL_CTX_new(method);
79 
80  set_certs(ctx);
81  SSL* ssl = SSL_new(ctx);
82 
83  SSL_set_fd(ssl, sockfd);
84 
85 
86 #ifdef NON_BLOCKING
87  NonBlockingSSL_Connect(ssl, ctx, sockfd);
88 #else
89  // if you get an error here see note at top of README
90  if (SSL_connect(ssl) != SSL_SUCCESS)
91  ClientError(ctx, ssl, sockfd, "SSL_connect failed");
92 #endif
93  showPeer(ssl);
94 
95  const char* cipher = 0;
96  int index = 0;
97  char list[1024];
98  strncpy(list, "cipherlist", 11);
99  while ( (cipher = SSL_get_cipher_list(ssl, index++)) ) {
100  strncat(list, ":", 2);
101  strncat(list, cipher, strlen(cipher) + 1);
102  }
103  printf("%s\n", list);
104  printf("Using Cipher Suite: %s\n", SSL_get_cipher(ssl));
105 
106  char msg[] = "hello yassl!";
107  if (SSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
108  ClientError(ctx, ssl, sockfd, "SSL_write failed");
109 
110  char reply[1024];
111  int input = SSL_read(ssl, reply, sizeof(reply));
112  if (input > 0) {
113  reply[input] = 0;
114  printf("Server response: %s\n", reply);
115  }
116 
117 #ifdef TEST_RESUME
118  SSL_SESSION* session = SSL_get_session(ssl);
119  SSL* sslResume = SSL_new(ctx);
120 #endif
121 
122  SSL_shutdown(ssl);
123  SSL_free(ssl);
124  tcp_close(sockfd);
125 
126 #ifdef TEST_RESUME
127  tcp_connect(sockfd);
128  SSL_set_fd(sslResume, sockfd);
129  SSL_set_session(sslResume, session);
130 
131  if (SSL_connect(sslResume) != SSL_SUCCESS)
132  ClientError(ctx, sslResume, sockfd, "SSL_resume failed");
133  showPeer(sslResume);
134 
135  if (SSL_write(sslResume, msg, sizeof(msg)) != sizeof(msg))
136  ClientError(ctx, sslResume, sockfd, "SSL_write failed");
137 
138  input = SSL_read(sslResume, reply, sizeof(reply));
139  if (input > 0) {
140  reply[input] = 0;
141  printf("Server response: %s\n", reply);
142  }
143 
144  SSL_shutdown(sslResume);
145  SSL_free(sslResume);
146  tcp_close(sockfd);
147 #endif // TEST_RESUME
148 
149  SSL_CTX_free(ctx);
150  ((func_args*)args)->return_code = 0;
151 }
152 
153 
154 #ifndef NO_MAIN_DRIVER
155 
156  int main(int argc, char** argv)
157  {
158  func_args args;
159 
160  args.argc = argc;
161  args.argv = argv;
162 
163  client_test(&args);
164  yaSSL_CleanUp();
165 
166  return args.return_code;
167  }
168 
169 #endif // NO_MAIN_DRIVER
170