packet forging

This commit is contained in:
Jack Halford 2017-09-01 19:29:17 +02:00
parent 6df67a60c7
commit 6908e10aab
8 changed files with 91 additions and 2 deletions

View file

@ -135,6 +135,9 @@ net/create_client.c\
net/create_server.c\
net/net_get.c\
net/net_send.c\
net/forge_tcp.c\
net/forge_ip.c\
net/reserve_port.c\
path/ft_path_notdir.c\
printing/ft_putchar.c\
printing/ft_putendl.c\
@ -201,6 +204,7 @@ str/ft_strtrim.c\
time/ft_mytime_free.c\
time/ft_mytime_get.c\
time/ft_time_isrecent.c\
time/epoch.c\
sys/open_new.c\
htb/ft_hash_string.c\
htb/hashtab_init.c\

View file

@ -12,6 +12,9 @@
#ifndef MYTIME_H
# define MYTIME_H
# include <sys/time.h>
# include "libft.h"
struct s_mytime
@ -27,8 +30,8 @@ struct s_mytime
typedef struct s_mytime t_mytime;
int ft_time_isrecent(time_t event);
t_mytime *ft_mytime_get(time_t epoch);
void ft_mytime_free(t_mytime **time);
size_t epoch_micro(void);
#endif

View file

@ -8,8 +8,12 @@
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
#include "mytime.h"
int create_server(int port, int backlog, char *protoname);
int create_client(char *addr, int port, char *protoname);
void listener(int domain, int sock, int proto, void (*handler)(void *buf, int bytes, struct sockaddr_in *addr));
@ -20,4 +24,10 @@ int net_get(int sock, char *msg, int size);
int net_get_fd(int sock, int fd, int size);
int net_get_large(int sock, int fd);
int reserve_port(int *port);
void tcphdr_init(struct tcphdr *header);
void iphdr_init(struct iphdr *header);
#endif

17
libft/srcs/net/forge_ip.c Normal file
View file

@ -0,0 +1,17 @@
#include "net.h"
void iphdr_init(struct iphdr *header)
{
memset(header, 0, sizeof(*header));
header->version = 4;
header->ihl = 5;
header->tos = 0;
header->tot_len = 0;
header->id = ntohl(epoch_micro());
header->frag_off = 0;
header->ttl = 255;
header->protocol = 0;
header->daddr = 0;
header->saddr = 0;
header->check = 0;
}

View file

@ -0,0 +1,20 @@
#include "net.h"
void tcphdr_init(struct tcphdr *header)
{
memset(header, 0, sizeof(*header));
header->source = htons(0);
header->dest = htons(0);
header->seq = epoch_micro();
header->ack_seq = 0;
header->doff = 5;
header->fin = 0;
header->syn = 0;
header->rst = 0;
header->psh = 0;
header->ack = 0;
header->urg = 0;
header->window = htons(1024);
header->check = 0;
header->urg_ptr = 0;
}

View file

@ -0,0 +1,25 @@
#include "net.h"
int reserve_port(int *port)
{
struct sockaddr_in sa;
int sockfd;
unsigned short i;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
return (1);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
i = 49152;
while (i < 65535)
{
sa.sin_port = htons(i);
if (bind(sockfd, (struct sockaddr*)&sa, sizeof(sa)) == 0)
{
*port = i;
return (0);
}
++i;
}
return (1);
}

View file

@ -36,7 +36,7 @@ void hexdump(void *pAddressIn, long lSize)
{
ucTmp = *pTmp++;
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
sprintf(szBuf + lIndex, "%02x ", (unsigned short)ucTmp);
if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
szBuf[lIndex2] = ucTmp;

10
libft/srcs/time/epoch.c Normal file
View file

@ -0,0 +1,10 @@
#include "mytime.h"
size_t epoch_micro(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
return (0);
return (tv.tv_sec * 1000000 + tv.tv_usec);
}