started work on client, doesnt compile but I'm going to grab a 🍺
This commit is contained in:
parent
2213d5fcb9
commit
f24ae0d3a6
14 changed files with 191 additions and 62 deletions
|
|
@ -6,7 +6,7 @@
|
|||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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 = \
|
||||
client/cli_ls.c\
|
||||
client/client.c\
|
||||
console_msg.c\
|
||||
crlf.c\
|
||||
server/cmd_cwd.c\
|
||||
server/cmd_list.c\
|
||||
server/cmd_pasv.c\
|
||||
|
|
@ -45,6 +43,8 @@ server/cmd_retr.c\
|
|||
server/cmd_stor.c\
|
||||
server/cmd_type.c\
|
||||
server/cmd_user.c\
|
||||
server/console_msg.c\
|
||||
server/crlf.c\
|
||||
server/dconn.c\
|
||||
server/server.c
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,15 +6,29 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
int cli_ls(int sock, char **av)
|
||||
int cli_ls(t_ftp *ftp, char **av)
|
||||
{
|
||||
(void)sock;
|
||||
(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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,42 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_p.h :+: :+: :+: */
|
||||
/* console_msg.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/01 19:14:28 by jhalford ### ########.fr */
|
||||
/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/08 19:58:17 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FTP_H
|
||||
# define FTP_H
|
||||
#include "ftp_client.h"
|
||||
|
||||
# define MAXLINE 256
|
||||
|
||||
enum e_ftp
|
||||
int console_msg(int level, char *str, ...)
|
||||
{
|
||||
REQUEST_FILE = 100,
|
||||
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,
|
||||
};
|
||||
va_list ap;
|
||||
|
||||
#include "ftp_server.h"
|
||||
|
||||
|
||||
#endif
|
||||
va_start(ap, str);
|
||||
if (g_debug >= level)
|
||||
{
|
||||
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
60
ftp/srcs/client/crlf.c
Normal 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
72
ftp/srcs/client/dconn.c
Normal 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); */
|
||||
/* } */
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 sock;
|
||||
int fd;
|
||||
struct stat buf;
|
||||
char *file;
|
||||
int i;
|
||||
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)
|
||||
return (ftp_ret(ftp, "550 file not found / no access"));
|
||||
if (buf.st_size)
|
||||
|
|
@ -32,11 +32,10 @@ int cmd_retr(t_ftp *ftp, char **av)
|
|||
split = ft_strsplit(file, '\n');
|
||||
i = -1;
|
||||
while (split[++i])
|
||||
ftp_send(sock, split[i]);
|
||||
ftp_send(ftp->d_sock, split[i]);
|
||||
ft_sstrfree(split);
|
||||
munmap(file, buf.st_size);
|
||||
}
|
||||
ftp_ret(ftp, "226 closing dataconn");
|
||||
close(sock);
|
||||
dconn_close(ftp);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 sock;
|
||||
int fd;
|
||||
char *msg;
|
||||
|
||||
sock = dconn_open(ftp);
|
||||
if ((fd = open(av[1], O_WRONLY | O_CREAT, 0644)) < 0)
|
||||
if (dconn_open(ftp) < 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"));
|
||||
while (1)
|
||||
{
|
||||
if (ftp_recv(sock, &msg))
|
||||
if (ftp_recv(ftp->d_sock, &msg))
|
||||
break ;
|
||||
write(fd, msg, ft_strlen(msg));
|
||||
ft_strdel(&msg);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -1,23 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* read_req.c :+: :+: :+: */
|
||||
/* crlf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/10/08 12:04:11 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/08 14:54:32 by jhalford ### ########.fr */
|
||||
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
|
||||
/* Updated: 2017/11/08 19:58:30 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ftp_server.h"
|
||||
|
||||
int read_req(int sock)
|
||||
{
|
||||
(void)sock;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ftp_recv(int sock, char **msg)
|
||||
{
|
||||
int ret;
|
||||
|
|
@ -30,8 +24,8 @@ int ftp_recv(int sock, char **msg)
|
|||
buf[ret - 2] = 0;
|
||||
else
|
||||
{
|
||||
console_msg(-1, "recv'd non-crlf message!");
|
||||
return (1);
|
||||
console_msg(1, "recv'd non-crlf message!");
|
||||
}
|
||||
*msg = ft_strdup(buf);
|
||||
console_msg(0, "%-5i<--- %s", getpid(), buf);
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue