From 3dc179ec01c0862ff3ed56f1cea77637c26202d1 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 8 Oct 2017 17:55:38 +0200 Subject: [PATCH] norme, maybe broken --- traceroute/Makefile | 5 +- traceroute/includes/traceroute.h | 10 +- traceroute/libft | 2 +- traceroute/srcs/main.c | 56 ++++++++++ traceroute/srcs/traceroute.c | 181 +++++++++++-------------------- 5 files changed, 129 insertions(+), 125 deletions(-) create mode 100644 traceroute/srcs/main.c diff --git a/traceroute/Makefile b/traceroute/Makefile index f3c02b04..fb4b340a 100644 --- a/traceroute/Makefile +++ b/traceroute/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/04/22 19:26:34 by jhalford ### ########.fr # +# Updated: 2017/10/08 17:53:44 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -30,7 +30,8 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ -traceroute.c\ +main.c\ +traceroute.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/traceroute/includes/traceroute.h b/traceroute/includes/traceroute.h index ba996d9a..b5538052 100644 --- a/traceroute/includes/traceroute.h +++ b/traceroute/includes/traceroute.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */ -/* Updated: 2017/04/22 15:52:07 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 17:52:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,10 +26,12 @@ #define PACKETSIZE 56 -struct s_packet +struct s_packet { - struct icmp hdr; - char msg[PACKETSIZE - sizeof(struct icmp)]; + struct icmp hdr; + char msg[PACKETSIZE - sizeof(struct icmp)]; }; +void traceroute(struct sockaddr *addr); + #endif diff --git a/traceroute/libft b/traceroute/libft index dd612f58..2f9ee6f8 160000 --- a/traceroute/libft +++ b/traceroute/libft @@ -1 +1 @@ -Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef +Subproject commit 2f9ee6f804e601312d28f6576e7d451f28530388 diff --git a/traceroute/srcs/main.c b/traceroute/srcs/main.c new file mode 100644 index 00000000..2781263e --- /dev/null +++ b/traceroute/srcs/main.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/08 17:18:05 by jhalford #+# #+# */ +/* Updated: 2017/10/08 17:55:15 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "traceroute.h" + +int g_pid = -1; + +struct addrinfo *resolve_host(char *hostname, char ip[]) +{ + struct addrinfo *result; + struct addrinfo hints; + struct sockaddr_in *addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_CANONNAME; + if (getaddrinfo(hostname, NULL, &hints, &result) != 0) + { + perror("getaddrinfo"); + exit(1); + } + addr = (struct sockaddr_in*)result->ai_addr; + inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN); + return (result); +} + +int main(int ac, char **av) +{ + struct addrinfo *result; + char ip[INET_ADDRSTRLEN]; + + if (ac != 2) + { + ft_usage("%s \n", av[0]); + return (1); + } + result = resolve_host(av[1], ip); + if (result->ai_next) + printf("traceroute: Warning: %s has multiple addresses; using %s\n", + result->ai_canonname, ip); + printf("traceroute to %s (%s), %i hops max %i byte packets\n", + FT_TRY(result->ai_canonname, ip), ip, 64, PACKETSIZE); + traceroute(result->ai_addr); + return (0); +} + diff --git a/traceroute/srcs/traceroute.c b/traceroute/srcs/traceroute.c index 5f341a79..940e1400 100644 --- a/traceroute/srcs/traceroute.c +++ b/traceroute/srcs/traceroute.c @@ -6,161 +6,106 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */ -/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 17:55:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "traceroute.h" -int g_pid=-1; +int g_pid; -unsigned short checksum(void *b, int len) +static int wait_for_reply(struct sockaddr *addr, unsigned char buf[1024]) { - unsigned short *buf = b; - unsigned int sum=0; - - for (sum = 0; len > 1; len -= 2) - sum += *buf++; - if (len == 1) - sum += *(unsigned char*)buf; - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - return (~sum); -} - -int wait_for_reply(struct sockaddr_in *addr, unsigned char buf[1024]) -{ - static int sd = 0; + static int sd; + sd = 0; if (sd == 0) { if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("socket"); - exit(0); + exit(1); } struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; - setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(struct timeval)); + setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, + (const char*)&tv, sizeof(struct timeval)); } bzero(buf, 1024); - - socklen_t len = sizeof(*addr); - return (recvfrom(sd, buf, 1024, 0, (struct sockaddr*)addr, &len)); + return (recvfrom(sd, buf, 1024, 0, addr, NULL)); } -struct icmp *get_icmp(void *buf) +static int probe_once(int sd, struct sockaddr *addr, + struct s_packet *pkt, struct icmp *icmp) { - struct ip *ip = buf; - struct icmp *icmp; - struct s_packet *pkt; + int bytes; + unsigned char buf[1024]; + double time; - int hlen = ip->ip_hl << 2; - pkt = (struct s_packet*)(buf + hlen); - icmp = &pkt->hdr; - return (icmp); + if (sendto(sd, &pkt, sizeof(pkt), 0, + addr, sizeof(*addr)) <= 0) + { + perror("sendto"); + exit(1); + } + time = time_milli(); + bytes = wait_for_reply(addr, buf); + time = time_milli() - time; + if (bytes < 0) + ft_printf(" *"); + else + { + ip_load_icmp(icmp, buf); + ft_printf(" %.3f ms", time); + } + return (0); } -int traceroute(struct sockaddr_in *dest) +static int probe_depth(int sd, struct sockaddr *addr, + struct s_packet pkt) +{ + struct icmp icmp; + int probe; + + probe = -1; + host_format(addr); + while (++probe < 3) + { + pkt.hdr.icmp_seq++; + pkt.hdr.icmp_cksum = cksum(&pkt, sizeof(pkt)); + probe_once(sd, addr, &pkt, &icmp); + } + ft_putchar('\n'); + return (icmp.icmp_type == ICMP_ECHOREPLY); +} + +void traceroute(struct sockaddr *addr) { - int ttl; int sd; - char addrstr[INET_ADDRSTRLEN]; - unsigned char buf[1024]; + int ttl; struct s_packet pkt; - struct sockaddr_in addr; - struct timeval t1, t2, trip; - double diff; if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("socket"); - return (1); + exit(1); } - /* if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) */ - /* perror("Request non blocking IO"); */ - for (ttl = 1; ttl < 255; ttl++) + g_pid = getpid(); + bzero(&pkt, sizeof(pkt)); + pkt.hdr.icmp_type = ICMP_ECHO; + pkt.hdr.icmp_id = g_pid; + ttl = 0; + while (++ttl < 255) { + ft_printf("%2d ", ttl); if (setsockopt(sd, 0, IP_TTL, &ttl, sizeof(ttl)) != 0) - perror("set TTL option"); - printf("%2d ", ttl); - fflush(stdout); - int addrshow = 0; - for (int probe = 0; probe < 3; probe++) { - bzero(&pkt, sizeof(pkt)); - pkt.hdr.icmp_type = ICMP_ECHO; - pkt.hdr.icmp_id = g_pid; - pkt.hdr.icmp_seq = (ttl - 1) * 3 + probe; - pkt.hdr.icmp_cksum = checksum(&pkt, sizeof(pkt)); - if (sendto(sd, &pkt, sizeof(pkt), 0, (struct sockaddr*)dest, sizeof(*dest)) <= 0) - perror("sendto"); - - if (gettimeofday(&t1, NULL) != 0) - return (1); - - int bytes = wait_for_reply(&addr, buf); - - if (gettimeofday(&t2, NULL) != 0) - return (1); - - if (bytes < 0) - { - printf(" *"); - fflush(stdout); - continue ; - } - timersub(&t2, &t1, &trip); - diff = (trip.tv_sec + trip.tv_usec / 1000000.0) * 1000.0; - if (!addrshow) - { - char hostname[1024]; - char servname[20]; - - if (getnameinfo((struct sockaddr*)&addr, sizeof(addr), hostname, sizeof(hostname), servname, sizeof(servname), 0)) - { - perror("getnameinfo"); - exit(1); - } - printf(" %s (%s)", hostname, inet_ntop(AF_INET, &(addr.sin_addr), addrstr, INET_ADDRSTRLEN)); addrshow = 1; - } - printf(" %.3f ms", diff); + perror("set TTL option"); + exit(1); } - printf("\n"); - struct icmp *icmp = get_icmp(buf); - if (icmp->icmp_type == ICMP_ECHOREPLY) + pkt.hdr.icmp_seq = (ttl - 1) * 3; + if (probe_depth(sd, addr, pkt)) break ; } - exit(0); -} - -int main(int ac, char **av) -{ - struct sockaddr_in *addr; - struct addrinfo *result, hints; - char addrstr[INET_ADDRSTRLEN]; - - if (ac != 2) - { - printf("usage: %s \n", av[0]); - exit(1); - } - memset (&hints, 0, sizeof (hints)); - hints.ai_family = PF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags |= AI_CANONNAME; - if (getaddrinfo(av[1], NULL, &hints, &result) != 0) - { - perror("getaddrinfo"); - exit(1); - } - addr = (struct sockaddr_in*)result->ai_addr; - inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET_ADDRSTRLEN); - g_pid = getpid(); - if (result->ai_next) - printf("traceroute: Warning: %s has multiple addresses; using %s\n", result->ai_canonname, addrstr); - printf("traceroute to %s (%s), %i hops max %i byte packets\n", - FT_TRY(result->ai_canonname, addrstr), addrstr, 64, PACKETSIZE); - exit(traceroute(addr)); }