diff --git a/ftp/Makefile b/ftp/Makefile index 4702696a..1e0d3339 100644 --- a/ftp/Makefile +++ b/ftp/Makefile @@ -34,21 +34,21 @@ SERVER_OBJ = $(OBJ_DIR)server.o CLIENT_OBJ = $(OBJ_DIR)client.o $(OBJ_DIR)cli_do_help.o SRC_BASE = \ -client.c\ -cli_do_sh.c\ cli_do_cd.c\ -cli_do_get.c\ -cli_do_put.c\ -cli_do_help.c\ cli_do_debug.c\ +cli_do_get.c\ +cli_do_help.c\ cli_do_local.c\ -read_req.c\ +cli_do_put.c\ +cli_do_sh.c\ +client.c\ console_msg.c\ +read_req.c\ req_init.c\ +serv_do_cd.c\ serv_do_get.c\ serv_do_put.c\ serv_do_sh.c\ -serv_do_cd.c\ server.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/ftp/README.md b/ftp/README.md index af99ef06..9ff93604 100644 --- a/ftp/README.md +++ b/ftp/README.md @@ -1,8 +1,2 @@ # ftp -:open_file_folder: ftp server and client - -:warning: This client/server doesn't conform to any RFC. please email me a tutorial if you have one because that RFC pdf is chinese to me. - -# todo - -conform to this https://www.rfc-editor.org/rfc/pdfrfc/rfc959.txt.pdf +:open_file_folder: ftp-like server and client diff --git a/ftp/libft b/ftp/libft index dd612f58..d57369fd 160000 --- a/ftp/libft +++ b/ftp/libft @@ -1 +1 @@ -Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef +Subproject commit d57369fd0a0bf6d5c2a78bad1961e4cd56e1c9e4 diff --git a/ftp/srcs/cli_do_cd.c b/ftp/srcs/cli_do_cd.c index 43e954e4..266920bc 100644 --- a/ftp/srcs/cli_do_cd.c +++ b/ftp/srcs/cli_do_cd.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:02:04 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:02:04 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int cli_do_cd(int sock, char **av) diff --git a/ftp/srcs/cli_do_debug.c b/ftp/srcs/cli_do_debug.c index 2bc66692..fd42a8a0 100644 --- a/ftp/srcs/cli_do_debug.c +++ b/ftp/srcs/cli_do_debug.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_debug.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:02:43 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:02:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int g_debug; diff --git a/ftp/srcs/cli_do_get.c b/ftp/srcs/cli_do_get.c index c5557226..a8126517 100644 --- a/ftp/srcs/cli_do_get.c +++ b/ftp/srcs/cli_do_get.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:02:48 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:03:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int cli_do_get(int sock, char **av) @@ -10,22 +22,17 @@ int cli_do_get(int sock, char **av) if (req_init(sock, REQUEST_FILE)) return (1); write(sock, av[1], ft_strlen(av[1])); - rep = read_req(sock); if (rep != FILENAME_OK) return (1); - rep = htons(TRANSFER_START); if ((fd = open_new(av[1], O_WRONLY | O_TRUNC | O_APPEND)) < 0) rep = htons(ABORT); write(sock, (char*)&rep, sizeof(rep)); - if (rep != htons(TRANSFER_START)) return (1); - console_msg(1, "TRANSFER_START"); net_get_large(sock, fd); console_msg(1, "TRANSFER DONE"); - return (0); } diff --git a/ftp/srcs/cli_do_help.c b/ftp/srcs/cli_do_help.c index b920cd74..bfdc26b9 100644 --- a/ftp/srcs/cli_do_help.c +++ b/ftp/srcs/cli_do_help.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_help.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:02:38 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:02:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int cli_do_help(int sock, char **av) diff --git a/ftp/srcs/cli_do_local.c b/ftp/srcs/cli_do_local.c index 8de8dbef..7d8cae2a 100644 --- a/ftp/srcs/cli_do_local.c +++ b/ftp/srcs/cli_do_local.c @@ -1,6 +1,16 @@ -#include "ft_p.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_local.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:02:12 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:02:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ -char **environ; +#include "ft_p.h" int cli_do_local(int sock, char **av) { diff --git a/ftp/srcs/cli_do_put.c b/ftp/srcs/cli_do_put.c index 65f2ac3e..279fc62e 100644 --- a/ftp/srcs/cli_do_put.c +++ b/ftp/srcs/cli_do_put.c @@ -1,11 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:04:23 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:05:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int cli_do_put(int sock, char **av) { - int req; - int fd; + int req; + int fd; struct stat stat; - char *file; + char *file; if (!av[1] || av[2]) return (console_msg(-1, "usage: put ")); @@ -16,16 +28,12 @@ int cli_do_put(int sock, char **av) else if ((file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (console_msg(-1, "mmap error")); - if (req_init(sock, REQUEST_PUT)) return (1); - write(sock, av[1], ft_strlen(av[1])); - req = read_req(sock); if (req != TRANSFER_START) return (1); - net_send_large(sock, file, stat.st_size); return (0); } diff --git a/ftp/srcs/cli_do_sh.c b/ftp/srcs/cli_do_sh.c index 10b72d89..bc955895 100644 --- a/ftp/srcs/cli_do_sh.c +++ b/ftp/srcs/cli_do_sh.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cli_do_sh.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:05:34 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:06:32 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int cli_do_sh(int sock, char **av) @@ -7,13 +19,10 @@ int cli_do_sh(int sock, char **av) int ret; command = ft_strsepjoin(av, ' '); - if (req_init(sock, REQUEST_SH)) return (1); - console_msg(1, "sending '%s' to server", command); write(sock, command, ft_strlen(command)); - while ((ret = read(sock, &buf, MAXLINE))) { buf[ret] = 0; diff --git a/ftp/srcs/cli_output.c b/ftp/srcs/cli_output.c deleted file mode 100644 index 2d780ad4..00000000 --- a/ftp/srcs/cli_output.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "ft_p.h" - -int read_req() -{ - int req; - - if (read(sock, (char*)&req, sizeof(req)) < 0) - return(1); - console_msg(0, "%i RECEIVED", req); - return (req); -} diff --git a/ftp/srcs/client.c b/ftp/srcs/client.c index ae8f9230..fbe302f9 100644 --- a/ftp/srcs/client.c +++ b/ftp/srcs/client.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */ -/* Updated: 2017/04/03 18:43:40 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 12:25:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ t_cmd_map *get_cmd(char *cmd) return (NULL); } -int do_client(int sock) +int do_client(int sock) { char *input; t_cmd_map *cmd; @@ -71,9 +71,9 @@ int do_client(int sock) } } -int main(int ac, char **av) +int main(int ac, char **av) { - int port; + int port; int sock; if (ac != 3) diff --git a/ftp/srcs/console_msg.c b/ftp/srcs/console_msg.c index 423d7343..255b60c2 100644 --- a/ftp/srcs/console_msg.c +++ b/ftp/srcs/console_msg.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* console_msg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:05:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int g_debug; diff --git a/ftp/srcs/read_req.c b/ftp/srcs/read_req.c index 971450ae..36f57cc6 100644 --- a/ftp/srcs/read_req.c +++ b/ftp/srcs/read_req.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_req.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:04:11 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:06:53 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int read_req(int sock) @@ -5,7 +17,7 @@ int read_req(int sock) int req; if (read(sock, (char*)&req, sizeof(req)) < 0) - return(0); + return (0); req = ntohs(req); console_msg(0, "%i RECEIVED", req); return (req); diff --git a/ftp/srcs/req_init.c b/ftp/srcs/req_init.c index e2a62db6..5977846c 100644 --- a/ftp/srcs/req_init.c +++ b/ftp/srcs/req_init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* req_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:07:00 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:07:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int req_init(int sock, int req) diff --git a/ftp/srcs/serv_do_cd.c b/ftp/srcs/serv_do_cd.c index 39f840fc..c75eef15 100644 --- a/ftp/srcs/serv_do_cd.c +++ b/ftp/srcs/serv_do_cd.c @@ -1,10 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* serv_do_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:08:05 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:08:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" char g_rootdir[PATH_MAX]; int serv_do_cd(int sock) { - char path[MAXLINE]; + char path[MAXLINE]; char *abspath; int ret; char *ok; @@ -12,23 +24,17 @@ int serv_do_cd(int sock) if ((ret = read(sock, path, MAXLINE)) < 0) return (CMD_FAIL); path[ret] = 0; - DG("received 'cd %s' command", path); - ok = NULL; if (!(abspath = realpath(path, ok))) return (CD_DIR_NOT_FOUND); - ft_strcpy(path, abspath); if (!*abspath) return (CD_DIR_NOT_FOUND); - ft_strdel(&abspath); - DG("absolute path is '%s'", path); DG("root dir is '%s'", g_rootdir); - if (!ft_strstr(path, g_rootdir)) return (CD_RESTRICTED_DIR); - return(chdir(path) ? CMD_FAIL : CMD_SUCCESS); + return (chdir(path) ? CMD_FAIL : CMD_SUCCESS); } diff --git a/ftp/srcs/serv_do_get.c b/ftp/srcs/serv_do_get.c index 7d589691..f3d5f071 100644 --- a/ftp/srcs/serv_do_get.c +++ b/ftp/srcs/serv_do_get.c @@ -1,39 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* serv_do_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:09:37 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:25:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" -int serv_do_get(int sock) +int serv_get_fname(int sock, char **file, struct stat *stat) { - char fname[MAXLINE]; - int req; int rep; int fd; - struct stat stat; - char *file; + char fname[MAXLINE]; ft_bzero(&fname, MAXLINE); rep = htons(FILENAME_OK); - file = NULL; if (read(sock, fname, MAXLINE) < 0) rep = htons(ERR_READ); else if ((fd = open(fname, O_RDONLY)) < 0) rep = htons(NO_SUCH_FILE); - else if ((fstat(fd, &stat)) < 0) + else if ((fstat(fd, stat)) < 0) rep = htons(ERR_STAT); - else if ((file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) + else if ((*file = mmap(NULL, stat->st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) rep = htons(ERR_MMAP); write(sock, (char*)&rep, sizeof(rep)); + return (rep); +} - if (ntohs(rep) != FILENAME_OK) +int serv_do_get(int sock) +{ + int req; + struct stat stat; + char *file; + + file = NULL; + if (serv_get_fname(sock, &file, &stat)) return (CMD_FAIL); - DG("GOING TO READ_REQ"); req = read_req(sock); if (req != TRANSFER_START) return (CMD_FAIL); - net_send_large(sock, file, stat.st_size); - - if (file && munmap(file, stat.st_size) < 0) - rep = htons(ERR_MMAP); + if (file) + munmap(file, stat.st_size); return (CMD_SUCCESS); } diff --git a/ftp/srcs/serv_do_put.c b/ftp/srcs/serv_do_put.c index ae941221..13d692e4 100644 --- a/ftp/srcs/serv_do_put.c +++ b/ftp/srcs/serv_do_put.c @@ -1,23 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* serv_do_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:07:11 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:07:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int serv_do_put(int sock) { - char fname[MAXLINE]; + char fname[MAXLINE]; int rep; int fd; ft_bzero(&fname, MAXLINE); - rep = htons(TRANSFER_START); if (read(sock, fname, MAXLINE) < 0) rep = htons(ABORT); else if ((fd = open_new(fname, O_WRONLY | O_TRUNC | O_APPEND)) < 0) rep = htons(ABORT); - write(sock, (char*)&rep, sizeof(rep)); if (rep != htons(TRANSFER_START)) return (CMD_FAIL); - net_get_large(sock, fd); return (CMD_SUCCESS); } diff --git a/ftp/srcs/serv_do_sh.c b/ftp/srcs/serv_do_sh.c index c7dfafb9..21932921 100644 --- a/ftp/srcs/serv_do_sh.c +++ b/ftp/srcs/serv_do_sh.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* serv_do_sh.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 12:08:49 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:10:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_p.h" int serv_do_sh(int sock) { - char command[MAXLINE]; + char command[MAXLINE]; int ret; if ((ret = read(sock, command, MAXLINE)) < 0) @@ -20,4 +32,3 @@ int serv_do_sh(int sock) DG("finished waiting for command"); return (CMD_SUCCESS); } - diff --git a/ftp/srcs/server.c b/ftp/srcs/server.c index f253758a..520d4267 100644 --- a/ftp/srcs/server.c +++ b/ftp/srcs/server.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */ -/* Updated: 2017/04/03 16:42:49 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 12:25:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,8 +91,8 @@ int ftp_daemon(int sock) int main(int ac, char **av) { - int port; - int sock; + int port; + int sock; g_av = av; getcwd(g_rootdir, PATH_MAX);