rename
This commit is contained in:
90
src/main.cpp
90
src/main.cpp
@@ -15,67 +15,73 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "server.h"
|
||||
#include "client.cpp"
|
||||
#include "util.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
// I stole the majority* of this multi-client sock code from IBM
|
||||
// of all places. Had to still modify it for my own uses but
|
||||
// laziness wins again!
|
||||
const int max_clients = config->getInt("net", "max_clients", 255);
|
||||
const int cport = config->getInt("net", "control_port", 21);
|
||||
|
||||
struct pollfd fds[MAXCLIENTS];
|
||||
struct pollfd fds[65535];
|
||||
struct clientfd {
|
||||
Client* client;
|
||||
bool close = false;
|
||||
} fdc[MAXCLIENTS];
|
||||
} fdc[65535];
|
||||
|
||||
bool run = true, compress_array = false;
|
||||
|
||||
void runClient(struct clientfd* cfd) {
|
||||
char inbuf[BUFFERSIZE];
|
||||
printf("[d] C(%i) Initialized\n", cfd->client->control_sock);
|
||||
//printf("[d] C(%i) Initialized\n", cfd->client->control_sock);
|
||||
// Loop as long as it is a valid file descriptor.
|
||||
try {
|
||||
while (fcntl(cfd->client->control_sock, F_GETFD) != -1) {
|
||||
printf("[d] C(%i) Attempting read...\n", cfd->client->control_sock);
|
||||
int rc = recv(cfd->client->control_sock, inbuf, sizeof(inbuf), 0);
|
||||
if (rc < 0) {
|
||||
if (errno != EWOULDBLOCK) {
|
||||
perror("recv() failed");
|
||||
//while (fcntl(cfd->client->control_sock, F_GETFD) != -1) {
|
||||
while (true) {
|
||||
if (cfd->client == nullptr) { break; }
|
||||
printf("[d] C(%i) Attempting read...\n", cfd->client->control_sock);
|
||||
int rc = recv(cfd->client->control_sock, inbuf, sizeof(inbuf), 0);
|
||||
if (rc < 0) {
|
||||
printf("[d] C(%i) Recieved empty packet\n", cfd->client->control_sock);
|
||||
if (errno != EWOULDBLOCK) {
|
||||
perror("recv() failed");
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rc == 0 || cfd->client == nullptr) {
|
||||
//printf("[d] C(%i) closed\n", cfd->client->control_sock);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rc == 0 || cfd->client == nullptr) {
|
||||
printf("[d] C(%i) closed\n", cfd->client->control_sock);
|
||||
break;
|
||||
}
|
||||
|
||||
std::string lin(inbuf);
|
||||
int len = lin.find("\r\n", 0);
|
||||
int cmdend = lin.find(" ", 0);
|
||||
if (cmdend >= len || cmdend == std::string::npos) cmdend = len;
|
||||
std::string cmd = toUpper(lin.substr(0, cmdend));
|
||||
std::string args = "";
|
||||
if (len > cmdend) args = lin.substr(cmdend+1, len-cmdend-1);
|
||||
|
||||
printf("[d] C(%i) >> '%s' '%s'\n", cfd->client->control_sock, cmd.c_str(), args.c_str());
|
||||
|
||||
if (cfd->client->receive(cmd, args) < 0) break;
|
||||
inbuf[0] = '\0';
|
||||
}
|
||||
std::string lin(inbuf);
|
||||
int len = lin.find("\r\n", 0);
|
||||
int cmdend = lin.find(" ", 0);
|
||||
if (cmdend >= len || cmdend == std::string::npos) cmdend = len;
|
||||
std::string cmd = toUpper(lin.substr(0, cmdend));
|
||||
std::string args = "";
|
||||
if (len > cmdend) args = lin.substr(cmdend+1, len-cmdend-1);
|
||||
|
||||
printf("[d] C(%i) >> '%s' '%s'\n", cfd->client->control_sock, cmd.c_str(), args.c_str());
|
||||
|
||||
if (cfd->client->receive(cmd, args) < 0) break;
|
||||
inbuf[0] = '\0';
|
||||
}
|
||||
} catch (...) {
|
||||
printf("[!] C(%i) Caught error!\n", cfd->client->control_sock);
|
||||
}
|
||||
cfd->client->thread.detach();
|
||||
printf("[d] C(%i) Marking for deletion...\n", cfd->client->control_sock);
|
||||
//printf("[d] C(%i) Marking for deletion...\n", cfd->client->control_sock);
|
||||
cfd->close = true;
|
||||
}
|
||||
|
||||
int main(int argc , char *argv[]) {
|
||||
std::string authType = config->getValue("main", "auth_engine", "plain");
|
||||
auth = getAuthByName(authType);
|
||||
auth->setOptions(config->get(authType));
|
||||
|
||||
int opt = 1,
|
||||
master_socket = -1,
|
||||
newsock = -1,
|
||||
@@ -103,7 +109,7 @@ int main(int argc , char *argv[]) {
|
||||
|
||||
ctrl_address.sin_family = AF_INET;
|
||||
ctrl_address.sin_addr.s_addr = INADDR_ANY;
|
||||
ctrl_address.sin_port = htons(CONTROL_PORT);
|
||||
ctrl_address.sin_port = htons(cport);
|
||||
|
||||
if (bind(master_socket, (struct sockaddr *)&ctrl_address, sizeof(ctrl_address))<0 < 0) {
|
||||
perror("bind() failed");
|
||||
@@ -117,8 +123,8 @@ int main(int argc , char *argv[]) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
memset(fds, 0 , sizeof(fds));
|
||||
memset(fdc, 0 , sizeof(fdc));
|
||||
memset(fds, 0, sizeof(fds));
|
||||
memset(fdc, 0, sizeof(fdc));
|
||||
|
||||
fds[0].fd = master_socket;
|
||||
fds[0].events = POLLIN;
|
||||
@@ -146,7 +152,6 @@ int main(int argc , char *argv[]) {
|
||||
printf("[!] C(%i) Error! revents = %d\n", fds[i].fd, fds[i].revents);
|
||||
goto conn_close;
|
||||
}
|
||||
|
||||
if (fds[i].fd == master_socket) {
|
||||
do {
|
||||
newsock = accept(master_socket, NULL, NULL);
|
||||
@@ -177,10 +182,11 @@ int main(int argc , char *argv[]) {
|
||||
fdc[i].client = nullptr;
|
||||
fdc[i].close = false;
|
||||
compress_array = true;
|
||||
} else {
|
||||
fds[i].revents = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (compress_array) {
|
||||
compress_array = false;
|
||||
for (int i = 0; i < nfds; i++) {
|
||||
@@ -189,7 +195,13 @@ int main(int argc , char *argv[]) {
|
||||
if (fds[j].fd == -1) {
|
||||
printf("[d] Compressing: %i(fd:%i) <= %i(fd:%i)\n", j, fds[j].fd, j+1, fds[j+1].fd);
|
||||
fds[j].fd = fds[j+1].fd;
|
||||
fds[j].revents = fds[j+1].revents;
|
||||
fds[j+1].fd = -1;
|
||||
printf("[d] Reinitialized fds of %i\n", j+1);
|
||||
fdc[j].client = fdc[j+1].client;
|
||||
fdc[j].close = fdc[j+1].close;
|
||||
fdc[j+1] = {};
|
||||
printf("[d] Reinitialized fdc of %i\n", j+1);
|
||||
}
|
||||
}
|
||||
i--;
|
||||
|
||||
Reference in New Issue
Block a user