fix: timeout changed to 2s + invalid free fix when the socket creation fails

This commit is contained in:
2025-06-16 01:16:16 +02:00
parent ddbaef1826
commit ff89de7fdc
5 changed files with 12 additions and 17 deletions

View File

@ -16,7 +16,7 @@ obj/%.o: src/%.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
$(NAME): $(OBJ) $(NAME): $(OBJ)
$(LD) $(LDFLAGS) -o $(NAME) $(OBJ) $(LD) -o $(NAME) $(OBJ) $(LDFLAGS)
clean: clean:
rm -rf obj rm -rf obj

View File

@ -7,7 +7,7 @@
#include "scan.h" #include "scan.h"
#define TIMEOUT 5 #define TIMEOUT 2
typedef enum { typedef enum {
CLOSED, CLOSED,

View File

@ -74,14 +74,10 @@ static void *create_packet(const struct scan *data, size_t packet_size)
iphdr->check = checksum(packet, packet_size); iphdr->check = checksum(packet, packet_size);
// this is starnakin stuff if (isudp)
switch ((int)isudp) {
case true:
create_udp_packet(packet + sizeof(struct iphdr), data); create_udp_packet(packet + sizeof(struct iphdr), data);
break; else
default:
create_tcp_packet(packet + sizeof(struct iphdr), data); create_tcp_packet(packet + sizeof(struct iphdr), data);
}
return packet; return packet;
} }
@ -98,12 +94,6 @@ int send_packet(const struct scan *data, int sockfd)
: sizeof(struct tcphdr)); : sizeof(struct tcphdr));
void *packet = create_packet(data, packet_size); void *packet = create_packet(data, packet_size);
int one = 1;
const int *val = &one;
if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
perror("Error setting IP_HDRINCL");
return -1;
}
sendto(sockfd, packet, packet_size, 0, (struct sockaddr *)&conn_addr, sendto(sockfd, packet, packet_size, 0, (struct sockaddr *)&conn_addr,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));

View File

@ -64,6 +64,13 @@ int scan(struct scan *data)
return -1; return -1;
} }
int one = 1;
const int *val = &one;
if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
perror("Error setting IP_HDRINCL");
return -1;
}
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 net, mask; bpf_u_int32 net, mask;

View File

@ -25,11 +25,9 @@ void *routine(void *p_data)
scan_data.port_end = port; scan_data.port_end = port;
scan_data.responses = scan_data.responses =
&thread_data->responses[port - thread_data->port_start]; &thread_data->responses[port - thread_data->port_start];
if (scan(&scan_data)) { if (scan(&scan_data))
free(p_data);
return NULL; return NULL;
} }
}
return NULL; return NULL;
} }