remove out of date backup files

This commit is contained in:
2023-12-10 22:03:49 -06:00
parent e106fa6f6b
commit b73dbed1ec
2 changed files with 0 additions and 400 deletions

View File

@@ -1,184 +0,0 @@
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstring>
#include <future>
#include <thread>
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <vector>
#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!
struct pollfd fds[MAXCLIENTS];
Client* fclients[MAXCLIENTS];
std::future<bool> fthreads[MAXCLIENTS];
bool run = true, compress_array = false;
bool runClient(int sock, Client* client) {
char inbuf[BUFFERSIZE];
int rc;
while (rc = recv(sock, inbuf, sizeof(inbuf), 0)) {
if (rc < 0) {
if (errno != EWOULDBLOCK) {
perror("recv() failed");
break;
}
continue;
}
if (rc == 0 || client == nullptr) {
printf("[d] (%i) closed\n", 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] (%i) >> '%s' '%s'\n", client->control_sock, cmd.c_str(), args.c_str());
if (client->receive(cmd, args) < 0) break;
inbuf[0] = '\0';
}
return true;
}
int main(int argc , char *argv[]) {
int opt = 1,
master_socket = -1,
newsock = -1,
nfds = 1,
current_size = 0;
char inbuf[BUFFERSIZE];
struct sockaddr_in ctrl_address;
if ((master_socket = socket(AF_INET , SOCK_STREAM , 0)) < 0) {
perror("socket() failed");
exit(-1);
}
if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
perror("setsockopt() failed");
close(master_socket);
exit(-1);
}
if (ioctl(master_socket, FIONBIO, (char *)&opt) < 0) {
perror("ioctl() failed");
close(master_socket);
exit(-1);
}
ctrl_address.sin_family = AF_INET;
ctrl_address.sin_addr.s_addr = INADDR_ANY;
ctrl_address.sin_port = htons(CONTROL_PORT);
if (bind(master_socket, (struct sockaddr *)&ctrl_address, sizeof(ctrl_address))<0 < 0) {
perror("bind() failed");
close(master_socket);
exit(-1);
}
if (listen(master_socket, 3) < 0) {
perror("listen() failed");
close(master_socket);
exit(-1);
}
memset(fds, 0 , sizeof(fds));
fds[0].fd = master_socket;
fds[0].events = POLLIN;
while (run) {
int pc = poll(fds, nfds, -1);
if (pc < 0) {
perror("poll() failed");
break;
}
if (pc == 0) {
printf("poll() timed out. End program.\n");
break;
}
current_size = nfds;
for (int i = 0; i < current_size; i++) {
inbuf[0] = '\0';
/*
if(fds[i].revents == 0)
continue;
if(fds[i].revents != POLLIN) {
printf("[!] Error! revents = %d\n", fds[i].revents);
run = false;
break;
}*/
if (fds[i].fd == master_socket) {
do {
newsock = accept(master_socket, NULL, NULL);
if (newsock < 0) {
if (errno != EWOULDBLOCK) {
perror("accept() failed");
run = false;
}
break;
}
printf("[d] (%i) initialized\n", newsock);
fds[nfds].fd = newsock;
fds[nfds].events = POLLIN;
Client* newClient = new Client(newsock);
fclients[nfds] = newClient;
fthreads[nfds] = std::async(runClient, newsock, newClient);
nfds++;
} while (newsock != -1);
} else {
if (fthreads[i].wait_for(0s) == std::future_status::ready) {
close(fds[i].fd);
fds[i].fd = -1;
compress_array = true;
fclients[i] = nullptr;
}
}
}
if (compress_array) {
compress_array = false;
for (int i = 0; i < nfds; i++) {
if (fds[i].fd == -1) {
for(int j = i; j < nfds; j++) {
fds[j].fd = fds[j+1].fd;
}
i--;
nfds--;
}
}
}
}
return 0;
}

View File

