client on its way, data transfer doesnt work well. need to define type I/A better

This commit is contained in:
Jack Halford 2017-11-09 15:17:35 +01:00
parent f24ae0d3a6
commit bd413e411a
21 changed files with 285 additions and 80 deletions

View file

@ -31,8 +31,15 @@ INC_DIR = includes/
OBJ_DIR = objs/
SRC_BASE = \
client/cli_debug.c\
client/cli_help.c\
client/cli_ls.c\
client/cli_pasv.c\
client/cli_user.c\
client/client.c\
client/console_msg.c\
client/crlf.c\
client/dconn.c\
server/cmd_cwd.c\
server/cmd_list.c\
server/cmd_pasv.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:50:56 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:51:20 by jhalford ### ########.fr */
/* Updated: 2017/11/09 14:11:15 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,11 +20,13 @@
# include <sys/mman.h>
# include <sys/wait.h>
# include <limits.h>
# include "ftp.h"
# include <signal.h>
# define FTP_CLIENT_USAGE "%s <addr> <port>"
# define ftp_cmd(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__)
typedef struct s_cmd_map t_cmd_map;
typedef struct s_ftp t_ftp;
struct s_cmd_map
{
@ -33,18 +35,49 @@ struct s_cmd_map
char *help;
};
enum e_dstate
{
DATA_NONE,
DATA_PASV,
DATA_ACTV,
};
struct s_ftp
{
int cmd_sock;
enum e_dstate data_state;
union {
struct sockaddr_in sin;
int sock;
} dconn;
int d_sock;
};
extern t_cmd_map g_cli_cmd[];
extern int g_debug;
t_cmd_map *get_cmd(char *cmd);
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 ftp_code(t_ftp *ftp);
int ftp_send(int sock, char *msg, ...);
int ftp_recv(int sock, char **msg);
int dconn_init(t_ftp *ftp);
int dconn_open(t_ftp *ftp);
int dconn_close(t_ftp *ftp);
int console_msg(int level, char *str, ...);
int cli_help(int sock, char **av);
int cli_debug(int sock, char **av);
int cli_local(int sock, char **av);
int cli_debug(t_ftp *ftp, char **av);
int cli_user(t_ftp *ftp, char **av);
int cli_ls(t_ftp *ftp, char **av);
int cli_sh(t_ftp *ftp, char **av);
int cli_get(t_ftp *ftp, char **av);
int cli_put(t_ftp *ftp, char **av);
int cli_cd(t_ftp *ftp, char **av);
int cli_help(t_ftp *ftp, char **av);
int cli_debug(t_ftp *ftp, char **av);
int cli_local(t_ftp *ftp, char **av);
int cli_pasv(t_ftp *ftp);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 15:56:59 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:54:02 by jhalford ### ########.fr */
/* Updated: 2017/11/09 10:25:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,6 @@
# include <arpa/inet.h>
# include "ftp.h"
# define REPOPATH "data/"
# define FTP_SERVER_USAGE "%s <port>"
@ -33,6 +31,7 @@ typedef struct s_ftp_cmd t_ftp_cmd;
enum e_dstate
{
DATA_NONE,
DATA_PASV,
DATA_ACTV,
};

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 13:20:26 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:03:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_debug(t_ftp *ftp, char **av)
{
(void)ftp;
if (av[1] != NULL && av[2] != NULL)
{
console_msg(-1, "usage: debug <level>");
return (1);
}
if (av[1])
g_debug = FT_ABS(ft_atoi(av[1]));
console_msg(0, "debug_level is set to %i", g_debug);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_help.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 14:04:01 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:12:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_help(t_ftp *ftp, char **av)
{
int i;
t_cmd_map *cmd;
(void)ftp;
if (!av[1])
{
i = -1;
while (g_cli_cmd[++i].key)
console_msg(0, "%s:\t%s", g_cli_cmd[i].key, g_cli_cmd[i].help);
}
else
{
i = 0;
while (av[++i])
{
cmd = get_cmd(av[i]);
if (cmd)
console_msg(0, "%s:\t%s", cmd->key, cmd->help);
else
console_msg(0, "%s:\tinvalid command", av[i]);
}
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:58:47 by jhalford ### ########.fr */
/* Updated: 2017/11/09 15:16:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,21 +14,21 @@
int cli_ls(t_ftp *ftp, char **av)
{
(void)sock;
(void)av;
int status;
int code;
char *msg;
cli_send(ftp, "LIST -a");
ftp_dconn(ftp);
if ((pid = fork()) < 0)
(void)av;
if (dconn_init(ftp) < 0)
return (-1);
if (pid == 0)
ftp_cmd(ftp, "LIST -a");
if (dconn_open(ftp) < 0)
return (-1);
while (1)
{
dup2(1, ftp->d_sock);
wait(&status);
if (ftp_recv(ftp->d_sock, &msg))
break ;
write(1, msg, ft_strlen(msg));
ft_strdel(&msg);
}
code = ftp_code(ftp);
kill(pid, SIGSTOP);
dconn_close(ftp);
return (0);
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_pasv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 11:19:41 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:53:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_pasv(t_ftp *ftp)
{
char *msg;
char **hostport;
int code;
char buf[INET_ADDRSTRLEN];
ftp_cmd(ftp, "PASV");
ftp_recv(ftp->cmd_sock, &msg);
code = ft_atoi(msg);
if (code != 227)
{
console_msg(2, "PASV failed (%i)", code);
return (-1);
}
hostport = ft_strsplit(msg + 4, ',');
ftp->dconn.sin.sin_family = AF_INET;
ftp->dconn.sin.sin_port = htons(256 * ft_atoi(hostport[4])
+ ft_atoi(hostport[5]));
ftp->dconn.sin.sin_addr.s_addr =
htonl(
256 * 256 * 256 * ft_atoi(hostport[0])
+ 256 * 256 * ft_atoi(hostport[1])
+ 256 * ft_atoi(hostport[2])
+ ft_atoi(hostport[3]));
ftp->data_state = DATA_PASV;
console_msg(1, "remote dconn @ %s:%i",
inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr, buf, sizeof(struct sockaddr_in)),
ntohs(ftp->dconn.sin.sin_port));
return (0);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cli_user.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 10:17:03 by jhalford #+# #+# */
/* Updated: 2017/11/09 10:21:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int cli_user(t_ftp *ftp, char **av)
{
int code;
if (av[1] == NULL || av[2] != NULL)
{
console_msg(-1, "usage: user <username>");
return (-1);
}
ftp_cmd(ftp, "USER %s", av[1]);
code = ftp_code(ftp);
return (0);
}

View file

@ -6,29 +6,28 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */
/* Updated: 2017/11/08 15:12:12 by jhalford ### ########.fr */
/* Updated: 2017/11/09 14:53:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_client.h"
int g_debug = 0;
int g_debug;
t_cmd_map g_cli_cmd[] =
{
{"ls", cli_ls, "list contents of remote directory"},
{"user", cli_user, "authentify user by name"},
/* {"cd", cli_do_cd, "change remote working directory"}, */
/* {"get", cli_do_get, "receive file"}, */
/* {"put", cli_do_put, "send one file"}, */
/* {"pwd", cli_do_sh, "print working directory on remote machine"}, */
/* {"quit", NULL, "terminate ftp session and exit"}, */
{"quit", NULL, "terminate ftp session and exit"},
/* {"?", cli_do_help, "print local help information"}, */
{"?", cli_help, "print local help information"},
{"debug", cli_debug, "set debugging level"},
/* {"l", cli_do_local, "execute a local command"}, */
/* {"debug", cli_do_debug, "toggle/set debugging mode"}, */
{0, 0, 0},
};
int g_debug = 1;
t_cmd_map *get_cmd(char *cmd)
{
@ -43,15 +42,17 @@ t_cmd_map *get_cmd(char *cmd)
return (NULL);
}
int do_client(int sock)
int do_client(t_ftp *ftp)
{
char *input;
t_cmd_map *cmd;
char **av;
int code;
code = ftp_code(ftp);
while (1)
{
if (!(input = readline("ft_p> ")))
if (!(input = readline("ftp> ")))
return (1);
if (*input)
{
@ -59,10 +60,7 @@ int do_client(int sock)
if (!(cmd = get_cmd(av[0])))
console_msg(-1, "?Invalid command");
else if (cmd->f)
{
(void)sock;
(cmd->f)(sock, av);
}
code = (cmd->f)(ftp, av);
else
return (0);
ft_sstrfree(av);
@ -75,6 +73,7 @@ int main(int ac, char **av)
{
int port;
int sock;
t_ftp ftp;
if (ac != 3)
ft_usage(FTP_CLIENT_USAGE, av[0]);
@ -84,7 +83,10 @@ int main(int ac, char **av)
perror(av[0]);
return (1);
}
do_client(sock);
ftp.cmd_sock = sock;
ftp.data_state = DATA_NONE;
ftp.d_sock = 0;
do_client(&ftp);
close(sock);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 12:05:23 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:58:17 by jhalford ### ########.fr */
/* Updated: 2017/11/09 13:47:56 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,13 +20,15 @@ int console_msg(int level, char *str, ...)
if (g_debug >= level)
{
if (level == -1)
ft_printf("{red}");
if (level == 0)
ft_printf("{blu}");
ft_dprintf(2, "{red}");
else if (level == 0)
ft_dprintf(2, "{blu}");
else if (level == 1)
ft_dprintf(2, "{gre}");
else
ft_printf("{mag}");
ft_vdprintf(1, str, ap);
ft_printf("{eoc}\n");
ft_dprintf(2, "{yel}");
ft_vdprintf(2, str, ap);
ft_dprintf(2, "{eoc}\n");
}
return (level);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:58:23 by jhalford ### ########.fr */
/* Updated: 2017/11/09 15:13:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,7 @@ int ftp_recv(int sock, char **msg)
return (1);
}
*msg = ft_strdup(buf);
console_msg(0, msg);
console_msg(0, "<--- %s", *msg);
return (0);
}
@ -41,6 +41,7 @@ int ftp_send(int sock, char *msg, ...)
va_start(ap, msg);
ft_vasprintf(&crlf_tmp, msg, ap);
console_msg(1, "---> %s", crlf_tmp);
crlf_msg = ft_strjoin(crlf_tmp, "\r\n");
if ((err = send(sock, crlf_msg, ft_strlen(crlf_msg), 0)) < 0)
{
@ -53,8 +54,11 @@ int ftp_send(int sock, char *msg, ...)
int ftp_code(t_ftp *ftp)
{
char *msg
char *msg;
int code;
ftp_recv(ftp->cmd_sock, &msg);
return(ft_atoi(msg));
code = ft_atoi(msg);
ft_strdel(&msg);
return (code);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/08 20:09:58 by jhalford ### ########.fr */
/* Updated: 2017/11/09 14:53:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,12 +16,10 @@ 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;
@ -32,41 +30,56 @@ 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_init(t_ftp *ftp)
{
if (ftp->data_state == DATA_NONE)
{
if (cli_pasv(ftp) < 0)
return (-1);
}
return (0);
}
int dconn_open(t_ftp *ftp)
{
int code;
code = ftp_code(ftp);
if (ftp->code == 125)
if (code == 150)
{
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)
else if (code == 125)
return (0);
else
{
console_msg(-1, "expected 125/150 code");
return (-1);
}
return (-1);
}
/* int dconn_close(t_ftp *ftp) */
/* { */
/* ftp_ret(ftp, "226 closing dataconn"); */
/* close(ftp->d_sock); */
/* ftp->d_sock = 0; */
/* return (0); */
/* } */
int dconn_close(t_ftp *ftp)
{
int code;
code = ftp_code(ftp);
if (code == 226)
{
close(ftp->d_sock);
ftp->data_state = DATA_NONE;
ftp->d_sock = 0;
console_msg(1, "dataconn closed");
}
else
{
console_msg(1, "keeping dataconn alive");
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 13:36:24 by jhalford #+# #+# */
/* Updated: 2017/11/02 15:24:00 by jhalford ### ########.fr */
/* Updated: 2017/11/09 09:58:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

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

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 18:42:50 by jhalford #+# #+# */
/* Updated: 2017/11/08 17:30:18 by jhalford ### ########.fr */
/* Updated: 2017/11/09 13:16:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 15:32:28 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:04:28 by jhalford ### ########.fr */
/* Updated: 2017/11/09 15:14:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 14:20:46 by jhalford #+# #+# */
/* Updated: 2017/11/08 15:14:02 by jhalford ### ########.fr */
/* Updated: 2017/11/09 12:31:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,11 @@
int cmd_user(t_ftp *ftp, char **av)
{
if (ftp->log_state == LOG_YES)
{
ftp_ret(ftp, "230 user '%s' logged in, proceed", ftp->username);
return (0);
}
ft_strcpy(ftp->username, av[1]);
ft_strcpy(ftp->path, REPOPATH);
ft_strcat(ftp->path, av[1]);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 14:55:15 by jhalford #+# #+# */
/* Updated: 2017/11/08 19:53:09 by jhalford ### ########.fr */
/* Updated: 2017/11/09 10:29:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,6 +55,7 @@ int dconn_open(t_ftp *ftp)
return (dconn_open_pasv(ftp));
else
{
ftp_ret(ftp, "425 can't establish data connection");
console_msg(1, "dconn_open() called but no dconn available");
return (-1);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */
/* Updated: 2017/11/08 16:39:15 by jhalford ### ########.fr */
/* Updated: 2017/11/09 10:25:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -73,7 +73,7 @@ int ftp_spawn(int sock)
ftp.cmd_sock = sock;
ftp.log_state = LOG_NONE;
ftp.d_sock = 0;
ftp.data_state = 0;
ftp.data_state = DATA_NONE;
ftp_ret(&ftp, "220 ready for user");
while (ftp.cmd_sock)
ftp_cmd(&ftp);