Skip to content

Commit

Permalink
Replaced current socket code with omrsock API
Browse files Browse the repository at this point in the history
Replace socket code for JitServer communication with omrsock API code

- Replaced networking include files with omrsock omrportsocktypes.h include file
- Replace all socket function calls with omrsock API calls
- Replace sockaddr_in with OMRAddrInfo and use getaddrinfo
- Replace all connfd with pointer to OMRSocket structure called socket.
- Replace all access to connfd needed for SSL with omrsock_socket_getfd.
- Replace all constants with omrsock constants.

Issue: eclipse-openj9#4102

Signed-off-by: Haley Cao <haleycao88@hotmail.com>
  • Loading branch information
Haley Cao committed Aug 27, 2020
1 parent ab44069 commit 11ac5a3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 113 deletions.
97 changes: 55 additions & 42 deletions runtime/compiler/net/ClientStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,10 @@
#include "control/Options.hpp"
#include "env/VerboseLog.hpp"
#include "net/LoadSSLLibs.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h> /* for TCP_NODELAY option */
#include <fcntl.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /// gethostname, read, write
#include <openssl/err.h>

namespace JITServer
Expand Down Expand Up @@ -128,67 +121,87 @@ int ClientStream::static_init(TR::PersistentInfo *info)

SSL_CTX *ClientStream::_sslCtx = NULL;

int openConnection(const std::string &address, uint32_t port, uint32_t timeoutMs)
omrsock_socket_t openConnection(const std::string &address, uint32_t port, uint32_t timeoutMs)
{
// TODO consider using newer API like getaddrinfo to support IPv6
struct hostent *entSer = gethostbyname(address.c_str());
if (!entSer)
throw StreamFailure("Cannot resolve server name");

struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
memcpy(&servAddr.sin_addr.s_addr, entSer->h_addr, entSer->h_length);
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(port);

int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);

omrsock_socket_t socket = NULL;
if (omrsock_socket(&socket, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT) < 0)
throw StreamFailure("Cannot create socket for JITServer");

int flag = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*)&flag, sizeof(flag)) < 0)
if (omrsock_setsockopt_int(socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_KEEPALIVE, &flag) < 0)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Cannot set option SO_KEEPALIVE on socket");
}

struct linger lingerVal = {1, 2}; // linger 2 seconds
if (setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (void *)&lingerVal, sizeof(lingerVal)) < 0)
OMRLinger linger;
omrsock_linger_init(&linger, 1, 2);
if (omrsock_setsockopt_linger(socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_LINGER, &linger) < 0)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Cannot set option SO_LINGER on socket");
}

struct timeval timeout = {(timeoutMs / 1000), ((timeoutMs % 1000) * 1000)};
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout)) < 0)
OMRTimeval timeout;
omrsock_timeval_init(&timeout, timeoutMs / 1000, (timeoutMs % 1000) * 1000);
if (omrsock_setsockopt_timeval(socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_RCVTIMEO, &timeout) < 0)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Cannot set option SO_RCVTIMEO on socket");
}

if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout)) < 0)
if (omrsock_setsockopt_timeval(socket, OMRSOCK_SOL_SOCKET, OMRSOCK_SO_SNDTIMEO, &timeout) < 0)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Cannot set option SO_SNDTIMEO on socket");
}

if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) < 0)
if (omrsock_setsockopt_int(socket, OMRSOCK_IPPROTO_TCP, OMRSOCK_TCP_NODELAY, &flag) < 0)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Cannot set option TCP_NODELAY on socket");
}

if (connect(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
omrsock_addrinfo_t hints = NULL;
OMRAddrInfoNode result;
OMRSockAddrStorage servAddr;
bool connected = false;
unsigned int len = 0;

if (omrsock_getaddrinfo_create_hints(&hints, OMRSOCK_AF_INET, OMRSOCK_STREAM, OMRSOCK_IPPROTO_DEFAULT, 0) < 0)
throw StreamFailure("Cannot create hints with omrsock API");

char portStr[10];
sprintf(portStr, "%u", port);
if (omrsock_getaddrinfo(address.c_str(), portStr, hints, &result))
throw StreamFailure("Cannot get addrinfo with omrsock API");
omrsock_addrinfo_length(&result, &len);

/* Try to connect to a server with results. */
for (unsigned int i = 0; i < len; i++) {
omrsock_addrinfo_address(&result, i, &servAddr);
if (omrsock_connect(socket, &servAddr) == 0)
{
connected = true;
break;
}
}

if (!connected)
{
close(sockfd);
omrsock_close(&socket);
throw StreamFailure("Connect failed");
}

return sockfd;
return socket;
}

BIO *openSSLConnection(SSL_CTX *ctx, int connfd)
BIO *openSSLConnection(SSL_CTX *ctx, omrsock_socket_t socket)
{
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);

if (!ctx)
return NULL;

Expand All @@ -201,7 +214,7 @@ BIO *openSSLConnection(SSL_CTX *ctx, int connfd)

(*OSSL_set_connect_state)(ssl);

if ((*OSSL_set_fd)(ssl, connfd) != 1)
if ((*OSSL_set_fd)(ssl, omrsock_socket_getfd(socket)) != 1)
{
(*OERR_print_errors_fp)(stderr);
(*OSSL_free)(ssl);
Expand Down Expand Up @@ -248,16 +261,16 @@ BIO *openSSLConnection(SSL_CTX *ctx, int connfd)

if (TR::Options::getVerboseOption(TR_VerboseJITServer))
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "SSL connection on socket 0x%x, Version: %s, Cipher: %s\n",
connfd, (*OSSL_get_version)(ssl), (*OSSL_get_cipher)(ssl));
omrsock_socket_getfd(socket), (*OSSL_get_version)(ssl), (*OSSL_get_cipher)(ssl));
return bio;
}

