Browse Source

Jupiter::Socket:

* Replaced 'host' with 'remote_host' and 'bound_host'
* Replace 'port' with 'remote_port' and 'bound_port'
* Fixed a bug involving sockets not getting closed.
release/0.19
JustinAJ 9 years ago
parent
commit
8cc73c997a
  1. 2
      Jupiter/SecureSocket.cpp
  2. 106
      Jupiter/Socket.cpp
  3. 21
      Jupiter/Socket.h
  4. BIN
      Release/Jupiter.lib

2
Jupiter/SecureSocket.cpp

@ -249,7 +249,7 @@ bool Jupiter::SecureSocket::initSSL()
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return false; return false;
} }
if (SSL_set_tlsext_host_name(Jupiter::SecureSocket::SSLdata_->handle, this->getHost()) != 1) // This error check is potentially redundant, but no documentation has been found. if (SSL_set_tlsext_host_name(Jupiter::SecureSocket::SSLdata_->handle, this->getRemoteHostnameC()) != 1) // This error check is potentially redundant, but no documentation has been found.
{ {
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return false; return false;

106
Jupiter/Socket.cpp

@ -54,11 +54,13 @@ char *Jupiter::Socket::Buffer::get_str() const
struct Jupiter::Socket::Data struct Jupiter::Socket::Data
{ {
Jupiter::Socket::Buffer buffer; Jupiter::Socket::Buffer buffer;
SOCKET rawSock; SOCKET rawSock = INVALID_SOCKET;
unsigned short port; unsigned short remote_port = 0;
Jupiter::CStringS host; unsigned short bound_port = 0;
int sockType; Jupiter::CStringS remote_host;
int sockProto; Jupiter::CStringS bound_host;
int sockType = SOCK_RAW;
int sockProto = IPPROTO_RAW;
#if defined _WIN32 #if defined _WIN32
unsigned long blockMode = 0; unsigned long blockMode = 0;
#endif #endif
@ -69,20 +71,18 @@ struct Jupiter::Socket::Data
Jupiter::Socket::Data::Data(size_t buffer_size) Jupiter::Socket::Data::Data(size_t buffer_size)
{ {
Jupiter::Socket::Data::buffer.setBufferSizeNoCopy(buffer_size); Jupiter::Socket::Data::buffer.setBufferSizeNoCopy(buffer_size);
Jupiter::Socket::Data::port = 0;
Jupiter::Socket::Data::rawSock = 0;
Jupiter::Socket::Data::sockType = SOCK_RAW;
Jupiter::Socket::Data::sockProto = IPPROTO_RAW;
} }
Jupiter::Socket::Data::Data(const Data &source) Jupiter::Socket::Data::Data(const Data &source)
{ {
Jupiter::Socket::Data::buffer = source.buffer; Jupiter::Socket::Data::buffer = source.buffer;
Jupiter::Socket::Data::port = source.port; Jupiter::Socket::Data::remote_port = source.remote_port;
Jupiter::Socket::Data::bound_port = source.bound_port;
Jupiter::Socket::Data::rawSock = source.rawSock; Jupiter::Socket::Data::rawSock = source.rawSock;
Jupiter::Socket::Data::sockType = source.sockType; Jupiter::Socket::Data::sockType = source.sockType;
Jupiter::Socket::Data::sockProto = source.sockProto; Jupiter::Socket::Data::sockProto = source.sockProto;
Jupiter::Socket::Data::host = source.host; Jupiter::Socket::Data::remote_host = source.remote_host;
Jupiter::Socket::Data::bound_host = source.bound_host;
#if defined _WIN32 #if defined _WIN32
Jupiter::Socket::Data::blockMode = source.blockMode; Jupiter::Socket::Data::blockMode = source.blockMode;
#endif #endif
@ -158,28 +158,42 @@ bool Jupiter::Socket::connect(const char *hostname, unsigned short iPort, const
if (!socketInit && !Jupiter::Socket::init()) if (!socketInit && !Jupiter::Socket::init())
return false; return false;
#endif // _WIN32 #endif // _WIN32
Jupiter::Socket::data_->host.set(hostname); Jupiter::Socket::data_->remote_host.set(hostname);
Jupiter::Socket::data_->port = iPort; Jupiter::Socket::data_->remote_port = iPort;
addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->port).c_str()); addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->remote_host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->remote_port).c_str());
if (info_head != nullptr) if (info_head != nullptr)
{ {
addrinfo *info = info_head; addrinfo *info = info_head;
do do
{ {
Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); if (clientAddress != nullptr)
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET)
{ {
info = info->ai_next; // bind will initialize our socket
continue; if (Jupiter::Socket::bind(clientAddress, clientPort, false) == false)
break;
}
else
{
Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto);
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET)
{
info = info->ai_next;
continue;
}
} }
if (clientAddress != nullptr)
this->bind(clientAddress, clientPort, false);
if (::connect(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR) if (::connect(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR)
{ {
#if defined _WIN32
closesocket(Jupiter::Socket::data_->rawSock);
#else // _WIN32
close(Jupiter::Socket::data_->rawSock);
#endif // WIN32
Jupiter::Socket::data_->rawSock = INVALID_SOCKET;
info = info->ai_next; info = info->ai_next;
continue; continue;
} }
Jupiter::Socket::freeAddrInfo(info_head); Jupiter::Socket::freeAddrInfo(info_head);
return true; return true;
} while (info != nullptr); } while (info != nullptr);
@ -194,20 +208,33 @@ bool Jupiter::Socket::bind(const char *hostname, unsigned short iPort, bool andL
if (!socketInit && !Jupiter::Socket::init()) if (!socketInit && !Jupiter::Socket::init())
return false; return false;
#endif // _WIN32 #endif // _WIN32
Jupiter::Socket::data_->host.set(hostname); Jupiter::Socket::data_->bound_host.set(hostname);
Jupiter::Socket::data_->port = iPort; Jupiter::Socket::data_->bound_port = iPort;
addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->port).c_str()); addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->bound_host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->bound_port).c_str());
if (info_head != nullptr) if (info_head != nullptr)
{ {
addrinfo *info = info_head; addrinfo *info = info_head;
do do
{ {
Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto);
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET || ::bind(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR) if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET)
{ {
info = info->ai_next; info = info->ai_next;
continue; continue;
} }
if (::bind(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR)
{
#if defined _WIN32
closesocket(Jupiter::Socket::data_->rawSock);
#else // _WIN32
close(Jupiter::Socket::data_->rawSock);
#endif // WIN32
Jupiter::Socket::data_->rawSock = INVALID_SOCKET;
info = info->ai_next;
continue;
}
Jupiter::Socket::freeAddrInfo(info_head); Jupiter::Socket::freeAddrInfo(info_head);
if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && ::listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR) if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && ::listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR)
return false; return false;
@ -360,8 +387,8 @@ Jupiter::Socket *Jupiter::Socket::accept()
r->data_->rawSock = tSock; r->data_->rawSock = tSock;
r->data_->sockType = Jupiter::Socket::data_->sockType; r->data_->sockType = Jupiter::Socket::data_->sockType;
r->data_->sockProto = Jupiter::Socket::data_->sockProto; r->data_->sockProto = Jupiter::Socket::data_->sockProto;
r->data_->host.set(resolved); r->data_->remote_host.set(resolved);
r->data_->port = static_cast<unsigned short>(Jupiter_strtoi(resolved_port, 10)); r->data_->remote_port = static_cast<unsigned short>(Jupiter_strtoi(resolved_port, 10));
return r; return r;
} }
return nullptr; return nullptr;
@ -421,19 +448,34 @@ bool Jupiter::Socket::getBlockingMode() const
#endif #endif
} }
const Jupiter::ReadableString &Jupiter::Socket::getHostname() const const Jupiter::ReadableString &Jupiter::Socket::getRemoteHostname() const
{
return Jupiter::Socket::data_->remote_host;
}
const char *Jupiter::Socket::getRemoteHostnameC() const
{
return Jupiter::Socket::data_->remote_host.c_str();
}
const Jupiter::ReadableString &Jupiter::Socket::getBoundHostname() const
{
return Jupiter::Socket::data_->bound_host;
}
const char *Jupiter::Socket::getBoundHostnameC() const
{ {
return Jupiter::Socket::data_->host; return Jupiter::Socket::data_->bound_host.c_str();
} }
const char *Jupiter::Socket::getHost() const unsigned short Jupiter::Socket::getRemotePort() const
{ {
return Jupiter::Socket::data_->host.c_str(); return Jupiter::Socket::data_->remote_port;
} }
unsigned short Jupiter::Socket::getPort() const unsigned short Jupiter::Socket::getBoundPort() const
{ {
return Jupiter::Socket::data_->port; return Jupiter::Socket::data_->bound_port;
} }
const Jupiter::ReadableString &Jupiter::Socket::getBuffer() const const Jupiter::ReadableString &Jupiter::Socket::getBuffer() const

