1
0
mirror of https://github.com/MariaDB/server.git synced 2025-09-02 09:41:40 +03:00
Files
mariadb/extra/yassl/src/socket_wrapper.cpp
unknown 62d2cadb62 Import yaSSL version 1.4.0
extra/yassl/FLOSS-EXCEPTIONS:
  Import patch yassl.diff
extra/yassl/README:
  Import patch yassl.diff
extra/yassl/examples/client/client.cpp:
  Import patch yassl.diff
extra/yassl/examples/echoclient/echoclient.cpp:
  Import patch yassl.diff
extra/yassl/examples/echoserver/echoserver.cpp:
  Import patch yassl.diff
extra/yassl/examples/server/server.cpp:
  Import patch yassl.diff
extra/yassl/include/buffer.hpp:
  Import patch yassl.diff
extra/yassl/include/cert_wrapper.hpp:
  Import patch yassl.diff
extra/yassl/include/crypto_wrapper.hpp:
  Import patch yassl.diff
extra/yassl/include/factory.hpp:
  Import patch yassl.diff
extra/yassl/include/openssl/ssl.h:
  Import patch yassl.diff
extra/yassl/include/socket_wrapper.hpp:
  Import patch yassl.diff
extra/yassl/include/yassl_error.hpp:
  Import patch yassl.diff
extra/yassl/include/yassl_imp.hpp:
  Import patch yassl.diff
extra/yassl/include/yassl_int.hpp:
  Import patch yassl.diff
extra/yassl/include/yassl_types.hpp:
  Import patch yassl.diff
extra/yassl/src/cert_wrapper.cpp:
  Import patch yassl.diff
extra/yassl/src/crypto_wrapper.cpp:
  Import patch yassl.diff
extra/yassl/src/handshake.cpp:
  Import patch yassl.diff
extra/yassl/src/socket_wrapper.cpp:
  Import patch yassl.diff
extra/yassl/src/ssl.cpp:
  Import patch yassl.diff
extra/yassl/src/template_instnt.cpp:
  Import patch yassl.diff
extra/yassl/src/yassl.cpp:
  Import patch yassl.diff
extra/yassl/src/yassl_error.cpp:
  Import patch yassl.diff
extra/yassl/src/yassl_imp.cpp:
  Import patch yassl.diff
extra/yassl/src/yassl_int.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/COPYING:
  Import patch yassl.diff
extra/yassl/taocrypt/INSTALL:
  Import patch yassl.diff
extra/yassl/taocrypt/README:
  Import patch yassl.diff
extra/yassl/taocrypt/include/asn.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/block.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/blowfish.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/error.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/file.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/integer.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/misc.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/pwdbased.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/include/twofish.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/algorithm.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/helpers.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/list.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/memory.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/memory_array.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/pair.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/stdexcept.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/mySTL/vector.hpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/algebra.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/asn.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/blowfish.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/des.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/dh.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/integer.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/md4.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/md5.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/random.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/ripemd.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/sha.cpp:
  Import patch yassl.diff
extra/yassl/taocrypt/src/template_instnt.cpp:
  Import patch yassl.diff
extra/yassl/testsuite/test.hpp:
  Import patch yassl.diff
BitKeeper/deleted/.del-algorithm.hpp:
  Delete: extra/yassl/mySTL/algorithm.hpp
BitKeeper/deleted/.del-helpers.hpp:
  Delete: extra/yassl/mySTL/helpers.hpp
BitKeeper/deleted/.del-list.hpp:
  Delete: extra/yassl/mySTL/list.hpp
BitKeeper/deleted/.del-pair.hpp:
  Delete: extra/yassl/mySTL/pair.hpp
BitKeeper/deleted/.del-stdexcept.hpp:
  Delete: extra/yassl/mySTL/stdexcept.hpp
BitKeeper/deleted/.del-vector.hpp:
  Delete: extra/yassl/mySTL/vector.hpp
BitKeeper/deleted/.del-memory.hpp:
  Delete: extra/yassl/mySTL/memory.hpp
extra/yassl/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/src/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/taocrypt/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/taocrypt/benchmark/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/taocrypt/src/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/taocrypt/src/misc.cpp:
  Remove the functions CRYPTO_lock and CRYPTO_add_lock as they would collide with OpenSSL functions