ClientStream::ClientStream(TR::PersistentInfo *info)
: CommunicationStream(), _versionCheckStatus(NOT_DONE)
{
int connfd = openConnection(info->getJITServerAddress(), info->getJITServerPort(), info->getSocketTimeout());
BIO *ssl = openSSLConnection(_sslCtx, connfd);
initStream(connfd, ssl);
omrsock_socket_t socket = openConnection(info->getJITServerAddress(), info->getJITServerPort(), info->getSocketTimeout());
BIO *ssl = openSSLConnection(_sslCtx, socket);
initStream(socket, ssl);
_numConnectionsOpened++;
}
};
33 changes: 22 additions & 11 deletions runtime/compiler/net/CommunicationStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "env/CompilerEnv.hpp"
#include "net/LoadSSLLibs.hpp"
#include "net/Message.hpp"
#include "infra/Statistics.hpp"

#include "omrportsocktypes.h"

namespace JITServer
{
Expand Down Expand Up @@ -64,7 +65,7 @@ class CommunicationStream
protected:
CommunicationStream() :
_ssl(NULL),
_connfd(-1)
_socket(NULL)
{
static_assert(
sizeof(messageNames) / sizeof(messageNames[0]) == MessageType_ARRAYSIZE,
Expand All @@ -73,16 +74,19 @@ class CommunicationStream

virtual ~CommunicationStream()
{
if (_connfd != -1)
close(_connfd);
if (_socket != NULL)
{
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
omrsock_close(&_socket);
}

if (_ssl)
(*OBIO_free_all)(_ssl);
}

void initStream(int connfd, BIO *ssl)
void initStream(omrsock_socket_t socket, BIO *ssl)
{
_connfd = connfd;
_socket = socket;
_ssl = ssl;
}

Expand All @@ -94,10 +98,14 @@ class CommunicationStream
void readMessage2(Message &msg);
void writeMessage(Message &msg);

int getConnFD() const { return _connfd; }
int getConnFD() const
{
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
return omrsock_socket_getfd(_socket);
}

BIO *_ssl; // SSL connection, null if not using SSL
int _connfd;
omrsock_socket_t _socket;
ServerMessage _sMsg;
ClientMessage _cMsg;

Expand Down Expand Up @@ -138,7 +146,8 @@ class CommunicationStream
int32_t totalBytesRead = 0;
while (totalBytesRead < size)
{
int32_t bytesRead = read(_connfd, data + totalBytesRead, size - totalBytesRead);
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
int32_t bytesRead = omrsock_recv(_socket, (uint8_t *)data + totalBytesRead, size - totalBytesRead, 0);
if (bytesRead <= 0)
{
throw JITServer::StreamFailure("JITServer I/O error: read error");
Expand All @@ -157,7 +166,8 @@ class CommunicationStream
}
else
{
bytesRead = read(_connfd, data, size);
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
bytesRead = omrsock_recv(_socket, (uint8_t *)data, size, 0);
}

if (bytesRead <= 0)
Expand Down Expand Up @@ -200,7 +210,8 @@ class CommunicationStream
int32_t totalBytesWritten = 0;
while (totalBytesWritten < size)
{
int32_t bytesWritten = write(_connfd, data + totalBytesWritten, size - totalBytesWritten);
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
int32_t bytesWritten = omrsock_send(_socket, (uint8_t *)(data + totalBytesWritten), size - totalBytesWritten, 0);
if (bytesWritten <= 0)
{
throw JITServer::StreamFailure("JITServer I/O error: write error");
Expand Down
4 changes: 2 additions & 2 deletions runtime/compiler/net/ServerStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ namespace JITServer
int ServerStream::_numConnectionsOpened = 0;
int ServerStream::_numConnectionsClosed = 0;

ServerStream::ServerStream(int connfd, BIO *ssl)
ServerStream::ServerStream(omrsock_socket_t socket, BIO *ssl)
: CommunicationStream()
{
initStream(connfd, ssl);
initStream(socket, ssl);
_numConnectionsOpened++;
_pClientSessionData = NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/compiler/net/ServerStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ class ServerStream : public CommunicationStream
/**
@brief Constructor of ServerStream class
@param connfd socket descriptor for the communication channel
@param socket Pointer to omrsock socket structure for the communication channel
@param ssl BIO for the SSL enabled stream
@param timeout timeout value (ms) to be set for connfd
@param timeout timeout value (ms) to be set for socket structure
*/
explicit ServerStream(int connfd, BIO *ssl);
explicit ServerStream(omrsock_socket_t socket, BIO *ssl);
virtual ~ServerStream()
{
_numConnectionsClosed++;
Expand Down
Loading

0 comments on commit 11ac5a3

Please sign in to comment.