21
Jupiter/Socket.h

@ -278,25 +278,34 @@ namespace Jupiter
virtual void closeSocket(); virtual void closeSocket();
/** /**
* @brief Returns the hostname that the socket is connected to or listening to. * @brief Returns the hostname that the socket is connected to.
* *
* @return String containing the hostname. * @return String containing the hostname.
*/ */
const Jupiter::ReadableString &getHostname() const; const Jupiter::ReadableString &getRemoteHostname() const;
const char *getRemoteHostnameC() const;
/** /**
* @brief Returns the hostname of the server that the socket is connected to. * @brief Returns the hostname that the socket is bound/listening to.
* *
* @return String containing the hostname. * @return String containing the hostname.
*/ */
const char *getHost() const; const Jupiter::ReadableString &getBoundHostname() const;
const char *getBoundHostnameC() const;
/** /**
* @brief Returns the port which the Socket is connected to or listening to. * @brief Returns the port which the Socket is connected to.
* *
* @return Port number. * @return Port number.
*/ */
unsigned short getPort() const; unsigned short getRemotePort() const;
/**
* @brief Returns the port which the Socket is bound/listening to.
*
* @return Port number.
*/
unsigned short getBoundPort() const;
/** /**
* @brief Returns the type of the socket. * @brief Returns the type of the socket.

BIN
Release/Jupiter.lib

Binary file not shown.
Loading…
Cancel
Save