packet forging
This commit is contained in:
parent
6df67a60c7
commit
6908e10aab
8 changed files with 91 additions and 2 deletions
|
|
@ -135,6 +135,9 @@ net/create_client.c\
|
||||||
net/create_server.c\
|
net/create_server.c\
|
||||||
net/net_get.c\
|
net/net_get.c\
|
||||||
net/net_send.c\
|
net/net_send.c\
|
||||||
|
net/forge_tcp.c\
|
||||||
|
net/forge_ip.c\
|
||||||
|
net/reserve_port.c\
|
||||||
path/ft_path_notdir.c\
|
path/ft_path_notdir.c\
|
||||||
printing/ft_putchar.c\
|
printing/ft_putchar.c\
|
||||||
printing/ft_putendl.c\
|
printing/ft_putendl.c\
|
||||||
|
|
@ -201,6 +204,7 @@ str/ft_strtrim.c\
|
||||||
time/ft_mytime_free.c\
|
time/ft_mytime_free.c\
|
||||||
time/ft_mytime_get.c\
|
time/ft_mytime_get.c\
|
||||||
time/ft_time_isrecent.c\
|
time/ft_time_isrecent.c\
|
||||||
|
time/epoch.c\
|
||||||
sys/open_new.c\
|
sys/open_new.c\
|
||||||
htb/ft_hash_string.c\
|
htb/ft_hash_string.c\
|
||||||
htb/hashtab_init.c\
|
htb/hashtab_init.c\
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@
|
||||||
|
|
||||||
#ifndef MYTIME_H
|
#ifndef MYTIME_H
|
||||||
# define MYTIME_H
|
# define MYTIME_H
|
||||||
|
|
||||||
|
# include <sys/time.h>
|
||||||
|
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
|
|
||||||
struct s_mytime
|
struct s_mytime
|
||||||
|
|
@ -27,8 +30,8 @@ struct s_mytime
|
||||||
typedef struct s_mytime t_mytime;
|
typedef struct s_mytime t_mytime;
|
||||||
|
|
||||||
int ft_time_isrecent(time_t event);
|
int ft_time_isrecent(time_t event);
|
||||||
|
|
||||||
t_mytime *ft_mytime_get(time_t epoch);
|
t_mytime *ft_mytime_get(time_t epoch);
|
||||||
void ft_mytime_free(t_mytime **time);
|
void ft_mytime_free(t_mytime **time);
|
||||||
|
size_t epoch_micro(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,12 @@
|
||||||
# include <sys/socket.h>
|
# include <sys/socket.h>
|
||||||
# include <netdb.h>
|
# include <netdb.h>
|
||||||
# include <netinet/in.h>
|
# include <netinet/in.h>
|
||||||
|
# include <netinet/ip.h>
|
||||||
|
# include <netinet/tcp.h>
|
||||||
# include <arpa/inet.h>
|
# include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "mytime.h"
|
||||||
|
|
||||||
int create_server(int port, int backlog, char *protoname);
|
int create_server(int port, int backlog, char *protoname);
|
||||||
int create_client(char *addr, int port, 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));
|
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_fd(int sock, int fd, int size);
|
||||||
int net_get_large(int sock, int fd);
|
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
|
#endif
|
||||||
|
|
|
||||||
17
libft/srcs/net/forge_ip.c
Normal file
17
libft/srcs/net/forge_ip.c
Normal 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;
|
||||||
|
}
|
||||||
20
libft/srcs/net/forge_tcp.c
Normal file
20
libft/srcs/net/forge_tcp.c
Normal 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;
|
||||||
|
}
|
||||||
25
libft/srcs/net/reserve_port.c
Normal file
25
libft/srcs/net/reserve_port.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -36,7 +36,7 @@ void hexdump(void *pAddressIn, long lSize)
|
||||||
{
|
{
|
||||||
ucTmp = *pTmp++;
|
ucTmp = *pTmp++;
|
||||||
|
|
||||||
sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
|
sprintf(szBuf + lIndex, "%02x ", (unsigned short)ucTmp);
|
||||||
if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
|
if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
|
||||||
szBuf[lIndex2] = ucTmp;
|
szBuf[lIndex2] = ucTmp;
|
||||||
|
|
||||||
|
|
|
||||||
10
libft/srcs/time/epoch.c
Normal file
10
libft/srcs/time/epoch.c
Normal 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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue