From 5cd2bf7039cf6d252bca101dade53882794d6674 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 23 Apr 2017 18:19:00 +0200 Subject: [PATCH] first commit --- nmap/.gitmodules | 3 + nmap/Makefile | 93 +++++++++++++++++++++++++ nmap/includes/ft_ping.h | 33 +++++++++ nmap/libft | 1 + nmap/srcs/ping.c | 146 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+) create mode 100644 nmap/.gitmodules create mode 100644 nmap/Makefile create mode 100644 nmap/includes/ft_ping.h create mode 160000 nmap/libft create mode 100644 nmap/srcs/ping.c diff --git a/nmap/.gitmodules b/nmap/.gitmodules new file mode 100644 index 00000000..c82793fb --- /dev/null +++ b/nmap/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = https://github.com/jzck/libft diff --git a/nmap/Makefile b/nmap/Makefile new file mode 100644 index 00000000..8023554f --- /dev/null +++ b/nmap/Makefile @@ -0,0 +1,93 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: wescande +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2016/08/29 21:32:58 by wescande #+# #+# # +# Updated: 2017/04/22 19:26:34 by jhalford ### ########.fr # +# # +# **************************************************************************** # + +NAME = ft_ping + +CC = gcc +W_FLAGS = -Wall -Wextra -Werror +D_FLAGS = +FLAGS = $(W_FLAGS) $(D_FLAGS) + +LEN_NAME = `printf "%s" $(NAME) |wc -c` +DELTA = $$(echo "$$(tput cols)-31-$(LEN_NAME)"|bc) + +LIBFT_DIR = libft/ +LIBFT_LIB = $(LIBFT_DIR)libft.a +LIBFT_INC = $(LIBFT_DIR)includes/ + +SRC_DIR = srcs/ +INC_DIR = includes/ +OBJ_DIR = objs/ + +SRC_BASE = \ +ping.c\ + +SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) +OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) +NB = $(words $(SRC_BASE)) +INDEX = 0 + +all : + @make -C $(LIBFT_DIR) + @make -j $(NAME) + +$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ) + @$(CC) $(OBJS) -o $@ \ + -I $(INC_DIR) \ + -I $(LIBFT_INC) \ + $(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) + @printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n" + +$(LIBFT_LIB): + @make -C $(LIBFT_DIR) + +$(OBJ_DIR) : + @mkdir -p $(OBJ_DIR) + +$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) + @$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB))))) + @$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB))))) + @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB) - 1)))) + @$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}")) + @printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) ft_p $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')" + @$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\ + -I $(INC_DIR)\ + -I $(LIBFT_INC) + @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) + +clean: cleanlib + @if [ -e $(OBJ_DIR) ]; then \ + rm -rf $(OBJ_DIR); \ + printf "\r\033[38;5;202m✗ clean $(NAME) \033[0m\033[K\n"; \ + fi; + +cleanlib: + @make -C $(LIBFT_DIR) clean + +fclean: clean fcleanlib + @for file in $(NAME); do \ + if [ -e $$file ]; then \ + rm -f $$file ; \ + printf "\r\033[38;5;196m✗ fclean $$file\033[0m\033[K\n"; \ + fi; \ + done; + +fcleanlib: cleanlib + @make -C $(LIBFT_DIR) fclean + +re: fclean all + +relib: fcleanlib $(LIBFT_LIB) + +.PHONY : fclean clean re relib cleanlib fcleanlib + +-include $(OBJS:.o=.d) diff --git a/nmap/includes/ft_ping.h b/nmap/includes/ft_ping.h new file mode 100644 index 00000000..9c727acf --- /dev/null +++ b/nmap/includes/ft_ping.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ping.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */ +/* Updated: 2017/04/22 15:52:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PING_H +# define FT_PING_H + +# include "libft.h" +# include +# include +# include +# include +# include +# include +# include +# include + +#define PACKETSIZE 64 +struct s_packet +{ + struct icmp icmp; + char msg[PACKETSIZE-sizeof(struct icmp)]; +}; + +#endif diff --git a/nmap/libft b/nmap/libft new file mode 160000 index 00000000..61ecc913 --- /dev/null +++ b/nmap/libft @@ -0,0 +1 @@ +Subproject commit 61ecc913bbc31dafab3fff2386e1cbfffd34596a diff --git a/nmap/srcs/ping.c b/nmap/srcs/ping.c new file mode 100644 index 00000000..a40cee6e --- /dev/null +++ b/nmap/srcs/ping.c @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */ +/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_ping.h" + +int g_pid=-1; + +unsigned short checksum(void *b, int len) +{ + unsigned short *buf = b; + unsigned int sum=0; + unsigned short result; + + for (sum = 0; len > 1; len -= 2) + sum += *buf++; + if (len == 1) + sum += *(unsigned char*)buf; + sum = (sum >> 16) + (sum & 0xFFFF); + sum += (sum >> 16); + result = ~sum; + return (result); +} + +void display(void *buf, int bytes, struct sockaddr_in *addr) +{ + int i; + struct ip *ip = buf; + struct icmp *icmp = buf + ip->ip_hl*4; + + printf("%d bytes from %s: ttl=%i\n", + ip->ip_len, inet_ntoa(addr->sin_addr), ip->ip_ttl); + printf("IPv%d: hrd-size=%d, pkt-size=%d, id=%d, frag-off=%d, protocol=%c\n, ttl=%i", + ip->ip_v, ip->ip_hl, ip->ip_len, ip->ip_id, ip->ip_off, ip->ip_p, ip->ip_ttl); + if (icmp->icmp_hun.ih_idseq.icd_id == g_pid) + { + printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n", + icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum), + icmp->icmp_hun.ih_idseq.icd_id, icmp->icmp_hun.ih_idseq.icd_seq); + (void)bytes; + } + for (i=0; i < bytes; i++) + { + if ( !(i & 15) ) printf("\n%i: ", i); + printf("%c ", ((char*)buf)[i]); + } + printf("\n"); +} + +void listener(struct sockaddr_in *addr) +{ + int sd; + struct sockaddr_in r_addr; + int bytes; + socklen_t len; + unsigned char buf[1024]; + + if ((sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)) < 0) + { + perror("socket"); + exit(0); + } + for (;;) + { + bzero(buf, sizeof(buf)); + len = sizeof(r_addr); + bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&r_addr, &len); + if (bytes > 0) + display(buf, bytes, addr); + else + perror("recvfrom"); + } +} + +void ping(struct sockaddr_in *addr) +{ + const int val; + int i; + int sd; + int cnt; + socklen_t len; + struct s_packet pkt; + struct sockaddr_in r_addr; + + if ((sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)) < 0) + { + perror("socket"); + return ; + } + if (setsockopt(sd, 0, IP_TTL, &val, sizeof(val)) != 0) + perror("set TTL option"); + if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) + perror("Request non blocking IO"); + cnt = 1; + while (1) + { + len = sizeof(r_addr); + recvfrom(sd, &pkt, sizeof(pkt), 0, (struct sockaddr*)&r_addr, &len); + bzero(&pkt, sizeof(pkt)); + pkt.icmp.icmp_type = ICMP_ECHO; + pkt.icmp.icmp_hun.ih_idseq.icd_id = g_pid; + for (i=0; i < (int)sizeof(pkt.msg); i++) + pkt.msg[i] = i+'0'; + pkt.msg[i] = 0; + pkt.icmp.icmp_hun.ih_idseq.icd_seq = cnt++; + pkt.icmp.icmp_cksum = checksum(&pkt, sizeof(pkt)); + if (sendto(sd, &pkt, sizeof(pkt), 0, (struct sockaddr*)addr, sizeof(*addr)) <= 0) + perror("sendto"); + sleep(1); + } +} + +int main(int ac, char **av) +{ + struct sockaddr_in *addr; + struct addrinfo *result; + + if (ac != 2) + { + printf("usage: %s \n", av[0]); + exit(1); + } + if (getaddrinfo(av[1], NULL, NULL, &result) != 0) + { + perror("getaddrinfo"); + exit(1); + } + addr = (struct sockaddr_in*)result->ai_addr; + if (fork() == 0) + { + listener(addr); + exit(0); + } + else + ping(addr); + wait(0); + return (0); +}