@@ -1,216 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include "tunnel.h"
int main(int argc , char *argv[]) {
int opt = 1;
int master_socket,
addrlen,
new_socket,
control_socket[MAXCLIENTS],
data_socket[MAXCLIENTS],
activity,
cs;
char* client_pwd[MAXCLIENTS];
char* client_user[MAXCLIENTS];
int max_sd;
struct sockaddr_in ctrl_address;
struct sockaddr_in data_address;
char send_buffer[BUFFERSIZE],
receive_buffer[BUFFERSIZE];
fd_set readfds;
for (int i = 0; i < MAXCLIENTS; i++) {
control_socket[i] = 0;
}
if((master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if(setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
ctrl_address.sin_family = AF_INET;
ctrl_address.sin_addr.s_addr = INADDR_ANY;
ctrl_address.sin_port = htons(CONTROL_PORT);
if (bind(master_socket, (struct sockaddr *)&ctrl_address, sizeof(ctrl_address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d\n", CONTROL_PORT);
if (listen(master_socket, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
//accept the incoming connection
addrlen = sizeof(ctrl_address);
puts("Waiting for connections ...");
while(1) {
FD_ZERO(&readfds);
FD_SET(master_socket, &readfds);
max_sd = master_socket;
send_buffer[0] = '\0';
receive_buffer[0] = '\0';
//add child sockets to set
for (int i = 0; i < MAXCLIENTS; i++) {
//socket descriptor
cs = control_socket[i];
//if valid socket descriptor then add to read list
if(cs > 0)
FD_SET(cs, &readfds);
//highest file descriptor number, need it for the select function
if(cs > max_sd)
max_sd = cs;
}
//wait for an activity on one of the sockets , timeout is NULL ,
//so wait indefinitely
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno!=EINTR)) {
printf("select error");
}
if (FD_ISSET(master_socket, &readfds)) {
if ((new_socket = accept(master_socket, (struct sockaddr *)&ctrl_address, (socklen_t*)&ctrl_address))<0) {
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("[DEBUG] New connection: socket fd %d, ip %s, port %d\n", new_socket, inet_ntoa(ctrl_address.sin_addr), ntohs(ctrl_address.sin_port));
//send new connection greeting message
sprintf(send_buffer, "220 %s %s\r\n", APPNAME, APPVER);
if(send(new_socket, send_buffer, strlen(send_buffer), 0) < 0)
perror("send");
//add new socket to array of sockets
for (int i = 0; i < MAXCLIENTS; i++) {
//if position is empty
if(control_socket[i] == 0) {
control_socket[i] = new_socket;
client_pwd[i] = "";
printf("Adding client %d to list\n", i);
break;
}
}
printf("CLIENTS [");
for (int i = 0; i < MAXCLIENTS; i++) {
if (control_socket[i] == 0)
printf(" ");
else
printf("#");
}
printf("]\r\n");
}
for (int i = 0; i < MAXCLIENTS; i++) {
cs = control_socket[i];
int bytes;
send_buffer[0] = '\0';
if (FD_ISSET(cs, &readfds)) {
for (int n = 0; n < BUFFERSIZE; n++) {
bytes = recv(cs, &receive_buffer[n], 1, 0);
if (bytes <= 0) break;
if (receive_buffer[n] == '\n' || receive_buffer[n] == '\r') {
receive_buffer[n] = '\0';
break;
}
}
if (bytes <= 0 || strlen(receive_buffer) <= 0) continue;
printf("[DEBUG] Received from client %i: '%s'\r\n", cs, receive_buffer);
if (strncmp(receive_buffer,"USER",4)==0) {
char name[32];
strncpy(name, &receive_buffer[5], strlen(receive_buffer)-5);
name[strlen(receive_buffer)-5] = '\0';
client_user[i] = name;
sprintf(send_buffer, "331 Password required\r\n");
} else if (strncmp(receive_buffer,"PASS",4)==0 || strncmp(receive_buffer,"pass",4)==0) {
sprintf(send_buffer, "230 Login successful\r\n");
} else if (strncmp(receive_buffer,"SYST",4)==0) {
sprintf(send_buffer, "215 LINUX\r\n");
} else if (strncmp(receive_buffer,"PWD",3)==0) {
sprintf(send_buffer, "257 '%s'\r\n", client_pwd[i]);
} else if (strncmp(receive_buffer,"CWD",3)==0 || strncmp(receive_buffer,"cwd",3)==0) {
char directory[BUFFERSIZE];
strncpy(directory, &receive_buffer[4], strlen(receive_buffer)-4);
directory[strlen(receive_buffer)-4] = '\0';
client_pwd[i] = (char*)directory;
sprintf(send_buffer, "%s\r\n", client_pwd[i]);
} else if (strncmp(receive_buffer,"TYPE",4)==0) {
sprintf(send_buffer, "200 OK\r\n");
} else if (strncmp(receive_buffer,"PASV",4)==0) {
sprintf(send_buffer, "502\r\n");
} else if (strncmp(receive_buffer,"PORT",4)==0) {
int ds = data_socket[i];
ds = socket(AF_INET, SOCK_STREAM, 0);
//local variables
unsigned char act_port[2];
int act_ip[4], port_dec;
char ip_decimal[40];
sscanf(receive_buffer, "PORT %d,%d,%d,%d,%d,%d",&act_ip[0],&act_ip[1],&act_ip[2],&act_ip[3],(int*)&act_port[0],(int*)&act_port[1]);
sprintf(ip_decimal, "%d.%d.%d.%d", act_ip[0], act_ip[1], act_ip[2],act_ip[3]);
printf("[DEBUG] Active IP of client %i is %s\n", cs, ip_decimal);
port_dec = act_port[0]*256+act_port[1];
printf("[DEBUG] Active port of client %i is %d\n", cs, port_dec);
data_address.sin_family = AF_INET;
data_address.sin_addr.s_addr = inet_addr(ip_decimal);
data_address.sin_port = htons(port_dec);
if (connect(ds, (struct sockaddr *)&data_address, (int) sizeof(struct sockaddr)) != 0) {
sprintf(send_buffer, "425 Unknown Error\r\n");
close(ds);
} else {
sprintf(send_buffer, "200 OK\r\n");
}
} else if (strncmp(receive_buffer,"QUIT",4)==0 || strncmp(receive_buffer,"quit",4)==0) {
sprintf(send_buffer, "221 Goodbye\r\n");
send(cs, send_buffer, strlen(send_buffer), 0);
getpeername(cs, (struct sockaddr*)&ctrl_address, (socklen_t*)&addrlen);
printf("[DEBUG] Client disconnected: ip %s, port %d\n", inet_ntoa(ctrl_address.sin_addr), ntohs(ctrl_address.sin_port));
close(cs);
control_socket[i] = 0;
continue;
} else {
sprintf(send_buffer, "500 Unrecognized command\r\n");
}
if (strlen(send_buffer)>0) {
bytes = send(cs, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) continue;
char debugSend[BUFFERSIZE];
strncpy(debugSend, send_buffer, strlen(send_buffer)-2);
debugSend[strlen(send_buffer)-2] = '\0';
printf("[DEBUG] Sent to client %i: '%s'\r\n", cs, debugSend);
}
}
}
}
return 0;
}