1
0
mirror of https://code.semirocket.science/wrapsix synced 2024-11-08 23:31:00 +02:00

Ethernet stuff moved to separated header file

Reworked IP addresses translation functions
Centralized initializing of random number generator
This commit is contained in:
Michal Zima 2011-11-08 21:15:36 +01:00
parent e0015145e2
commit 214c42728b
5 changed files with 55 additions and 36 deletions

34
src/ethernet.h Normal file
View File

@ -0,0 +1,34 @@
/*
* WrapSix
* Copyright (C) 2008-2011 Michal Zima <xhire@mujmalysvet.cz>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View File

@ -1,6 +1,6 @@
/* /*
* WrapSix * WrapSix
* Copyright (C) 2008-2010 Michal Zima <xhire@mujmalysvet.cz> * Copyright (C) 2008-2011 Michal Zima <xhire@mujmalysvet.cz>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -19,6 +19,8 @@
#ifndef IPV6_H #ifndef IPV6_H
#define IPV6_H #define IPV6_H
#include "ethernet.h" /* s_ethernet */
/* IPv6 address structure */ /* IPv6 address structure */
struct s_ipv6_addr { struct s_ipv6_addr {
unsigned char addr[16]; unsigned char addr[16];
@ -36,6 +38,6 @@ struct s_ipv6 {
struct s_ipv6_addr ip_dest; /* 128 b; destination address */ struct s_ipv6_addr ip_dest; /* 128 b; destination address */
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
int ipv6(struct s_ethernet *eth, char *payload); int ipv6(struct s_ethernet *eth, char *packet);
#endif /* IPV6_H */ #endif /* IPV6_H */

View File

@ -1,6 +1,6 @@
/* /*
* WrapSix * WrapSix
* Copyright (C) 2008-2010 Michal Zima <xhire@mujmalysvet.cz> * Copyright (C) 2008-2011 Michal Zima <xhire@mujmalysvet.cz>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * 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); result->last_packet = time(NULL);
/* generate some outgoing port */ /* generate some outgoing port */
srand((unsigned int) time(NULL));
do { do {
/* return port from range 1024 - 65535 */ /* return port from range 1024 - 65535 */
connection->ipv4_port_src = (rand() % 64511) + 1024; connection->ipv4_port_src = (rand() % 64511) + 1024;

View File

@ -1,6 +1,6 @@
/* /*
* WrapSix * WrapSix
* Copyright (C) 2008-2010 Michal Zima <xhire@mujmalysvet.cz> * Copyright (C) 2008-2011 Michal Zima <xhire@mujmalysvet.cz>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -23,10 +23,13 @@
#include <netinet/in.h> /* htons */ #include <netinet/in.h> /* htons */
#include <net/ethernet.h> /* ETHERTYPE_* */ #include <net/ethernet.h> /* ETHERTYPE_* */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* srand */
#include <string.h> /* strncpy */ #include <string.h> /* strncpy */
#include <sys/ioctl.h> /* ioctl, SIOCGIFINDEX */ #include <sys/ioctl.h> /* ioctl, SIOCGIFINDEX */
#include <time.h> /* time */
#include <unistd.h> /* close */ #include <unistd.h> /* close */
#include "ethernet.h"
#include "ipv4.h" #include "ipv4.h"
#include "ipv6.h" #include "ipv6.h"
#include "nat.h" #include "nat.h"
@ -85,6 +88,9 @@ int main(int argc, char **argv)
/* initiate NAT tables */ /* initiate NAT tables */
nat_init(); nat_init();
/* initiate random numbers generator */
srand((unsigned int) time(NULL));
/* sniff! :c) */ /* sniff! :c) */
for (;;) { for (;;) {
addr_size = sizeof(addr); 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);
memcpy(&ipv4_addr, &ipv6_addr + 12, 4);
return ipv4_addr;
} }
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);
ipv6_addr = wrapsix_ipv6_prefix;
memcpy(&ipv6_addr + 12, &ipv4_addr, 4);
return ipv6_addr;
} }

View File

@ -19,27 +19,13 @@
#ifndef WRAPPER_H #ifndef WRAPPER_H
#define WRAPPER_H #define WRAPPER_H
/* MAC address structure */ #include "ipv4.h"
struct s_mac_addr { #include "ipv6.h"
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__));
extern struct s_ipv6_addr ndp_multicast_addr; extern struct s_ipv6_addr ndp_multicast_addr;
extern struct s_ipv6_addr wrapsix_ipv6_prefix; extern struct s_ipv6_addr wrapsix_ipv6_prefix;
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_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);
#endif /* WRAPPER_H */ #endif /* WRAPPER_H */