1 /* 2 Copyright (c) 2000, 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 58 59 typedef unsigned char byte; 60 61 62 // Wraps Windows Sockets and BSD Sockets 63 class Socket { 64 socket_t socket_; // underlying socket descriptor 65 bool wouldBlock_; // if non-blocking data, for last read 66 bool nonBlocking_; // is option set 67 public: 68 explicit Socket(socket_t s = INVALID_SOCKET); 69 ~Socket(); 70 71 void set_fd(socket_t s); 72 uint get_ready() const; 73 socket_t get_fd() const; 74 75 uint send(const byte* buf, unsigned int len, unsigned int& sent, 76 int flags = 0); 77 uint receive(byte* buf, unsigned int len, int flags = 0); 78 79 bool wait(); 80 bool WouldBlock() const; 81 bool IsNonBlocking() const; 82 83 void closeSocket(); 84 void shutDown(int how = SD_SEND); 85 86 static int get_lastError(); 87 static void set_lastError(int error); 88 private: 89 Socket(const Socket&); // hide copy 90 Socket& operator= (const Socket&); // and assign 91 }; 92 93 94 } // naemspace 95 96 #endif // yaSSL_SOCKET_WRAPPER_HPP 97