extra/yassl/taocrypt/test/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
extra/yassl/testsuite/Makefile.am:
  mySTL directory has moved from yassl/ to yassl/taocrypt
2006-09-25 16:40:29 +02:00

211 lines
4.3 KiB
C++

/* socket_wrapper.cpp
*
* Copyright (C) 2003 Sawtooth Consulting Ltd.
*
* This file is part of yaSSL.
*
* yaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPL as it
* is applied to yaSSL. View the full text of the exception in the file
* FLOSS-EXCEPTIONS in the directory of this software distribution.
*
* yaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* The socket wrapper source implements a Socket class that hides the
* differences between Berkely style sockets and Windows sockets, allowing
* transparent TCP access.
*/
#include "runtime.hpp"
#include "socket_wrapper.hpp"
#ifndef _WIN32
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>
#endif // _WIN32
#if defined(__sun) || defined(__SCO_VERSION__) || defined(__NETWARE__)
#include <sys/filio.h>
#endif
#ifdef _WIN32
const int SOCKET_EINVAL = WSAEINVAL;
const int SOCKET_EWOULDBLOCK = WSAEWOULDBLOCK;
const int SOCKET_EAGAIN = WSAEWOULDBLOCK;
#else
const int SOCKET_EINVAL = EINVAL;
const int SOCKET_EWOULDBLOCK = EWOULDBLOCK;
const int SOCKET_EAGAIN = EAGAIN;
#endif // _WIN32
namespace yaSSL {
Socket::Socket(socket_t s)
: socket_(s), wouldBlock_(false), blocking_(false)
{}
void Socket::set_fd(socket_t s)
{
socket_ = s;
}
socket_t Socket::get_fd() const
{
return socket_;
}
Socket::~Socket()
{
closeSocket();
}
void Socket::closeSocket()
{
if (socket_ != INVALID_SOCKET) {
#ifdef _WIN32
closesocket(socket_);
#else
close(socket_);
#endif
socket_ = INVALID_SOCKET;
}
}
uint Socket::get_ready() const
{
#ifdef _WIN32
unsigned long ready = 0;
ioctlsocket(socket_, FIONREAD, &ready);
#else
/*
64-bit Solaris requires the variable passed to
FIONREAD be a 32-bit value.
*/
unsigned int ready = 0;
ioctl(socket_, FIONREAD, &ready);
#endif
return ready;
}
uint Socket::send(const byte* buf, unsigned int sz, int flags) const
{
const byte* pos = buf;
const byte* end = pos + sz;
assert(socket_ != INVALID_SOCKET);
while (pos != end) {
int sent = ::send(socket_, reinterpret_cast<const char *>(pos),
static_cast<int>(end - pos), flags);
if (sent == -1)
return 0;
pos += sent;
}
return sz;
}
uint Socket::receive(byte* buf, unsigned int sz, int flags)
{
assert(socket_ != INVALID_SOCKET);
wouldBlock_ = false;
int recvd = ::recv(socket_, reinterpret_cast<char *>(buf), sz, flags);
// idea to seperate error from would block by arnetheduck@gmail.com
if (recvd == -1) {
if (get_lastError() == SOCKET_EWOULDBLOCK ||
get_lastError() == SOCKET_EAGAIN) {
wouldBlock_ = true;
blocking_ = true; // socket can block, only way to tell for win32
return 0;
}
}
else if (recvd == 0)
return static_cast<uint>(-1);
return recvd;
}
// wait if blocking for input, return false for error
bool Socket::wait()
{
byte b;
return receive(&b, 1, MSG_PEEK) != static_cast<uint>(-1);
}
void Socket::shutDown(int how)
{
assert(socket_ != INVALID_SOCKET);
shutdown(socket_, how);
}
int Socket::get_lastError()
{
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
bool Socket::WouldBlock() const
{
return wouldBlock_;
}
bool Socket::IsBlocking() const
{
return blocking_;
}
void Socket::set_lastError(int errorCode)
{
#ifdef _WIN32
WSASetLastError(errorCode);
#else
errno = errorCode;
#endif
}
} // namespace