fix: various bug fixes on allocations and stuff (still wip)
This commit is contained in:
parent
1ce702d169
commit
675248cbff
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ ft_nmap
|
||||
.cache
|
||||
compile_commands.json
|
||||
tags
|
||||
dlopen.supp
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "scan.h"
|
||||
|
||||
typedef enum {
|
||||
OPEN,
|
||||
OPEN = 1,
|
||||
CLOSE,
|
||||
FILTERED,
|
||||
UNFILTERED,
|
||||
|
@ -10,7 +10,7 @@ int dns_lookup(char *ip_addr, 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.",
|
||||
dprintf(2, "Hostname %s doesn't exist or has invalid format.\n",
|
||||
hostname);
|
||||
return -1;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <pcap.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dns.h"
|
||||
#include "interface.h"
|
||||
@ -29,7 +30,7 @@ int main(int ac, char **av)
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct response responses[1024];
|
||||
struct response responses[1024] = {0};
|
||||
|
||||
struct thread *single_thread = malloc(sizeof(struct thread));
|
||||
if (get_interface_name(&single_thread->host) < 0)
|
||||
|
@ -15,7 +15,6 @@ void create_udp_packet(struct udphdr *udphdr, const struct scan *data)
|
||||
udphdr->source = htons(1234);
|
||||
udphdr->dest = htons(data->port);
|
||||
udphdr->len = sizeof(struct udphdr);
|
||||
udphdr->check = 0;
|
||||
udphdr->check = checksum(udphdr, sizeof(struct udphdr));
|
||||
}
|
||||
|
||||
@ -23,17 +22,13 @@ int create_tcp_packet(struct tcphdr *tcphdr, const struct scan *data)
|
||||
{
|
||||
tcphdr->source = htons(1234);
|
||||
tcphdr->dest = htons(data->port);
|
||||
tcphdr->seq = 0;
|
||||
tcphdr->ack_seq = 0;
|
||||
tcphdr->doff = sizeof(struct tcphdr) / sizeof(int);
|
||||
tcphdr->fin = data->type == SCAN_XMAS || data->type == SCAN_FIN;
|
||||
tcphdr->syn = data->type == SCAN_SYN;
|
||||
tcphdr->rst = 0;
|
||||
tcphdr->psh = data->type == SCAN_XMAS;
|
||||
tcphdr->ack = data->type == SCAN_ACK;
|
||||
tcphdr->urg = data->type == SCAN_XMAS;
|
||||
tcphdr->window = htons(5840);
|
||||
tcphdr->check = 0;
|
||||
tcphdr->urg_ptr = 0;
|
||||
|
||||
struct pshdr pshdr;
|
||||
@ -58,7 +53,7 @@ int create_tcp_packet(struct tcphdr *tcphdr, const struct scan *data)
|
||||
static void *create_packet(const struct scan *data, size_t packet_size)
|
||||
{
|
||||
const bool isudp = data->type == SCAN_UDP;
|
||||
void *packet = malloc(packet_size);
|
||||
void *packet = calloc(packet_size, 1);
|
||||
if (!packet) {
|
||||
dprintf(2,
|
||||
"ft_nmap: allocation failed during packet creation\n");
|
||||
@ -74,7 +69,6 @@ static void *create_packet(const struct scan *data, size_t packet_size)
|
||||
iphdr->frag_off = 0;
|
||||
iphdr->ttl = 48;
|
||||
iphdr->protocol = isudp ? IPPROTO_UDP : IPPROTO_TCP;
|
||||
iphdr->check = 0;
|
||||
iphdr->saddr = inet_addr(data->host->ip);
|
||||
iphdr->daddr = inet_addr(data->dest_addr);
|
||||
|
||||
@ -96,6 +90,7 @@ int send_packets(const struct scan *data, int sockfd)
|
||||
{
|
||||
struct sockaddr_in conn_addr;
|
||||
conn_addr.sin_family = AF_INET;
|
||||
conn_addr.sin_port = htons(80);
|
||||
conn_addr.sin_addr.s_addr = inet_addr(data->dest_addr);
|
||||
|
||||
size_t packet_size = sizeof(struct iphdr) +
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "packet.h"
|
||||
#include "response.h"
|
||||
@ -89,10 +90,11 @@ int scan(const struct scan *data)
|
||||
|
||||
send_packets(data, sockfd);
|
||||
|
||||
// TODO test with another cnt value
|
||||
if (pcap_dispatch(handle, 10, dispatch_callback, (u_char *)data)) {
|
||||
;
|
||||
if (!pcap_dispatch(handle, 1, dispatch_callback, (u_char *)data)) {
|
||||
printf("timeout\n");
|
||||
}
|
||||
pcap_close(handle);
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user