This commit is contained in:
Jack Halford 2017-10-08 12:26:10 +02:00
parent c3069f5743
commit a20a644697
20 changed files with 208 additions and 79 deletions

View file

@ -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))

View file

@ -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

@ -1 +1 @@
Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef
Subproject commit d57369fd0a0bf6d5c2a78bad1961e4cd56e1c9e4

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_help.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -1,6 +1,16 @@
#include "ft_p.h"
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_local.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
@ -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);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_do_sh.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* console_msg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_req.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* req_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* serv_do_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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];
@ -12,22 +24,16 @@ 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);

View file

@ -1,39 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* serv_do_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* serv_do_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
@ -7,17 +19,14 @@ int serv_do_put(int sock)
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);
}

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* serv_do_sh.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
@ -20,4 +32,3 @@ int serv_do_sh(int sock)
DG("finished waiting for command");
return (CMD_SUCCESS);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */