started work on client, doesnt compile but I'm going to grab a 🍺

This commit is contained in:
Jack Halford 2017-11-08 20:12:24 +01:00
parent 2213d5fcb9
commit f24ae0d3a6
14 changed files with 191 additions and 62 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/11/02 15:58:48 by jhalford ### ########.fr # # Updated: 2017/11/08 19:53:04 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -33,8 +33,6 @@ OBJ_DIR = objs/
SRC_BASE = \ SRC_BASE = \
client/cli_ls.c\ client/cli_ls.c\
client/client.c\ client/client.c\
console_msg.c\
crlf.c\
server/cmd_cwd.c\ server/cmd_cwd.c\
server/cmd_list.c\ server/cmd_list.c\
server/cmd_pasv.c\ server/cmd_pasv.c\
@ -45,6 +43,8 @@ server/cmd_retr.c\
server/cmd_stor.c\ server/cmd_stor.c\
server/cmd_type.c\ server/cmd_type.c\
server/cmd_user.c\ server/cmd_user.c\
server/console_msg.c\
server/crlf.c\
server/dconn.c\ server/dconn.c\
server/server.c server/server.c

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */ /* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */
/* Updated: 2017/11/08 13:48:11 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:51:20 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */ /* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:03:08 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:54:02 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,15 +6,29 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */ /* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */
/* Updated: 2017/11/08 13:50:20 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:58:47 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_client.h" #include "ftp_client.h"
int cli_ls(int sock, char **av) int cli_ls(t_ftp *ftp, char **av)
{ {
(void)sock; (void)sock;
(void)av; (void)av;
int status;
int code;
cli_send(ftp, "LIST -a");
ftp_dconn(ftp);
if ((pid = fork()) < 0)
return (-1);
if (pid == 0)
{
dup2(1, ftp->d_sock);
wait(&status);
}
code = ftp_code(ftp);
kill(pid, SIGSTOP);
return (0); return (0);
} }

View file

@ -1,42 +1,32 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_p.h :+: :+: :+: */ /* console_msg.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */ /* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
/* Updated: 2017/11/01 19:14:28 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:58:17 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FTP_H #include "ftp_client.h"
# define FTP_H
# define MAXLINE 256 int console_msg(int level, char *str, ...)
enum e_ftp
{ {
REQUEST_FILE = 100, va_list ap;
REQUEST_PUT,
REQUEST_SH,
REQUEST_CD,
CMD_NOT_SUPPORTED = 150,
CMD_SUPPORTED = 160,
FILENAME_OK = 700,
NO_SUCH_FILE,
TRANSFER_START,
CD_DIR_NOT_FOUND,
CD_RESTRICTED_DIR,
ABORT = 800,
ERR_READ,
ERR_STAT,
ERR_MMAP,
CMD_SUCCESS = 900,
CMD_FAIL,
};
#include "ftp_server.h" va_start(ap, str);
if (g_debug >= level)
{
#endif if (level == -1)
ft_printf("{red}");
if (level == 0)
ft_printf("{blu}");
else
ft_printf("{mag}");
ft_vdprintf(1, str, ap);
ft_printf("{eoc}\n");
}
return (level);
}

60
ftp/srcs/client/crlf.c Normal file
View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* crlf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:58:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int ftp_recv(int sock, char **msg)
{
int ret;
char buf[1024];
if ((ret = recv(sock, buf, 1024, 0)) < 0)
return (ret);
buf[ret] = 0;
if (buf[ret - 1] == '\n' && buf[ret - 2] == '\r')
buf[ret - 2] = 0;
else
{
console_msg(-1, "recv'd non-crlf message!");
return (1);
}
*msg = ft_strdup(buf);
console_msg(0, msg);
return (0);
}
int ftp_send(int sock, char *msg, ...)
{
int err;
char *crlf_tmp;
char *crlf_msg;
va_list ap;
va_start(ap, msg);
ft_vasprintf(&crlf_tmp, msg, ap);
crlf_msg = ft_strjoin(crlf_tmp, "\r\n");
if ((err = send(sock, crlf_msg, ft_strlen(crlf_msg), 0)) < 0)
{
return (err);
}
ft_strdel(&crlf_tmp);
ft_strdel(&crlf_msg);
return (ft_atoi(msg));
}
int ftp_code(t_ftp *ftp)
{
char *msg
ftp_recv(ftp->cmd_sock, &msg);
return(ft_atoi(msg));
}

72
ftp/srcs/client/dconn.c Normal file
View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dconn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/08 20:09:58 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
static int dconn_open_pasv(t_ftp *ftp)
{
int sock;
ftp_ret(ftp, "150 about to open data connection");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(sock, (struct sockaddr*)&ftp->dconn.sin,
sizeof(struct sockaddr_in)) < 0)
{
ftp_ret(ftp, "425 can't open data conn");
return(-1);
}
ftp->d_sock = sock;
return (0);
}
static int dconn_open_actv(t_ftp *ftp)
{
int sock;
ftp_ret(ftp, "150 about to accept");
if ((sock = accept(ftp->dconn.sock, NULL, NULL)) < 0)
{
ftp_ret(ftp, "425 can't open data conn");
return(-1);
}
ftp->d_sock = sock;
return (0);
}
int dconn_open(t_ftp *ftp)
{
int code;
code = ftp_code(ftp);
if (ftp->code == 125)
{
if (ftp->data_state == DATA_ACTV)
return (dconn_open_actv(ftp));
else if (ftp->data_state == DATA_PASV)
return (dconn_open_pasv(ftp));
}
else if (ftp->code == 150)
return (0);
else
{
console_msg(-1, "expected 125/150 code");
return (-1);
}
}
/* int dconn_close(t_ftp *ftp) */
/* { */
/* ftp_ret(ftp, "226 closing dataconn"); */
/* close(ftp->d_sock); */
/* ftp->d_sock = 0; */
/* return (0); */
/* } */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 19:22:12 by jhalford #+# #+# */ /* Created: 2017/11/01 19:22:12 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:30:24 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:39:32 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 13:54:57 by jhalford #+# #+# */ /* Created: 2017/11/08 13:54:57 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:31:19 by jhalford ### ########.fr */ /* Updated: 2017/11/08 18:31:11 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 15:32:28 by jhalford #+# #+# */ /* Created: 2017/11/02 15:32:28 by jhalford #+# #+# */
/* Updated: 2017/11/08 15:12:28 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:04:28 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,14 +14,14 @@
int cmd_retr(t_ftp *ftp, char **av) int cmd_retr(t_ftp *ftp, char **av)
{ {
int sock;
int fd; int fd;
struct stat buf; struct stat buf;
char *file; char *file;
int i; int i;
char **split; char **split;
sock = dconn_open(ftp); if (dconn_open(ftp) < 0)
return (-1);
if ((fd = open(av[1], O_RDONLY)) < 0 || fstat(fd, &buf) < 0) if ((fd = open(av[1], O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (ftp_ret(ftp, "550 file not found / no access")); return (ftp_ret(ftp, "550 file not found / no access"));
if (buf.st_size) if (buf.st_size)
@ -32,11 +32,10 @@ int cmd_retr(t_ftp *ftp, char **av)
split = ft_strsplit(file, '\n'); split = ft_strsplit(file, '\n');
i = -1; i = -1;
while (split[++i]) while (split[++i])
ftp_send(sock, split[i]); ftp_send(ftp->d_sock, split[i]);
ft_sstrfree(split); ft_sstrfree(split);
munmap(file, buf.st_size); munmap(file, buf.st_size);
} }
ftp_ret(ftp, "226 closing dataconn"); dconn_close(ftp);
close(sock);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 16:01:54 by jhalford #+# #+# */ /* Created: 2017/11/02 16:01:54 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:09:21 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:15:07 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,16 +14,16 @@
int cmd_stor(t_ftp *ftp, char **av) int cmd_stor(t_ftp *ftp, char **av)
{ {
int sock;
int fd; int fd;
char *msg; char *msg;
sock = dconn_open(ftp); if (dconn_open(ftp) < 0)
if ((fd = open(av[1], O_WRONLY | O_CREAT, 0644)) < 0) return (-1);
if ((fd = open(av[1], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
return (ftp_ret(ftp, "550 couldn't open/create file")); return (ftp_ret(ftp, "550 couldn't open/create file"));
while (1) while (1)
{ {
if (ftp_recv(sock, &msg)) if (ftp_recv(ftp->d_sock, &msg))
break ; break ;
write(fd, msg, ft_strlen(msg)); write(fd, msg, ft_strlen(msg));
ft_strdel(&msg); ft_strdel(&msg);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */ /* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
/* Updated: 2017/11/01 19:14:19 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:57:41 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,23 +1,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* read_req.c :+: :+: :+: */ /* crlf.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 12:04:11 by jhalford #+# #+# */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/08 14:54:32 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:58:30 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_server.h" #include "ftp_server.h"
int read_req(int sock)
{
(void)sock;
return (0);
}
int ftp_recv(int sock, char **msg) int ftp_recv(int sock, char **msg)
{ {
int ret; int ret;
@ -30,8 +24,8 @@ int ftp_recv(int sock, char **msg)
buf[ret - 2] = 0; buf[ret - 2] = 0;
else else
{ {
console_msg(-1, "recv'd non-crlf message!");
return (1); return (1);
console_msg(1, "recv'd non-crlf message!");
} }
*msg = ft_strdup(buf); *msg = ft_strdup(buf);
console_msg(0, "%-5i<--- %s", getpid(), buf); console_msg(0, "%-5i<--- %s", getpid(), buf);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */ /* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:30:21 by jhalford ### ########.fr */ /* Updated: 2017/11/08 19:53:09 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */