This commit is contained in:
Your Name 2024-09-15 16:45:31 +03:00
parent 42c2279ec5
commit 784164ec7b

View File

@ -20,18 +20,31 @@
#define MSG4 "No namespace available.\n"
#ifndef VERSION
#define VERSION "1.2"
#define VERSION "1.2.1"
#endif
size_t max_clients = 15;
size_t max_size = 1;
size_t buf_size = 32000;
size_t name_len = 15;
typedef struct {
/* Clients on thread */
size_t max_clients;
char *dir = ".";
char *prog_name;
char *motd;
/* Max file size */
size_t max_size;
/* Buffer size for thread */
size_t buf_size;
/* Max filename lenght */
size_t name_len;
/* Directory for files */
char *dir;
/* Message of the day */
char *motd;
} CFG;
CFG cfg = {.max_clients = 15, .max_size = 1, .buf_size = 32000, .name_len = 15, .dir = "."};
/* Server sockets */
int ifd;
size_t ifd_clients;
@ -85,14 +98,14 @@ NS_ERROR:
char *rand_string(void) {
char abc[] = "asdfghjklzxcvbnmqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
char *res = malloc(name_len + 1);
char *res = malloc(cfg.name_len + 1);
if (res == NULL)
return NULL;
for (size_t i = 0; i < name_len; i++)
for (size_t i = 0; i < cfg.name_len; i++)
res[i] = abc[rand() % (sizeof(abc) - 1)];
res[name_len] = '\0';
res[cfg.name_len] = '\0';
return res;
}
@ -121,7 +134,7 @@ int send_str(int fd, char *str, ssize_t size, int flag) {
else
ret = write(fd, str, size);
if (ret < 0 && (errno != EAGAIN || errno != EINTR))
if (ret < 0 && !(errno == EAGAIN || errno == EINTR))
return 1;
else if (ret == 0)
@ -135,7 +148,6 @@ int send_str(int fd, char *str, ssize_t size, int flag) {
}
void upload(int cfd, pid_t par) {
int ret = -1;
int ffd = -1;
char *buf = NULL;
@ -157,21 +169,21 @@ void upload(int cfd, pid_t par) {
goto UPL_ERROR;
/* Sending */
buf = malloc(buf_size + 1);
buf = malloc(cfg.buf_size + 1);
if (buf == NULL)
goto UPL_ERROR;
size_t tbytes = 0;
while (1) {
ssize_t rbytes = recv(cfd, buf, buf_size, 0);
if (rbytes < 0 && (errno != EAGAIN || errno != EINTR))
ssize_t rbytes = recv(cfd, buf, cfg.buf_size, 0);
if (rbytes < 0 && !(errno == EAGAIN || errno == EINTR))
goto UPL_ERROR;
else if (rbytes == 0)
break;
tbytes += (size_t)rbytes;
if (tbytes / 1024 / 1024 >= max_size) {
if (tbytes / 1024 / 1024 >= cfg.max_size) {
send_str(cfd, MSG2, sizeof(MSG2), WRITE);
break;
}
@ -180,8 +192,6 @@ void upload(int cfd, pid_t par) {
goto UPL_ERROR;
}
ret = 0;
/* Clear */
UPL_ERROR:
if (buf)
@ -190,9 +200,6 @@ UPL_ERROR:
if (ffd != -1)
close(ffd);
if (ret == -1)
fprintf(stderr, "%s: input thread: %s: %s\n", prog_name, (filename) ? filename : "NULL", strerror(errno));
if (filename)
free(filename);
@ -202,25 +209,20 @@ UPL_ERROR:
}
void load(int cfd, pid_t par) {
int ret = -1;
int ffd = -1;
char *buf = NULL;
/* For INFO cmd */
name_len += 4;
cfg.name_len += 4;
/* Get filename */
char *filename = malloc(name_len + 1);
char *filename = malloc(cfg.name_len + 1);
if (filename == NULL)
goto LD_ERROR;
ssize_t rbytes = recv(cfd, filename, name_len, 0);
if (rbytes <= 0) {
if (rbytes == 0)
ret = 0;
ssize_t rbytes = recv(cfd, filename, cfg.name_len, 0);
if (rbytes <= 0)
goto LD_ERROR;
}
filename[rbytes] = '\0';
char *ptr = strchr(filename, '\n');
@ -233,12 +235,11 @@ void load(int cfd, pid_t par) {
/* INFO */
if (!strcmp(filename, "INFO")) {
char msg[336];
int ret2 = snprintf(msg, sizeof(msg), "Max file size: %zuMB\nMax clients in thread: %zu\nBuffer size: %zu\nMOTD: %s\n", max_size, max_clients, buf_size, (motd) ? motd : "");
int ret = snprintf(msg, sizeof(msg), "Max file size: %zuMB\nMax clients in thread: %zu\nBuffer size: %zu\nMOTD: %s\n", cfg.max_size, cfg.max_clients, cfg.buf_size, (cfg.motd) ? cfg.motd : "");
if (send_str(cfd, msg, (ssize_t)ret2, SEND))
if (send_str(cfd, msg, (ssize_t)ret, SEND))
goto LD_ERROR;
ret = 0;
goto LD_ERROR;
}
@ -250,13 +251,13 @@ void load(int cfd, pid_t par) {
}
/* Sending */
buf = malloc(buf_size + 1);
buf = malloc(cfg.buf_size + 1);
if (buf == NULL)
goto LD_ERROR;
while (1) {
rbytes = read(ffd, buf, buf_size);
if (rbytes < 0 && (errno != EAGAIN || errno != EINTR))
rbytes = read(ffd, buf, cfg.buf_size);
if (rbytes < 0 && !(errno == EAGAIN || errno == EINTR))
goto LD_ERROR;
else if (rbytes == 0)
@ -266,8 +267,6 @@ void load(int cfd, pid_t par) {
goto LD_ERROR;
}
ret = 0;
/* Clear */
LD_ERROR:
if (buf)
@ -276,9 +275,6 @@ LD_ERROR:
if (ffd != -1)
close(ffd);
if (ret == -1)
fprintf(stderr, "%s: output thread: %s: %s\n", prog_name, (filename) ? filename : "NULL", strerror(errno));
if (filename)
free(filename);
@ -318,7 +314,7 @@ void Proc(int fd, int flag) {
if (cfd < 0)
continue;
if (flag == INPUT && ifd_clients < max_clients) {
if (flag == INPUT && ifd_clients < cfg.max_clients) {
ifd_clients++;
if (fork() == 0) {
@ -327,7 +323,7 @@ void Proc(int fd, int flag) {
}
}
else if (flag == OUTPUT && ofd_clients < max_clients) {
else if (flag == OUTPUT && ofd_clients < cfg.max_clients) {
ofd_clients++;
if (fork() == 0) {
@ -345,7 +341,6 @@ void Proc(int fd, int flag) {
int main(int argc, char **argv) {
srand(time(NULL));
prog_name = argv[0];
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
@ -356,9 +351,10 @@ int main(int argc, char **argv) {
int backlog = 1;
int iport = 8080;
int oport = 8081;
char daemon_flag = 0;
int opt;
while ((opt = getopt(argc, argv, "i:o:m:d:s:n:b:t:l:V")) != -1) {
while ((opt = getopt(argc, argv, "i:o:m:d:s:n:b:t:l:DV")) != -1) {
switch (opt) {
case 'i':
iport = atoi(optarg);
@ -369,62 +365,72 @@ int main(int argc, char **argv) {
break;
case 'm':
sscanf(optarg, "%zu", &max_clients);
sscanf(optarg, "%zu", &cfg.max_clients);
break;
case 'd':
dir = optarg;
cfg.dir = optarg;
break;
case 's':
sscanf(optarg, "%zu", &max_size);
sscanf(optarg, "%zu", &cfg.max_size);
break;
case 'n':
sscanf(optarg, "%zu", &name_len);
sscanf(optarg, "%zu", &cfg.name_len);
break;
case 'b':
sscanf(optarg, "%zu", &buf_size);
sscanf(optarg, "%zu", &cfg.buf_size);
break;
case 't':
if (strlen(optarg) > 256) {
fprintf(stderr, "%s: MOTD very long\n", prog_name);
fprintf(stderr, "%s: MOTD very long\n", argv[0]);
return 1;
}
motd = optarg;
cfg.motd = optarg;
break;
case 'l':
backlog = atoi(optarg);
break;
case 'D':
daemon_flag = 1;
break;
case 'V':
puts("Version: " VERSION "\nWritten under WTFPL License.");
return 0;
default:
printf("%s [iomdsnbtlV] - Simple file sharing server\n\t-i NUM Input port\tdefault: %d\n\t-o NUM Output port\tdefault: %d\n\t-m NUM Max clients\tdefault: %zu\n\t-d STR Working dir\tdefault: %s\n\t-s NUM Max file size\tdefault: %zuMB\n\t-n NUM Filename length\tdefault: %zu\n\t-b NUM Buffer size\tdefault: %zuB\n\t-t STR Set MOTD\n\t-l listen() backlog\tdefault: %d\n\t-V Print version and license\n", prog_name, iport, oport, max_clients, dir, max_size, name_len, buf_size, backlog);
printf("%s [iomdsnbtlDV] - Simple file sharing server\n\t-i NUM Input port\tdefault: %d\n\t-o NUM Output port\tdefault: %d\n\t-m NUM Max clients\tdefault: %zu\n\t-d STR Working dir\tdefault: %s\n\t-s NUM Max file size\tdefault: %zuMB\n\t-n NUM Filename length\tdefault: %zu\n\t-b NUM Buffer size\tdefault: %zuB\n\t-t STR Set MOTD\n\t-l listen() backlog\tdefault: %d\n\t-D Run as daemon\n\t-V Print version and license\n", argv[0], iport, oport, cfg.max_clients, cfg.dir, cfg.max_size, cfg.name_len, cfg.buf_size, backlog);
return 0;
}
}
if (chdir(dir) < 0) {
fprintf(stderr, "%s: chdir: %s\n", prog_name, strerror(errno));
if (daemon_flag)
if (daemon(1, 1) < 0) {
fprintf(stderr, "%s: daemon: %s\n", argv[0], strerror(errno));
return 1;
}
if (chdir(cfg.dir) < 0) {
fprintf(stderr, "%s: chdir: %s\n", argv[0], strerror(errno));
return 1;
}
ifd = new_server(iport, backlog);
if (ifd == -1) {
fprintf(stderr, "%s: new_server: input socket: %s\n", prog_name, strerror(errno));
fprintf(stderr, "%s: new_server: input socket: %s\n", argv[0], strerror(errno));
return 1;
}
ofd = new_server(oport, backlog);
if (ofd == -1) {
fprintf(stderr, "%s: new_server: output socket: %s\n", prog_name, strerror(errno));
fprintf(stderr, "%s: new_server: output socket: %s\n", argv[0], strerror(errno));
return 1;
}