This commit is contained in:
Jack Halford 2017-10-30 00:27:41 +01:00
parent e78c41d420
commit cd99c04ca5
4 changed files with 56 additions and 14 deletions

View file

@ -27,6 +27,8 @@
# include <sys/wait.h>
# include <limits.h>
# include <pwd.h>
typedef struct s_cmd_map t_cmd_map;
struct s_cmd_map
@ -67,6 +69,9 @@ int ftp_daemon(int sock);
int ftp_spawn(int sock);
int ftp_cmd(int sock, int req);
int ftp_send(int sock, char *msg, size_t size);
int ftp_recv(int sock, char buf[], size_t size);
int serv_do_get(int sock);
int serv_do_put(int sock);
int serv_do_sh(int sock);

View file

@ -21,7 +21,7 @@
*/
# define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}"
# define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__
# define DG(s, ...) ft_dprintf(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__)
# define DG(s, ...) ft_dprintf(STDERR,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__)
/*
** DEBUG with no malloc

View file

@ -14,11 +14,31 @@
int read_req(int sock)
{
int req;
if (read(sock, (char*)&req, sizeof(req)) < 0)
(void)sock;
return (0);
}
int ftp_recv(int sock, char buf[], size_t size)
{
int ret;
if ((ret = recv(sock, buf, size, 0)) < 0)
return (0);
if (ret >= 2)
buf[ret - 2] = 0;
else
buf[ret] = 0;
/* req = ntohs(req); */
console_msg(0, "%-5i<--- %s (%i)", getpid(), buf, ret);
return (0);
}
int ftp_send(int sock, char *msg, size_t size)
{
int err;
if ((err = send(sock, msg, size, 0)) < 0)
return (err);
msg[ft_strlen(msg) - 1] = 0;
console_msg(0, "%-5i---> %s", getpid(), msg);
return (0);
req = ntohs(req);
console_msg(0, "%i RECEIVED", req);
return (req);
}

View file

@ -50,18 +50,35 @@ int ftp_cmd(int sock, int req)
return (1);
}
int ftp_login(int sock)
{
char buf[100];
ftp_send(sock, "220 \r\n", 6);
ftp_recv(sock, buf, sizeof(buf));
if (ft_strncmp(buf, "USER ", 5) != 0)
console_msg(0, "expected USER");
if (ft_strncmp(buf + 5, "jack", 4) != 0)
console_msg(0, "wrong user");
DG("sending 331");
ftp_send(sock, "331 \r\n", 6);
ftp_recv(sock, buf, sizeof(buf));
return (0);
}
int ftp_spawn(int sock)
{
int req;
DG("new connection");
while ((req = read_req(sock)))
ftp_login(sock);
while (1)
{
DG("==== %i NEW REQUEST ====", req);
ftp_cmd(sock, req);
DG("==== DONE ====");
break ;
(void)req;
/* DG("==== %i NEW REQUEST ====", req); */
/* ftp_cmd(sock, req); */
/* DG("==== DONE ===="); */
}
DG("end of connection");
close(sock);
return (0);
}