MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
socket_wrapper.hpp
1 /*
2  Copyright (c) 2005, 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 
20 /* The socket wrapper header defines a Socket class that hides the differences
21  * between Berkely style sockets and Windows sockets, allowing transparent TCP
22  * access.
23  */
24 
25 
26 #ifndef yaSSL_SOCKET_WRAPPER_HPP
27 #define yaSSL_SOCKET_WRAPPER_HPP
28 
29 
30 #ifdef _WIN32
31  #include <winsock2.h>
32 #else
33  #include <sys/time.h>
34  #include <sys/types.h>
35  #include <sys/socket.h>
36  #include <unistd.h>
37  #include <netinet/in.h>
38  #include <arpa/inet.h>
39 #endif
40 
41 
42 namespace yaSSL {
43 
44 typedef unsigned int uint;
45 
46 #ifdef _WIN32
47  typedef SOCKET socket_t;
48 #else
49  typedef int socket_t;
50  const socket_t INVALID_SOCKET = -1;
51  const int SD_RECEIVE = 0;
52  const int SD_SEND = 1;
53  const int SD_BOTH = 2;
54  const int SOCKET_ERROR = -1;
55 #endif
56 
57  extern "C" {
58  #include "openssl/transport_types.h"
59  }
60 
61 typedef unsigned char byte;
62 
63 
64 // Wraps Windows Sockets and BSD Sockets
65 class Socket {
66  socket_t socket_; // underlying socket descriptor
67  bool wouldBlock_; // if non-blocking data, for last read
68  bool nonBlocking_; // is option set
69  void *ptr_; // Argument to transport function
70  yaSSL_send_func_t send_func_; // Function to send data
71  yaSSL_recv_func_t recv_func_; // Function to receive data
72 public:
73  explicit Socket(socket_t s = INVALID_SOCKET);
74  ~Socket();
75 
76  void set_fd(socket_t s);
77  uint get_ready() const;
78  socket_t get_fd() const;
79 
80  void set_transport_ptr(void *ptr);
81  void set_transport_recv_function(yaSSL_recv_func_t recv_func);
82  void set_transport_send_function(yaSSL_send_func_t send_func);
83 
84  uint send(const byte* buf, unsigned int len, unsigned int& sent);
85  uint receive(byte* buf, unsigned int len);
86 
87  bool wait();
88  bool WouldBlock() const;
89  bool IsNonBlocking() const;
90 
91  void closeSocket();
92  void shutDown(int how = SD_SEND);
93 
94  static int get_lastError();
95  static void set_lastError(int error);
96 private:
97  Socket(const Socket&); // hide copy
98  Socket& operator= (const Socket&); // and assign
99 };
100 
101 
102 } // naemspace
103 
104 #endif // yaSSL_SOCKET_WRAPPER_HPP