Compare commits

..

6 commits

5 changed files with 41 additions and 15 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/23 15:06:41 by rparodi #+# #+# */
/* Updated: 2026/03/24 13:30:42 by rparodi ### ########.fr */
/* Updated: 2026/03/30 15:52:18 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,10 +20,12 @@ typedef struct s_flags {
bool verbose;
bool flood;
bool quiet;
bool reverse_dns;
bool no_reverse_dns;
int payload_size;
uint64_t wait;
uint64_t timeout;
uint64_t count;
char *content;
char *destination;
char *input;
} t_flags;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/23 14:49:37 by rparodi #+# #+# */
/* Updated: 2026/03/30 12:19:17 by rparodi ### ########.fr */
/* Updated: 2026/03/30 15:48:05 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,6 +22,8 @@
# define BONUS 0
#endif
#define ICMP_PAYLOAD 56
typedef struct s_args {
char short_option;
char *long_option;
@ -35,6 +37,6 @@ extern const t_args _flags[];
size_t args_size();
uint64_t check_num_arguments(char *arg);
void check_flags(int argc, char **argv, t_flags *flags);
void check_target(const char *target);
void check_target(t_flags *flags, const char *target);
void print_help(const size_t size);
void print_usage(const size_t size);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/30 12:06:19 by rparodi #+# #+# */
/* Updated: 2026/03/30 12:40:57 by rparodi ### ########.fr */
/* Updated: 2026/03/30 14:25:18 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -93,7 +93,7 @@ void check_flags(int argc, char **argv, t_flags *flags) {
break;
case 'n':
INFO_LOG("argument no-reverse");
flags->reverse_dns = true;
flags->no_reverse_dns = true;
break;
case 'c':
INFO_LOG("argument count");

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/23 14:49:36 by rparodi #+# #+# */
/* Updated: 2026/03/30 12:22:07 by rparodi ### ########.fr */
/* Updated: 2026/03/30 15:53:23 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,8 +16,12 @@
#include <stdlib.h>
#include <getopt.h>
static const char *_find_target(int argc, char **argv) {
const char *target = NULL;
static void _init_flags(t_flags *flags) {
flags->payload_size = ICMP_PAYLOAD;
}
static char *_find_target(int argc, char **argv) {
char *target = NULL;
for (int i = optind; i < argc; i++) {
if (argv[i][0] != '-') {
@ -26,20 +30,23 @@ static const char *_find_target(int argc, char **argv) {
}
if (target == NULL || target[0] == '\0') {
dprintf(2, "%s: missing host operand \n Try 'ping --help' or 'ping --usage' for more information.", argv[0]);
ERROR_LOG("ft_ping: missing host operand \n Try 'ft_ping --help' or 'ft_ping --usage' for more information.");
exit(64);
}
return (target);
}
void parsing_args(int argc, char **argv, t_flags *flags)
{
if (argc == 1) {
dprintf(2, "%s: missing host operand \n Try 'ping --help' or 'ping --usage' for more information.", argv[0]);
ERROR_LOG("ft_ping: missing host operand \n Try 'ft_ping --help' or 'ft_ping --usage' for more information.");
exit(64);
}
_init_flags(flags);
check_flags(argc, argv, flags);
check_target(_find_target(argc, argv));
check_target(flags, _find_target(argc, argv));
SUCCESS_LOG("PARSING finished with success");
printf("FT_PING: %s (%s): %d data bytes\n", flags->input, flags->destination, flags->payload_size);
}

View file

@ -6,21 +6,34 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/30 12:09:48 by rparodi #+# #+# */
/* Updated: 2026/03/30 12:33:54 by rparodi ### ########.fr */
/* 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>
void check_target(const char *target) {
struct addrinfo hints = {0}, *res;
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;
@ -30,5 +43,7 @@ void check_target(const char *target) {
ERROR_LOG("ft_ping: unknown host");
exit(EXIT_FAILURE);
}
flags->input = target;
flags->destination = _ip_addr(res);
freeaddrinfo(res);
}