From 6908e10aab019a59e142c6fd92d969260d9cc054 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 1 Sep 2017 19:29:17 +0200 Subject: [PATCH] packet forging --- libft/Makefile | 4 ++++ libft/includes/mytime.h | 5 ++++- libft/includes/net.h | 10 ++++++++++ libft/srcs/net/forge_ip.c | 17 +++++++++++++++++ libft/srcs/net/forge_tcp.c | 20 ++++++++++++++++++++ libft/srcs/net/reserve_port.c | 25 +++++++++++++++++++++++++ libft/srcs/printing/hexdump.c | 2 +- libft/srcs/time/epoch.c | 10 ++++++++++ 8 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 libft/srcs/net/forge_ip.c create mode 100644 libft/srcs/net/forge_tcp.c create mode 100644 libft/srcs/net/reserve_port.c create mode 100644 libft/srcs/time/epoch.c diff --git a/libft/Makefile b/libft/Makefile index b5abab40..1e31ed7d 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -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\ diff --git a/libft/includes/mytime.h b/libft/includes/mytime.h index 61444457..524d2973 100644 --- a/libft/includes/mytime.h +++ b/libft/includes/mytime.h @@ -12,6 +12,9 @@ #ifndef MYTIME_H # define MYTIME_H + +# include + # 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 diff --git a/libft/includes/net.h b/libft/includes/net.h index 91698c7f..284db344 100644 --- a/libft/includes/net.h +++ b/libft/includes/net.h @@ -8,8 +8,12 @@ # include # include # include +# include +# include # include +#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 diff --git a/libft/srcs/net/forge_ip.c b/libft/srcs/net/forge_ip.c new file mode 100644 index 00000000..40fc91da --- /dev/null +++ b/libft/srcs/net/forge_ip.c @@ -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; +} diff --git a/libft/srcs/net/forge_tcp.c b/libft/srcs/net/forge_tcp.c new file mode 100644 index 00000000..e78fea37 --- /dev/null +++ b/libft/srcs/net/forge_tcp.c @@ -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; +} diff --git a/libft/srcs/net/reserve_port.c b/libft/srcs/net/reserve_port.c new file mode 100644 index 00000000..89e251bd --- /dev/null +++ b/libft/srcs/net/reserve_port.c @@ -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); +} diff --git a/libft/srcs/printing/hexdump.c b/libft/srcs/printing/hexdump.c index 924e2004..2a51dc6c 100644 --- a/libft/srcs/printing/hexdump.c +++ b/libft/srcs/printing/hexdump.c @@ -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; diff --git a/libft/srcs/time/epoch.c b/libft/srcs/time/epoch.c new file mode 100644 index 00000000..eda297db --- /dev/null +++ b/libft/srcs/time/epoch.c @@ -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); +}