new net category for server/client stuff
This commit is contained in:
parent
9ba51ed172
commit
df11d70ab4
11 changed files with 136 additions and 0 deletions
|
|
@ -128,6 +128,11 @@ mem/ft_memdel.c\
|
||||||
mem/ft_memmove.c\
|
mem/ft_memmove.c\
|
||||||
mem/ft_memset.c\
|
mem/ft_memset.c\
|
||||||
mem/ft_realloc.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\
|
path/ft_path_notdir.c\
|
||||||
printing/ft_putchar.c\
|
printing/ft_putchar.c\
|
||||||
printing/ft_putendl.c\
|
printing/ft_putendl.c\
|
||||||
|
|
@ -196,6 +201,7 @@ sys/ft_xattr_count.c\
|
||||||
sys/ft_xattr_print.c\
|
sys/ft_xattr_print.c\
|
||||||
sys/is_directory.c\
|
sys/is_directory.c\
|
||||||
sys/open_access.c\
|
sys/open_access.c\
|
||||||
|
sys/open_new.c\
|
||||||
time/ft_mytime_free.c\
|
time/ft_mytime_free.c\
|
||||||
time/ft_mytime_get.c\
|
time/ft_mytime_get.c\
|
||||||
time/ft_time_isrecent.c
|
time/ft_time_isrecent.c
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,16 @@
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
# include <stdarg.h>
|
# include <stdarg.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** DEBUG with no malloc
|
||||||
|
*/
|
||||||
# define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}"
|
# define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}"
|
||||||
# define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__
|
# 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(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
** DEBUG with no malloc
|
||||||
|
*/
|
||||||
# define DG2 ft_putstr(__FILE__"\t");ft_putnbr(__LINE__)
|
# define DG2 ft_putstr(__FILE__"\t");ft_putnbr(__LINE__)
|
||||||
# define DGW(d) DG2;d;ft_putchar('\n')
|
# define DGW(d) DG2;d;ft_putchar('\n')
|
||||||
# define DGS(s) DGW(ft_putstr(": "s"="))
|
# define DGS(s) DGW(ft_putstr(": "s"="))
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@
|
||||||
# include "math.h"
|
# include "math.h"
|
||||||
# include "mytime.h"
|
# include "mytime.h"
|
||||||
# include "get_next_line.h"
|
# include "get_next_line.h"
|
||||||
|
# include "net.h"
|
||||||
# include "sys.h"
|
# include "sys.h"
|
||||||
|
|
||||||
struct s_stos
|
struct s_stos
|
||||||
|
|
|
||||||
19
libft/includes/net.h
Normal file
19
libft/includes/net.h
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef NET_H
|
||||||
|
# define NET_H
|
||||||
|
|
||||||
|
# define ACK 2
|
||||||
|
# define NACK 3
|
||||||
|
|
||||||
|
# include <sys/socket.h>
|
||||||
|
# include <netdb.h>
|
||||||
|
# include <netinet/in.h>
|
||||||
|
# include <arpa/inet.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -34,6 +34,7 @@ int ft_xattr_count(char *path);
|
||||||
char *ft_getenv(char **env, char *key);
|
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_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);
|
int is_directory(const char *path);
|
||||||
char *create_directory(const char *path, const char *old_pathnames);
|
char *create_directory(const char *path, const char *old_pathnames);
|
||||||
|
|
||||||
|
|
|
||||||
18
libft/srcs/net/create_client.c
Normal file
18
libft/srcs/net/create_client.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
19
libft/srcs/net/create_server.c
Normal file
19
libft/srcs/net/create_server.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
13
libft/srcs/net/net_get.c
Normal file
13
libft/srcs/net/net_get.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
16
libft/srcs/net/net_get_fd.c
Normal file
16
libft/srcs/net/net_get_fd.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
14
libft/srcs/net/net_send.c
Normal file
14
libft/srcs/net/net_send.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
23
libft/srcs/sys/open_new.c
Normal file
23
libft/srcs/sys/open_new.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue