new net category for server/client stuff

This commit is contained in:
Jack Halford 2017-04-05 00:44:32 +02:00
parent 9ba51ed172
commit df11d70ab4
11 changed files with 136 additions and 0 deletions

View file

@ -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

View file

@ -16,10 +16,16 @@
# include "libft.h"
# include <stdarg.h>
/*
** 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"="))

View file

@ -34,6 +34,7 @@
# include "math.h"
# include "mytime.h"
# include "get_next_line.h"
# include "net.h"
# include "sys.h"
struct s_stos

19
libft/includes/net.h Normal file
View 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

View file

@ -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);

View 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);
}

View 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
View 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);
}

View 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
View 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
View 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);
}