going to add a proper readline to libft next

This commit is contained in:
Jack Halford 2017-04-03 17:25:07 +02:00
parent 9d86bea9d1
commit 92daf7c8a6
9 changed files with 141 additions and 40 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/04/02 21:32:01 by jhalford ### ########.fr # # Updated: 2017/04/03 16:12:06 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -35,8 +35,9 @@ CLIENT_OBJ = $(OBJ_DIR)client.o
SRC_BASE = \ SRC_BASE = \
client.c\ client.c\
server.c\ ftp.c\
test.c ftp_cmd.c\
server.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))

1
ftp/auteur Normal file
View file

@ -0,0 +1 @@
jhalford

View file

@ -6,17 +6,36 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */ /* Created: 2017/04/02 19:18:58 by jhalford #+# #+# */
/* Updated: 2017/04/02 20:23:38 by jhalford ### ########.fr */ /* Updated: 2017/04/03 17:12:59 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FT_P_H #ifndef FT_P_H
# define FT_P_H # define FT_P_H
# define FTP_SERVER_USAGE "%s <port>"
# define FTP_CLIENT_USAGE "%s <addr> <port>"
# define FTP_BUF 1024
# define FTP_REPLY_BUF 1024
#include "libft.h" #include "libft.h"
#include "sys/socket.h" #include "sys/socket.h"
#include "netdb.h" #include "netdb.h"
#include "netinet/in.h" #include "netinet/in.h"
#include "arpa/inet.h" #include "arpa/inet.h"
typedef struct s_ftp_reply t_ftp_reply;
struct s_ftp_reply
{
int code;
char *data;
};
extern char **g_av;
int ftp_daemon(int sock);
int ftp_spawn(int cs);
int ftp_cmd(char *cmd, t_ftp_reply *reply);
#endif #endif

@ -1 +1 @@
Subproject commit ab685ea78b2fd776d07e2a150a8a2a884a48c80f Subproject commit 83fa039e360fc16134f1068c30dd4eb1e1c62d8c

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/04/02 21:40:32 by jhalford ### ########.fr */ /* Updated: 2017/04/03 17:20:42 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,11 @@ int main(int ac, char **av)
perror(av[0]); perror(av[0]);
return (1); return (1);
} }
write(sock, "bonjour\n", 8); while (1)
{
ft_readline();
write(sock, "bonjour\n", 8);
}
close(sock); close(sock);
return (0); return (0);
} }

64
ftp/srcs/ftp.c Normal file
View file

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_p.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/03 16:08:44 by jhalford #+# #+# */
/* Updated: 2017/04/03 17:20:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_p.h"
char **g_av;
int ftp_reply(int sock, t_ftp_reply *reply)
{
write(sock, reply->data, ft_strlen(reply->data));
return (0);
}
int ftp_spawn(int cs)
{
int r;
char buf[1024];
t_ftp_reply reply;
ft_printf("{yel}{inv}%i {gre} new connection {eoc}\n", getpid());
while ((r = read(cs, buf, FTP_BUF)) > 0)
{
buf[r - 1] = 0;
ft_printf("{yel}{inv}%i {eoc} received %i bytes: [%s]\n", getpid(), r, buf);
ft_bzero(&reply, sizeof(reply));
ftp_cmd(buf, &reply);
ftp_reply(cs, &reply);
}
ft_printf("{yel}{inv}%i {red} end of connection {eoc}\n", getpid());
close(cs);
return (0);
}
int ftp_daemon(int sock)
{
int cs;
struct sockaddr_in csin;
socklen_t cslen;
pid_t pid;
while (1)
{
cs = accept(sock, (struct sockaddr*)&csin, &cslen);
if ((pid = fork()) < 0)
{
perror(g_av[0]);
return (1);
}
if (pid == 0)
exit(ftp_spawn(cs));
close(cs);
}
close(sock);
return (0);
}

39
ftp/srcs/ftp_cmd.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ftp_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/03 16:42:58 by jhalford #+# #+# */
/* Updated: 2017/04/03 17:20:02 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_p.h"
t_stof g_ftp_cmdlist[] =
{
{"ls", NULL},
{0, 0},
};
int ftp_cmd(char *cmd, t_ftp_reply *reply)
{
char **av;
int i;
reply->code = 0;
av = ft_split_whitespaces(cmd);
i = -1;
while (g_ftp_cmdlist[++i].key)
{
if (ft_strcmp(g_ftp_cmdlist[i].key, av[0]) == 0)
{
ft_asprintf(&reply->data, "%s: Valid command !!!\n", av[0]);
return (0);
}
}
ft_asprintf(&reply->data, "%s: ?Invalid command\n", av[0]);
return (0);
}

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/04/02 21:40:33 by jhalford ### ########.fr */ /* Updated: 2017/04/03 16:42:49 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,8 @@
#define FTP_SERVER_USAGE "%s <port>" #define FTP_SERVER_USAGE "%s <port>"
char **g_av = NULL;
int create_server(int port) int create_server(int port)
{ {
int sock; int sock;
@ -28,7 +30,7 @@ int create_server(int port)
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_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, 42); listen(sock, 2);
return (sock); return (sock);
} }
@ -36,13 +38,8 @@ int main(int ac, char **av)
{ {
int port; int port;
int sock; int sock;
int cs;
struct sockaddr_in csin;
socklen_t cslen;
int r;
char buf[1024];
pid_t pid;
g_av = av;
if (ac != 2) if (ac != 2)
ft_usage(FTP_SERVER_USAGE, av[0]); ft_usage(FTP_SERVER_USAGE, av[0]);
port = ft_atoi(av[1]); port = ft_atoi(av[1]);
@ -51,26 +48,6 @@ int main(int ac, char **av)
perror(av[0]); perror(av[0]);
return (1); return (1);
} }
while (1) ftp_daemon(sock);
{
cs = accept(sock, (struct sockaddr*)&csin, &cslen);
if ((pid = fork()) < 0)
{
perror(av[0]);
return (1);
}
if (pid == 0)
{
while ((r = read(cs, buf, 1023)) > 0)
{
buf[r - 1] = 0;
ft_printf("==%i== received %i bytes: [%s]\n", getpid(), r, buf);
}
close(cs);
exit (0);
}
close(cs);
}
close(sock);
return (0); return (0);
} }

View file

@ -1,4 +0,0 @@
int test(void)
{
return (0);
}