irc socket on CTCPReaderWriterClient

pull/14/head
Tom Early 7 years ago
parent c03fb351a5
commit 54d0f7d4f9

@ -275,7 +275,6 @@ bool CQnetGateway::ReadConfig(char *cfgFile)
// gateway // gateway
path.assign("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+"ip", estr, g2_external.ip, 7, IP_SIZE);
cfg.GetValue(path+"port", estr, g2_external.port, 1024, 65535); cfg.GetValue(path+"port", estr, g2_external.port, 1024, 65535);
cfg.GetValue(path+"header_regen", estr, GATEWAY_HEADER_REGEN); cfg.GetValue(path+"header_regen", estr, GATEWAY_HEADER_REGEN);
@ -2397,7 +2396,7 @@ bool CQnetGateway::Init(char *cfgfile)
} }
compute_aprs_hash(); 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(); bool ok = ii->open();
if (!ok) { if (!ok) {
printf("irc open failed\n"); printf("irc open failed\n");

@ -97,7 +97,7 @@ private:
std::string gate2link, link2gate, gate2modem[3], modem2gate; 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 GATEWAY_SEND_QRGS_MAP, GATEWAY_HEADER_REGEN, APRS_ENABLE, playNotInCache;
bool LOG_DEBUG, LOG_IRC, LOG_DTMF, LOG_QSO; bool LOG_DEBUG, LOG_IRC, LOG_DTMF, LOG_QSO;

@ -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_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_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_ip_d='0.0.0.0' # the g2 port
gateway_port_d=40000 # don't change gateway_port_d=40000 # don't change
gateway_gate2link_d='gate2link' # Unix sockets between qngateway and QnetLink gateway_gate2link_d='gate2link' # Unix sockets between qngateway and QnetLink

@ -6,11 +6,12 @@
#include "IRCClient.h" #include "IRCClient.h"
#include "IRCutils.h" #include "IRCutils.h"
#include "../TCPReaderWriterClient.h"
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
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); 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; 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); proto = new IRCProtocol(app, this->callsign, password, update_channel, versionInfo);
recvQ = NULL; recvQ = NULL;
@ -57,26 +53,10 @@ void IRCClient::stopWork()
#define MAXIPV4ADDR 10 #define MAXIPV4ADDR 10
void IRCClient::Entry() void IRCClient::Entry()
{ {
CTCPReaderWriterClient ircSock;
unsigned int numAddr;
struct sockaddr_in addr[MAXIPV4ADDR];
struct sockaddr_in myaddr;
int state = 0; int state = 0;
int timer = 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) { while (true) {
@ -94,158 +74,24 @@ void IRCClient::Entry()
if (timer == 0) { if (timer == 0) {
timer = 30; timer = 30;
if (getAllIPV4Addresses(host_name, port, &numAddr, addr, MAXIPV4ADDR) == 0) { if (! ircSock.Open(host_name, AF_UNSPEC, std::to_string(port))) {
//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; state = 4;
} else { timer = 0;
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
} }
} }
break; 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) { case 4:
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: {
recvQ = new IRCMessageQueue(); recvQ = new IRCMessageQueue();
sendQ = new IRCMessageQueue(); sendQ = new IRCMessageQueue();
recv = new IRCReceiver(sock, recvQ); recv = new IRCReceiver(&ircSock, recvQ);
recv->startWork(); recv->startWork();
proto->setNetworkReady(true); proto->setNetworkReady(true);
state = 5; state = 5;
timer = 0; timer = 0;
}
break; break;
@ -274,18 +120,12 @@ void IRCClient::Entry()
int len = strlen(buf); int len = strlen(buf);
if (buf[len - 1] == 10) { // is there a NL char at the end? if (buf[len - 1] == 10) { // is there a NL char at the end?
int r = send(sock, buf, len, 0); if (ircSock.Write((unsigned char *)buf, len)) {
printf("IRCClient::Entry: short write\n");
if (r != len) {
printf("IRCClient::Entry: short write %d < %d\n", r, len);
timer = 0; timer = 0;
state = 6; state = 6;
} }
/* else
{
printf("write %d bytes (%s)\n", len, out.c_str());
} */
} else { } else {
printf("IRCClient::Entry: no NL at end, len=%d\n", len); printf("IRCClient::Entry: no NL at end, len=%d\n", len);
@ -313,7 +153,7 @@ void IRCClient::Entry()
delete recvQ; delete recvQ;
delete sendQ; delete sendQ;
close(sock); ircSock.Close();
if (terminateThread) { // request to end the thread if (terminateThread) { // request to end the thread
printf("IRCClient::Entry: thread terminated at state=%d\n", state); printf("IRCClient::Entry: thread terminated at state=%d\n", state);

@ -10,8 +10,7 @@
class IRCClient class IRCClient
{ {
public: public:
IRCClient(IRCApplication *app, const std::string &update_channel, const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, 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 &versionInfo, const std::string &localAddr);
virtual ~IRCClient(); virtual ~IRCClient();
bool startWork(); bool startWork();

@ -9,8 +9,7 @@ struct CIRCDDBPrivate {
IRCDDBApp *app; IRCDDBApp *app;
}; };
CIRCDDB::CIRCDDB(const std::string &hostName, unsigned int port, const std::string &callsign, const std::string &password, const std::string &versionInfo)
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)
: d(new CIRCDDBPrivate) : 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->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() CIRCDDB::~CIRCDDB()

@ -21,7 +21,7 @@ struct CIRCDDBPrivate;
class CIRCDDB class CIRCDDB
{ {
public: 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(); ~CIRCDDB();
// A false return implies a network error, or unable to log in // A false return implies a network error, or unable to log in

@ -6,7 +6,7 @@
#include "IRCMessage.h" #include "IRCMessage.h"
#include "IRCReceiver.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; struct timeval tv;
tv.tv_sec = 1; tv.tv_sec = 1;
@ -14,26 +14,27 @@ static int doRead(int sock, char * buf, int buf_size)
fd_set rdset; fd_set rdset;
fd_set errset; fd_set errset;
int fd = ircSock->GetFD();
FD_ZERO(&rdset); FD_ZERO(&rdset);
FD_ZERO(&errset); FD_ZERO(&errset);
FD_SET(sock, &rdset); FD_SET(fd, &rdset);
FD_SET(sock, &errset); FD_SET(fd, &errset);
int res; int res;
res = select(sock+1, &rdset, NULL, &errset, &tv); res = select(fd+1, &rdset, NULL, &errset, &tv);
if ( res < 0 ) { if ( res < 0 ) {
printf("IRCReceiver::doread: select() error.\n"); printf("IRCReceiver::doread: select() error.\n");
return -1; return -1;
} else if ( res > 0 ) { } else if ( res > 0 ) {
if (FD_ISSET(sock, &errset)) { if (FD_ISSET(fd, &errset)) {
printf("IRCReceiver::doRead: FD_ISSET error\n"); printf("IRCReceiver::doRead: FD_ISSET error\n");
return -1; return -1;
} }
if (FD_ISSET(sock, &rdset)) { if (FD_ISSET(fd, &rdset)) {
res = recv(sock, buf, buf_size, 0); res = ircSock->Read((unsigned char *)buf, buf_size);
if (res < 0) { if (res < 0) {
printf("IRCReceiver::doRead: recv error\n"); printf("IRCReceiver::doRead: recv error\n");
@ -59,7 +60,7 @@ void IRCReceiver::Entry()
while (!terminateThread) { while (!terminateThread) {
char buf[200]; char buf[200];
int r = doRead(sock, buf, sizeof buf); int r = doRead(ircSock, buf, sizeof buf);
if (r < 0) { if (r < 0) {
recvQ->signalEOF(); recvQ->signalEOF();
@ -133,9 +134,9 @@ void IRCReceiver::Entry()
return; return;
} }
IRCReceiver::IRCReceiver(int sock, IRCMessageQueue *q) IRCReceiver::IRCReceiver(CTCPReaderWriterClient *sock, IRCMessageQueue *q)
{ {
this->sock = sock; ircSock = sock;
recvQ = q; recvQ = q;
} }

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <future> #include <future>
#include "IRCMessageQueue.h" #include "IRCMessageQueue.h"
#include "../TCPReaderWriterClient.h"
class IRCReceiver class IRCReceiver
{ {
public: public:
IRCReceiver(int sock, IRCMessageQueue *q); IRCReceiver(CTCPReaderWriterClient *ircSock, IRCMessageQueue *q);
virtual ~IRCReceiver(); virtual ~IRCReceiver();
bool startWork(); bool startWork();
void stopWork(); void stopWork();
@ -14,6 +15,7 @@ protected:
virtual void Entry(); virtual void Entry();
private: private:
CTCPReaderWriterClient *ircSock;
bool terminateThread; bool terminateThread;
int sock; int sock;
IRCMessageQueue *recvQ; IRCMessageQueue *recvQ;

Loading…
Cancel
Save

Powered by TurnKey Linux.