add
This commit is contained in:
19
src/checksum.c
Normal file
19
src/checksum.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
uint16_t checksum(const uint16_t *data, int len)
|
||||||
|
{
|
||||||
|
uint32_t sum = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (len > 1)
|
||||||
|
{
|
||||||
|
sum += data[i++];
|
||||||
|
len -= 2;
|
||||||
|
}
|
||||||
|
if (len == 1)
|
||||||
|
sum += ((uint8_t *)data)[i];
|
||||||
|
|
||||||
|
while (sum >> 16)
|
||||||
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||||
|
return (~sum);
|
||||||
|
}
|
5
src/checksum.h
Normal file
5
src/checksum.h
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
uint16_t checksum(const uint16_t *data, int len);
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
void set_default_config(struct config *conf)
|
void set_default_config(struct config *conf)
|
||||||
{
|
{
|
||||||
|
conf->data_size = 56;
|
||||||
conf->count = 5;
|
conf->count = 5;
|
||||||
conf->flood = false;
|
conf->flood = false;
|
||||||
conf->interval = 1;
|
conf->interval = 1;
|
||||||
|
@ -11,6 +11,7 @@ struct config {
|
|||||||
uint64_t count;
|
uint64_t count;
|
||||||
uint64_t interval;
|
uint64_t interval;
|
||||||
uint64_t time_to_live;
|
uint64_t time_to_live;
|
||||||
|
uint64_t data_size;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
22
src/data.c
Normal file
22
src/data.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int get_data(char *payload, uint64_t payload_size)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int red;
|
||||||
|
|
||||||
|
fd = open("/dev/random", O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
red = read(fd, payload, payload_size);
|
||||||
|
if (red >= 0 && (uint64_t) red == payload_size)
|
||||||
|
{
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
5
src/data.h
Normal file
5
src/data.h
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int get_data(char *payload, uint64_t payload_size);
|
41
src/dns.c
Normal file
41
src/dns.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
int dns_lookup(char *ip_addr, const char *hostname,
|
||||||
|
struct sockaddr_in *addr_con)
|
||||||
|
{
|
||||||
|
struct hostent *host = gethostbyname2(hostname, AF_INET);
|
||||||
|
if (!host) {
|
||||||
|
dprintf(2,
|
||||||
|
"Hostname '%s' doesn't exist or has invalid format.\n",
|
||||||
|
hostname);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
strcpy(ip_addr, inet_ntoa(*(struct in_addr *)host->h_addr));
|
||||||
|
(*addr_con).sin_family = host->h_addrtype;
|
||||||
|
(*addr_con).sin_port = htons(0);
|
||||||
|
(*addr_con).sin_addr.s_addr = *(long *)host->h_addr;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int reverse_dns_lookup(char *ip_addr, char *host)
|
||||||
|
{
|
||||||
|
struct sockaddr_in tmp_addr;
|
||||||
|
|
||||||
|
tmp_addr.sin_family = AF_INET;
|
||||||
|
tmp_addr.sin_addr.s_addr = inet_addr(ip_addr);
|
||||||
|
if (getnameinfo((struct sockaddr *)&tmp_addr,
|
||||||
|
sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0,
|
||||||
|
NI_NAMEREQD)) {
|
||||||
|
dprintf(2, "Could not resolve reverse lookup of %s\n", ip_addr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
5
src/dns.h
Normal file
5
src/dns.h
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
int dns_lookup(const char *host, struct sockaddr_in *addr);
|
58
src/main.c
58
src/main.c
@ -1,16 +1,20 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "dns.h"
|
||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
#include <netinet/ip_icmp.h>
|
#include <netinet/ip_icmp.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
|
|
||||||
static void signal_handler(int code)
|
static void signal_handler(int code)
|
||||||
@ -51,38 +55,17 @@ int init_socket(const struct config *conf)
|
|||||||
return sock_fd;
|
return sock_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t checksum(const uint16_t *data, int len)
|
|
||||||
|
|
||||||
|
int send_packet(int sock_fd, struct in_addr *dest, void *packet, uint64_t packet_size)
|
||||||
{
|
{
|
||||||
uint32_t sum = 0;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
while (len > 1)
|
|
||||||
{
|
|
||||||
sum += data[i++];
|
|
||||||
len -= 2;
|
|
||||||
}
|
|
||||||
if (len == 1)
|
|
||||||
sum += ((uint8_t *)data)[i];
|
|
||||||
|
|
||||||
while (sum >> 16)
|
|
||||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
||||||
return (~sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
int send_packet(int sock_fd, const char* host, uint64_t seq, const struct config *conf)
|
|
||||||
{
|
|
||||||
(void) conf;
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
inet_aton(host, &addr.sin_addr);
|
|
||||||
|
|
||||||
struct icmphdr icmp_hdr;
|
memset(&addr, 0, sizeof(struct sockaddr_in));
|
||||||
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
addr.sin_family = AF_INET;
|
||||||
icmp_hdr.type = ICMP_ECHO;
|
addr.sin_addr = *dest;
|
||||||
icmp_hdr.un.echo.id = getpid();
|
|
||||||
icmp_hdr.un.echo.sequence = seq;
|
|
||||||
icmp_hdr.checksum = checksum((uint16_t*)&icmp_hdr, sizeof(icmp_hdr));
|
|
||||||
|
|
||||||
if (sendto(sock_fd, &icmp_hdr, sizeof(icmp_hdr), 0, (struct sockaddr*)&addr, sizeof(addr)) == -1)
|
if (sendto(sock_fd, packet, packet_size, 0, (struct sockaddr*)&addr, sizeof(addr)) == -1)
|
||||||
{
|
{
|
||||||
dprintf(2, "ft_printf: failed to send packet\n");
|
dprintf(2, "ft_printf: failed to send packet\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -109,34 +92,43 @@ int recv_packet(int sock_fd, const char *host, uint64_t seq, const struct config
|
|||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
|
struct config conf;
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
|
||||||
(void)ac;
|
(void)ac;
|
||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
struct config conf;
|
|
||||||
set_default_config(&conf);
|
set_default_config(&conf);
|
||||||
const char *host = parsing((const char * const *) av, &conf);
|
const char *host = parsing((const char * const *) av, &conf);
|
||||||
if (host == NULL)
|
if (host == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
if (dns_lookup(host, &addr))
|
||||||
|
return 1;
|
||||||
|
|
||||||
const int sock_fd = init_socket(&conf);
|
const int sock_fd = init_socket(&conf);
|
||||||
if (sock_fd == -1)
|
if (sock_fd == -1)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
static uint64_t seq = 1;
|
char ipstr[INET6_ADDRSTRLEN];
|
||||||
|
inet_ntop(AF_INET, &addr, ipstr, sizeof ipstr);
|
||||||
|
printf("IP: %s\n", ipstr);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
|
||||||
while (loop)
|
while (loop)
|
||||||
{
|
{
|
||||||
if (send_packet(sock_fd, host, seq, &conf))
|
if (send_packet(sock_fd, NULL, NULL, 0))
|
||||||
{
|
{
|
||||||
close(sock_fd);
|
close(sock_fd);
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
if (recv_packet(sock_fd, host, seq, &conf))
|
if (recv_packet(sock_fd, host, 0, &conf))
|
||||||
{
|
{
|
||||||
close(sock_fd);
|
close(sock_fd);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
seq++;
|
|
||||||
sleep(conf.interval);
|
sleep(conf.interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
47
src/packet.c
Normal file
47
src/packet.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "packet.h"
|
||||||
|
#include "checksum.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "data.h"
|
||||||
|
#include "data.h"
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <netinet/ip_icmp.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
uint64_t get_packet_size(uint64_t data_size)
|
||||||
|
{
|
||||||
|
return + sizeof(struct icmphdr) + data_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *create_packet(uint64_t data_size)
|
||||||
|
{
|
||||||
|
static uint32_t seq = 1;
|
||||||
|
struct icmphdr *icmp_hdr;
|
||||||
|
char *data;
|
||||||
|
byte *packet;
|
||||||
|
|
||||||
|
packet = malloc(get_packet_size(data_size));
|
||||||
|
if (packet == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset(packet, 0, get_packet_size(data_size));
|
||||||
|
icmp_hdr = (void *) packet;
|
||||||
|
data = (char*) packet + sizeof(struct icmphdr);
|
||||||
|
|
||||||
|
if (get_data(data, data_size))
|
||||||
|
{
|
||||||
|
free(packet);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
icmp_hdr->type = ICMP_ECHO;
|
||||||
|
icmp_hdr->un.echo.id = getpid();
|
||||||
|
icmp_hdr->un.echo.sequence = seq;
|
||||||
|
icmp_hdr->checksum = checksum((uint16_t*)icmp_hdr, get_packet_size(data_size));
|
||||||
|
|
||||||
|
seq++;
|
||||||
|
|
||||||
|
return packet;
|
||||||
|
}
|
7
src/packet.h
Normal file
7
src/packet.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef uint8_t byte;
|
||||||
|
|
||||||
|
uint64_t get_packet_size(uint64_t payload_size);
|
Reference in New Issue
Block a user