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" #define MSG4 "No namespace available.\n"
#ifndef VERSION #ifndef VERSION
#define VERSION "1.2" #define VERSION "1.2.1"
#endif #endif
size_t max_clients = 15; typedef struct {
size_t max_size = 1; /* Clients on thread */
size_t buf_size = 32000; size_t max_clients;
size_t name_len = 15;
char *dir = "."; /* Max file size */
char *prog_name; 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; char *motd;
} CFG;
CFG cfg = {.max_clients = 15, .max_size = 1, .buf_size = 32000, .name_len = 15, .dir = "."};
/* Server sockets */
int ifd; int ifd;
size_t ifd_clients; size_t ifd_clients;
@ -85,14 +98,14 @@ NS_ERROR:
char *rand_string(void) { char *rand_string(void) {
char abc[] = "asdfghjklzxcvbnmqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890"; char abc[] = "asdfghjklzxcvbnmqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
char *res = malloc(name_len + 1); char *res = malloc(cfg.name_len + 1);
if (res == NULL) if (res == NULL)
return 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[i] = abc[rand() % (sizeof(abc) - 1)];
res[name_len] = '\0'; res[cfg.name_len] = '\0';
return res; return res;
} }
@ -121,7 +134,7 @@ int send_str(int fd, char *str, ssize_t size, int flag) {
else else
ret = write(fd, str, size); ret = write(fd, str, size);
if (ret < 0 && (errno != EAGAIN || errno != EINTR)) if (ret < 0 && !(errno == EAGAIN || errno == EINTR))
return 1; return 1;
else if (ret == 0) 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) { void upload(int cfd, pid_t par) {
int ret = -1;
int ffd = -1; int ffd = -1;
char *buf = NULL; char *buf = NULL;
@ -157,21 +169,21 @@ void upload(int cfd, pid_t par) {
goto UPL_ERROR; goto UPL_ERROR;
/* Sending */ /* Sending */
buf = malloc(buf_size + 1); buf = malloc(cfg.buf_size + 1);
if (buf == NULL) if (buf == NULL)
goto UPL_ERROR; goto UPL_ERROR;
size_t tbytes = 0; size_t tbytes = 0;
while (1) { while (1) {
ssize_t rbytes = recv(cfd, buf, buf_size, 0); ssize_t rbytes = recv(cfd, buf, cfg.buf_size, 0);
if (rbytes < 0 && (errno != EAGAIN || errno != EINTR)) if (rbytes < 0 && !(errno == EAGAIN || errno == EINTR))
goto UPL_ERROR; goto UPL_ERROR;
else if (rbytes == 0) else if (rbytes == 0)
break; break;
tbytes += (size_t)rbytes; tbytes += (size_t)rbytes;
if (tbytes / 1024 / 1024 >= max_size) { if (tbytes / 1024 / 1024 >= cfg.max_size) {
send_str(cfd, MSG2, sizeof(MSG2), WRITE); send_str(cfd, MSG2, sizeof(MSG2), WRITE);
break; break;
} }
@ -180,8 +192,6 @@ void upload(int cfd, pid_t par) {
goto UPL_ERROR; goto UPL_ERROR;
} }
ret = 0;
/* Clear */ /* Clear */
UPL_ERROR: UPL_ERROR:
if (buf) if (buf)
@ -190,9 +200,6 @@ UPL_ERROR:
if (ffd != -1) if (ffd != -1)
close(ffd); close(ffd);
if (ret == -1)
fprintf(stderr, "%s: input thread: %s: %s\n", prog_name, (filename) ? filename : "NULL", strerror(errno));
if (filename) if (filename)
free(filename); free(filename);
@ -202,25 +209,20 @@ UPL_ERROR:
} }
void load(int cfd, pid_t par) { void load(int cfd, pid_t par) {
int ret = -1;
int ffd = -1; int ffd = -1;
char *buf = NULL; char *buf = NULL;
/* For INFO cmd */ /* For INFO cmd */
name_len += 4; cfg.name_len += 4;
/* Get filename */ /* Get filename */
char *filename = malloc(name_len + 1); char *filename = malloc(cfg.name_len + 1);
if (filename == NULL) if (filename == NULL)
goto LD_ERROR; goto LD_ERROR;
ssize_t rbytes = recv(cfd, filename, name_len, 0); ssize_t rbytes = recv(cfd, filename, cfg.name_len, 0);
if (rbytes <= 0) { if (rbytes <= 0)
if (rbytes == 0)
ret = 0;
goto LD_ERROR; goto LD_ERROR;
}
filename[rbytes] = '\0'; filename[rbytes] = '\0';
char *ptr = strchr(filename, '\n'); char *ptr = strchr(filename, '\n');
@ -233,12 +235,11 @@ void load(int cfd, pid_t par) {
/* INFO */ /* INFO */
if (!strcmp(filename, "INFO")) { if (!strcmp(filename, "INFO")) {
char msg[336]; 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; goto LD_ERROR;
ret = 0;
goto LD_ERROR; goto LD_ERROR;
} }
@ -250,13 +251,13 @@ void load(int cfd, pid_t par) {
} }
/* Sending */ /* Sending */
buf = malloc(buf_size + 1); buf = malloc(cfg.buf_size + 1);
if (buf == NULL) if (buf == NULL)
goto LD_ERROR; goto LD_ERROR;
while (1) { while (1) {
rbytes = read(ffd, buf, buf_size); rbytes = read(ffd, buf, cfg.buf_size);
if (rbytes < 0 && (errno != EAGAIN || errno != EINTR)) if (rbytes < 0 && !(errno == EAGAIN || errno == EINTR))
goto LD_ERROR; goto LD_ERROR;
else if (rbytes == 0) else if (rbytes == 0)
@ -266,8 +267,6 @@ void load(int cfd, pid_t par) {
goto LD_ERROR; goto LD_ERROR;
} }
ret = 0;
/* Clear */ /* Clear */
LD_ERROR: LD_ERROR:
if (buf) if (buf)
@ -276,9 +275,6 @@ LD_ERROR:
if (ffd != -1) if (ffd != -1)
close(ffd); close(ffd);
if (ret == -1)
fprintf(stderr, "%s: output thread: %s: %s\n", prog_name, (filename) ? filename : "NULL", strerror(errno));
if (filename) if (filename)
free(filename); free(filename);
@ -318,7 +314,7 @@ void Proc(int fd, int flag) {
if (cfd < 0) if (cfd < 0)
continue; continue;
if (flag == INPUT && ifd_clients < max_clients) { if (flag == INPUT && ifd_clients < cfg.max_clients) {
ifd_clients++; ifd_clients++;
if (fork() == 0) { 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++; ofd_clients++;
if (fork() == 0) { if (fork() == 0) {
@ -345,7 +341,6 @@ void Proc(int fd, int flag) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
srand(time(NULL)); srand(time(NULL));
prog_name = argv[0];
signal(SIGUSR1, sig_handler); signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler); signal(SIGUSR2, sig_handler);
@ -356,9 +351,10 @@ int main(int argc, char **argv) {
int backlog = 1; int backlog = 1;
int iport = 8080; int iport = 8080;
int oport = 8081; int oport = 8081;
char daemon_flag = 0;
int opt; 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) { switch (opt) {
case 'i': case 'i':
iport = atoi(optarg); iport = atoi(optarg);
@ -369,62 +365,72 @@ int main(int argc, char **argv) {
break; break;
case 'm': case 'm':
sscanf(optarg, "%zu", &max_clients); sscanf(optarg, "%zu", &cfg.max_clients);
break; break;
case 'd': case 'd':
dir = optarg; cfg.dir = optarg;
break; break;
case 's': case 's':
sscanf(optarg, "%zu", &max_size); sscanf(optarg, "%zu", &cfg.max_size);
break; break;
case 'n': case 'n':
sscanf(optarg, "%zu", &name_len); sscanf(optarg, "%zu", &cfg.name_len);
break; break;
case 'b': case 'b':
sscanf(optarg, "%zu", &buf_size); sscanf(optarg, "%zu", &cfg.buf_size);
break; break;
case 't': case 't':
if (strlen(optarg) > 256) { if (strlen(optarg) > 256) {
fprintf(stderr, "%s: MOTD very long\n", prog_name); fprintf(stderr, "%s: MOTD very long\n", argv[0]);
return 1; return 1;
} }
motd = optarg; cfg.motd = optarg;
break; break;
case 'l': case 'l':
backlog = atoi(optarg); backlog = atoi(optarg);
break; break;
case 'D':
daemon_flag = 1;
break;
case 'V': case 'V':
puts("Version: " VERSION "\nWritten under WTFPL License."); puts("Version: " VERSION "\nWritten under WTFPL License.");
return 0; return 0;
default: 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; return 0;
} }
} }
if (chdir(dir) < 0) { if (daemon_flag)
fprintf(stderr, "%s: chdir: %s\n", prog_name, strerror(errno)); 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; return 1;
} }
ifd = new_server(iport, backlog); ifd = new_server(iport, backlog);
if (ifd == -1) { 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; return 1;
} }
ofd = new_server(oport, backlog); ofd = new_server(oport, backlog);
if (ofd == -1) { 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; return 1;
} }