Add passive mode (only for localhost) and fix random thread crashes

This commit is contained in:
2023-12-12 19:56:25 -06:00
parent d996ec9fb8
commit 184288ce24
2 changed files with 99 additions and 59 deletions

View File

@@ -28,7 +28,6 @@ using namespace std::chrono_literals;
struct pollfd fds[MAXCLIENTS];
struct clientfd {
Client* client;
std::thread thread;
bool close = false;
} fdc[MAXCLIENTS];
@@ -38,6 +37,7 @@ void runClient(struct clientfd* cfd) {
char inbuf[BUFFERSIZE];
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);
@@ -67,9 +67,12 @@ void runClient(struct clientfd* cfd) {
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);
cfd->close = true;
cfd->thread.detach();
}
int main(int argc , char *argv[]) {
@@ -79,10 +82,9 @@ int main(int argc , char *argv[]) {
nfds = 1,
current_size = 0;
char inbuf[BUFFERSIZE];
struct sockaddr_in ctrl_address;
if ((master_socket = socket(AF_INET , SOCK_STREAM , 0)) < 0) {
if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket() failed");
exit(-1);
}
@@ -137,8 +139,6 @@ int main(int argc , char *argv[]) {
current_size = nfds;
for (int i = 0; i < current_size; i++) {
inbuf[0] = '\0';
if(fds[i].revents == 0)
continue;
@@ -158,56 +158,22 @@ int main(int argc , char *argv[]) {
break;
}
printf("[d] C(%i) Accepted client%i\n", newsock);
// If we assign thead if it is still attached,
// we suffer greatly, and by we I mean me.
if (fdc[nfds].thread.joinable()) {
printf("[!] C(%i) Thread still joinable! Detaching...\n", newsock);
// Pray the thread will end itself.
fdc[nfds].thread.detach();
}
printf("[d] C(%i) Accepted client\n", newsock);
fds[nfds].fd = newsock;
fds[nfds].events = POLLIN;
fdc[nfds].close = false;
fdc[nfds].client = new Client(newsock);
fdc[nfds].thread = std::thread(runClient, &fdc[nfds]);
fdc[nfds].client->thread = std::thread(runClient, &fdc[nfds]);
nfds++;
} while (newsock != -1);
} else {
/*
int rc = recv(fds[i].fd, inbuf, sizeof(inbuf), 0);
if (rc < 0) {
if (errno != EWOULDBLOCK) {
perror("recv() failed");
fdc[i].close = true;
}
continue;
}
if (rc == 0 || fdc[i].client == nullptr) {
printf("[d] (%i) closed\n", fds[i].fd);
fdc[i].close = true;
}
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] (%i) >> '%s' '%s'\n", fds[i].fd, cmd.c_str(), args.c_str());
if (fdc[i].client->receive(cmd, args) < 0) fdc[i].close = true;
inbuf[0] = '\0';
*/
if (fdc[i].close) {
conn_close:
printf("[d] C(%i) Deleting client...\n", fds[i].fd);
close(fds[i].fd);
fds[i].fd = -1;
if (fdc[i].thread.joinable()) fdc[i].thread.detach();
if (fdc[i].client->thread.joinable())
fdc[i].client->thread.detach();
fdc[i].client = nullptr;
fdc[i].close = false;
compress_array = true;
@@ -217,18 +183,19 @@ int main(int argc , char *argv[]) {
if (compress_array) {
compress_array = false;
printf("[d] Compressing...\n");
for (int i = 0; i < nfds; i++) {
if (fds[i].fd == -1) {
for(int j = i; j < nfds; j++) {
printf("[d] Compressing: id %i to fd %i\n", j, fds[j+1].fd);
fds[j].fd = fds[j+1].fd;
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;
fdc[j].client = fdc[j+1].client;
}
}
i--;
nfds--;
}
}
printf("[d] Compressing complete!\n");
}
}
return 0;