26 #include "runtime.hpp"
27 #include "socket_wrapper.hpp"
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
35 #include <sys/ioctl.h>
40 #if defined(__sun) || defined(__SCO_VERSION__)
41 #include <sys/filio.h>
45 const int SOCKET_EINVAL = WSAEINVAL;
46 const int SOCKET_EWOULDBLOCK = WSAEWOULDBLOCK;
47 const int SOCKET_EAGAIN = WSAEWOULDBLOCK;
49 const int SOCKET_EINVAL = EINVAL;
50 const int SOCKET_EWOULDBLOCK = EWOULDBLOCK;
51 const int SOCKET_EAGAIN = EAGAIN;
58 extern "C" long system_recv(
void *ptr,
void *
buf,
size_t count)
60 yaSSL::socket_t *socket = (yaSSL::socket_t *) ptr;
61 return ::recv(*socket, reinterpret_cast<char *>(buf), count, 0);
65 extern "C" long system_send(
void *ptr,
const void *buf,
size_t count)
67 yaSSL::socket_t *socket = (yaSSL::socket_t *) ptr;
68 return ::send(*socket, reinterpret_cast<const char *>(buf), count, 0);
78 Socket::Socket(socket_t s)
79 : socket_(s), wouldBlock_(false), nonBlocking_(false),
80 ptr_(&socket_), send_func_(system_send), recv_func_(system_recv)
84 void Socket::set_fd(socket_t s)
90 socket_t Socket::get_fd()
const
102 void Socket::closeSocket()
104 if (socket_ != INVALID_SOCKET) {
106 closesocket(socket_);
110 socket_ = INVALID_SOCKET;
115 uint Socket::get_ready()
const
118 unsigned long ready = 0;
119 ioctlsocket(socket_, FIONREAD, &ready);
125 unsigned int ready = 0;
126 ioctl(socket_, FIONREAD, &ready);
133 void Socket::set_transport_ptr(
void *ptr)
139 void Socket::set_transport_recv_function(yaSSL_recv_func_t recv_func)
141 recv_func_ = recv_func;
145 void Socket::set_transport_send_function(yaSSL_send_func_t send_func)
147 send_func_ = send_func;
151 uint Socket::send(
const byte* buf,
unsigned int sz,
unsigned int& written)
153 const byte* pos =
buf;
154 const byte* end = pos + sz;
160 int sent = send_func_(ptr_, pos, static_cast<int>(end - pos));
162 if (get_lastError() == SOCKET_EWOULDBLOCK ||
163 get_lastError() == SOCKET_EAGAIN) {
168 return static_cast<uint
>(-1);
178 uint Socket::receive(byte* buf,
unsigned int sz)
182 int recvd = recv_func_(ptr_, buf, sz);
186 if (get_lastError() == SOCKET_EWOULDBLOCK ||
187 get_lastError() == SOCKET_EAGAIN) {
194 return static_cast<uint
>(-1);
200 void Socket::shutDown(
int how)
202 shutdown(socket_, how);
206 int Socket::get_lastError()
209 return WSAGetLastError();
216 bool Socket::WouldBlock()
const
222 bool Socket::IsNonBlocking()
const
228 void Socket::set_lastError(
int errorCode)
231 WSASetLastError(errorCode);