ft_ping/parsing/sources/target.c

49 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* target.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/30 12:09:48 by rparodi #+# #+# */
/* Updated: 2026/03/30 15:42:55 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "macro.h"
#include "struct.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
char *_ip_addr(struct addrinfo *res) {
char ip_str[INET_ADDRSTRLEN];
struct sockaddr_in *ip = (struct sockaddr_in *)res->ai_addr;
inet_ntop(AF_INET, &ip->sin_addr, ip_str, sizeof(ip_str));
return strdup(ip_str);
}
void check_target(t_flags *flags, char *target) {
struct addrinfo hints, *res;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMP;
int err = getaddrinfo(target, NULL, &hints, &res);
if (err != 0) {
ERROR_LOG("ft_ping: unknown host");
exit(EXIT_FAILURE);
}
flags->input = target;
flags->destination = _ip_addr(res);
freeaddrinfo(res);
}