From 2213d5fcb93b60232d7963cd927baa68fdebc36f Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 8 Nov 2017 17:31:38 +0100 Subject: [PATCH] PASV/PORT work in harmony :thumbs_up: --- ftp/.gitignore | 2 ++ ftp/Makefile | 4 +++- ftp/includes/ftp_client.h | 17 ++++++++------- ftp/includes/ftp_server.h | 44 ++++++++++++++++++++++++-------------- ftp/srcs/crlf.c | 2 +- ftp/srcs/ftp_dataconn.c | 27 ----------------------- ftp/srcs/server/cmd_list.c | 2 +- ftp/srcs/server/cmd_pasv.c | 3 +-- ftp/srcs/server/cmd_port.c | 2 +- ftp/srcs/server/dconn.c | 4 ++-- 10 files changed, 48 insertions(+), 59 deletions(-) delete mode 100644 ftp/srcs/ftp_dataconn.c diff --git a/ftp/.gitignore b/ftp/.gitignore index f2ad8530..777e6baf 100644 --- a/ftp/.gitignore +++ b/ftp/.gitignore @@ -1,2 +1,4 @@ client server +!srcs/client +!srcs/server diff --git a/ftp/Makefile b/ftp/Makefile index 4d5e241f..57320b11 100644 --- a/ftp/Makefile +++ b/ftp/Makefile @@ -31,12 +31,13 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ +client/cli_ls.c\ client/client.c\ console_msg.c\ crlf.c\ -ftp_dataconn.c\ server/cmd_cwd.c\ server/cmd_list.c\ +server/cmd_pasv.c\ server/cmd_port.c\ server/cmd_pwd.c\ server/cmd_quit.c\ @@ -44,6 +45,7 @@ server/cmd_retr.c\ server/cmd_stor.c\ server/cmd_type.c\ server/cmd_user.c\ +server/dconn.c\ server/server.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/ftp/includes/ftp_client.h b/ftp/includes/ftp_client.h index b1a69787..8642a6e0 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/01 17:12:03 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 13:48:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,13 +37,14 @@ extern t_cmd_map g_cli_cmd[]; t_cmd_map *get_cmd(char *cmd); -int cli_do_sh(int sock, char **av); -int cli_do_get(int sock, char **av); -int cli_do_put(int sock, char **av); -int cli_do_cd(int sock, char **av); +int cli_ls(int sock, char **av); +int cli_sh(int sock, char **av); +int cli_get(int sock, char **av); +int cli_put(int sock, char **av); +int cli_cd(int sock, char **av); -int cli_do_help(int sock, char **av); -int cli_do_debug(int sock, char **av); -int cli_do_local(int sock, char **av); +int cli_help(int sock, char **av); +int cli_debug(int sock, char **av); +int cli_local(int sock, char **av); #endif diff --git a/ftp/includes/ftp_server.h b/ftp/includes/ftp_server.h index 7c9942af..5584bf17 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/02 16:33:37 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 17:03:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,26 +31,37 @@ typedef struct s_ftp t_ftp; typedef struct s_ftp_cmd t_ftp_cmd; -struct s_ftp +enum e_dstate +{ + DATA_PASV, + DATA_ACTV, +}; + +enum e_lstate +{ + LOG_NONE, + LOG_YES, +}; + +struct s_ftp { int cmd_sock; - struct sockaddr_in data_sin; + enum e_lstate log_state; char username[100]; char path[100]; - int state; + enum e_dstate data_state; + union { + struct sockaddr_in sin; + int sock; + } dconn; + int d_sock; }; -enum e_state +struct s_ftp_cmd { - IDLE, - LOGGED_IN, -}; - -struct s_ftp_cmd -{ - char *name; - int (*f)(); - int statelock; + char *name; + int (*f)(); + enum e_lstate statelock; }; @@ -60,16 +71,17 @@ extern char g_rootdir[PATH_MAX]; int ftp_send(int sock, char *msg, ...); int ftp_recv(int sock, char **msg); -int ftp_dataconn(t_ftp *ftp); +int dconn_open(t_ftp *ftp); +int dconn_close(t_ftp *ftp); int console_msg(int level, char *str, ...); -int ftp_login(t_ftp *ftp); int cmd_user(t_ftp *ftp, char **av); int cmd_quit(t_ftp *ftp, char **av); int cmd_retr(t_ftp *ftp, char **av); int cmd_stor(t_ftp *ftp, char **av); int cmd_cwd(t_ftp *ftp, char **av); int cmd_pwd(t_ftp *ftp, char **av); +int cmd_pasv(t_ftp *ftp, char **av); int cmd_port(t_ftp *ftp, char **av); int cmd_type(t_ftp *ftp, char **av); int cmd_list(t_ftp *ftp, char **av); diff --git a/ftp/srcs/crlf.c b/ftp/srcs/crlf.c index ce15bad6..581c4924 100644 --- a/ftp/srcs/crlf.c +++ b/ftp/srcs/crlf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/08 12:04:11 by jhalford #+# #+# */ -/* Updated: 2017/11/02 17:09:42 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 14:54:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ftp/srcs/ftp_dataconn.c b/ftp/srcs/ftp_dataconn.c deleted file mode 100644 index 57771b4a..00000000 --- a/ftp/srcs/ftp_dataconn.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ftp_dataconn.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/11/02 13:30:18 by jhalford #+# #+# */ -/* Updated: 2017/11/02 15:40:05 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ftp_server.h" - -int ftp_dataconn(t_ftp *ftp) -{ - char buf[INET_ADDRSTRLEN]; - int sock; - - sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - ftp_ret(ftp, "150 about to open data conn"); - if (connect(sock, (struct sockaddr*)&ftp->data_sin, - sizeof(ftp->data_sin)) < 0) - return(ftp_ret(ftp, "425 can't open data conn")); - console_msg(1, "dataconn @ %s:%i", inet_ntop(AF_INET, &ftp->data_sin.sin_addr, buf, sizeof(struct sockaddr_in)), ntohs(ftp->data_sin.sin_port)); - return (sock); -} diff --git a/ftp/srcs/server/cmd_list.c b/ftp/srcs/server/cmd_list.c index bc09a760..5007eb2f 100644 --- a/ftp/srcs/server/cmd_list.c +++ b/ftp/srcs/server/cmd_list.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 19:22:12 by jhalford #+# #+# */ -/* Updated: 2017/11/08 17:19:24 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 17:30:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ftp/srcs/server/cmd_pasv.c b/ftp/srcs/server/cmd_pasv.c index e99c4aa5..ad3b9091 100644 --- a/ftp/srcs/server/cmd_pasv.c +++ b/ftp/srcs/server/cmd_pasv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/08 13:54:57 by jhalford #+# #+# */ -/* Updated: 2017/11/08 17:15:09 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 17:31:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,6 @@ int cmd_pasv(t_ftp *ftp, char **av) struct sockaddr_in sin; (void)av; - return (ftp_ret(ftp, "502 not implemented")); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr("127.0.0.1"); port = 35000; diff --git a/ftp/srcs/server/cmd_port.c b/ftp/srcs/server/cmd_port.c index 6270d1b0..65f1f5d2 100644 --- a/ftp/srcs/server/cmd_port.c +++ b/ftp/srcs/server/cmd_port.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 18:42:50 by jhalford #+# #+# */ -/* Updated: 2017/11/08 17:17:44 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 17:30:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/ftp/srcs/server/dconn.c b/ftp/srcs/server/dconn.c index dcb1bb02..9af69e29 100644 --- a/ftp/srcs/server/dconn.c +++ b/ftp/srcs/server/dconn.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */ -/* Updated: 2017/11/08 17:22:37 by jhalford ### ########.fr */ +/* Updated: 2017/11/08 17:30:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ int dconn_open(t_ftp *ftp) int dconn_close(t_ftp *ftp) { - return (ftp_ret(ftp, "226 closing dataconn")); + ftp_ret(ftp, "226 closing dataconn"); close(ftp->d_sock); ftp->d_sock = 0; return (0);