aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ppsocket.cc
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>1999-07-01 20:42:30 +0000
committerFritz Elfert <felfert@to.com>1999-07-01 20:42:30 +0000
commit8c29602b21efacced9d7ff4cd1f3c4101dda5f65 (patch)
tree1dd05b02b68eb2ab19710f8f64a50f40585ef31d /lib/ppsocket.cc
parent207d61513c4cd934466fa4d504bfadc50029325c (diff)
downloadplptools-8c29602b21efacced9d7ff4cd1f3c4101dda5f65.tar.gz
plptools-8c29602b21efacced9d7ff4cd1f3c4101dda5f65.tar.bz2
plptools-8c29602b21efacced9d7ff4cd1f3c4101dda5f65.zip
proper error handling and auto-reconnect
modified rfsv enums.
Diffstat (limited to 'lib/ppsocket.cc')
-rw-r--r--lib/ppsocket.cc1130
1 files changed, 516 insertions, 614 deletions
diff --git a/lib/ppsocket.cc b/lib/ppsocket.cc
index 6babcc7..9230f81 100644
--- a/lib/ppsocket.cc
+++ b/lib/ppsocket.cc
@@ -49,240 +49,236 @@
//This constructor is useful when converting a socket
//to a derived class of socket
-ppsocket::ppsocket(const ppsocket& another)
+ppsocket::ppsocket(const ppsocket & another)
{
- m_Socket = another.m_Socket;
- m_HostAddr = another.m_HostAddr;
- m_PeerAddr = another.m_PeerAddr;
- m_Bound = another.m_Bound;
- m_Timeout = another.m_Timeout;
- m_LastError = another.m_LastError;
+ m_Socket = another.m_Socket;
+ m_HostAddr = another.m_HostAddr;
+ m_PeerAddr = another.m_PeerAddr;
+ m_Bound = another.m_Bound;
+ m_Timeout = another.m_Timeout;
+ m_LastError = another.m_LastError;
- m_Timeout = INFINITE;
+ m_Timeout = INFINITE;
}
-
+
ppsocket::ppsocket()
{
- m_Socket = INVALID_SOCKET;
+ m_Socket = INVALID_SOCKET;
- memset(&m_HostAddr, 0, sizeof(m_HostAddr));
- memset(&m_PeerAddr, 0, sizeof(m_PeerAddr));
+ memset(&m_HostAddr, 0, sizeof(m_HostAddr));
+ memset(&m_PeerAddr, 0, sizeof(m_PeerAddr));
- ((struct sockaddr_in*) &m_HostAddr)->sin_family = AF_INET;
- ((struct sockaddr_in*) &m_PeerAddr)->sin_family = AF_INET;
+ ((struct sockaddr_in *) &m_HostAddr)->sin_family = AF_INET;
+ ((struct sockaddr_in *) &m_PeerAddr)->sin_family = AF_INET;
- m_Bound = false;
+ m_Bound = false;
- m_Timeout = INFINITE;
+ m_Timeout = INFINITE;
}
ppsocket::~ppsocket()
{
- if (m_Socket != INVALID_SOCKET)
- {
- close(m_Socket);
- }
+ if (m_Socket != INVALID_SOCKET) {
+ shutdown(m_Socket, 3);
+ close(m_Socket);
+ }
}
-bool ppsocket::startup(void)
-{
-#ifdef WIN32
- WSADATA wd;
- m_LastError = WSAStartup(MAKEWORD(1, 1), &wd);
+bool ppsocket::
+startup(void)
+{
+#ifdef WIN32
+ WSADATA wd;
+ m_LastError = WSAStartup(MAKEWORD(1, 1), &wd);
#else
- m_LastError = 0;
+ m_LastError = 0;
#endif
- return(m_LastError == 0 ? true : false);
+ return (m_LastError == 0 ? true : false);
}
-//pre : must be bound and peer info set
-bool ppsocket::reconnect()
+bool ppsocket::
+reconnect()
{
- printPeer();
- if (::connect(m_Socket, &m_PeerAddr, sizeof(m_PeerAddr)) != 0)
- {
- m_LastError = lastErrorCode();
- cout << "Reconnect failed : status : " << m_LastError << endl << flush;
- return(false);
- }
- return(true);
+ if (m_Socket != INVALID_SOCKET) {
+ shutdown(m_Socket, 3);
+ close(m_Socket);
+ }
+ m_Socket = INVALID_SOCKET;
+ if (!createSocket())
+ return (false);
+ m_LastError = 0;
+ m_Bound = 0;
+ if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ if (::connect(m_Socket, &m_PeerAddr, sizeof(m_PeerAddr)) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ return (true);
}
-void ppsocket::printPeer()
+void ppsocket::
+printPeer()
{
- char* pPeer = 0;
- int port;
+ char *pPeer = 0;
+ int port;
- pPeer = inet_ntoa(((struct sockaddr_in*) &m_PeerAddr)->sin_addr);
- if (pPeer)
- {
- port = ntohs(((struct sockaddr_in*) &m_PeerAddr)->sin_port);
- cout << "Peer : " << pPeer << " Port : " << port << endl;
- }
- else
- {
- cout << "Error getting Peer details\n";
- }
+ pPeer = inet_ntoa(((struct sockaddr_in *) &m_PeerAddr)->sin_addr);
+ if (pPeer) {
+ port = ntohs(((struct sockaddr_in *) &m_PeerAddr)->sin_port);
+ cout << "Peer : " << pPeer << " Port : " << port << endl;
+ } else
+ cerr << "Error getting Peer details\n";
}
-bool ppsocket::connect(char* Peer, int PeerPort, char* Host, int HostPort)
+bool ppsocket::
+connect(char *Peer, int PeerPort, char *Host, int HostPort)
{
- //****************************************************
- //* If we aren't already bound set the host and bind *
- //****************************************************
-
- if (!bindSocket(Host, HostPort))
- {
- if (m_LastError != 0)
- {
- return(false);
- }
- }
-
- //****************
- //* Set the peer *
- //****************
- if (!setPeer(Peer, PeerPort))
- {
- return(false);
- }
-
- //***********
- //* Connect *
- //***********
- if (::connect(m_Socket, &m_PeerAddr, sizeof(m_PeerAddr)) != 0)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
+ //****************************************************
+ //* If we aren't already bound set the host and bind *
+ //****************************************************
- return(true);
+ if (!bindSocket(Host, HostPort)) {
+ if (m_LastError != 0) {
+ return (false);
+ }
+ }
+ //****************
+ //* Set the peer *
+ //****************
+ if (!setPeer(Peer, PeerPort)) {
+ return (false);
+ }
+ //***********
+ //* Connect *
+ //***********
+ if (::connect(m_Socket, &m_PeerAddr, sizeof(m_PeerAddr)) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ return (true);
}
-bool ppsocket::listen(char* Host, int Port)
+bool ppsocket::
+listen(char *Host, int Port)
{
- //****************************************************
- //* If we aren't already bound set the host and bind *
- //****************************************************
-
- if (!bindSocket(Host, Port))
- {
- if (m_LastError != 0)
- {
- return(false);
- }
- }
-
- //**********************
- //* Listen on the port *
- //**********************
+ //****************************************************
+ //* If we aren't already bound set the host and bind *
+ //****************************************************
- if (::listen(m_Socket, 5) != 0)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
+ if (!bindSocket(Host, Port)) {
+ if (m_LastError != 0) {
+ return (false);
+ }
+ }
+ //**********************
+ //* Listen on the port *
+ //**********************
- return(true);
+ if (::listen(m_Socket, 5) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ return (true);
}
-ppsocket* ppsocket::accept(char* Peer, int MaxLen)
-{
- unsigned int len;
- ppsocket* accepted;
- char* peer;
-
- //*****************************************************
- //* Allocate a new object to hold the accepted socket *
- //*****************************************************
-
- accepted = new ppsocket;
-
- if (!accepted)
- {
- m_LastError = lastErrorCode();
- return(NULL);
- }
-
- //***********************
- //* Accept a connection *
- //***********************
-
- len = sizeof(struct sockaddr);
- fcntl(m_Socket, F_SETFL, O_NONBLOCK);
- accepted->m_Socket = ::accept(m_Socket, &accepted->m_PeerAddr, &len);
-
- if (accepted->m_Socket == INVALID_SOCKET)
- {
- m_LastError = lastErrorCode();
- delete accepted;
- return (NULL);
- }
+ppsocket *ppsocket::
+accept(char *Peer, int MaxLen)
+{
+ unsigned int len;
+ ppsocket *accepted;
+ char *peer;
- //****************************************************
- //* Got a connection so fill in the other attributes *
- //****************************************************
+ //*****************************************************
+ //* Allocate a new object to hold the accepted socket *
+ //*****************************************************
- accepted->m_HostAddr = m_HostAddr;
- accepted->m_Bound = true;
-
- //****************************************************
- //* If required get the name of the connected client *
- //****************************************************
-
- if (Peer)
- {
- peer = inet_ntoa(((struct sockaddr_in*) &accepted->m_PeerAddr)->sin_addr);
- if (peer)
- {
- strncpy(Peer, peer, MaxLen);
- Peer[MaxLen] = '\0';
- }
- }
- else
- {
- strcpy(Peer, "");
- }
+ accepted = new ppsocket;
- return(accepted);
+ if (!accepted) {
+ m_LastError = lastErrorCode();
+ return (NULL);
+ }
+ //***********************
+ //* Accept a connection *
+ //***********************
+
+ len = sizeof(struct sockaddr);
+ fcntl(m_Socket, F_SETFL, O_NONBLOCK);
+ accepted->m_Socket =::accept(m_Socket, &accepted->m_PeerAddr, &len);
+
+ if (accepted->m_Socket == INVALID_SOCKET) {
+ m_LastError = lastErrorCode();
+ delete accepted;
+ return (NULL);
+ }
+ //****************************************************
+ //* Got a connection so fill in the other attributes *
+ //****************************************************
+
+ accepted->m_HostAddr = m_HostAddr;
+ accepted->m_Bound = true;
+
+ //****************************************************
+ //* If required get the name of the connected client *
+ //****************************************************
+
+ if (Peer) {
+ peer = inet_ntoa(((struct sockaddr_in *) &accepted->m_PeerAddr)->sin_addr);
+ if (peer) {
+ strncpy(Peer, peer, MaxLen);
+ Peer[MaxLen] = '\0';
+ }
+ } else {
+ strcpy(Peer, "");
+ }
+ return (accepted);
}
-int ppsocket::printf(const char* Format, ...)
-{ int i;
-va_list ap;
-char s[512];
+int ppsocket::
+printf(const char *Format,...)
+{
+ int i;
+ va_list ap;
+ char s[512];
+
+ va_start(ap, Format);
+ vsprintf(s, Format, ap);
+ va_end(ap);
-va_start(ap, Format);
-vsprintf(s, Format, ap);
-va_end(ap);
+ i = writeTimeout(s, strlen(s), 0);
-i = writeTimeout(s, strlen(s), 0);
+ return (i);
+}
-return(i);
-}
-
-bool ppsocket::dataToGet() const {
- fd_set io;
- FD_ZERO(&io);
- FD_SET(m_Socket, &io);
- struct timeval t;
- t.tv_usec = 0;
- t.tv_sec = 0;
- return (select(m_Socket+1, &io, NULL, NULL, &t) != 0) ? true : false;
+bool ppsocket::
+dataToGet() const
+{
+ fd_set io;
+ FD_ZERO(&io);
+ FD_SET(m_Socket, &io);
+ struct timeval t;
+ t.tv_usec = 0;
+ t.tv_sec = 0;
+ return (select(m_Socket + 1, &io, NULL, NULL, &t) != 0) ? true : false;
}
-int ppsocket::getBufferStore(bufferStore &a, bool wait) {
+int ppsocket::
+getBufferStore(bufferStore & a, bool wait)
+{
/* Returns a 0 for for no message,
* 1 for message OK, and -1 for socket problem
*/
@@ -291,17 +287,18 @@ int ppsocket::getBufferStore(bufferStore &a, bool wait) {
long count = 0;
unsigned char *buff;
unsigned char *bp;
- if (!wait && !dataToGet()) return 0;
+ if (!wait && !dataToGet())
+ return 0;
a.init();
- if (readTimeout((char *)&l, sizeof(l), 0) != sizeof(l))
+ if (readTimeout((char *) &l, sizeof(l), 0) != sizeof(l))
return -1;
l = ntohl(l);
bp = buff = new unsigned char[l];
while (l > 0) {
- int j = readTimeout((char *)bp, l, 0);
+ int j = readTimeout((char *) bp, l, 0);
if (j == SOCKET_ERROR || j == 0) {
- delete [] buff;
+ delete[]buff;
return -1;
}
count += j;
@@ -309,545 +306,450 @@ int ppsocket::getBufferStore(bufferStore &a, bool wait) {
bp += j;
};
a.init(buff, count);
- delete [] buff;
-#ifdef SOCKET_DIAGNOSTICS
- cout << "ppsocket got " << a << endl;
-#endif
+ delete[]buff;
return (a.getLen() == 0) ? 0 : 1;
}
-bool ppsocket::sendBufferStore(const bufferStore &a) {
-#ifdef SOCKET_DIAGNOSTICS
- cout << "ppsocket sending " << a << endl;
-#endif
+bool ppsocket::
+sendBufferStore(const bufferStore & a)
+{
long l = a.getLen();
long hl = htonl(l);
long sent = 0;
- int retries = 0;
+ int retries = 0;
int i;
- i = writeTimeout((char *)&hl, sizeof(hl), 0);
+ i = writeTimeout((char *) &hl, sizeof(hl), 0);
if (i != sizeof(hl))
return false;
- while (l > 0) {
+ while (l > 0) {
i = writeTimeout(a.getString(sent), l, 0);
if (i == SOCKET_ERROR || i == 0)
- return(false);
+ return (false);
sent += i;
l -= i;
- if (++retries > 5) {
+ if (++retries > 5) {
m_LastError = 0;
- return(false);
+ return (false);
}
}
return true;
}
-int ppsocket::readEx(char* Data, int cTerm, int MaxLen)
-{
- int i, j;
+int ppsocket::
+readEx(char *Data, int cTerm, int MaxLen)
+{
+ int i, j;
- for (i = 0; i < MaxLen; i++)
- {
- j = readTimeout(Data + i, 1, 0);
-
- if (j == SOCKET_ERROR || j == 0)
- {
- Data[i] = '\0';
- return(i > 0 ? i : 0);
- }
+ for (i = 0; i < MaxLen; i++) {
+ j = readTimeout(Data + i, 1, 0);
- if (Data[i] == cTerm)
- break;
- }
+ if (j == SOCKET_ERROR || j == 0) {
+ Data[i] = '\0';
+ return (i > 0 ? i : 0);
+ }
+ if (Data[i] == cTerm)
+ break;
+ }
- return(i+1);
+ return (i + 1);
}
-bool ppsocket::puts(const char* Data)
-{
- int tosend, sent, retries, i;
-
- tosend = strlen(Data);
- sent = retries = 0;
-
- while (tosend > 0)
- {
- i = writeTimeout(Data + sent, tosend, 0);
-
- if (i == SOCKET_ERROR || i == 0)
- return(sent > 0 ? true : false);
-
- sent += i;
- tosend -= i;
+bool ppsocket::
+puts(const char *Data)
+{
+ int tosend, sent, retries, i;
- if (++retries > 5)
- {
- m_LastError = 0;
- return(false);
- }
- }
+ tosend = strlen(Data);
+ sent = retries = 0;
- return(true);
-}
+ while (tosend > 0) {
+ i = writeTimeout(Data + sent, tosend, 0);
+ if (i == SOCKET_ERROR || i == 0)
+ return (sent > 0 ? true : false);
+ sent += i;
+ tosend -= i;
-char ppsocket::sgetc(void)
-{
- int i;
- char c;
+ if (++retries > 5) {
+ m_LastError = 0;
+ return (false);
+ }
+ }
- i = readTimeout(&c, 1, 0);
- if (i == SOCKET_ERROR || i == 0)
- {
- return(0);
- }
- return(c);
+ return (true);
}
-bool ppsocket::sputc(char c)
-{
- int i;
- cout << hex << (int)c << endl;
-
- i = writeTimeout(&c, 1, 0);
-
- if (i == SOCKET_ERROR || i == 0)
- {
- return(false);
- }
+char ppsocket::
+sgetc(void)
+{
+ int i;
+ char c;
- return(true);
+ i = readTimeout(&c, 1, 0);
+ if (i == SOCKET_ERROR || i == 0) {
+ return (0);
+ }
+ return (c);
}
-int ppsocket::read(void* Data, size_t Size, size_t NumObj)
-{
- int i = readTimeout((char*) Data, Size*NumObj, 0);
+bool ppsocket::
+sputc(char c)
+{
+ int i;
- return(i);
+ cout << hex << (int) c << endl;
+ i = writeTimeout(&c, 1, 0);
+ if (i == SOCKET_ERROR || i == 0)
+ return (false);
+ return (true);
}
-int ppsocket::write(const void* Data, size_t Size, size_t NumObj)
-{
- int i = writeTimeout((char*) Data, Size*NumObj, 0);
-
- return(i);
-}
-
+int ppsocket::
+read(void *Data, size_t Size, size_t NumObj)
+{
+ int i = readTimeout((char *) Data, Size * NumObj, 0);
-int ppsocket::recv(char* buf, int len, int flags)
-{
- int i = ::recv(m_Socket, buf, len, flags);
-
- if (i < 0)
- m_LastError = lastErrorCode();
-
- return(i);
+ return (i);
}
-int ppsocket::send(const char* buf, int len, int flags)
-{
- int i = ::send(m_Socket, buf, len, flags);
-
- if (i < 0)
- m_LastError = lastErrorCode();
+int ppsocket::
+write(const void *Data, size_t Size, size_t NumObj)
+{
+ int i = writeTimeout((char *) Data, Size * NumObj, 0);
- return(i);
+ return (i);
}
-int ppsocket::readTimeout(char* buf, int len, int flags)
-{
- int i;
-
- //*********************************************************
- //* If there is no timeout use the Berkeley recv function *
- //*********************************************************
-
- if (m_Timeout == INFINITE)
- {
- i = ::recv(m_Socket, buf, len, flags);
-
- if (i == SOCKET_ERROR)
- {
- m_LastError = lastErrorCode();
- }
- }
-
- //********************************************
- //* If there is a timeout use overlapped i/o *
- //********************************************
-
- else
- {
- i = SOCKET_ERROR;
- }
+int ppsocket::
+recv(char *buf, int len, int flags)
+{
+ int i =::recv(m_Socket, buf, len, flags);
+ if (i < 0)
+ m_LastError = lastErrorCode();
- return(i);
+ return (i);
}
-int ppsocket::writeTimeout(const char* buf, int len, int flags)
-{
- int i;
- // If there is no timeout use the Berkeley send function
-
- if (m_Timeout == INFINITE)
- {
- i = ::send(m_Socket, buf, len, flags);
-
- if (i == SOCKET_ERROR)
- {
- m_LastError = lastErrorCode();
- }
- }
- else
- {
- // If there is a timeout use overlapped i/o
- i = SOCKET_ERROR;
- }
-
- return(i);
-}
-
-bool ppsocket::closeSocket(void)
+int ppsocket::
+send(const char *buf, int len, int flags)
{
- if (close(m_Socket) != 0)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
+ int i =::send(m_Socket, buf, len, flags);
- m_Socket = INVALID_SOCKET;
+ if (i < 0)
+ m_LastError = lastErrorCode();
- return(true);
+ return (i);
}
-bool ppsocket::bindSocket(char* Host, int Port)
+int ppsocket::
+readTimeout(char *buf, int len, int flags)
{
+ int i;
- // If we are already bound return FALSE but with no last error
-
- m_LastError = 0;
-
- if (m_Bound)
- {
- return(false);
- }
-
- // If the socket hasn't been created create it now
+ //*********************************************************
+ //* If there is no timeout use the Berkeley recv function *
+ //*********************************************************
- if (m_Socket == INVALID_SOCKET)
- {
- if (!createSocket())
- {
- return(false);
- }
- }
+ if (m_Timeout == INFINITE) {
+ i =::recv(m_Socket, buf, len, flags);
- // If a host name was supplied then use it
- if (!setHost(Host, Port))
- {
- return(false);
- }
+ if (i == SOCKET_ERROR) {
+ m_LastError = lastErrorCode();
+ }
+ }
+ //********************************************
+ //* If there is a timeout use overlapped i/o *
+ //********************************************
- // Now bind the socket
- if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) != 0)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
+ else {
+ i = SOCKET_ERROR;
+ }
- m_Bound = true;
- return(true);
+ return (i);
}
-bool ppsocket::bindInRange(char* Host, int Low, int High, int Retries)
-{
- int port, i;
-
- // If we are already bound return FALSE but with no last error
-
- m_LastError = 0;
-
- if (m_Bound)
- {
- return(false);
- }
-
- // If the socket hasn't been created create it now
+int ppsocket::
+writeTimeout(const char *buf, int len, int flags)
+{
+ int i;
+ // If there is no timeout use the Berkeley send function
- if (m_Socket == INVALID_SOCKET)
- {
- if (!createSocket())
- {
- return(false);
- }
- }
- // If the number of retries is greater than the range then work through the
- // range sequentially.
+ if (m_Timeout == INFINITE) {
+ i =::send(m_Socket, buf, len, flags);
- if (Retries > High - Low)
- {
- for (port = Low; port <= High; port++)
- {
- if (!setHost(Host, port))
- return(false);
+ if (i == SOCKET_ERROR) {
+ m_LastError = lastErrorCode();
+ }
+ } else {
+ // If there is a timeout use overlapped i/o
+ i = SOCKET_ERROR;
+ }
- if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) == 0)
- break;
- }
+ return (i);
+}
- if (port > High)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- }
+bool ppsocket::
+closeSocket(void)
+{
+ shutdown(m_Socket, 3);
+ if (close(m_Socket) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ m_Socket = INVALID_SOCKET;
- // Else select numbers within the range at random
+ return (true);
+}
- else
- {
- for (i = 0; i < Retries; i++)
- {
- port = Low + (rand() % (High - Low));
- if (!setHost(Host, port))
- return(false);
+bool ppsocket::
+bindSocket(char *Host, int Port)
+{
- if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) == 0)
- break;
- }
+ // If we are already bound return FALSE but with no last error
- if (i >= Retries)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- }
-
- m_Bound = true;
-
- return(true);
-}
-
-bool ppsocket::linger(bool LingerOn, int LingerTime)
-{
- int i;
- struct linger l;
-
- // If the socket hasn't been created create it now
+ m_LastError = 0;
- if (m_Socket == INVALID_SOCKET)
- {
- if (!createSocket())
- {
- return(false);
- }
- }
- // Set the lingering
+ if (m_Bound) {
+ return (false);
+ }
+ // If the socket hasn't been created create it now
- if (LingerOn)
- {
- l.l_onoff = 1;
- l.l_linger = LingerTime;
- }
- else
- {
- l.l_onoff = 0;
- l.l_linger = 0;
- }
- i = setsockopt(m_Socket, SOL_SOCKET, SO_LINGER, (const char*) &l, sizeof(l));
-
- // Check for errors
-
- if (i != 0)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
-
- // Return indicating success
-
- return(true);
-}
-
-
-bool ppsocket::createSocket(void)
-{
- // If the socket has already been created just return true
- if (m_Socket != INVALID_SOCKET)
- {
- return(true);
- }
-
- // Create the socket
- m_Socket = ::socket(PF_INET, SOCK_STREAM, 0);
-
- if (m_Socket == INVALID_SOCKET)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
-
- // By default set no lingering
-
- linger(FALSE, 0);
-
- // Return indicating success
-
- return(true);
-}
-
-bool ppsocket::setPeer(char* Peer, int Port)
-{
- struct hostent* he;
-
- // If a peer name was supplied then use it
- if (Peer)
- {
- he = gethostbyname(Peer);
- if (!he)
- {
- unsigned long ipaddress = inet_addr(Peer);
- if (ipaddress == INADDR_NONE)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
-
- he = gethostbyaddr((const char*) &ipaddress, 4, PF_INET);
- if (!he)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- }
- memcpy((void*) &((struct sockaddr_in*) &m_PeerAddr)->sin_addr, (void*) he->h_addr_list[0], 4);
- }
+ if (m_Socket == INVALID_SOCKET) {
+ if (!createSocket()) {
+ return (false);
+ }
+ }
+ // If a host name was supplied then use it
+ if (!setHost(Host, Port)) {
+ return (false);
+ }
+ // Now bind the socket
+ if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ m_Bound = true;
- // If a port name was supplied use it
-
- if (Port > 0)
- {
- ((struct sockaddr_in*) &m_PeerAddr)->sin_port = htons((SHORT) Port);
- }
-
- return(true);
+ return (true);
}
-bool ppsocket::getPeer(char* Peer, int MaxLen, int* Port)
-{
- char* peer;
-
- if (Peer)
- {
- peer = inet_ntoa(((struct sockaddr_in*) &m_PeerAddr)->sin_addr);
-
- if (!peer)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
-
- strncpy(Peer, peer, MaxLen);
- Peer[MaxLen] = '\0';
- }
-
- if (Port)
- {
- *Port = ntohs(((struct sockaddr_in*) &m_PeerAddr)->sin_port);
- }
+bool ppsocket::
+bindInRange(char *Host, int Low, int High, int Retries)
+{
+ int port, i;
- return(false);
+ // If we are already bound return FALSE but with no last error
+ m_LastError = 0;
+ if (m_Bound) {
+ return (false);
+ }
+ // If the socket hasn't been created create it now
+ if (m_Socket == INVALID_SOCKET) {
+ if (!createSocket()) {
+ return (false);
+ }
+ }
+ // If the number of retries is greater than the range then work
+ // through the range sequentially.
+ if (Retries > High - Low) {
+ for (port = Low; port <= High; port++) {
+ if (!setHost(Host, port))
+ return (false);
+
+ if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) == 0)
+ break;
+ }
+ if (port > High) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ } else {
+ for (i = 0; i < Retries; i++) {
+ port = Low + (rand() % (High - Low));
+ if (!setHost(Host, port))
+ return (false);
+ if (::bind(m_Socket, &m_HostAddr, sizeof(m_HostAddr)) == 0)
+ break;
+ }
+ if (i >= Retries) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ }
+ m_Bound = true;
+ return (true);
}
+bool ppsocket::
+linger(bool LingerOn, int LingerTime)
+{
+ int i;
+ struct linger l;
-bool ppsocket::setHost(char* Host, int Port)
-{
- struct hostent* he;
+ // If the socket hasn't been created create it now
+ if (m_Socket == INVALID_SOCKET) {
+ if (!createSocket()) {
+ return (false);
+ }
+ }
+ // Set the lingering
+ if (LingerOn) {
+ l.l_onoff = 1;
+ l.l_linger = LingerTime;
+ } else {
+ l.l_onoff = 0;
+ l.l_linger = 0;
+ }
+ i = setsockopt(m_Socket, SOL_SOCKET, SO_LINGER, (const char *) &l, sizeof(l));
+ // Check for errors
+ if (i != 0) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ // Return indicating success
+ return (true);
+}
- // If a host name was supplied then use it
- if (Host)
- {
- he = gethostbyname(Host);
-
- if (!he)
- {
- unsigned long ipaddress = inet_addr(Host);
- if (ipaddress == INADDR_NONE)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- he = gethostbyaddr((const char*) &ipaddress, 4, PF_INET);
-
- if (!he)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- }
+bool ppsocket::
+createSocket(void)
+{
+ // If the socket has already been created just return true
+ if (m_Socket != INVALID_SOCKET)
+ return (true);
+ // Create the socket
+ m_Socket =::socket(PF_INET, SOCK_STREAM, 0);
+ if (m_Socket == INVALID_SOCKET) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ // By default set no lingering
+ linger(FALSE, 0);
+ // Return indicating success
+ return (true);
+}
- memcpy((void*) &((struct sockaddr_in*) &m_HostAddr)->sin_addr, (void*) he->h_addr_list[0], 4);
- }
+bool ppsocket::
+setPeer(char *Peer, int Port)
+{
+ struct hostent *he;
+
+ // If a peer name was supplied then use it
+ if (Peer) {
+ he = gethostbyname(Peer);
+ if (!he) {
+ unsigned long ipaddress = inet_addr(Peer);
+ if (ipaddress == INADDR_NONE) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ he = gethostbyaddr((const char *) &ipaddress, 4, PF_INET);
+ if (!he) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ }
+ memcpy((void *) &((struct sockaddr_in *) &m_PeerAddr)->sin_addr, (void *) he->h_addr_list[0], 4);
+ }
+ // If a port name was supplied use it
+ if (Port > 0)
+ ((struct sockaddr_in *) &m_PeerAddr)->sin_port = htons((SHORT) Port);
+ return (true);
+}
- // If a port name was supplied use it
+bool ppsocket::
+getPeer(char *Peer, int MaxLen, int *Port)
+{
+ char *peer;
- if (Port > 0)
- {
- ((struct sockaddr_in*) &m_HostAddr)->sin_port = htons((SHORT) Port);
- }
+ if (Peer) {
+ peer = inet_ntoa(((struct sockaddr_in *) &m_PeerAddr)->sin_addr);
+ if (!peer) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ strncpy(Peer, peer, MaxLen);
+ Peer[MaxLen] = '\0';
+ }
+ if (Port)
+ *Port = ntohs(((struct sockaddr_in *) &m_PeerAddr)->sin_port);
+ return (false);
+}
- return(true);
-}
+bool ppsocket::
+setHost(char *Host, int Port)
+{
+ struct hostent *he;
+
+ // If a host name was supplied then use it
+ if (Host) {
+ he = gethostbyname(Host);
+ if (!he) {
+ unsigned long ipaddress = inet_addr(Host);
+ if (ipaddress == INADDR_NONE) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ he = gethostbyaddr((const char *) &ipaddress, 4, PF_INET);
+ if (!he) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ }
+ memcpy((void *) &((struct sockaddr_in *) &m_HostAddr)->sin_addr, (void *) he->h_addr_list[0], 4);
+ }
+ // If a port name was supplied use it
-bool ppsocket::getHost(char* Host, int MaxLen, int* Port)
-{
- char* host;
+ if (Port > 0)
+ ((struct sockaddr_in *) &m_HostAddr)->sin_port = htons((SHORT) Port);
+ return (true);
+}
- if (Host)
- {
- host = inet_ntoa(((struct sockaddr_in*) &m_HostAddr)->sin_addr);
-
- if (!host)
- {
- m_LastError = lastErrorCode();
- return(false);
- }
- strncpy(Host, host, MaxLen);
- Host[MaxLen] = '\0';
- }
+bool ppsocket::
+getHost(char *Host, int MaxLen, int *Port)
+{
+ char *host;
- if (Port)
- {
- *Port = ntohs(((struct sockaddr_in*) &m_HostAddr)->sin_port);
- }
- return(false);
+ if (Host) {
+ host = inet_ntoa(((struct sockaddr_in *) &m_HostAddr)->sin_addr);
+ if (!host) {
+ m_LastError = lastErrorCode();
+ return (false);
+ }
+ strncpy(Host, host, MaxLen);
+ Host[MaxLen] = '\0';
+ }
+ if (Port)
+ *Port = ntohs(((struct sockaddr_in *) &m_HostAddr)->sin_port);
+ return (false);
}
-DWORD ppsocket::lastErrorCode()
+DWORD ppsocket::
+lastErrorCode()
{
- return errno;
+ return errno;
}
-void ppsocket::setSocketInvalid()
+void ppsocket::
+setSocketInvalid()
{
- m_Socket = INVALID_SOCKET;
+ m_Socket = INVALID_SOCKET;
}