norme, maybe broken
This commit is contained in:
parent
4e9160f0dd
commit
3dc179ec01
5 changed files with 129 additions and 125 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2016/08/29 21:32:58 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/
|
OBJ_DIR = objs/
|
||||||
|
|
||||||
SRC_BASE = \
|
SRC_BASE = \
|
||||||
traceroute.c\
|
main.c\
|
||||||
|
traceroute.c
|
||||||
|
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/04/22 14:10:24 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -32,4 +32,6 @@ struct s_packet
|
||||||
char msg[PACKETSIZE - sizeof(struct icmp)];
|
char msg[PACKETSIZE - sizeof(struct icmp)];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void traceroute(struct sockaddr *addr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef
|
Subproject commit 2f9ee6f804e601312d28f6576e7d451f28530388
|
||||||
56
traceroute/srcs/main.c
Normal file
56
traceroute/srcs/main.c
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <addr>\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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -6,161 +6,106 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/04/22 14:10:24 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"
|
#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;
|
static int sd;
|
||||||
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;
|
|
||||||
|
|
||||||
|
sd = 0;
|
||||||
if (sd == 0)
|
if (sd == 0)
|
||||||
{
|
{
|
||||||
if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
|
if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
|
||||||
{
|
{
|
||||||
perror("socket");
|
perror("socket");
|
||||||
exit(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 5;
|
tv.tv_sec = 5;
|
||||||
tv.tv_usec = 0;
|
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);
|
bzero(buf, 1024);
|
||||||
|
return (recvfrom(sd, buf, 1024, 0, addr, NULL));
|
||||||
socklen_t len = sizeof(*addr);
|
|
||||||
return (recvfrom(sd, buf, 1024, 0, (struct sockaddr*)addr, &len));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
int bytes;
|
||||||
struct icmp *icmp;
|
|
||||||
struct s_packet *pkt;
|
|
||||||
|
|
||||||
int hlen = ip->ip_hl << 2;
|
|
||||||
pkt = (struct s_packet*)(buf + hlen);
|
|
||||||
icmp = &pkt->hdr;
|
|
||||||
return (icmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
int traceroute(struct sockaddr_in *dest)
|
|
||||||
{
|
|
||||||
int ttl;
|
|
||||||
int sd;
|
|
||||||
char addrstr[INET_ADDRSTRLEN];
|
|
||||||
unsigned char buf[1024];
|
unsigned char buf[1024];
|
||||||
|
double time;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 sd;
|
||||||
|
int ttl;
|
||||||
struct s_packet pkt;
|
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)
|
if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
|
||||||
{
|
{
|
||||||
perror("socket");
|
perror("socket");
|
||||||
return (1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) */
|
g_pid = getpid();
|
||||||
/* perror("Request non blocking IO"); */
|
|
||||||
for (ttl = 1; ttl < 255; 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));
|
bzero(&pkt, sizeof(pkt));
|
||||||
pkt.hdr.icmp_type = ICMP_ECHO;
|
pkt.hdr.icmp_type = ICMP_ECHO;
|
||||||
pkt.hdr.icmp_id = g_pid;
|
pkt.hdr.icmp_id = g_pid;
|
||||||
pkt.hdr.icmp_seq = (ttl - 1) * 3 + probe;
|
ttl = 0;
|
||||||
pkt.hdr.icmp_cksum = checksum(&pkt, sizeof(pkt));
|
while (++ttl < 255)
|
||||||
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(" *");
|
ft_printf("%2d ", ttl);
|
||||||
fflush(stdout);
|
if (setsockopt(sd, 0, IP_TTL, &ttl, sizeof(ttl)) != 0)
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
timersub(&t2, &t1, &trip);
|
|
||||||
diff = (trip.tv_sec + trip.tv_usec / 1000000.0) * 1000.0;
|
|
||||||
if (!addrshow)
|
|
||||||
{
|
{
|
||||||
char hostname[1024];
|
perror("set TTL option");
|
||||||
char servname[20];
|
|
||||||
|
|
||||||
if (getnameinfo((struct sockaddr*)&addr, sizeof(addr), hostname, sizeof(hostname), servname, sizeof(servname), 0))
|
|
||||||
{
|
|
||||||
perror("getnameinfo");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
printf(" %s (%s)", hostname, inet_ntop(AF_INET, &(addr.sin_addr), addrstr, INET_ADDRSTRLEN)); addrshow = 1;
|
pkt.hdr.icmp_seq = (ttl - 1) * 3;
|
||||||
}
|
if (probe_depth(sd, addr, pkt))
|
||||||
printf(" %.3f ms", diff);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
struct icmp *icmp = get_icmp(buf);
|
|
||||||
if (icmp->icmp_type == ICMP_ECHOREPLY)
|
|
||||||
break ;
|
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 <addr>\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));
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue