epsv/ipv6 done. norme too.

This commit is contained in:
Jack Halford 2017-11-12 15:02:56 +01:00
parent 4cb22f02a7
commit 85d0716fbe
40 changed files with 338 additions and 284 deletions

View file

@ -49,6 +49,7 @@ client/crlf.c\
client/dconn.c\ client/dconn.c\
server/cmd_cwd.c\ server/cmd_cwd.c\
server/cmd_dele.c\ server/cmd_dele.c\
server/cmd_epsv.c\
server/cmd_list.c\ server/cmd_list.c\
server/cmd_mkd.c\ server/cmd_mkd.c\
server/cmd_pasv.c\ server/cmd_pasv.c\

View file

@ -6,12 +6,12 @@
/* 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/10 19:46:45 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:39:39 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FTP_CLIENT #ifndef FTP_CLIENT_H
# define FTP_CLIENT # define FTP_CLIENT_H
# include "libft.h" # include "libft.h"
# include <stdio.h> # include <stdio.h>
@ -23,7 +23,7 @@
# include <signal.h> # include <signal.h>
# define FTP_CLIENT_USAGE "%s <addr> <port>" # define FTP_CLIENT_USAGE "%s <addr> <port>"
# define ftp_cmd(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__) # define FTP_CMD(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__)
typedef struct s_cmd_map t_cmd_map; typedef struct s_cmd_map t_cmd_map;
typedef struct s_ftp t_ftp; typedef struct s_ftp t_ftp;
@ -42,14 +42,17 @@ enum e_dstate
DATA_ACTV, DATA_ACTV,
}; };
union u_conn
{
struct sockaddr_in sin;
int sock;
};
struct s_ftp struct s_ftp
{ {
int cmd_sock; int cmd_sock;
enum e_dstate data_state; enum e_dstate data_state;
union { union u_conn dconn;
struct sockaddr_in sin;
int sock;
} dconn;
int d_sock; int d_sock;
}; };

View file

@ -6,12 +6,12 @@
/* 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/10 19:39:47 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:40:29 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FTP_SERVER #ifndef FTP_SERVER_H
# define FTP_SERVER # define FTP_SERVER_H
# include "libft.h" # include "libft.h"
# include <stdio.h> # include <stdio.h>
@ -24,7 +24,7 @@
# define REPOPATH "data/" # define REPOPATH "data/"
# define FTP_SERVER_USAGE "%s <port>" # define FTP_SERVER_USAGE "%s <port>"
# define ftp_ret(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__) # define FTP_RET(ftp, ...) ftp_send((ftp)->cmd_sock, ##__VA_ARGS__)
typedef struct s_ftp t_ftp; typedef struct s_ftp t_ftp;
typedef struct s_ftp_cmd t_ftp_cmd; typedef struct s_ftp_cmd t_ftp_cmd;
@ -34,6 +34,8 @@ enum e_dstate
DATA_NONE, DATA_NONE,
DATA_PASV, DATA_PASV,
DATA_ACTV, DATA_ACTV,
DATA_EPSV,
DATA_EACTV,
}; };
enum e_lstate enum e_lstate
@ -42,6 +44,12 @@ enum e_lstate
LOG_YES, LOG_YES,
}; };
union u_conn
{
struct sockaddr_in sin;
int sock;
};
struct s_ftp struct s_ftp
{ {
int cmd_sock; int cmd_sock;
@ -49,10 +57,7 @@ struct s_ftp
char username[100]; char username[100];
char path[100]; char path[100];
enum e_dstate data_state; enum e_dstate data_state;
union { union u_conn dconn;
struct sockaddr_in sin;
int sock;
} dconn;
int d_sock; int d_sock;
}; };
@ -63,7 +68,6 @@ struct s_ftp_cmd
enum e_lstate statelock; enum e_lstate statelock;
}; };
extern char **g_av; extern char **g_av;
extern int g_debug; extern int g_debug;
extern char g_rootdir[PATH_MAX]; extern char g_rootdir[PATH_MAX];
@ -82,6 +86,7 @@ int cmd_stor(t_ftp *ftp, char **av);
int cmd_cwd(t_ftp *ftp, char **av); int cmd_cwd(t_ftp *ftp, char **av);
int cmd_pwd(t_ftp *ftp, char **av); int cmd_pwd(t_ftp *ftp, char **av);
int cmd_pasv(t_ftp *ftp, char **av); int cmd_pasv(t_ftp *ftp, char **av);
int cmd_epsv(t_ftp *ftp, char **av);
int cmd_port(t_ftp *ftp, char **av); int cmd_port(t_ftp *ftp, char **av);
int cmd_type(t_ftp *ftp, char **av); int cmd_type(t_ftp *ftp, char **av);
int cmd_list(t_ftp *ftp, char **av); int cmd_list(t_ftp *ftp, char **av);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */ /* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:14:42 by jhalford ### ########.fr */ /* Updated: 2017/11/12 12:21:30 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@
# include "mytime.h" # include "mytime.h"
int create_server(int port, int backlog, char *protoname); int create_server(int port, int backlog, char *protoname);
int create_client(char *addr, int port, char *protoname); int create_tcpclient(char *host, char *port);
void listener(int domain, int sock, int proto, void listener(int domain, int sock, int proto,
void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); void (*handler)(void *buf, int bytes, struct sockaddr_in *addr));

View file

@ -6,45 +6,49 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:59:28 by jhalford #+# #+# */ /* Created: 2017/10/07 17:59:28 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:29:49 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:15:13 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
void resolve_host(struct sockaddr *s, char *hostname) struct addrinfo *resolve_host(char *hostname, char *port)
{ {
struct addrinfo *result; struct addrinfo *result;
struct addrinfo hints; struct addrinfo hints;
int ecode;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME; hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(hostname, NULL, &hints, &result) != 0) if ((ecode = getaddrinfo(hostname, port, &hints, &result)) != 0)
{ {
perror("getaddrinfo"); ft_dprintf(2, "getaddrinfo: %s\n", gai_strerror(ecode));
exit(1); exit(1);
} }
ft_memcpy(((struct sockaddr_in*)s), return (result);
result->ai_addr, sizeof(struct sockaddr));
freeaddrinfo(result);
} }
int create_client(char *host, int port, char *protoname) int create_tcpclient(char *host, char *port)
{ {
struct addrinfo *ai;
int sock; int sock;
struct protoent *proto;
struct sockaddr_in sin;
if (!(proto = getprotobyname(protoname))) ai = resolve_host(host, port);
return (-1); while (ai)
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto); {
resolve_host((struct sockaddr*)&sin, host); if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))
sin.sin_family = AF_INET; < 0)
sin.sin_port = htons(port);
if (connect(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0)
return (-1); return (-1);
if (connect(sock, ai->ai_addr, ai->ai_addrlen) == 0)
break ;
close(sock);
sock = -1;
ai = ai->ai_next;
}
freeaddrinfo(ai);
return (sock); return (sock);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:02:51 by jhalford #+# #+# */ /* Created: 2017/10/07 18:02:51 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:02:51 by jhalford ### ########.fr */ /* Updated: 2017/11/12 11:39:18 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,14 +16,14 @@ int create_server(int port, int backlog, char *protoname)
{ {
int sock; int sock;
struct protoent *proto; struct protoent *proto;
struct sockaddr_in sin; struct sockaddr_in6 sin;
if (!(proto = getprotobyname(protoname))) if (!(proto = getprotobyname(protoname)))
return (-1); return (-1);
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto); sock = socket(PF_INET6, SOCK_STREAM, proto->p_proto);
sin.sin_family = AF_INET; sin.sin6_family = AF_INET6;
sin.sin_port = htons(port); sin.sin6_port = htons(port);
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin6_addr = in6addr_any;
if (bind(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0) if (bind(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0)
return (-1); return (-1);
listen(sock, backlog); listen(sock, backlog);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 15:55:33 by jhalford #+# #+# */ /* Created: 2017/03/20 15:55:33 by jhalford #+# #+# */
/* Updated: 2017/11/10 15:52:55 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:15:48 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
/* /*
** span the complement of a string ** span the complement of a string
*/ */
size_t ft_strcspn(char *s, const char *delim) size_t ft_strcspn(char *s, const char *delim)
{ {
char *str; char *str;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 15:53:38 by jhalford #+# #+# */ /* Created: 2017/11/10 15:53:38 by jhalford #+# #+# */
/* Updated: 2017/11/10 16:01:43 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:15:25 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
/* /*
** locate multiple characters in a string ** locate multiple characters in a string
*/ */
char *ft_strpbrk(const char *s1, const char *s2) char *ft_strpbrk(const char *s1, const char *s2)
{ {
char *str; char *str;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 18:08:31 by jhalford #+# #+# */ /* Created: 2017/03/07 18:08:31 by jhalford #+# #+# */
/* Updated: 2017/11/10 15:56:57 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
/* /*
** token generator ** token generator
*/ */
char *ft_strtok(char *s, const char *delim) char *ft_strtok(char *s, const char *delim)
{ {
static char *lasts; static char *lasts;
@ -39,17 +40,20 @@ char *ft_strtok(char *s, const char *delim)
/* /*
** also returns empty strings in the case of multiple delimiters ** also returns empty strings in the case of multiple delimiters
*/ */
char *ft_strtok_single(char *str, char const *delims) char *ft_strtok_single(char *str, char const *delims)
{ {
static char *src = NULL; static char *src = NULL;
char *p; char *p;
char *ret = 0; char *ret;
ret = 0;
if (str != NULL) if (str != NULL)
src = str; src = str;
if (src == NULL) if (src == NULL)
return NULL; return (NULL);
if ((p = strpbrk (src, delims)) != NULL) { if ((p = strpbrk(src, delims)) != NULL)
{
*p = 0; *p = 0;
ret = src; ret = src;
src = ++p; src = ++p;
@ -59,5 +63,5 @@ char *ft_strtok_single(char *str, char const *delims)
ret = src; ret = src;
src = NULL; src = NULL;
} }
return ret; return (ret);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 12:42:21 by jhalford #+# #+# */ /* Created: 2017/11/10 12:42:21 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:30:55 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ int cli_cd(t_ftp *ftp, char **av)
console_msg(-1, "usage: cd <dir>"); console_msg(-1, "usage: cd <dir>");
return (-1); return (-1);
} }
ftp_cmd(ftp, "CWD %s", av[1]); FTP_CMD(ftp, "CWD %s", av[1]);
ftp_code(ftp); ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:41:33 by jhalford #+# #+# */ /* Created: 2017/11/10 19:41:33 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:41:33 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ int cli_dele(t_ftp *ftp, char **av)
{ {
if (!av[1] || av[2]) if (!av[1] || av[2])
return (console_msg(-1, "usage: unlink <directory>")); return (console_msg(-1, "usage: unlink <directory>"));
ftp_cmd(ftp, "DELE %s", av[1]); FTP_CMD(ftp, "DELE %s", av[1]);
ftp_code(ftp); ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 13:00:28 by jhalford #+# #+# */ /* Created: 2017/11/10 13:00:28 by jhalford #+# #+# */
/* Updated: 2017/11/10 18:55:01 by jhalford ### ########.fr */ /* Updated: 2017/11/12 15:02:03 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@ int cli_get(t_ftp *ftp, char **av)
return (-1); return (-1);
if (dconn_init(ftp) < 0) if (dconn_init(ftp) < 0)
return (cleanup(fd, av[1])); return (cleanup(fd, av[1]));
ftp_cmd(ftp, "RETR %s", av[1]); FTP_CMD(ftp, "RETR %s", av[1]);
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
return (cleanup(fd, av[1])); return (cleanup(fd, av[1]));
size = ftp_recvraw(ftp->d_sock, &msg); size = ftp_recvraw(ftp->d_sock, &msg);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 14:04:01 by jhalford #+# #+# */ /* Created: 2017/11/09 14:04:01 by jhalford #+# #+# */
/* Updated: 2017/11/09 14:12:55 by jhalford ### ########.fr */ /* Updated: 2017/11/12 15:02: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:43:10 by jhalford #+# #+# */ /* Created: 2017/11/08 13:43:10 by jhalford #+# #+# */
/* Updated: 2017/11/10 18:39:39 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ int cli_ls(t_ftp *ftp, char **av)
(void)av; (void)av;
if (dconn_init(ftp) < 0) if (dconn_init(ftp) < 0)
return (-1); return (-1);
ftp_cmd(ftp, "LIST -a"); FTP_CMD(ftp, "LIST -a");
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
return (-1); return (-1);
while (1) while (1)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:27:35 by jhalford #+# #+# */ /* Created: 2017/11/10 19:27:35 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:40:44 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ int cli_mkd(t_ftp *ftp, char **av)
{ {
if (!av[1] || av[2]) if (!av[1] || av[2])
return (console_msg(-1, "usage: mkdir <directory>")); return (console_msg(-1, "usage: mkdir <directory>"));
ftp_cmd(ftp, "MKD %s", av[1]); FTP_CMD(ftp, "MKD %s", av[1]);
ftp_code(ftp); ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 11:19:41 by jhalford #+# #+# */ /* Created: 2017/11/09 11:19:41 by jhalford #+# #+# */
/* Updated: 2017/11/10 16:48:51 by jhalford ### ########.fr */ /* Updated: 2017/11/12 15:01:58 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,12 +16,10 @@ int cli_pasv(t_ftp *ftp)
{ {
char *msg; char *msg;
char **hostport; char **hostport;
int code;
char buf[INET_ADDRSTRLEN]; char buf[INET_ADDRSTRLEN];
ftp_cmd(ftp, "PASV"); FTP_CMD(ftp, "PASV");
code = ftp_msg(ftp, &msg); if (ftp_msg(ftp, &msg) != 227)
if (code != 227)
return (-1); return (-1);
hostport = ft_strsplit(msg + 4, ','); hostport = ft_strsplit(msg + 4, ',');
ftp->dconn.sin.sin_family = AF_INET; ftp->dconn.sin.sin_family = AF_INET;
@ -35,7 +33,8 @@ int cli_pasv(t_ftp *ftp)
+ ft_atoi(hostport[3])); + ft_atoi(hostport[3]));
ftp->data_state = DATA_PASV; ftp->data_state = DATA_PASV;
console_msg(1, "remote dconn @ %s:%i", console_msg(1, "remote dconn @ %s:%i",
inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr, buf, sizeof(struct sockaddr_in)), inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr,
buf, sizeof(struct sockaddr_in)),
ntohs(ftp->dconn.sin.sin_port)); ntohs(ftp->dconn.sin.sin_port));
ft_strdel(&msg); ft_strdel(&msg);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 16:20:14 by jhalford #+# #+# */ /* Created: 2017/11/10 16:20:14 by jhalford #+# #+# */
/* Updated: 2017/11/10 18:57:02 by jhalford ### ########.fr */ /* Updated: 2017/11/12 15:01:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,23 +27,13 @@ int cli_put(t_ftp *ftp, char **av)
} }
if (!buf.st_size || (file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE, if (!buf.st_size || (file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0)) == MAP_FAILED) MAP_PRIVATE, fd, 0)) == MAP_FAILED)
{ return (close(fd));
close(fd);
return (-1);
}
close(fd); close(fd);
if (dconn_init(ftp) < 0) if (dconn_init(ftp) < 0)
{ return (munmap(file, buf.st_size));
munmap(file, buf.st_size); FTP_CMD(ftp, "STOR %s", av[1]);
return (-1);
}
ftp_cmd(ftp, "STOR %s", av[1]);
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
{ return (munmap(file, buf.st_size));
munmap(file, buf.st_size);
return (-1);
}
send(ftp->d_sock, file, buf.st_size, 0); send(ftp->d_sock, file, buf.st_size, 0);
close(ftp->d_sock); close(ftp->d_sock);
dconn_close(ftp); dconn_close(ftp);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 12:33:09 by jhalford #+# #+# */ /* Created: 2017/11/10 12:33:09 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:27:13 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
int cli_pwd(t_ftp *ftp, char **av) int cli_pwd(t_ftp *ftp, char **av)
{ {
(void)av; (void)av;
ftp_cmd(ftp, "PWD"); FTP_CMD(ftp, "PWD");
ftp_code(ftp); ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:24:11 by jhalford #+# #+# */ /* Created: 2017/11/10 19:24:11 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:36:59 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ int cli_rmd(t_ftp *ftp, char **av)
{ {
if (!av[1] || av[2]) if (!av[1] || av[2])
return (console_msg(-1, "usage: rmdir <directory>")); return (console_msg(-1, "usage: rmdir <directory>"));
ftp_cmd(ftp, "RMD %s", av[1]); FTP_CMD(ftp, "RMD %s", av[1]);
ftp_code(ftp); ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 10:17:03 by jhalford #+# #+# */ /* Created: 2017/11/09 10:17:03 by jhalford #+# #+# */
/* Updated: 2017/11/09 10:21:45 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:19:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ int cli_user(t_ftp *ftp, char **av)
console_msg(-1, "usage: user <username>"); console_msg(-1, "usage: user <username>");
return (-1); return (-1);
} }
ftp_cmd(ftp, "USER %s", av[1]); FTP_CMD(ftp, "USER %s", av[1]);
code = ftp_code(ftp); code = ftp_code(ftp);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */ /* Created: 2017/04/02 19:18:31 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:42:00 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:56:28 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,6 @@ t_cmd_map g_cli_cmd[] =
{"?", cli_help, "print local help information"}, {"?", cli_help, "print local help information"},
{"debug", cli_debug, "set debugging level"}, {"debug", cli_debug, "set debugging level"},
/* {"l", cli_do_local, "execute a local command"}, */
{0, 0, 0}, {0, 0, 0},
}; };
int g_debug = 1; int g_debug = 1;
@ -74,14 +73,12 @@ int do_client(t_ftp *ftp)
int main(int ac, char **av) int main(int ac, char **av)
{ {
int port;
int sock; int sock;
t_ftp ftp; t_ftp ftp;
if (ac != 3) if (ac != 3)
ft_usage(FTP_CLIENT_USAGE, av[0]); ft_usage(FTP_CLIENT_USAGE, av[0]);
port = ft_atoi(av[2]); if ((sock = create_tcpclient(av[1], av[2])) < 0)
if ((sock = create_client(av[1], port, "tcp")) < 0)
{ {
perror(av[0]); perror(av[0]);
return (1); return (1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/10 18:52:09 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:56:15 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
/* /*
** stream mode with file structure --> raw data no EOF ** stream mode with file structure --> raw data no EOF
*/ */
int ftp_recvraw(int sock, char **msg) int ftp_recvraw(int sock, char **msg)
{ {
int ret; int ret;

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/10 18:20:14 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:58:42 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 13:36:24 by jhalford #+# #+# */ /* Created: 2017/11/02 13:36:24 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:34:43 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:44 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ int cmd_cwd(t_ftp *ftp, char **av)
if (chdir(av[1])) if (chdir(av[1]))
{ {
console_msg(1, "chdir(%s) failed errno=%i", av[1], errno); console_msg(1, "chdir(%s) failed errno=%i", av[1], errno);
ftp_ret(ftp, "550 directory not found"); FTP_RET(ftp, "550 directory not found");
} }
else else
{ {
@ -30,10 +30,10 @@ int cmd_cwd(t_ftp *ftp, char **av)
{ {
console_msg(0, "%s -> %s", buf, ftp->path); console_msg(0, "%s -> %s", buf, ftp->path);
chdir(old); chdir(old);
ftp_ret(ftp, "550 unauthorized directory"); FTP_RET(ftp, "550 unauthorized directory");
} }
else else
ftp_ret(ftp, "200 success"); FTP_RET(ftp, "200 success");
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:38:40 by jhalford #+# #+# */ /* Created: 2017/11/10 19:38:40 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:39:34 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:44 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,8 @@ int cmd_dele(t_ftp *ftp, char **av)
{ {
(void)av; (void)av;
if (unlink(av[1]) < 0) if (unlink(av[1]) < 0)
return (ftp_ret(ftp, "550 unlink error")); return (FTP_RET(ftp, "550 unlink error"));
else else
return (ftp_ret(ftp, "250 file '%s' deleted", av[1])); return (FTP_RET(ftp, "250 file '%s' deleted", av[1]));
return (0); return (0);
} }

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_epsv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/12 11:13:03 by jhalford #+# #+# */
/* Updated: 2017/11/12 14:37:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftp_server.h"
int cmd_epsv(t_ftp *ftp, char **av)
{
uint16_t port;
int sock;
struct sockaddr_in6 sin;
(void)av;
sin.sin6_family = AF_INET6;
sin.sin6_addr = in6addr_any;
port = 35000;
while (++port < USHRT_MAX)
{
sin.sin6_port = htons(port);
sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (bind(sock, (const struct sockaddr *)&sin,
sizeof(struct sockaddr_in6)) == 0
&& listen(sock, 3) == 0)
{
ftp->data_state = DATA_EPSV;
ftp->dconn.sock = sock;
console_msg(1, "listening on %i (EPSV)", port);
return (FTP_RET(ftp, "229 (|||%hu|)", ntohs(sin.sin6_port)));
}
}
return (FTP_RET(ftp, "500 couldn't find local port"));
}

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/10 12:20:02 by jhalford ### ########.fr */ /* Updated: 2017/11/12 11:24:47 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:17:53 by jhalford #+# #+# */ /* Created: 2017/11/10 19:17:53 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:47:06 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,6 @@
int cmd_mkd(t_ftp *ftp, char **av) int cmd_mkd(t_ftp *ftp, char **av)
{ {
if (mkdir(av[1], 0755) < 0) if (mkdir(av[1], 0755) < 0)
return (ftp_ret(ftp, "550 mkdir() error")); return (FTP_RET(ftp, "550 mkdir() error"));
return (ftp_ret(ftp, "257 '%s' created", av[1])); return (FTP_RET(ftp, "257 '%s' created", av[1]));
} }

View file

@ -6,12 +6,26 @@
/* 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/09 11:19:13 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:56:01 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_server.h" #include "ftp_server.h"
static int pasv_ok(t_ftp *ftp, int sock, struct sockaddr_in sin)
{
ftp->data_state = DATA_PASV;
ftp->dconn.sock = sock;
console_msg(1, "listening on %i (PASV)", ntohs(sin.sin_port));
return (FTP_RET(ftp, "227 %i,%i,%i,%i,%i,%i",
(ntohl(sin.sin_addr.s_addr) >> (8 * 3)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8 * 2)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8 * 1)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8 * 0)) & 0xff,
(ntohs(sin.sin_port) >> (8 * 1)) & 0xff,
(ntohs(sin.sin_port) >> (8 * 0)) & 0xff));
}
int cmd_pasv(t_ftp *ftp, char **av) int cmd_pasv(t_ftp *ftp, char **av)
{ {
uint16_t port; uint16_t port;
@ -29,19 +43,7 @@ int cmd_pasv(t_ftp *ftp, char **av)
if (bind(sock, (const struct sockaddr *)&sin, if (bind(sock, (const struct sockaddr *)&sin,
sizeof(struct sockaddr_in)) == 0 sizeof(struct sockaddr_in)) == 0
&& listen(sock, 3) == 0) && listen(sock, 3) == 0)
{ return (pasv_ok(ftp, sock, sin));
ftp->data_state = DATA_PASV;
ftp->dconn.sock = sock;
console_msg(1, "listening on %i (PASV)", port);
return (ftp_ret(ftp, "227 %i,%i,%i,%i,%i,%i",
(ntohl(sin.sin_addr.s_addr) >> (8*3)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8*2)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8*1)) & 0xff,
(ntohl(sin.sin_addr.s_addr) >> (8*0)) & 0xff,
(ntohs(sin.sin_port) >> (8*1)) & 0xff,
(ntohs(sin.sin_port) >> (8*0)) & 0xff
));
} }
} return (FTP_RET(ftp, "500 couldn't find local port"));
return (ftp_ret(ftp, "500 couldn't find local port"));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 18:42:50 by jhalford #+# #+# */ /* Created: 2017/11/01 18:42:50 by jhalford #+# #+# */
/* Updated: 2017/11/09 13:16:28 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:46:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ int cmd_port(t_ftp *ftp, char **av)
char buf[INET_ADDRSTRLEN]; char buf[INET_ADDRSTRLEN];
if (!av[1]) if (!av[1])
return (ftp_ret(ftp, "501 syntax error in parameter")); return (FTP_RET(ftp, "501 syntax error in parameter"));
if (ftp->data_state == DATA_PASV) if (ftp->data_state == DATA_PASV)
close(ftp->dconn.sock); close(ftp->dconn.sock);
hostport = ft_strsplit(av[1], ','); hostport = ft_strsplit(av[1], ',');
@ -33,7 +33,8 @@ int cmd_port(t_ftp *ftp, char **av)
+ ft_atoi(hostport[3])); + ft_atoi(hostport[3]));
ftp->data_state = DATA_ACTV; ftp->data_state = DATA_ACTV;
console_msg(1, "remote dconn @ %s:%i", console_msg(1, "remote dconn @ %s:%i",
inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr, buf, sizeof(struct sockaddr_in)), inet_ntop(AF_INET, &ftp->dconn.sin.sin_addr,
buf, sizeof(struct sockaddr_in)),
ntohs(ftp->dconn.sin.sin_port)); ntohs(ftp->dconn.sin.sin_port));
return (ftp_ret(ftp, "200 ip/port ok")); return (FTP_RET(ftp, "200 ip/port ok"));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 17:39:01 by jhalford #+# #+# */ /* Created: 2017/11/01 17:39:01 by jhalford #+# #+# */
/* Updated: 2017/11/02 14:18:58 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,6 @@ int cmd_pwd(t_ftp *ftp, char **av)
(void)av; (void)av;
getcwd(path, 200); getcwd(path, 200);
ftp_ret(ftp, "257 \"%s\"", path); FTP_RET(ftp, "257 \"%s\"", path);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 15:20:58 by jhalford #+# #+# */ /* Created: 2017/11/02 15:20:58 by jhalford #+# #+# */
/* Updated: 2017/11/02 15:31:17 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,6 @@ int cmd_quit(t_ftp *ftp, char **av)
(void)av; (void)av;
close(ftp->cmd_sock); close(ftp->cmd_sock);
ftp->cmd_sock = 0; ftp->cmd_sock = 0;
ftp_ret(ftp, "221 service closing control connection"); FTP_RET(ftp, "221 service closing control connection");
return (1); return (1);
} }

View file

@ -6,27 +6,21 @@
/* 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/10 18:54:04 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:50:38 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ftp_server.h" #include "ftp_server.h"
int cmd_retr(t_ftp *ftp, char **av) static int retr_internal(t_ftp *ftp, struct stat buf, int fd)
{ {
int fd;
struct stat buf;
char *file; char *file;
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)
{
if ((file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE, if ((file = mmap(NULL, buf.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0)) == MAP_FAILED) MAP_PRIVATE, fd, 0)) == MAP_FAILED)
{ {
close(fd); close(fd);
return (ftp_ret(ftp, "550 mmap failed")); return (FTP_RET(ftp, "550 mmap failed"));
} }
close(fd); close(fd);
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
@ -37,12 +31,21 @@ int cmd_retr(t_ftp *ftp, char **av)
send(ftp->d_sock, file, buf.st_size, 0); send(ftp->d_sock, file, buf.st_size, 0);
dconn_close(ftp); dconn_close(ftp);
munmap(file, buf.st_size); munmap(file, buf.st_size);
return (0);
} }
else
int cmd_retr(t_ftp *ftp, char **av)
{ {
int fd;
struct stat buf;
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)
return (retr_internal(ftp, buf, fd));
close(fd);
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
return (-1); return (-1);
dconn_close(ftp); dconn_close(ftp);
}
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 19:21:10 by jhalford #+# #+# */ /* Created: 2017/11/10 19:21:10 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:48:53 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,6 @@
int cmd_rmd(t_ftp *ftp, char **av) int cmd_rmd(t_ftp *ftp, char **av)
{ {
if (remove(av[1]) < 0) if (remove(av[1]) < 0)
return (ftp_ret(ftp, "550 remove(3) error")); return (FTP_RET(ftp, "550 remove(3) error"));
return (ftp_ret(ftp, "257 '%s' deleted", av[1])); return (FTP_RET(ftp, "257 '%s' deleted", av[1]));
} }

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/10 19:17:46 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ int cmd_stor(t_ftp *ftp, char **av)
if (dconn_open(ftp) < 0) if (dconn_open(ftp) < 0)
return (-1); return (-1);
if ((fd = open(av[1], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) 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"));
size = ftp_recvraw(ftp->d_sock, &msg); size = ftp_recvraw(ftp->d_sock, &msg);
write(fd, msg, size); write(fd, msg, size);
ft_strdel(&msg); ft_strdel(&msg);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 18:14:51 by jhalford #+# #+# */ /* Created: 2017/11/01 18:14:51 by jhalford #+# #+# */
/* Updated: 2017/11/10 13:29:46 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,16 +15,16 @@
int cmd_type(t_ftp *ftp, char **av) int cmd_type(t_ftp *ftp, char **av)
{ {
if (!av[1]) if (!av[1])
ftp_ret(ftp, "501 syntax error in parameter"); FTP_RET(ftp, "501 syntax error in parameter");
if (av[1][0] == 'A') if (av[1][0] == 'A')
{ {
ftp_ret(ftp, "200 type A ok"); FTP_RET(ftp, "200 type A ok");
} }
else if (av[1][0] == 'I') else if (av[1][0] == 'I')
{ {
ftp_ret(ftp, "200 type I ok"); FTP_RET(ftp, "200 type I ok");
} }
else else
ftp_ret(ftp, "500 parameter unrecognized"); FTP_RET(ftp, "500 parameter unrecognized");
return (1); return (1);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/02 14:20:46 by jhalford #+# #+# */ /* Created: 2017/11/02 14:20:46 by jhalford #+# #+# */
/* Updated: 2017/11/09 12:31:01 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:37:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ int cmd_user(t_ftp *ftp, char **av)
{ {
if (ftp->log_state == LOG_YES) if (ftp->log_state == LOG_YES)
{ {
ftp_ret(ftp, "230 user '%s' logged in, proceed", ftp->username); FTP_RET(ftp, "230 user '%s' logged in, proceed", ftp->username);
return (0); return (0);
} }
ft_strcpy(ftp->username, av[1]); ft_strcpy(ftp->username, av[1]);
@ -24,10 +24,10 @@ int cmd_user(t_ftp *ftp, char **av)
ft_strcat(ftp->path, av[1]); ft_strcat(ftp->path, av[1]);
if (mkdir(ftp->path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 if (mkdir(ftp->path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0
&& chdir(ftp->path) < 0) && chdir(ftp->path) < 0)
return (ftp_ret(ftp, "530 mkdir/chdir")); return (FTP_RET(ftp, "530 mkdir/chdir"));
if (getcwd(ftp->path, 100) < 0) if (getcwd(ftp->path, 100) < 0)
return (ftp_ret(ftp, "530 getcwd")); return (FTP_RET(ftp, "530 getcwd"));
ftp_ret(ftp, "230 user '%s' logged in, proceed", ftp->username); FTP_RET(ftp, "230 user '%s' logged in, proceed", ftp->username);
console_msg(1, "logon: %s@ftp://%s", ftp->username, ftp->path); console_msg(1, "logon: %s@ftp://%s", ftp->username, ftp->path);
ftp->log_state = LOG_YES; ftp->log_state = LOG_YES;
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */ /* Created: 2017/11/08 19:52:07 by jhalford #+# #+# */
/* Updated: 2017/11/10 18:55:45 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:45:09 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
/* /*
** stream mode with file structure --> raw data no EOF ** stream mode with file structure --> raw data no EOF
*/ */
int ftp_recvraw(int sock, char **msg) int ftp_recvraw(int sock, char **msg)
{ {
int ret; int ret;

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/10 17:45:13 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:44:50 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,12 +16,12 @@ static int dconn_open_actv(t_ftp *ftp)
{ {
int sock; int sock;
ftp_ret(ftp, "150 about to open data connection"); FTP_RET(ftp, "150 about to open data connection");
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(sock, (struct sockaddr*)&ftp->dconn.sin, if (connect(sock, (struct sockaddr*)&ftp->dconn.sin,
sizeof(struct sockaddr_in)) < 0) sizeof(struct sockaddr_in)) < 0)
{ {
ftp_ret(ftp, "425 can't open data conn"); FTP_RET(ftp, "425 can't open data conn");
return (-1); return (-1);
} }
ftp->d_sock = sock; ftp->d_sock = sock;
@ -32,10 +32,10 @@ static int dconn_open_pasv(t_ftp *ftp)
{ {
int sock; int sock;
ftp_ret(ftp, "150 about to accept"); FTP_RET(ftp, "150 about to accept");
if ((sock = accept(ftp->dconn.sock, NULL, NULL)) < 0) if ((sock = accept(ftp->dconn.sock, NULL, NULL)) < 0)
{ {
ftp_ret(ftp, "425 can't open data conn"); FTP_RET(ftp, "425 can't open data conn");
return (-1); return (-1);
} }
ftp->d_sock = sock; ftp->d_sock = sock;
@ -46,16 +46,16 @@ int dconn_open(t_ftp *ftp)
{ {
if (ftp->d_sock) if (ftp->d_sock)
{ {
ftp_ret(ftp, "125 data connection already open; transfer starting"); FTP_RET(ftp, "125 data connection already open; transfer starting");
return (0); return (0);
} }
else if (ftp->data_state == DATA_ACTV) else if (ftp->data_state == DATA_ACTV)
return (dconn_open_actv(ftp)); return (dconn_open_actv(ftp));
else if (ftp->data_state == DATA_PASV) else if (ftp->data_state == DATA_PASV || ftp->data_state == DATA_EPSV)
return (dconn_open_pasv(ftp)); return (dconn_open_pasv(ftp));
else else
{ {
ftp_ret(ftp, "425 can't establish data connection"); FTP_RET(ftp, "425 can't establish data connection");
console_msg(1, "dconn_open() called but no dconn available"); console_msg(1, "dconn_open() called but no dconn available");
return (-1); return (-1);
} }
@ -63,7 +63,7 @@ int dconn_open(t_ftp *ftp)
int dconn_close(t_ftp *ftp) int dconn_close(t_ftp *ftp)
{ {
ftp_ret(ftp, "226 closing dataconn"); FTP_RET(ftp, "226 closing dataconn");
if (ftp->data_state == DATA_PASV) if (ftp->data_state == DATA_PASV)
{ {
close(ftp->dconn.sock); close(ftp->dconn.sock);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */ /* Created: 2017/04/02 15:02:48 by jhalford #+# #+# */
/* Updated: 2017/11/10 19:57:20 by jhalford ### ########.fr */ /* Updated: 2017/11/12 14:44:33 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,7 @@ t_ftp_cmd g_ftp_cmd[] =
{"STOR", cmd_stor, LOG_YES}, {"STOR", cmd_stor, LOG_YES},
{"CWD", cmd_cwd, LOG_YES}, {"CWD", cmd_cwd, LOG_YES},
{"PASV", cmd_pasv, LOG_YES}, {"PASV", cmd_pasv, LOG_YES},
{"EPSV", cmd_epsv, LOG_YES},
{"PORT", cmd_port, LOG_YES}, {"PORT", cmd_port, LOG_YES},
{"PWD", cmd_pwd, LOG_YES}, {"PWD", cmd_pwd, LOG_YES},
{"TYPE", cmd_type, LOG_YES}, {"TYPE", cmd_type, LOG_YES},
@ -36,13 +37,11 @@ t_ftp_cmd g_ftp_cmd[] =
}; };
int g_debug = 2; int g_debug = 2;
int ftp_cmd(t_ftp *ftp) int ftp_cmd(t_ftp *ftp, char *msg)
{ {
int i; int i;
char *msg;
char **av; char **av;
ftp_recv(ftp->cmd_sock, &msg);
i = -1; i = -1;
while (g_ftp_cmd[++i].name) while (g_ftp_cmd[++i].name)
{ {
@ -50,9 +49,7 @@ int ftp_cmd(t_ftp *ftp)
ft_strlen(g_ftp_cmd[i].name)) == 0) ft_strlen(g_ftp_cmd[i].name)) == 0)
{ {
if (ftp->log_state < g_ftp_cmd[i].statelock) if (ftp->log_state < g_ftp_cmd[i].statelock)
{ return (FTP_RET(ftp, "530 not logged in"));
return (ftp_ret(ftp, "530 not logged in"));
}
if (!(g_ftp_cmd[i].f)) if (!(g_ftp_cmd[i].f))
break ; break ;
else else
@ -64,22 +61,26 @@ int ftp_cmd(t_ftp *ftp)
return (0); return (0);
} }
} }
ftp_ret(ftp, "502 command not implemented"); FTP_RET(ftp, "502 command not implemented");
ft_strdel(&msg);
return (1); return (1);
} }
int ftp_spawn(int sock) int ftp_spawn(int sock)
{ {
t_ftp ftp; t_ftp ftp;
char *msg;
ftp.cmd_sock = sock; ftp.cmd_sock = sock;
ftp.log_state = LOG_NONE; ftp.log_state = LOG_NONE;
ftp.d_sock = 0; ftp.d_sock = 0;
ftp.data_state = DATA_NONE; ftp.data_state = DATA_NONE;
ftp_ret(&ftp, "220 ready for user"); FTP_RET(&ftp, "220 ready for user");
while (ftp.cmd_sock) while (ftp.cmd_sock)
ftp_cmd(&ftp); {
ftp_recv(ftp.cmd_sock, &msg);
ftp_cmd(&ftp, msg);
ft_strdel(&msg);
}
close(ftp.cmd_sock); close(ftp.cmd_sock);
return (0); return (0);
} }