MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
server.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 /* server.cpp */
20 
21 
22 #include "../../testsuite/test.hpp"
23 
24 
25 void ServerError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& sockfd, const char* msg)
26 {
27  SSL_CTX_free(ctx);
28  SSL_free(ssl);
29  tcp_close(sockfd);
30  err_sys(msg);
31 }
32 
33 
34 #ifdef NON_BLOCKING
35  void NonBlockingSSL_Accept(SSL* ssl, SSL_CTX* ctx, SOCKET_T& clientfd)
36  {
37  int ret = SSL_accept(ssl);
38  int err = SSL_get_error(ssl, 0);
39  while (ret != SSL_SUCCESS && (err == SSL_ERROR_WANT_READ ||
40  err == SSL_ERROR_WANT_WRITE)) {
41  if (err == SSL_ERROR_WANT_READ)
42  printf("... server would read block\n");
43  else
44  printf("... server would write block\n");
45  #ifdef _WIN32
46  Sleep(1000);
47  #else
48  sleep(1);
49  #endif
50  ret = SSL_accept(ssl);
51  err = SSL_get_error(ssl, 0);
52  }
53  if (ret != SSL_SUCCESS)
54  ServerError(ctx, ssl, clientfd, "SSL_accept failed");
55  }
56 #endif
57 
58 
59 THREAD_RETURN YASSL_API server_test(void* args)
60 {
61 #ifdef _WIN32
62  WSADATA wsd;
63  WSAStartup(0x0002, &wsd);
64 #endif
65 
66  SOCKET_T sockfd = 0;
67  SOCKET_T clientfd = 0;
68  int argc = 0;
69  char** argv = 0;
70 
71  set_args(argc, argv, *static_cast<func_args*>(args));
72  tcp_accept(sockfd, clientfd, *static_cast<func_args*>(args));
73 
74  tcp_close(sockfd);
75 
76  SSL_METHOD* method = TLSv1_server_method();
77  SSL_CTX* ctx = SSL_CTX_new(method);
78 
79  //SSL_CTX_set_cipher_list(ctx, "RC4-SHA:RC4-MD5");
80  SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
81  set_serverCerts(ctx);
82  DH* dh = set_tmpDH(ctx);
83 
84  SSL* ssl = SSL_new(ctx);
85  SSL_set_fd(ssl, clientfd);
86 
87 #ifdef NON_BLOCKING
88  NonBlockingSSL_Accept(ssl, ctx, clientfd);
89 #else
90  if (SSL_accept(ssl) != SSL_SUCCESS)
91  ServerError(ctx, ssl, clientfd, "SSL_accept failed");
92 #endif
93 
94  showPeer(ssl);
95  printf("Using Cipher Suite: %s\n", SSL_get_cipher(ssl));
96 
97  char command[1024];
98  int input = SSL_read(ssl, command, sizeof(command));
99  if (input > 0) {
100  command[input] = 0;
101  printf("First client command: %s\n", command);
102  }
103 
104  char msg[] = "I hear you, fa shizzle!";
105  if (SSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
106  ServerError(ctx, ssl, clientfd, "SSL_write failed");
107 
108  DH_free(dh);
109  SSL_CTX_free(ctx);
110  SSL_shutdown(ssl);
111  SSL_free(ssl);
112 
113  tcp_close(clientfd);
114 
115  ((func_args*)args)->return_code = 0;
116  return 0;
117 }
118 
119 
120 #ifndef NO_MAIN_DRIVER
121 
122  int main(int argc, char** argv)
123  {
124  func_args args;
125 
126  args.argc = argc;
127  args.argv = argv;
128 
129  server_test(&args);
130  yaSSL_CleanUp();
131 
132  return args.return_code;
133  }
134 
135 #endif // NO_MAIN_DRIVER
136