From 8922e82aaee6d826205af8ddb816911810e6d7c4 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 12 Nov 2017 19:19:56 +0100 Subject: [PATCH] stuff --- ftp/includes/ftp_client.h | 3 ++- ftp/includes/ftp_server.h | 4 +--- ftp/libft/srcs/char/ft_isascii.c | 2 +- ftp/libft/srcs/net/create_client.c | 8 +++++--- ftp/libft/srcs/net/create_server.c | 8 +++++++- ftp/srcs/client/cli_put.c | 20 +++++++++++++++--- ftp/srcs/client/crlf.c | 30 ++++++++++++++++++++++++++- ftp/srcs/client/dconn.c | 11 +++++++--- ftp/srcs/server/cmd_stor.c | 5 ++++- ftp/srcs/server/cmd_user.c | 33 ++++++++++++++++++++++-------- ftp/srcs/server/crlf.c | 10 +++++---- ftp/srcs/server/server.c | 2 +- 12 files changed, 106 insertions(+), 30 deletions(-) diff --git a/ftp/includes/ftp_client.h b/ftp/includes/ftp_client.h index 871c8a37..c6da12fe 100644 --- a/ftp/includes/ftp_client.h +++ b/ftp/includes/ftp_client.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:39:39 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 19:04:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ int ftp_msg(t_ftp *ftp, char **msg); int ftp_code(t_ftp *ftp); int ftp_send(int sock, char *msg, ...); +int ftp_sendraw(int sock, char *file, off_t size); int ftp_recv(int sock, char **msg); int ftp_recvraw(int sock, char **msg); diff --git a/ftp/includes/ftp_server.h b/ftp/includes/ftp_server.h index dc615039..bbaab6c0 100644 --- a/ftp/includes/ftp_server.h +++ b/ftp/includes/ftp_server.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:40:29 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 19:02:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,6 @@ # include -# define REPOPATH "data/" - # define FTP_SERVER_USAGE "%s " # define FTP_RET(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__) diff --git a/ftp/libft/srcs/char/ft_isascii.c b/ftp/libft/srcs/char/ft_isascii.c index 52a85067..8adecb33 100644 --- a/ftp/libft/srcs/char/ft_isascii.c +++ b/ftp/libft/srcs/char/ft_isascii.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:56:28 by jhalford #+# #+# */ -/* Updated: 2016/11/03 15:35:42 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 18:12:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ftp/libft/srcs/net/create_client.c b/ftp/libft/srcs/net/create_client.c index 2d24fd20..5adfa143 100644 --- a/ftp/libft/srcs/net/create_client.c +++ b/ftp/libft/srcs/net/create_client.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 17:59:28 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:15:13 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 17:57:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,8 +35,10 @@ int create_tcpclient(char *host, char *port) { struct addrinfo *ai; int sock; + struct addrinfo *first; - ai = resolve_host(host, port); + first = resolve_host(host, port); + ai = first; while (ai) { if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) @@ -48,7 +50,7 @@ int create_tcpclient(char *host, char *port) sock = -1; ai = ai->ai_next; } - freeaddrinfo(ai); + freeaddrinfo(first); return (sock); } diff --git a/ftp/libft/srcs/net/create_server.c b/ftp/libft/srcs/net/create_server.c index 4cc71497..d23017ae 100644 --- a/ftp/libft/srcs/net/create_server.c +++ b/ftp/libft/srcs/net/create_server.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 18:02:51 by jhalford #+# #+# */ -/* Updated: 2017/11/12 11:39:18 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 18:26:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,12 @@ int create_server(int port, int backlog, char *protoname) sin.sin6_addr = in6addr_any; if (bind(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0) return (-1); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, + sizeof(int)) < 0) + return (-1); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, + sizeof(int)) < 0) + return (-1); listen(sock, backlog); return (sock); } diff --git a/ftp/srcs/client/cli_put.c b/ftp/srcs/client/cli_put.c index 74a3d964..d94d7d04 100644 --- a/ftp/srcs/client/cli_put.c +++ b/ftp/srcs/client/cli_put.c @@ -6,12 +6,23 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/10 16:20:14 by jhalford #+# #+# */ -/* Updated: 2017/11/12 15:01:06 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 19:19:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ftp_client.h" +static char *fname(char *path) +{ + char *bs; + + bs = ft_strrchr(path, '/'); + if (bs) + return (bs + 1); + else + return (path); +} + int cli_put(t_ftp *ftp, char **av) { struct stat buf; @@ -25,17 +36,20 @@ int cli_put(t_ftp *ftp, char **av) console_msg(0, "no such file or directory"); return (-1); } - if (!buf.st_size || (file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE, + if (!buf.st_size || (file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (close(fd)); close(fd); if (dconn_init(ftp) < 0) return (munmap(file, buf.st_size)); - FTP_CMD(ftp, "STOR %s", av[1]); + FTP_CMD(ftp, "STOR %s", fname(av[1])); if (dconn_open(ftp) < 0) return (munmap(file, buf.st_size)); + console_msg(0, "Upload in progess, please wait... %i", buf.st_size); + /* ftp_sendraw(ftp->d_sock, file, buf.st_size); */ send(ftp->d_sock, file, buf.st_size, 0); close(ftp->d_sock); + ftp->d_sock = 0; dconn_close(ftp); munmap(file, buf.st_size); return (0); diff --git a/ftp/srcs/client/crlf.c b/ftp/srcs/client/crlf.c index 2822cd90..f7666842 100644 --- a/ftp/srcs/client/crlf.c +++ b/ftp/srcs/client/crlf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:56:15 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 19:11:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ ** stream mode with file structure --> raw data no EOF */ +#define M (1024 * 1024) + int ftp_recvraw(int sock, char **msg) { int ret; @@ -61,6 +63,32 @@ int ftp_recv(int sock, char **msg) return (0); } +int ftp_sendraw(int sock, char *file, off_t size) +{ + off_t sent; + off_t chunk; + int ret; + + sent = 0; + chunk = M / 512; + DG("size=%zu", size); + while (sent < size) + { + if (size - sent < chunk) + chunk = size - sent; + DG("sent=%zu", sent); + ret = send(sock, file, chunk, 0); + DG("ret=%i", ret); + console_msg(2, "---> sendraw error"); + file += chunk; + sent += chunk; + } + DG("sent=%zu", sent); + console_msg(1, "---> rawsend done size %zu", size); + return (0); +} + + int ftp_send(int sock, char *msg, ...) { int err; diff --git a/ftp/srcs/client/dconn.c b/ftp/srcs/client/dconn.c index 9ea92c4d..0fba7b67 100644 --- a/ftp/srcs/client/dconn.c +++ b/ftp/srcs/client/dconn.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:58:42 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 18:48:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,13 +69,18 @@ int dconn_close(t_ftp *ftp) { int code; + DG("check"); if ((code = ftp_code(ftp)) < 0) return (-1); + DG("check"); if (code == 226) { - close(ftp->d_sock); + if (ftp->d_sock != 0) + { + close(ftp->d_sock); + ftp->d_sock = 0; + } ftp->data_state = DATA_NONE; - ftp->d_sock = 0; console_msg(1, "dataconn closed"); } else diff --git a/ftp/srcs/server/cmd_stor.c b/ftp/srcs/server/cmd_stor.c index ba9429d7..b0e5c513 100644 --- a/ftp/srcs/server/cmd_stor.c +++ b/ftp/srcs/server/cmd_stor.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/02 16:01:54 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 18:35:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,10 @@ int cmd_stor(t_ftp *ftp, char **av) if (dconn_open(ftp) < 0) return (-1); if ((fd = open(av[1], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) + { + dconn_close(ftp); return (FTP_RET(ftp, "550 couldn't open/create file")); + } size = ftp_recvraw(ftp->d_sock, &msg); write(fd, msg, size); ft_strdel(&msg); diff --git a/ftp/srcs/server/cmd_user.c b/ftp/srcs/server/cmd_user.c index 9c7a304f..f7d4058e 100644 --- a/ftp/srcs/server/cmd_user.c +++ b/ftp/srcs/server/cmd_user.c @@ -6,12 +6,26 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/02 14:20:46 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 18:28:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ftp_server.h" +static int strisalnum(char *str) +{ + char *s; + + s = str; + while (*s) + { + if (!ft_isalnum(*s)) + return (0); + s++; + } + return (1); +} + int cmd_user(t_ftp *ftp, char **av) { if (ftp->log_state == LOG_YES) @@ -19,15 +33,18 @@ int cmd_user(t_ftp *ftp, char **av) FTP_RET(ftp, "230 user '%s' logged in, proceed", ftp->username); return (0); } + if (!strisalnum(av[1])) + return (FTP_RET(ftp, "530 only ASCII in username")); ft_strcpy(ftp->username, av[1]); - ft_strcpy(ftp->path, REPOPATH); + getcwd(ftp->path, 1024); + ft_strcat(ftp->path, "/"); ft_strcat(ftp->path, av[1]); - if (mkdir(ftp->path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 - && chdir(ftp->path) < 0) - return (FTP_RET(ftp, "530 mkdir/chdir")); - if (getcwd(ftp->path, 100) < 0) - return (FTP_RET(ftp, "530 getcwd")); - FTP_RET(ftp, "230 user '%s' logged in, proceed", ftp->username); + console_msg(2, "userpath=%s", ftp->path); + if ((mkdir(ftp->path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 + && errno != EEXIST) + || chdir(ftp->path) < 0) + return (FTP_RET(ftp, "530 mkdir/chdir error")); + FTP_RET(ftp, "230 user logged in, proceed", ftp->username); console_msg(1, "logon: %s@ftp://%s", ftp->username, ftp->path); ftp->log_state = LOG_YES; return (0); diff --git a/ftp/srcs/server/crlf.c b/ftp/srcs/server/crlf.c index 39608e0a..73939995 100644 --- a/ftp/srcs/server/crlf.c +++ b/ftp/srcs/server/crlf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ -/* Updated: 2017/11/12 15:13:42 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 19:02:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,16 +16,18 @@ ** stream mode with file structure --> raw data no EOF */ +#define M (1024 * 1024) + int ftp_recvraw(int sock, char **msg) { int ret; - char buf[1024]; + char buf[10 * M]; void *tmp; int size; - tmp = NULL; size = 0; - while ((ret = recv(sock, buf, 1024, 0)) > 0) + tmp = NULL; + while ((ret = recv(sock, buf, 10 * M, 0)) > 0) { buf[ret] = 0; *msg = ft_strnew(size + ret); diff --git a/ftp/srcs/server/server.c b/ftp/srcs/server/server.c index 0a14ff96..1c87fe76 100644 --- a/ftp/srcs/server/server.c +++ b/ftp/srcs/server/server.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */ -/* Updated: 2017/11/12 14:44:33 by jhalford ### ########.fr */ +/* Updated: 2017/11/12 17:59:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */