From 54d0f7d4f9fb5a8ba4cbc5ffb8c46b188df3beed Mon Sep 17 00:00:00 2001 From: Tom Early Date: Fri, 5 Apr 2019 16:29:48 -0700 Subject: [PATCH] irc socket on CTCPReaderWriterClient --- QnetGateway.cpp | 3 +- QnetGateway.h | 2 +- defaults | 1 - ircddb/IRCClient.cpp | 184 +++-------------------------------------- ircddb/IRCClient.h | 3 +- ircddb/IRCDDB.cpp | 5 +- ircddb/IRCDDB.h | 2 +- ircddb/IRCReceiver.cpp | 21 ++--- ircddb/IRCReceiver.h | 4 +- 9 files changed, 32 insertions(+), 193 deletions(-) diff --git a/QnetGateway.cpp b/QnetGateway.cpp index 4e267da..e6559bc 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -275,7 +275,6 @@ bool CQnetGateway::ReadConfig(char *cfgFile) // gateway path.assign("gateway_"); - cfg.GetValue(path+"local_irc_ip", estr, GATEWAY_LOCAL_IRC_IP, 7, IP_SIZE); cfg.GetValue(path+"ip", estr, g2_external.ip, 7, IP_SIZE); cfg.GetValue(path+"port", estr, g2_external.port, 1024, 65535); cfg.GetValue(path+"header_regen", estr, GATEWAY_HEADER_REGEN); @@ -2397,7 +2396,7 @@ bool CQnetGateway::Init(char *cfgfile) } compute_aprs_hash(); - ii = new CIRCDDB(ircddb.ip, ircddb.port, owner, IRCDDB_PASSWORD, IRCDDB_VERSION, GATEWAY_LOCAL_IRC_IP); + ii = new CIRCDDB(ircddb.ip, ircddb.port, owner, IRCDDB_PASSWORD, IRCDDB_VERSION); bool ok = ii->open(); if (!ok) { printf("irc open failed\n"); diff --git a/QnetGateway.h b/QnetGateway.h index 2afc5d4..5e7b3ba 100644 --- a/QnetGateway.h +++ b/QnetGateway.h @@ -97,7 +97,7 @@ private: std::string gate2link, link2gate, gate2modem[3], modem2gate; - std::string OWNER, owner, GATEWAY_LOCAL_IRC_IP, FILE_STATUS, FILE_DTMF, FILE_ECHOTEST, IRCDDB_PASSWORD, FILE_QNVOICE_FILE; + std::string OWNER, owner, FILE_STATUS, FILE_DTMF, FILE_ECHOTEST, IRCDDB_PASSWORD, FILE_QNVOICE_FILE; bool GATEWAY_SEND_QRGS_MAP, GATEWAY_HEADER_REGEN, APRS_ENABLE, playNotInCache; bool LOG_DEBUG, LOG_IRC, LOG_DTMF, LOG_QSO; diff --git a/defaults b/defaults index d170d39..f00a759 100644 --- a/defaults +++ b/defaults @@ -44,7 +44,6 @@ ircddb_password_d='1111111111111' # not needed for rr.openquad.net # gateway_header_regen_d=true # regenerate headers from incoming data gateway_send_qrgs_maps_d=true # send frequency, offset, coordinates and url to irc-server -gateway_local_irc_ip_d='0.0.0.0' # the local port on the gateway for the IRC TCP socket gateway_ip_d='0.0.0.0' # the g2 port gateway_port_d=40000 # don't change gateway_gate2link_d='gate2link' # Unix sockets between qngateway and QnetLink diff --git a/ircddb/IRCClient.cpp b/ircddb/IRCClient.cpp index 2498c30..bb4e43d 100644 --- a/ircddb/IRCClient.cpp +++ b/ircddb/IRCClient.cpp @@ -6,11 +6,12 @@ #include "IRCClient.h" #include "IRCutils.h" +#include "../TCPReaderWriterClient.h" #include #include -IRCClient::IRCClient(IRCApplication *app, const std::string &update_channel, const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo, const std::string &localAddr) +IRCClient::IRCClient(IRCApplication *app, const std::string &update_channel, const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo) { safeStringCopy(host_name, hostName.c_str(), sizeof host_name); @@ -22,11 +23,6 @@ IRCClient::IRCClient(IRCApplication *app, const std::string &update_channel, con this->app = app; - if (localAddr.empty()) - safeStringCopy(local_addr, "0.0.0.0", sizeof local_addr); - else - safeStringCopy(local_addr, localAddr.c_str(), sizeof local_addr); - proto = new IRCProtocol(app, this->callsign, password, update_channel, versionInfo); recvQ = NULL; @@ -57,26 +53,10 @@ void IRCClient::stopWork() #define MAXIPV4ADDR 10 void IRCClient::Entry() { - - unsigned int numAddr; - - struct sockaddr_in addr[MAXIPV4ADDR]; - - struct sockaddr_in myaddr; + CTCPReaderWriterClient ircSock; int state = 0; int timer = 0; - int sock = 0; - unsigned int currentAddr = 0; - - numAddr = 0; - - int result = getAllIPV4Addresses(local_addr, 0, &numAddr, &myaddr, 1); - - if ((result != 0) || (numAddr != 1)) { - printf("IRCClient::Entry: local address not parseable, using 0.0.0.0\n"); - memset(&myaddr, 0x00, sizeof(struct sockaddr_in)); - } while (true) { @@ -94,159 +74,25 @@ void IRCClient::Entry() if (timer == 0) { timer = 30; - if (getAllIPV4Addresses(host_name, port, &numAddr, addr, MAXIPV4ADDR) == 0) { - //printf("IRCClient::Entry: number of DNS entries %d\n", numAddr); - if (numAddr > 0) { - currentAddr = 0; - state = 1; - timer = 0; - } - } - } - break; - - case 1: - if (terminateThread) { - printf("IRCClient::Entry: thread terminated at state=%d\n", state); - return; - } - - if (timer == 0) { - sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - - if (sock < 0) { - printf("IRCClient::Entry: could not create socket!\n"); - timer = 30; - state = 0; - } else { - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { - printf("IRCClient::Entry: fcntl error\n"); - close(sock); - timer = 30; - state = 0; - } - else { - unsigned char *h = (unsigned char *) &(myaddr.sin_addr); - int res; - - if ((h[0] != 0) || (h[1] != 0) || (h[2] != 0) || (h[3] != 0)) - printf("IRCClient::Entry: bind: local address %d.%d.%d.%d\n", h[0], h[1], h[2], h[3]); - - res = bind(sock, (struct sockaddr *) &myaddr, sizeof (struct sockaddr_in)); - - if (res != 0) { - printf("IRCClient::Entry: bind error\n"); - close(sock); - state = 0; - timer = 30; - break; - } - - - h = (unsigned char *) &(addr[currentAddr].sin_addr); - //printf("IRCClient::Entry: trying to connect to %d.%d.%d.%d\n", h[0], h[1], h[2], h[3]); - - res = connect(sock, (struct sockaddr *) (addr + currentAddr), sizeof (struct sockaddr_in)); - - if (res == 0) { - printf("IRCClient::Entry: connected to %d.%d.%d.%d\n", h[0], h[1], h[2], h[3]); - state = 4; - } else { - if (errno == EINPROGRESS) { - //printf("IRCClient::Entry: connect in progress\n"); - state = 3; - timer = 10; // 5 second timeout - } else { - printf("IRCClient::Entry: connect\n"); - close(sock); - currentAddr++; - if (currentAddr >= numAddr) { - state = 0; - timer = 30; - } else { - state = 1; - timer = 4; - } - } - } - } // connect + if (! ircSock.Open(host_name, AF_UNSPEC, std::to_string(port))) { + state = 4; + timer = 0; } } break; - case 3: { - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - fd_set myset; - FD_ZERO(&myset); - FD_SET(sock, &myset); - int res = select(sock+1, NULL, &myset, NULL, &tv); - - if (res < 0) { - printf("IRCClient::Entry: select\n"); - close(sock); - state = 0; - timer = 30; - } else if (res > 0) { // connect is finished - socklen_t val_len; - int value; - - val_len = sizeof value; - - if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *) &value, &val_len) < 0) { - printf("IRCClient::Entry: getsockopt error\n"); - close(sock); - state = 0; - timer = 30; - } else { - if (value != 0) { - printf("IRCClient::Entry: SO_ERROR=%d\n", value); - close(sock); - currentAddr ++; - if (currentAddr >= numAddr) { - state = 0; - timer = 30; - } else { - state = 1; - timer = 2; - } - } else { - printf("IRCClient::Entry: connected2\n"); - state = 4; - } - } - - } else if (timer == 0) { - // select timeout and timer timeout - //printf("IRCClient::Entry: connect timeout\n"); - close(sock); - currentAddr++; - if (currentAddr >= numAddr) { - state = 0; - timer = 30; - } else { - state = 1; // open new socket - timer = 2; - } - } - } - break; - - case 4: { + case 4: recvQ = new IRCMessageQueue(); sendQ = new IRCMessageQueue(); - recv = new IRCReceiver(sock, recvQ); + recv = new IRCReceiver(&ircSock, recvQ); recv->startWork(); proto->setNetworkReady(true); state = 5; timer = 0; - - } - break; + break; case 5: @@ -274,18 +120,12 @@ void IRCClient::Entry() int len = strlen(buf); if (buf[len - 1] == 10) { // is there a NL char at the end? - int r = send(sock, buf, len, 0); - - if (r != len) { - printf("IRCClient::Entry: short write %d < %d\n", r, len); + if (ircSock.Write((unsigned char *)buf, len)) { + printf("IRCClient::Entry: short write\n"); timer = 0; state = 6; } - /* else - { - printf("write %d bytes (%s)\n", len, out.c_str()); - } */ } else { printf("IRCClient::Entry: no NL at end, len=%d\n", len); @@ -313,7 +153,7 @@ void IRCClient::Entry() delete recvQ; delete sendQ; - close(sock); + ircSock.Close(); if (terminateThread) { // request to end the thread printf("IRCClient::Entry: thread terminated at state=%d\n", state); diff --git a/ircddb/IRCClient.h b/ircddb/IRCClient.h index f79f711..e64d381 100644 --- a/ircddb/IRCClient.h +++ b/ircddb/IRCClient.h @@ -10,8 +10,7 @@ class IRCClient { public: - IRCClient(IRCApplication *app, const std::string &update_channel, const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, - const std::string &versionInfo, const std::string &localAddr); + IRCClient(IRCApplication *app, const std::string &update_channel, const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo); virtual ~IRCClient(); bool startWork(); diff --git a/ircddb/IRCDDB.cpp b/ircddb/IRCDDB.cpp index d23571b..6e0e736 100644 --- a/ircddb/IRCDDB.cpp +++ b/ircddb/IRCDDB.cpp @@ -9,8 +9,7 @@ struct CIRCDDBPrivate { IRCDDBApp *app; }; - -CIRCDDB::CIRCDDB(const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo, const std::string &localAddr) +CIRCDDB::CIRCDDB(const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo) : d(new CIRCDDBPrivate) { @@ -18,7 +17,7 @@ CIRCDDB::CIRCDDB(const std::string &hostName, unsigned int port, const std::stri d->app = new IRCDDBApp(update_channel); - d->client = new IRCClient(d->app, update_channel, hostName, port, callsign, password, versionInfo, localAddr); + d->client = new IRCClient(d->app, update_channel, hostName, port, callsign, password, versionInfo); } CIRCDDB::~CIRCDDB() diff --git a/ircddb/IRCDDB.h b/ircddb/IRCDDB.h index af2691b..3cbfddf 100644 --- a/ircddb/IRCDDB.h +++ b/ircddb/IRCDDB.h @@ -21,7 +21,7 @@ struct CIRCDDBPrivate; class CIRCDDB { public: - CIRCDDB(const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo, const std::string &localAddr = ""); + CIRCDDB(const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo); ~CIRCDDB(); // A false return implies a network error, or unable to log in diff --git a/ircddb/IRCReceiver.cpp b/ircddb/IRCReceiver.cpp index f2c0bcf..25d0a59 100644 --- a/ircddb/IRCReceiver.cpp +++ b/ircddb/IRCReceiver.cpp @@ -6,7 +6,7 @@ #include "IRCMessage.h" #include "IRCReceiver.h" -static int doRead(int sock, char * buf, int buf_size) +static int doRead(CTCPReaderWriterClient *ircSock, char *buf, int buf_size) { struct timeval tv; tv.tv_sec = 1; @@ -14,26 +14,27 @@ static int doRead(int sock, char * buf, int buf_size) fd_set rdset; fd_set errset; + int fd = ircSock->GetFD(); FD_ZERO(&rdset); FD_ZERO(&errset); - FD_SET(sock, &rdset); - FD_SET(sock, &errset); + FD_SET(fd, &rdset); + FD_SET(fd, &errset); int res; - res = select(sock+1, &rdset, NULL, &errset, &tv); + res = select(fd+1, &rdset, NULL, &errset, &tv); if ( res < 0 ) { printf("IRCReceiver::doread: select() error.\n"); return -1; } else if ( res > 0 ) { - if (FD_ISSET(sock, &errset)) { + if (FD_ISSET(fd, &errset)) { printf("IRCReceiver::doRead: FD_ISSET error\n"); return -1; } - if (FD_ISSET(sock, &rdset)) { - res = recv(sock, buf, buf_size, 0); + if (FD_ISSET(fd, &rdset)) { + res = ircSock->Read((unsigned char *)buf, buf_size); if (res < 0) { printf("IRCReceiver::doRead: recv error\n"); @@ -59,7 +60,7 @@ void IRCReceiver::Entry() while (!terminateThread) { char buf[200]; - int r = doRead(sock, buf, sizeof buf); + int r = doRead(ircSock, buf, sizeof buf); if (r < 0) { recvQ->signalEOF(); @@ -133,9 +134,9 @@ void IRCReceiver::Entry() return; } -IRCReceiver::IRCReceiver(int sock, IRCMessageQueue *q) +IRCReceiver::IRCReceiver(CTCPReaderWriterClient *sock, IRCMessageQueue *q) { - this->sock = sock; + ircSock = sock; recvQ = q; } diff --git a/ircddb/IRCReceiver.h b/ircddb/IRCReceiver.h index 8f5323f..e1241cd 100644 --- a/ircddb/IRCReceiver.h +++ b/ircddb/IRCReceiver.h @@ -1,11 +1,12 @@ #pragma once #include #include "IRCMessageQueue.h" +#include "../TCPReaderWriterClient.h" class IRCReceiver { public: - IRCReceiver(int sock, IRCMessageQueue *q); + IRCReceiver(CTCPReaderWriterClient *ircSock, IRCMessageQueue *q); virtual ~IRCReceiver(); bool startWork(); void stopWork(); @@ -14,6 +15,7 @@ protected: virtual void Entry(); private: + CTCPReaderWriterClient *ircSock; bool terminateThread; int sock; IRCMessageQueue *recvQ;