add: implement signal

This commit is contained in:
2025-10-29 09:42:53 -05:00
parent e67dcbb34b
commit 2038f5364f

View File

@ -1,3 +1,4 @@
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@ -12,6 +13,9 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <stdbool.h>
bool loop = true;
void print_err(const char *format, ...)
{
@ -134,6 +138,12 @@ int packet_check(char *packet, size_t packet_size)
return tmp - checksum_bak;
}
void signal_handler(int code)
{
(void) code;
loop = false;
}
int main(int ac, char **av)
{
(void) ac;
@ -141,6 +151,8 @@ int main(int ac, char **av)
size_t payload_size = 48;
size_t packet_size = sizeof(struct icmphdr) + payload_size;
signal(SIGINT, signal_handler);
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd == -1)
@ -174,7 +186,7 @@ int main(int ac, char **av)
struct sockaddr_in sender;
socklen_t len = sizeof(sender);
while (1) {
while (loop) {
struct timeval stop, start;