summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2014-01-21 00:41:55 +0100
committerTreeki <treeki@gmail.com>2014-01-21 00:41:55 +0100
commit8b32f42df825a03284f5c340499394b93dc1a65d (patch)
tree88481b62ea3d9609d352857127d13ce3624fd9a2
parent84e469bef68f67cd56c4a643dcfc9d050c5cd398 (diff)
downloadbounce4-8b32f42df825a03284f5c340499394b93dc1a65d.tar.gz
bounce4-8b32f42df825a03284f5c340499394b93dc1a65d.zip
refactoring: add basic implementation of the IRCServer class
-rwxr-xr-xbuild.sh2
-rw-r--r--client.cpp3
-rw-r--r--core.h29
-rw-r--r--ircserver.cpp9
-rw-r--r--mobileclient.cpp14
-rw-r--r--server.cpp16
6 files changed, 55 insertions, 18 deletions
diff --git a/build.sh b/build.sh
index c8cff18..2bfdfb3 100755
--- a/build.sh
+++ b/build.sh
@@ -1,7 +1,7 @@
#!/bin/sh
mkdir -p binary
-NETCODE="socketcommon.cpp client.cpp mobileclient.cpp server.cpp netcore.cpp"
+NETCODE="socketcommon.cpp client.cpp mobileclient.cpp server.cpp ircserver.cpp netcore.cpp"
SOURCES="$NETCODE main.cpp dns.cpp"
FLAGS="-std=c++11 -lgnutls -pthread -g"
diff --git a/client.cpp b/client.cpp
index b4f76c5..9cbb889 100644
--- a/client.cpp
+++ b/client.cpp
@@ -166,12 +166,13 @@ void Client::handlePacket(Packet::Type type, char *data, int size) {
if (!isNullSessionKey(reqKey)) {
printf("[fd=%d] Trying to resume session...", sock);
- printf("(last they received = %d, last we sent = %d)\n", lastReceivedByClient, nextPacketID - 1);
+ printf("(last they received = %d)\n", lastReceivedByClient);
Client *other = netCore->findClientWithSessionKey(reqKey);
printf("[fd=%d] Got client %p\n", sock, other);
if (other && other->authState == AS_AUTHED) {
+ printf("Valid: last packet we sent = %d\n", other->nextPacketID - 1);
// Yep, we can go!
other->resumeSession(this, lastReceivedByClient);
return;
diff --git a/core.h b/core.h
index cf2f8a9..cd45356 100644
--- a/core.h
+++ b/core.h
@@ -139,15 +139,18 @@ private:
};
struct Server : SocketRWCommon {
- char ircHostname[256];
- int ircPort;
+ int port;
+ bool useTls;
+
int dnsQueryId;
- bool ircUseTls;
Server(NetCore *_netCore);
~Server();
- void beginConnect();
+protected:
+ void connect(const char *hostname, int _port, bool _useTls);
+
+public:
void tryConnectPhase();
void connectionSuccessful();
@@ -158,6 +161,24 @@ private:
void handleLine(char *line, int size);
};
+struct IRCNetworkConfig {
+ char hostname[512];
+ char nickname[128];
+ char realname[128];
+ char password[128];
+ int port;
+ bool useTls;
+};
+
+struct IRCServer : Server {
+ Bouncer *bouncer;
+ IRCNetworkConfig config;
+
+ IRCServer(Bouncer *_bouncer);
+
+ void connect();
+};
+
struct NetCore {
NetCore();
diff --git a/ircserver.cpp b/ircserver.cpp
new file mode 100644
index 0000000..94456dc
--- /dev/null
+++ b/ircserver.cpp
@@ -0,0 +1,9 @@
+#include "core.h"
+
+IRCServer::IRCServer(Bouncer *_bouncer) : Server(_bouncer) {
+ bouncer = _bouncer;
+}
+
+void IRCServer::connect() {
+ Server::connect(config.hostname, config.port, config.useTls);
+}
diff --git a/mobileclient.cpp b/mobileclient.cpp
index d7930a1..2377a35 100644
--- a/mobileclient.cpp
+++ b/mobileclient.cpp
@@ -1,6 +1,7 @@
#include "core.h"
MobileClient::MobileClient(Bouncer *_bouncer) : Client(_bouncer) {
+ bouncer = _bouncer;
}
void MobileClient::sessionStartEvent() {
@@ -33,11 +34,11 @@ void MobileClient::handleDebugCommand(char *line, int size) {
} else if (strcmp(line, "quit") == 0) {
netCore->quitFlag = true;
} else if (strncmp(&line[1], "ddsrv ", 6) == 0) {
- Server *srv = new Server(netCore);
- strcpy(srv->ircHostname, &line[7]);
- srv->ircPort = 1191;
- srv->ircUseTls = (line[0] == 's');
- netCore->registerServer(srv);
+ IRCServer *srv = new IRCServer(bouncer);
+ strcpy(srv->config.hostname, &line[7]);
+ srv->config.port = 1191;
+ srv->config.useTls = (line[0] == 's');
+ bouncer->registerServer(srv);
Buffer pkt;
pkt.writeStr("Your wish is my command!");
@@ -46,7 +47,8 @@ void MobileClient::handleDebugCommand(char *line, int size) {
} else if (strncmp(line, "connsrv", 7) == 0) {
int sid = line[7] - '0';
- netCore->servers[sid]->beginConnect();
+ // ugly hack, fuck casting, will fix later
+ ((IRCServer*)netCore->servers[sid])->connect();
} else if (line[0] >= '0' && line[0] <= '9') {
int sid = line[0] - '0';
netCore->servers[sid]->outputBuf.append(&line[1], size - 1);
diff --git a/server.cpp b/server.cpp
index 5739159..e892aa9 100644
--- a/server.cpp
+++ b/server.cpp
@@ -3,7 +3,6 @@
Server::Server(NetCore *_netCore) : SocketRWCommon(_netCore) {
dnsQueryId = -1;
- ircUseTls = false;
}
Server::~Server() {
if (dnsQueryId != -1)
@@ -46,10 +45,13 @@ void Server::processReadBuffer() {
-void Server::beginConnect() {
+void Server::connect(const char *hostname, int _port, bool _useTls) {
if (state == CS_DISCONNECTED) {
+ port = _port;
+ useTls = _useTls;
+
DNS::closeQuery(dnsQueryId); // just in case
- dnsQueryId = DNS::makeQuery(ircHostname);
+ dnsQueryId = DNS::makeQuery(hostname);
if (dnsQueryId == -1) {
// TODO: better error reporting
@@ -91,10 +93,10 @@ void Server::tryConnectPhase() {
// We have our non-blocking socket, let's try connecting!
sockaddr_in outAddr;
outAddr.sin_family = AF_INET;
- outAddr.sin_port = htons(ircPort);
+ outAddr.sin_port = htons(port);
outAddr.sin_addr.s_addr = result.s_addr;
- if (connect(sock, (sockaddr *)&outAddr, sizeof(outAddr)) == -1) {
+ if (::connect(sock, (sockaddr *)&outAddr, sizeof(outAddr)) == -1) {
if (errno == EINPROGRESS) {
state = CS_WAITING_CONNECT;
} else {
@@ -117,7 +119,7 @@ void Server::connectionSuccessful() {
outputBuf.clear();
// Do we need to do any TLS junk?
- if (ircUseTls) {
+ if (useTls) {
int initRet = gnutls_init(&tls, GNUTLS_CLIENT);
if (initRet != GNUTLS_E_SUCCESS) {
printf("[Server::connectionSuccessful] gnutls_init borked\n");
@@ -147,3 +149,5 @@ void Server::close() {
dnsQueryId = -1;
}
}
+
+