diff --git a/libft/Makefile b/libft/Makefile index 6b54b9eb..8086a31a 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -134,11 +134,12 @@ mem/ft_realloc.c\ net/cksum.c\ net/create_client.c\ net/create_server.c\ -net/forge_ip.c\ -net/forge_tcp.c\ +net/host.c\ +net/ip.c\ net/net_get.c\ net/net_send.c\ net/reserve_port.c\ +net/tcp.c\ path/ft_path_notdir.c\ printing/ft_putchar.c\ printing/ft_putendl.c\ diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 30bc5d56..586112bf 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/libft/includes/lst.h b/libft/includes/lst.h index 17b91150..8188f934 100644 --- a/libft/includes/lst.h +++ b/libft/includes/lst.h @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/libft/includes/net.h b/libft/includes/net.h index b69b7e51..b568ceba 100644 --- a/libft/includes/net.h +++ b/libft/includes/net.h @@ -6,7 +6,7 @@ /* 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" +/* +** utilities +*/ + +int reserve_port(int *port); 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_client(char *addr, int port, char *protoname); void listener(int domain, int sock, int proto, void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); +/* +** lazy framing +*/ int net_send(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_fd(int sock, int fd, int size); 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 diff --git a/libft/includes/rs.h b/libft/includes/rs.h index 0296bbdb..ad7ddd74 100644 --- a/libft/includes/rs.h +++ b/libft/includes/rs.h @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/libft/srcs/net/host.c b/libft/srcs/net/host.c new file mode 100644 index 00000000..34ec7909 --- /dev/null +++ b/libft/srcs/net/host.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* host.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft/srcs/net/forge_ip.c b/libft/srcs/net/ip.c similarity index 81% rename from libft/srcs/net/forge_ip.c rename to libft/srcs/net/ip.c index c40ceeac..85bd9213 100644 --- a/libft/srcs/net/forge_ip.c +++ b/libft/srcs/net/ip.c @@ -6,13 +6,13 @@ /* 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" -void iphdr_init(struct ip *header) +void ip_hdrinit(struct ip *header) { memset(header, 0, sizeof(*header)); 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_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)); +} diff --git a/libft/srcs/net/forge_tcp.c b/libft/srcs/net/tcp.c similarity index 91% rename from libft/srcs/net/forge_tcp.c rename to libft/srcs/net/tcp.c index b0c84dcb..83c0d746 100644 --- a/libft/srcs/net/forge_tcp.c +++ b/libft/srcs/net/tcp.c @@ -6,13 +6,13 @@ /* 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" -void tcphdr_init(struct tcphdr *header) +void tcp_hdrinit(struct tcphdr *header) { memset(header, 0, sizeof(*header)); header->th_sport = htons(0);