norme and net refactor

This commit is contained in:
Jack Halford 2017-10-08 16:53:06 +02:00
parent 80837f0428
commit ba333562dd
8 changed files with 74 additions and 13 deletions

View file

@ -134,11 +134,12 @@ mem/ft_realloc.c\
net/cksum.c\ net/cksum.c\
net/create_client.c\ net/create_client.c\
net/create_server.c\ net/create_server.c\
net/forge_ip.c\ net/host.c\
net/forge_tcp.c\ net/ip.c\
net/net_get.c\ net/net_get.c\
net/net_send.c\ net/net_send.c\
net/reserve_port.c\ net/reserve_port.c\
net/tcp.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\

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2017/10/08 12:56:29 by jhalford ### ########.fr */ /* Updated: 2017/10/08 15:59:49 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ /* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */
/* Updated: 2017/05/20 20:01:20 by ariard ### ########.fr */ /* Updated: 2017/10/08 16:00:12 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */ /* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */
/* Updated: 2017/10/08 14:36:35 by jhalford ### ########.fr */ /* Updated: 2017/10/08 16:52:48 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,21 +28,40 @@
# include "mytime.h" # include "mytime.h"
/*
** utilities
*/
int reserve_port(int *port);
unsigned short cksum(void *b, int len); unsigned short cksum(void *b, int len);
int host_format(struct sockaddr *addr);
/*
** lazy setup
*/
int create_server(int port, int backlog, char *protoname); int create_server(int port, int backlog, char *protoname);
int create_client(char *addr, int port, char *protoname); int create_client(char *addr, int port, char *protoname);
void listener(int domain, int sock, int proto, void listener(int domain, int sock, int proto,
void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); void (*handler)(void *buf, int bytes, struct sockaddr_in *addr));
/*
** lazy framing
*/
int net_send(int sock, char *msg, int size); int net_send(int sock, char *msg, int size);
int net_send_large(int sock, char *msg, int size); int net_send_large(int sock, char *msg, int size);
int net_get(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); int net_get_fd(int sock, int fd, int size);
int net_get_large(int sock, int fd); int net_get_large(int sock, int fd);
int reserve_port(int *port); /*
** ip
*/
void ip_hdrinit(struct ip *hdr);
void ip_load_icmp(struct icmp *icmp, void *buf);
void tcphdr_init(struct tcphdr *header); /*
void iphdr_init(struct ip *header); ** tcp
*/
void tcp_hdrinit(struct tcphdr *header);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:05:30 by jhalford #+# #+# */ /* Created: 2017/10/07 18:05:30 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:18:16 by jhalford ### ########.fr */ /* Updated: 2017/10/08 15:59:58 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

31
libft/srcs/net/host.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* host.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 15:47:45 by jhalford #+# #+# */
/* Updated: 2017/10/08 15:58:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "net.h"
int host_format(struct sockaddr *addr)
{
char dn[1024];
char sv[20];
char ip[INET_ADDRSTRLEN];
if (getnameinfo(addr, sizeof(*addr), dn, sizeof(dn),
sv, sizeof(sv), 0))
{
perror("getnameinfo");
return (1);
}
printf(" %s (%s)", dn,
inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr),
ip, INET_ADDRSTRLEN));
return (0);
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:02:39 by jhalford #+# #+# */ /* Created: 2017/10/07 18:02:39 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:18:07 by jhalford ### ########.fr */ /* Updated: 2017/10/08 15:52:39 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "net.h" #include "net.h"
void iphdr_init(struct ip *header) void ip_hdrinit(struct ip *header)
{ {
memset(header, 0, sizeof(*header)); memset(header, 0, sizeof(*header));
header->ip_v = 4; header->ip_v = 4;
@ -27,3 +27,13 @@ void iphdr_init(struct ip *header)
memset(&header->ip_src, 0, sizeof(struct in_addr)); memset(&header->ip_src, 0, sizeof(struct in_addr));
memset(&header->ip_dst, 0, sizeof(struct in_addr)); memset(&header->ip_dst, 0, sizeof(struct in_addr));
} }
void ip_load_icmp(struct icmp *icmp, void *buf)
{
struct ip *ip;
int hlen;
ip = buf;
hlen = ip->ip_hl << 2;
ft_memcpy(icmp, buf + hlen, sizeof(struct icmp));
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:02:34 by jhalford #+# #+# */ /* Created: 2017/10/07 18:02:34 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:17:04 by jhalford ### ########.fr */ /* Updated: 2017/10/08 15:54:16 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "net.h" #include "net.h"
void tcphdr_init(struct tcphdr *header) void tcp_hdrinit(struct tcphdr *header)
{ {
memset(header, 0, sizeof(*header)); memset(header, 0, sizeof(*header));
header->th_sport = htons(0); header->th_sport = htons(0);