You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
532 lines
15 KiB
532 lines
15 KiB
#include "clientPort.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "clientPort", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "clientPort", this, _LOG_ERROR, X )
|
|
|
|
//#define DUBUG_EXTRA
|
|
//#define DUBUG_COMMUNICATION
|
|
|
|
clientPort::clientPort():transportSocket(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_nPort = 0;
|
|
memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
|
|
//m_sHostName = NULL;
|
|
m_bKeepAlive = true;
|
|
};
|
|
|
|
clientPort::clientPort(CSTR URI):transportSocket(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_bKeepAlive = true;
|
|
if( URI!= NULL ) {
|
|
char *ptChop = NULL;
|
|
char *ptSt = NULL;
|
|
int tSize = 0;
|
|
ptChop = strstr(URI, "/");
|
|
if (ptChop != NULL){
|
|
ptChop = strstr(ptChop + 1, "/");
|
|
ptSt = ptChop + 1;
|
|
ptChop = strstr(ptChop + 1, "/");
|
|
tSize = ptChop - ptSt;
|
|
ptSt[tSize] = (char)0;
|
|
strncpy( m_cHostName, ptSt, MAX_HOST_STRING_SIZE );
|
|
m_nPort = 80;//??
|
|
//if( connectTo(ptSt, m_nPort) ){
|
|
// char MsgBuffer[64];
|
|
// sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort);
|
|
// LOG(MsgBuffer);
|
|
//} else {
|
|
// LOG_ERROR("Cannot Connect!");
|
|
//};
|
|
};
|
|
} else {
|
|
memset( m_cHostName, 0, 128 );
|
|
m_nPort = 0;
|
|
};
|
|
};
|
|
|
|
clientPort::clientPort(CSTR Host, int Port, bool KeepALive ) :transportSocket(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_bKeepAlive = KeepALive;
|
|
if( strlen(Host) ){
|
|
strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );
|
|
m_nPort = Port;
|
|
} else {
|
|
memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
|
|
m_nPort = 0;
|
|
};
|
|
//if( connectTo( Host, Port ) ) {
|
|
// char MsgBuffer[64];
|
|
// sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort );
|
|
// LOG(MsgBuffer);
|
|
//} else {
|
|
// LOG_ERROR("Cannot Connect!");
|
|
//};
|
|
};
|
|
|
|
clientPort::~clientPort(){
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Destructor!");
|
|
#endif
|
|
disconnect();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Disconnected!");
|
|
LOG("Destructor Done!");
|
|
#endif
|
|
};
|
|
|
|
bool clientPort::create(){
|
|
|
|
if ( strlen( m_cHostName) == 0 ) {
|
|
LOG_ERROR("Host to connect is not Set!");
|
|
return false;
|
|
};
|
|
if( m_nPort == 0 ) {
|
|
LOG_ERROR("Port to connect is not Set!");
|
|
return false;
|
|
};
|
|
if( connectTo( m_cHostName, m_nPort ) ){
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Connect %s [%d] OK", m_cHostName, m_nPort);
|
|
LOG(tB);
|
|
#endif
|
|
FD_SET( m_nSocketFD, &m_sReaderSet );
|
|
if ( m_nReaderSetMax < m_nSocketFD ) m_nReaderSetMax = m_nSocketFD;
|
|
return transportSocket::create();
|
|
} else {
|
|
#ifdef DEBUG_EXTRA
|
|
LOG_ERROR("Cannot Connect!");
|
|
#endif
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool clientPort::_connect(void){
|
|
int iResult;
|
|
long arg;
|
|
struct addrinfo * result, *ptr, hints;
|
|
char cBuffer[256];
|
|
struct sockaddr_in serv_addr;
|
|
#ifdef __linux__
|
|
struct hostent *server;
|
|
m_nSocketFD = socket( AF_INET, SOCK_STREAM, 0);
|
|
if( m_nSocketFD < 0 ){
|
|
LOG_ERROR("ERROR creating socket");
|
|
return false;
|
|
};
|
|
server = gethostbyname( m_cHostName );
|
|
if( server == NULL ){
|
|
sprintf( cBuffer, "ERROR, no such host [%s]", m_cHostName );
|
|
LOG_ERROR(cBuffer);
|
|
return false;
|
|
};
|
|
bzero((char *) &serv_addr, sizeof(serv_addr));
|
|
serv_addr.sin_family = AF_INET;
|
|
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
|
|
serv_addr.sin_port = htons( m_nPort );
|
|
fd_set fdset;
|
|
struct timeval tv;
|
|
|
|
if( ( arg = fcntl( m_nSocketFD, F_GETFL, NULL)) < 0) {
|
|
sprintf( cBuffer, "Error fcntl(..., F_GETFL) (%s)", strerror(errno) );
|
|
LOG_ERROR(cBuffer);
|
|
return false;
|
|
};
|
|
arg |= O_NONBLOCK;
|
|
if( fcntl( m_nSocketFD, F_SETFL, arg) < 0) {
|
|
sprintf( cBuffer, "Error fcntl(..., F_SETFL) (%s)", strerror(errno) );
|
|
LOG_ERROR(cBuffer);
|
|
return false;
|
|
};
|
|
|
|
// Trying to connect with timeout
|
|
iResult = connect( m_nSocketFD, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
|
if( iResult < 0 ){
|
|
if( errno == EINPROGRESS ){
|
|
//LOG("EINPROGRESS in connect() - selecting");
|
|
do {
|
|
tv.tv_sec = 15;
|
|
tv.tv_usec = 0;
|
|
FD_ZERO( &fdset );
|
|
FD_SET( m_nSocketFD, &fdset );
|
|
iResult = select( m_nSocketFD+1, NULL, &fdset, NULL, &tv );
|
|
if( iResult < 0 && errno != EINTR ) {
|
|
#ifdef DEBUG
|
|
sprintf( cBuffer, "Error connecting %d - %s", errno, strerror(errno) );
|
|
LOG_ERROR(cBuffer);
|
|
#endif
|
|
return false;
|
|
} else if ( iResult > 0) {
|
|
// Socket selected for write
|
|
socklen_t par = sizeof(int);
|
|
if( getsockopt( m_nSocketFD, SOL_SOCKET, SO_ERROR, (void*)(&iResult), &par) < 0 ){
|
|
#ifdef DEBUG
|
|
sprintf( cBuffer, "Error in getsockopt() %d - %s", errno, strerror(errno) );
|
|
LOG_ERROR(cBuffer);
|
|
#endif
|
|
return false;
|
|
};
|
|
// Check the value returned...
|
|
if( iResult ){
|
|
#ifdef DEBUG
|
|
sprintf( cBuffer, "Error in delayed connection() %d - %s", iResult, strerror(iResult) );
|
|
LOG_ERROR(cBuffer);
|
|
#endif
|
|
return false;
|
|
};
|
|
break;
|
|
} else {
|
|
#ifdef DEBUG
|
|
LOG_ERROR( "Timeout in select() - Cancelling!" );
|
|
#endif
|
|
return false;
|
|
};
|
|
} while (1);
|
|
} else {
|
|
#ifdef DEBUG
|
|
sprintf( cBuffer, "Error connecting %d - %s", errno, strerror(errno) );
|
|
LOG_ERROR(cBuffer);
|
|
#endif
|
|
return false;
|
|
};
|
|
};
|
|
#if 0
|
|
// Set to blocking mode again...
|
|
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
arg &= (~O_NONBLOCK);
|
|
if( fcntl(soc, F_SETFL, arg) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
#endif
|
|
#else
|
|
// Initialize Winsock
|
|
WSADATA wsaData;
|
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
if (iResult != 0) {
|
|
sprintf(cBuffer, "WSAStartup failed with error: %d\n", iResult);
|
|
LOG_ERROR(cBuffer);
|
|
return false;
|
|
};
|
|
ZeroMemory( &hints, sizeof(hints) );
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
// Resolve the server address and port
|
|
sprintf( cBuffer, "%d", m_nPort );
|
|
iResult = getaddrinfo( m_cHostName, cBuffer, &hints, &result );
|
|
if (iResult != 0) {
|
|
sprintf(cBuffer, "getaddrinfo for [%s:%d] failed with error: %d", m_cHostName, m_nPort, iResult);
|
|
LOG_ERROR(cBuffer);
|
|
WSACleanup();
|
|
return false;
|
|
};
|
|
|
|
// Attempt to connect to an address until one succeeds
|
|
ptr = result;
|
|
while( ptr != NULL ) {
|
|
// Create a SOCKET for connecting to server
|
|
m_nSocketFD = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
|
|
if( m_nSocketFD == INVALID_SOCKET ){
|
|
sprintf( cBuffer, "Socket Creation failed with error: %ld", WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
WSACleanup();
|
|
return false;
|
|
};
|
|
// Connect to server.
|
|
iResult = connect( m_nSocketFD, ptr->ai_addr, (int)ptr->ai_addrlen );
|
|
if (iResult == SOCKET_ERROR) {
|
|
sprintf(cBuffer, "Socket Connection failed[%d] with error: %ld", iResult, WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
closesocket(m_nSocketFD);
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
ptr = ptr->ai_next;
|
|
continue;
|
|
};
|
|
break;
|
|
};
|
|
freeaddrinfo(result);
|
|
if (m_nSocketFD == INVALID_SOCKET) {
|
|
sprintf( cBuffer, "Unable Connect to the Server[%s:%d]!", m_cHostName, m_nPort );
|
|
LOG_ERROR(cBuffer);
|
|
WSACleanup();
|
|
return false;
|
|
};
|
|
#endif
|
|
/*
|
|
setConnectionStatus(CS_CONNECTED);
|
|
#ifdef DEBUG
|
|
LOG("In case THREADS are running -> SHUTTING DOWN THREADS");
|
|
#endif
|
|
if( uninit() ) {
|
|
#ifdef DEBUG
|
|
LOG("STARTING IO THREADS");
|
|
#endif
|
|
return init();
|
|
} else {
|
|
LOG_ERROR("CAN NOT SHUTDOWN IO THREADS!");
|
|
};
|
|
*/
|
|
return true;//false;
|
|
};
|
|
|
|
bool clientPort::connectTo(CSTR Host, int Port){
|
|
if( m_eConnectionStatus == CS_CONNECTED ){
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
#ifdef __linux__
|
|
shutdown( m_nSocketFD, SHUT_RDWR );
|
|
close( m_nSocketFD );
|
|
m_nSocketFD = -1;
|
|
#else
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
#endif
|
|
};
|
|
//if( m_sHostName == NULL ) m_sHostName = (CSTR) malloc( strlen( Host ) +1 );
|
|
if( m_cHostName != Host ) strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );//sprintf( m_sHostName, "%s", Host );// Prevent Self Copy
|
|
m_nPort = Port;
|
|
return _connect();
|
|
};
|
|
|
|
int clientPort::sendBlock(unsigned char *Data, int DataSize){
|
|
if( m_eConnectionStatus != CS_CONNECTED ){
|
|
if( connectTo( m_cHostName, m_nPort ) ){
|
|
char MsgBuffer[CLOG];
|
|
sprintf(MsgBuffer, "Reconnect %s [%d] OK", m_cHostName, m_nPort);
|
|
LOG(MsgBuffer);
|
|
};
|
|
};
|
|
|
|
if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
|
|
|
|
#ifdef __linux__
|
|
int RetVal;
|
|
if( m_nSocketFD > -1 ){
|
|
//sprintf(cBuffer,"Write into Socket %p %d", Data, DataSize );
|
|
//LOG(cBuffer);
|
|
RetVal = write(m_nSocketFD, Data, DataSize);
|
|
//LOG("Data writen into Socket");
|
|
if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
|
|
LOG_ERROR("Error acures while writing data into the Socket");
|
|
//shutdown( m_nSocketFD, 2 );
|
|
//close( m_nSocketFD );
|
|
//m_nSocketFD = -1;
|
|
disconnect();
|
|
return -1;
|
|
};
|
|
if( RetVal < 0 ) RetVal = 0;
|
|
#ifdef DUBUG_EXTRA
|
|
{
|
|
char cBuffer[128];
|
|
sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
|
|
LOG(cBuffer);
|
|
#ifdef DUBUG_COMMUNICATION
|
|
for (int i = 0; i < RetVal; i++) {
|
|
sprintf(cBuffer, "0x%02X", Data[i]);
|
|
LOG(cBuffer);
|
|
};
|
|
#endif
|
|
}
|
|
#endif
|
|
} else {
|
|
LOG_ERROR("Not Connected!");
|
|
return -1;
|
|
};
|
|
#else
|
|
DWORD RetVal;
|
|
if (m_nSocketFD != INVALID_SOCKET){
|
|
// Send an initial buffer
|
|
RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
|
|
if (RetVal == SOCKET_ERROR) {
|
|
char cBuffer[CLOG];
|
|
//m_bConnected = false;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
return -1;
|
|
};
|
|
//sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
|
|
//LOG(cBuffer);
|
|
if (!m_bKeepAlive){
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
//m_bConnected = false;
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
};
|
|
} else {
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
//m_bConnected = false;
|
|
return -1;
|
|
};
|
|
#endif
|
|
return RetVal;
|
|
};
|
|
|
|
int clientPort::recptBlock(unsigned char *Buffer, int *BufferLength){
|
|
if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
|
|
#ifdef __linux__
|
|
int RetVal;
|
|
if( m_nSocketFD > -1 ) {
|
|
RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );
|
|
if( RetVal < 1 ){
|
|
if( errno!=EAGAIN && errno!=EINTR ) {
|
|
LOG_ERROR("Peer Closes Connection!");
|
|
//shutdown( m_nSocketFD, 2 );
|
|
//close( m_nSocketFD );
|
|
disconnect();
|
|
*BufferLength = 0;
|
|
//m_nSocketFD = -1;
|
|
return -1;
|
|
};
|
|
};
|
|
if( !m_bKeepAlive ){
|
|
//shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
|
|
//close( m_nSocketFD );
|
|
//m_nSocketFD = -1;
|
|
disconnect();
|
|
};
|
|
} else {
|
|
LOG_ERROR("Not Connected!");
|
|
*BufferLength = 0;
|
|
return -1;
|
|
};
|
|
#else
|
|
DWORD RetVal;
|
|
if( m_nSocketFD != INVALID_SOCKET ){
|
|
// Receive until the peer closes the connection
|
|
RetVal = recv( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
|
|
if( RetVal < 1 ){
|
|
if( errno!=EAGAIN && errno!=EINTR ) {
|
|
//setConnectionStatus(CS_DISCONNECTED);
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
return -1;
|
|
};
|
|
};
|
|
if (!m_bKeepAlive){
|
|
//setConnectionStatus(CS_DISCONNECTED);
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
//m_bConnected = false;
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
};
|
|
} else {
|
|
//setConnectionStatus(CS_DISCONNECTED);
|
|
LOG_ERROR("Not Connected!");
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
return 0;
|
|
};
|
|
#endif
|
|
if (RetVal > 0) {
|
|
#ifdef DUBUG_EXTRA
|
|
char cBuffer[165];
|
|
sprintf(cBuffer, "Bytes received: %d", RetVal);
|
|
LOG(cBuffer);
|
|
#ifdef DUBUG_COMMUNICATION
|
|
for (int i = 0; i < RetVal; i++) {
|
|
sprintf(cBuffer, "0x%02X", Buffer[i]);
|
|
LOG(cBuffer);
|
|
};
|
|
#endif
|
|
#endif
|
|
*BufferLength = RetVal;
|
|
return RetVal;
|
|
};
|
|
|
|
*BufferLength = 0;
|
|
|
|
if (RetVal == -1) return 0;
|
|
|
|
if (RetVal == 0){
|
|
errno = 0;
|
|
return 0;
|
|
};
|
|
|
|
/* r < -1, should never happen. */
|
|
errno = EIO;
|
|
|
|
return 0;
|
|
};
|
|
|
|
bool clientPort::disconnect(void){
|
|
if( m_eConnectionStatus == CS_CONNECTED ){
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
#ifdef __linux__
|
|
shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
|
|
close( m_nSocketFD );
|
|
m_nSocketFD = -1;
|
|
#else
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
#endif
|
|
};
|
|
return true;
|
|
};
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
CSTR clientPort::getLocalIp4(){
|
|
#ifdef __linux__
|
|
struct sockaddr_in server_addr, client_addr;
|
|
socklen_t client_addr_len = sizeof(client_addr);
|
|
char ip_str[INET_ADDRSTRLEN];
|
|
// Get the local address of the connected socket
|
|
if (getsockname( m_nSocketFD, (struct sockaddr *)&client_addr, &client_addr_len) < 0) {
|
|
LOG_ERROR("getsockname failed");
|
|
return NULL;
|
|
};
|
|
|
|
// Convert the IP address to a human-readable string
|
|
if (inet_ntop(AF_INET, &client_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
|
|
LOG_ERROR("inet_ntop failed");
|
|
return NULL;
|
|
};
|
|
return strdup( ip_str );
|
|
#else
|
|
return NULL;
|
|
#endif
|
|
};
|
|
CSTR clientPort::getRemoteIp4(){
|
|
#ifdef __linux__
|
|
char ip_str[INET_ADDRSTRLEN];
|
|
struct sockaddr_in peer_addr;
|
|
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
if (getpeername( m_nSocketFD, (struct sockaddr *)&peer_addr, &peer_addr_len) == -1) {
|
|
LOG_ERROR("Error getting peer name");
|
|
return NULL;
|
|
};
|
|
if (inet_ntop(AF_INET, &peer_addr.sin_addr, ip_str, INET_ADDRSTRLEN) == NULL) {
|
|
LOG_ERROR("Error converting IP to string");
|
|
return NULL;
|
|
};
|
|
return strdup(ip_str);
|
|
#else
|
|
return NULL;
|
|
#endif
|
|
};
|
|
|
|
//#undef DUBUG_EXTRA
|
|
//#undef DUBUG_COMMUNICATION
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|
|
|