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