diff --git a/libft/Makefile b/libft/Makefile index b1861156..3e8c47af 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -128,6 +128,11 @@ mem/ft_memdel.c\ mem/ft_memmove.c\ mem/ft_memset.c\ mem/ft_realloc.c\ +net/create_client.c\ +net/create_server.c\ +net/net_get.c\ +net/net_get_fd.c\ +net/net_send.c\ path/ft_path_notdir.c\ printing/ft_putchar.c\ printing/ft_putendl.c\ @@ -196,6 +201,7 @@ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ sys/is_directory.c\ sys/open_access.c\ +sys/open_new.c\ time/ft_mytime_free.c\ time/ft_mytime_get.c\ time/ft_time_isrecent.c diff --git a/libft/includes/error.h b/libft/includes/error.h index c19a7c55..ce1e812b 100644 --- a/libft/includes/error.h +++ b/libft/includes/error.h @@ -16,10 +16,16 @@ # include "libft.h" # include +/* +** DEBUG with no malloc +*/ # 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__) +/* +** DEBUG with no malloc +*/ # define DG2 ft_putstr(__FILE__"\t");ft_putnbr(__LINE__) # define DGW(d) DG2;d;ft_putchar('\n') # define DGS(s) DGW(ft_putstr(": "s"=")) diff --git a/libft/includes/libft.h b/libft/includes/libft.h index bb59a320..7e2bd5ed 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -34,6 +34,7 @@ # include "math.h" # include "mytime.h" # include "get_next_line.h" +# include "net.h" # include "sys.h" struct s_stos diff --git a/libft/includes/net.h b/libft/includes/net.h new file mode 100644 index 00000000..c339753b --- /dev/null +++ b/libft/includes/net.h @@ -0,0 +1,19 @@ +#ifndef NET_H +# define NET_H + +# define ACK 2 +# define NACK 3 + +# include +# include +# include +# include + +int create_server(int port, int backlog, char *protoname); +int create_client(char *addr, int port, char *protoname); + +int net_send(int sock, char *msg, int size); +int net_get(int sock, char *msg, int size); +int net_get_fd(int sock, int fd, int size); + +#endif diff --git a/libft/includes/sys.h b/libft/includes/sys.h index b3c42e22..93edda19 100644 --- a/libft/includes/sys.h +++ b/libft/includes/sys.h @@ -34,6 +34,7 @@ int ft_xattr_count(char *path); char *ft_getenv(char **env, char *key); int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); +int open_new(char *filename, int oflag); int is_directory(const char *path); char *create_directory(const char *path, const char *old_pathnames); diff --git a/libft/srcs/net/create_client.c b/libft/srcs/net/create_client.c new file mode 100644 index 00000000..77411380 --- /dev/null +++ b/libft/srcs/net/create_client.c @@ -0,0 +1,18 @@ +#include "libft.h" + +int create_client(char *addr, int port, char *protoname) +{ + int sock; + struct protoent *proto; + struct sockaddr_in sin; + + if (!(proto = getprotobyname(protoname))) + return (-1); + sock = socket(PF_INET, SOCK_STREAM, proto->p_proto); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + sin.sin_addr.s_addr = inet_addr(addr); + if (connect(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0) + return (-1); + return (sock); +} diff --git a/libft/srcs/net/create_server.c b/libft/srcs/net/create_server.c new file mode 100644 index 00000000..3ab05efb --- /dev/null +++ b/libft/srcs/net/create_server.c @@ -0,0 +1,19 @@ +#include "libft.h" + +int create_server(int port, int backlog, char *protoname) +{ + int sock; + struct protoent *proto; + struct sockaddr_in sin; + + if (!(proto = getprotobyname(protoname))) + return (-1); + sock = socket(PF_INET, SOCK_STREAM, proto->p_proto); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + sin.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0) + return (-1); + listen(sock, backlog); + return (sock); +} diff --git a/libft/srcs/net/net_get.c b/libft/srcs/net/net_get.c new file mode 100644 index 00000000..5be9fc5d --- /dev/null +++ b/libft/srcs/net/net_get.c @@ -0,0 +1,13 @@ +#include "libft.h" + +int net_get(int sock, char *msg, int size) +{ + int ack; + + ack = htons(ACK); + if (read(sock, msg, size) < 0) + return (-1); + if (write(sock, (char*)&ack, sizeof(ack)) < 0) + return (-1); + return (0); +} diff --git a/libft/srcs/net/net_get_fd.c b/libft/srcs/net/net_get_fd.c new file mode 100644 index 00000000..ca3e4750 --- /dev/null +++ b/libft/srcs/net/net_get_fd.c @@ -0,0 +1,16 @@ +#include "libft.h" + +int net_get_fd(int sock, int fd, int size) +{ + int ack; + char msg[size]; + + ack = htons(ACK); + if (read(sock, msg, size) < 0) + return (-1); + if (write(sock, (char*)&ack, sizeof(ack)) < 0) + return (-1); + if (write(fd, msg, size) < 0) + return (-1); + return (0); +} diff --git a/libft/srcs/net/net_send.c b/libft/srcs/net/net_send.c new file mode 100644 index 00000000..e4a76e0e --- /dev/null +++ b/libft/srcs/net/net_send.c @@ -0,0 +1,14 @@ +#include "libft.h" + +int net_send(int sock, char *msg, int size) +{ + int ack; + + if (write(sock, msg, size) < 0) + return (-1); + if (read(sock, (char*)&ack, sizeof(ack)) < 0) + return (-1); + if (ntohs(ack) != ACK) + return (1); + return (0); +} diff --git a/libft/srcs/sys/open_new.c b/libft/srcs/sys/open_new.c new file mode 100644 index 00000000..0dc5529e --- /dev/null +++ b/libft/srcs/sys/open_new.c @@ -0,0 +1,23 @@ +#include "libft.h" + +int open_new(char *filename, int oflag) +{ + char *fname; + int fd; + int i; + int len; + + len = ft_strlen(filename); + fname = ft_strnew(len + 4); + ft_strcpy(fname, filename); + i = 0; + while (i < 10 && (fd = open(fname, oflag | O_CREAT | O_EXCL, 0644)) < 0 + && errno == EEXIST) + { + fname[len] = '('; + fname[len + 1] = ++i + '0'; + fname[len + 2] = ')'; + } + ft_strdel(&fname); + return (fd); +}