From 214c42728bde933eabf9c88e3ff8ca644be18503 Mon Sep 17 00:00:00 2001 From: Michal Zima Date: Tue, 8 Nov 2011 21:15:36 +0100 Subject: [PATCH] Ethernet stuff moved to separated header file Reworked IP addresses translation functions Centralized initializing of random number generator --- src/ethernet.h | 34 ++++++++++++++++++++++++++++++++++ src/ipv6.h | 6 ++++-- src/nat.c | 3 +-- src/wrapper.c | 26 ++++++++++++-------------- src/wrapper.h | 22 ++++------------------ 5 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 src/ethernet.h diff --git a/src/ethernet.h b/src/ethernet.h new file mode 100644 index 0000000..0f1e854 --- /dev/null +++ b/src/ethernet.h @@ -0,0 +1,34 @@ +/* + * WrapSix + * Copyright (C) 2008-2011 Michal Zima + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef ETHERNET_H +#define ETHERNET_H + +/* MAC address structure */ +struct s_mac_addr { + unsigned char addr[6]; +} __attribute__ ((__packed__)); + +/* Ethernet header structure */ +struct s_ethernet { + struct s_mac_addr dest; /* 48 b; destination host (MAC) address */ + struct s_mac_addr src; /* 48 b; source host (MAC) address */ + unsigned short type; /* 16 b; IP/ARP/RARP/... */ +} __attribute__ ((__packed__)); + +#endif /* ETHERNET_H */ diff --git a/src/ipv6.h b/src/ipv6.h index 49e5e10..27e3903 100644 --- a/src/ipv6.h +++ b/src/ipv6.h @@ -1,6 +1,6 @@ /* * WrapSix - * Copyright (C) 2008-2010 Michal Zima + * Copyright (C) 2008-2011 Michal Zima * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -19,6 +19,8 @@ #ifndef IPV6_H #define IPV6_H +#include "ethernet.h" /* s_ethernet */ + /* IPv6 address structure */ struct s_ipv6_addr { unsigned char addr[16]; @@ -36,6 +38,6 @@ struct s_ipv6 { struct s_ipv6_addr ip_dest; /* 128 b; destination address */ } __attribute__ ((__packed__)); -int ipv6(struct s_ethernet *eth, char *payload); +int ipv6(struct s_ethernet *eth, char *packet); #endif /* IPV6_H */ diff --git a/src/nat.c b/src/nat.c index ffab40c..9370602 100644 --- a/src/nat.c +++ b/src/nat.c @@ -1,6 +1,6 @@ /* * WrapSix - * Copyright (C) 2008-2010 Michal Zima + * Copyright (C) 2008-2011 Michal Zima * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -89,7 +89,6 @@ struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4, result->last_packet = time(NULL); /* generate some outgoing port */ - srand((unsigned int) time(NULL)); do { /* return port from range 1024 - 65535 */ connection->ipv4_port_src = (rand() % 64511) + 1024; diff --git a/src/wrapper.c b/src/wrapper.c index 3744452..0513c83 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -1,6 +1,6 @@ /* * WrapSix - * Copyright (C) 2008-2010 Michal Zima + * Copyright (C) 2008-2011 Michal Zima * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -23,10 +23,13 @@ #include /* htons */ #include /* ETHERTYPE_* */ #include +#include /* srand */ #include /* strncpy */ #include /* ioctl, SIOCGIFINDEX */ +#include /* time */ #include /* close */ +#include "ethernet.h" #include "ipv4.h" #include "ipv6.h" #include "nat.h" @@ -85,6 +88,9 @@ int main(int argc, char **argv) /* initiate NAT tables */ nat_init(); + /* initiate random numbers generator */ + srand((unsigned int) time(NULL)); + /* sniff! :c) */ for (;;) { addr_size = sizeof(addr); @@ -138,21 +144,13 @@ int process(char *packet) } } -struct s_ipv4_addr ipv6_to_ipv4(struct s_ipv6_addr *ipv6_addr) +void ipv6_to_ipv4(struct s_ipv6_addr *ipv6_addr, struct s_ipv4_addr *ipv4_addr) { - struct s_ipv4_addr ipv4_addr; - - memcpy(&ipv4_addr, &ipv6_addr + 12, 4); - - return ipv4_addr; + memcpy(ipv4_addr, &ipv6_addr + 12, 4); } -struct s_ipv6_addr ipv4_to_ipv6(struct s_ipv4_addr *ipv4_addr) +void ipv4_to_ipv6(struct s_ipv4_addr *ipv4_addr, struct s_ipv6_addr *ipv6_addr) { - struct s_ipv6_addr ipv6_addr; - - ipv6_addr = wrapsix_ipv6_prefix; - memcpy(&ipv6_addr + 12, &ipv4_addr, 4); - - return ipv6_addr; + *ipv6_addr = wrapsix_ipv6_prefix; + memcpy(ipv6_addr + 12, &ipv4_addr, 4); } diff --git a/src/wrapper.h b/src/wrapper.h index e6deba5..7de6edf 100644 --- a/src/wrapper.h +++ b/src/wrapper.h @@ -19,27 +19,13 @@ #ifndef WRAPPER_H #define WRAPPER_H -/* MAC address structure */ -struct s_mac_addr { - unsigned char a; - unsigned char b; - unsigned char c; - unsigned char d; - unsigned char e; - unsigned char f; -} __attribute__ ((__packed__)); - -/* Ethernet header structure */ -struct s_ethernet { - struct s_mac_addr dest; /* 48 b; destination host (MAC) address */ - struct s_mac_addr src; /* 48 b; source host (MAC) address */ - unsigned short type; /* 16 b; IP/ARP/RARP/... */ -} __attribute__ ((__packed__)); +#include "ipv4.h" +#include "ipv6.h" extern struct s_ipv6_addr ndp_multicast_addr; extern struct s_ipv6_addr wrapsix_ipv6_prefix; -struct s_ipv4_addr ipv6_to_ipv4(struct s_ipv6_addr *ipv6_addr); -struct s_ipv6_addr ipv4_to_ipv6(struct s_ipv4_addr *ipv4_addr); +void ipv6_to_ipv4(struct s_ipv6_addr *ipv6_addr, struct s_ipv4_addr *ipv4_addr); +void ipv4_to_ipv6(struct s_ipv4_addr *ipv4_addr, struct s_ipv6_addr *ipv6_addr); #endif /* WRAPPER_H */