1
0
mirror of https://code.semirocket.science/wrapsix synced 2024-12-22 04:20:18 +02:00

Specialized logging functions; can control debug

This commit is contained in:
Michal Zima 2012-07-04 11:37:42 +02:00
parent fbbf20ba0a
commit 34025797b1
15 changed files with 254 additions and 104 deletions

View File

@ -1,11 +1,36 @@
AC_INIT([WrapSix], [0.1.99.0], [xhire@mujmalysvet.cz])
AM_INIT_AUTOMAKE([-Wall -Werror])
# prevent automatic adding of "-g -O2" to CFLAGS
OLD_CFLAGS=$CFLAGS
AC_PROG_CC
CFLAGS=$OLD_CFLAGS
AC_CONFIG_FILES([
Makefile
src/Makefile
])
CFLAGS="-g -ggdb -O0 -pipe -pedantic -Wshadow -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wold-style-definition -Wdeclaration-after-statement -Wmissing-declarations -Wmissing-prototypes -Wredundant-decls -Wmissing-noreturn -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wformat-nonliteral -Wformat-security -Wswitch-enum -Wswitch-default -Winit-self -Wmissing-include-dirs -Wundef -Waggregate-return -Wmissing-format-attribute -Wnested-externs -Wunsafe-loop-optimizations"
###
# Configuration options
###
AC_ARG_ENABLE([debug],
AS_HELP_STRING([--enable-debug], [enable debugging]),
[debug=$enableval],
[debug=no])
if test "x$debug" = "xyes"; then
AM_CFLAGS="${AM_CFLAGS} -g -ggdb -O0 -pipe -pedantic -Wshadow -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wold-style-definition -Wdeclaration-after-statement -Wmissing-declarations -Wmissing-prototypes -Wredundant-decls -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wformat-nonliteral -Wformat-security -Wswitch-enum -Wswitch-default -Winit-self -Wmissing-include-dirs -Wundef -Waggregate-return -Wnested-externs -Wunsafe-loop-optimizations"
AC_DEFINE([DEBUG])
else
AM_CFLAGS="${AM_CFLAGS} -O2"
fi
###
# Final commands
###
AC_SUBST([AM_CFLAGS])
AC_OUTPUT

View File

@ -7,6 +7,7 @@ wrapsix_wrapper_SOURCES = \
icmp.c icmp.h \
ipv4.c ipv4.h \
ipv6.c ipv6.h \
log.c log.h \
nat.c nat.h \
radixtree.c radixtree.h \
tcp.c tcp.h \

View File

@ -18,11 +18,11 @@
#include <net/ethernet.h> /* ETHERTYPE_* */
#include <netinet/in.h> /* htons */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memcmp, memset */
#include "arp.h"
#include "log.h"
#include "transmitter.h"
#include "wrapper.h"
@ -32,8 +32,8 @@
* @param ethq Ethernet header of the packet
* @param payload Data of the packet
*
* @return 0 for success
* @return 1 for failure
* @return 0 for success
* @return 1 for failure
*/
int arp(struct s_ethernet *ethq, char *payload)
{
@ -51,7 +51,7 @@ int arp(struct s_ethernet *ethq, char *payload)
/* test if this packet belongs to us */
if (memcmp(&wrapsix_ipv4_addr, &arpq->ip_dest, 4)) {
printf("[Debug] This is unfamiliar packet\n");
log_debug("This is unfamiliar ARP packet");
return 1;
}
@ -60,7 +60,7 @@ int arp(struct s_ethernet *ethq, char *payload)
/* allocate enough memory */
if ((packet = (unsigned char *) malloc(ARP_PACKET_SIZE)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
memset(packet, 0x0, ARP_PACKET_SIZE);

View File

@ -17,13 +17,13 @@
*/
#include <netinet/in.h> /* htonl */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memcpy */
#include "checksum.h"
#include "ipv4.h"
#include "ipv6.h"
#include "log.h"
/**
* General checksum computation function
@ -31,7 +31,7 @@
* @param data Pointer to data of which to compute the checksum
* @param length Length of the data (in bytes)
*
* @return Checksum
* @return Checksum
*/
unsigned short checksum(const void *data, int length)
{
@ -76,7 +76,7 @@ unsigned short checksum(const void *data, int length)
* number of octets
* @param new_len Length of new data
*
* @return Updated checksum
* @return Updated checksum
*/
unsigned short checksum_update(unsigned short old_sum,
unsigned short *old_data, short old_len,
@ -116,7 +116,7 @@ unsigned short checksum_update(unsigned short old_sum,
* @param proto Protocol in the payload
* @param payload Pointer to payload data
*
* @return Checksum
* @return Checksum
*/
unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
struct s_ipv4_addr ip_dest,
@ -128,7 +128,7 @@ unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
unsigned short sum;
if ((buffer = malloc(sizeof(struct s_ipv4_pseudo) + length)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 0;
}
@ -158,7 +158,7 @@ unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
* @param proto Protocol in the payload
* @param payload Pointer to payload data
*
* @return Checksum
* @return Checksum
*/
unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
struct s_ipv6_addr ip_dest,
@ -170,7 +170,7 @@ unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
unsigned short sum;
if ((buffer = malloc(sizeof(struct s_ipv6_pseudo) + length)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 0;
}
@ -202,7 +202,7 @@ unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
* @param ip4_dest New destination IPv4 address
* @param new_port New transport layer address (port)
*
* @return Checksum
* @return Checksum
*/
unsigned short checksum_ipv4_update(unsigned short old_sum,
struct s_ipv6_addr ip6_src,
@ -241,7 +241,7 @@ unsigned short checksum_ipv4_update(unsigned short old_sum,
* @param ip6_dest New destination IPv6 address
* @param new_port New transport layer address (port)
*
* @return Checksum
* @return Checksum
*/
unsigned short checksum_ipv6_update(unsigned short old_sum,
struct s_ipv4_addr ip4_src,

View File

@ -18,13 +18,13 @@
#include <net/ethernet.h> /* ETHERTYPE_* */
#include <netinet/in.h> /* htons */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memcpy, memset */
#include "checksum.h"
#include "icmp.h"
#include "ipv6.h"
#include "log.h"
#include "nat.h"
#include "transmitter.h"
#include "wrapper.h"
@ -65,7 +65,7 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
if (icmp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
@ -81,8 +81,8 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
0, echo->id);
if (connection == NULL) {
printf("[Debug] Incoming connection wasn't "
"found in NAT\n");
log_debug("Incoming connection wasn't found in "
"NAT");
return 1;
}
@ -94,8 +94,8 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
break;
default:
printf("[Debug] ICMPv4 Type: unknown [%d/0x%x]\n",
icmp->type, icmp->type);
log_debug("ICMPv4 Type: unknown [%d/0x%x]",
icmp->type, icmp->type);
return 1;
}
@ -103,7 +103,7 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
if ((packet = (unsigned char *) malloc(sizeof(struct s_ethernet) +
sizeof(struct s_ipv6) +
payload_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
eth6 = (struct s_ethernet *) packet;
@ -177,7 +177,7 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
if (icmp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
@ -192,8 +192,8 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
echo->id, 0);
if (connection == NULL) {
printf("[Debug] Error! Outgoing connection "
"wasn't found/created in NAT!\n");
log_warn("Outgoing connection wasn't "
"found/created in NAT!");
return 1;
}
@ -213,15 +213,15 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
(struct s_icmp_ndp_ns *) icmp_data);
default:
printf("[Debug] ICMPv6 Type: unknown [%d/0x%x]\n",
icmp->type, icmp->type);
log_debug("ICMPv6 Type: unknown [%d/0x%x]",
icmp->type, icmp->type);
return 1;
}
/* allocate memory for translated packet */
if ((packet = (unsigned char *) malloc(sizeof(struct s_ipv4) +
htons(ip6->len))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
ip4 = (struct s_ipv4 *) packet;
@ -280,7 +280,7 @@ int icmp_ndp(struct s_ethernet *ethq, struct s_ipv6 *ipq,
/* first check whether the request belongs to us */
if (memcmp(&wrapsix_ipv6_prefix, &ndp_ns->target, 12) != 0) {
printf("[Debug] [NDP] This is unfamiliar packet\n");
log_debug("This is unfamiliar NDP packet");
return 1;
}
@ -290,7 +290,7 @@ int icmp_ndp(struct s_ethernet *ethq, struct s_ipv6 *ipq,
sizeof(struct s_icmp) + \
sizeof(struct s_icmp_ndp_na)
if ((packet = (unsigned char *) malloc(NDP_PACKET_SIZE)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
memset(packet, 0x0, NDP_PACKET_SIZE);

View File

@ -17,11 +17,11 @@
*/
#include <netinet/in.h> /* IPPROTO_* */
#include <stdio.h>
#include <string.h> /* memcmp */
#include "icmp.h"
#include "ipv4.h"
#include "log.h"
#include "tcp.h"
#include "udp.h"
#include "wrapper.h"
@ -47,7 +47,7 @@ int ipv4(struct s_ethernet *eth, char *packet)
/* test if this packet belongs to us */
if (memcmp(&wrapsix_ipv4_addr, &ip->ip_dest, 4) != 0) {
printf("[Debug] [IPv4] This is unfamiliar packet\n");
log_debug("This is unfamiliar IPv4 packet");
return 1;
}
@ -58,17 +58,17 @@ int ipv4(struct s_ethernet *eth, char *packet)
switch (ip->proto) {
case IPPROTO_TCP:
printf("[Debug] IPv4 Protocol: TCP\n");
log_debug("IPv4 Protocol: TCP");
return tcp_ipv4(eth, ip, payload, data_size);
case IPPROTO_UDP:
printf("[Debug] IPv4 Protocol: UDP\n");
log_debug("IPv4 Protocol: UDP");
return udp_ipv4(eth, ip, payload, data_size);
case IPPROTO_ICMP:
printf("[Debug] IPv4 Protocol: ICMP\n");
log_debug("IPv4 Protocol: ICMP");
return icmp_ipv4(eth, ip, payload, data_size);
default:
printf("[Debug] IPv4 Protocol: unknown [%d/0x%x]\n",
ip->proto, ip->proto);
log_debug("IPv4 Protocol: unknown [%d/0x%x]",
ip->proto, ip->proto);
return 1;
}
}

View File

@ -17,11 +17,11 @@
*/
#include <netinet/in.h> /* IPPROTO_* */
#include <stdio.h>
#include <string.h> /* memcmp */
#include "icmp.h"
#include "ipv6.h"
#include "log.h"
#include "tcp.h"
#include "udp.h"
#include "wrapper.h"
@ -47,23 +47,23 @@ int ipv6(struct s_ethernet *eth, char *packet)
/* test if this packet belongs to us */
if (memcmp(&wrapsix_ipv6_prefix, &ip->ip_dest, 12) != 0 &&
memcmp(&ndp_multicast_addr, &ip->ip_dest, 13) != 0) {
printf("[Debug] [IPv6] This is unfamiliar packet\n");
log_debug("This is unfamiliar IPv6 packet");
return 1;
}
switch (ip->next_header) {
case IPPROTO_TCP:
printf("[Debug] IPv6 Protocol: TCP\n");
log_debug("IPv6 Protocol: TCP");
return tcp_ipv6(eth, ip, payload);
case IPPROTO_UDP:
printf("[Debug] IPv6 Protocol: UDP\n");
log_debug("IPv6 Protocol: UDP");
return udp_ipv6(eth, ip, payload);
case IPPROTO_ICMPV6:
printf("[Debug] IPv6 Protocol: ICMP\n");
log_debug("IPv6 Protocol: ICMP");
return icmp_ipv6(eth, ip, payload);
default:
printf("[Debug] IPv6 Protocol: unknown [%d/0x%x]\n",
ip->next_header, ip->next_header);
log_debug("IPv6 Protocol: unknown [%d/0x%x]",
ip->next_header, ip->next_header);
return 1;
}
}

100
src/log.c Normal file
View File

@ -0,0 +1,100 @@
/*
* WrapSix
* Copyright (C) 2008-2012 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/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include "log.h"
/**
* Logs debugging stuff to stdout, but only when compiled with debug enabled.
*
* @param msg Formatted message
* @param ... Parameters to be included in the message
*/
void log_debug(const char *msg, ...)
{
#ifdef DEBUG
va_list args;
fprintf(stdout, "[Debug] ");
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fprintf(stdout, "\n");
#endif /* DEBUG */
}
/**
* Logs information to stdout.
*
* @param msg Formatted message
* @param ... Parameters to be included in the message
*/
void log_info(const char *msg, ...)
{
va_list args;
fprintf(stdout, "[Info] ");
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fprintf(stdout, "\n");
}
/**
* Logs warnings to stderr.
*
* @param msg Formatted message
* @param ... Parameters to be included in the message
*/
void log_warn(const char *msg, ...)
{
va_list args;
fprintf(stderr, "[Warning] ");
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
}
/**
* Logs errors to stderr.
*
* @param msg Formatted message
* @param ... Parameters to be included in the message
*/
void log_error(const char *msg, ...)
{
va_list args;
fprintf(stderr, "[Error] ");
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
}

27
src/log.h Normal file
View File

@ -0,0 +1,27 @@
/*
* WrapSix
* Copyright (C) 2008-2012 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 LOG_H
#define LOG_H
void log_debug(const char *msg, ...);
void log_info(const char *msg, ...);
void log_warn(const char *msg, ...);
void log_error(const char *msg, ...);
#endif /* LOG_H */

View File

@ -16,13 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <time.h> /* time */
#include "ethernet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "log.h"
#include "nat.h"
#include "radixtree.h"
#include "wrapper.h"
@ -120,7 +120,7 @@ struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
/* if no connection is found, let's create one */
if ((connection =
(struct s_nat *) malloc(sizeof(struct s_nat))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return NULL;
}
@ -198,7 +198,7 @@ struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
* @param id Fragment identification
* @param nat Connection to save
*
* @return Connection
* @return Connection
*/
struct s_nat *nat_in_fragments(radixtree_t *nat_proto4,
struct s_ipv4_addr ipv4_src,

View File

@ -16,10 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> /* fprintf */
#include <stdlib.h> /* free, malloc */
#include <string.h> /* memcpy */
#include "log.h"
#include "radixtree.h"
/**
@ -32,7 +32,7 @@ radixtree_t *radixtree_create(void)
radixtree_t *radixtree;
if ((radixtree = (radixtree_t *) malloc(sizeof(radixtree_t))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return NULL;
}
memset(radixtree, 0, sizeof(radixtree_t));
@ -207,7 +207,7 @@ void *radixtree_lookup(radixtree_t *root,
* @param count Variable into which is put information about number of
* chunks produced
*
* @return Array of chunks
* @return Array of chunks
*/
unsigned char *radixtree_chunker(void *data, unsigned char size, unsigned char *count)
{
@ -220,7 +220,7 @@ unsigned char *radixtree_chunker(void *data, unsigned char size, unsigned char *
memcpy(count, &counter, sizeof(unsigned char));
if ((chunks = (unsigned char *) malloc(counter * sizeof(unsigned char))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return NULL;
}

View File

@ -18,7 +18,6 @@
#include <net/ethernet.h> /* ETHERTYPE_* */
#include <netinet/in.h> /* htons */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memcpy */
@ -26,6 +25,7 @@
#include "ethernet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "log.h"
#include "nat.h"
#include "tcp.h"
#include "transmitter.h"
@ -78,7 +78,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
if (tcp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be
* processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
}
@ -88,8 +88,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
tcp->port_src, tcp->port_dest);
if (connection == NULL) {
printf("[Debug] Incoming connection wasn't found in "
"NAT\n");
log_debug("Incoming connection wasn't found in NAT");
return 1;
}
@ -105,7 +104,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
MTU + sizeof(struct s_ethernet) :
sizeof(struct s_ethernet) + sizeof(struct s_ipv6) +
payload_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
eth6 = (struct s_ethernet *) packet;
@ -201,8 +200,8 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
ip4->id, NULL);
if (connection == NULL) {
printf("[Debug] Incoming connection wasn't found in "
"fragments table\n");
log_debug("Incoming connection wasn't found in "
"fragments table");
return 1;
}
@ -213,7 +212,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
MTU + sizeof(struct s_ethernet) :
sizeof(struct s_ethernet) + sizeof(struct s_ipv6) +
sizeof(struct s_ipv6_fragment) + payload_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
eth6 = (struct s_ethernet *) packet;
@ -311,7 +310,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
/* if this is the last fragment, remove the entry from table */
if (!(ip4->flags_offset & htons(IPV4_FLAG_MORE_FRAGMENTS))) {
printf("[Debug] Removing fragment entry\n");
log_debug("Removing fragment entry");
nat_in_fragments_cleanup(nat4_tcp_fragments,
ip4->ip_src, ip4->id);
}
@ -354,7 +353,7 @@ int tcp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
if (tcp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
@ -364,15 +363,14 @@ int tcp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
tcp->port_src, tcp->port_dest);
if (connection == NULL) {
printf("[Debug] Error! Outgoing connection wasn't "
"found/created in NAT!\n");
log_warn("Outgoing connection wasn't found/created in NAT");
return 1;
}
/* allocate memory for translated packet */
if ((packet = (unsigned char *) malloc(sizeof(struct s_ipv4) +
htons(ip6->len))) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
ip4 = (struct s_ipv4 *) packet;

View File

@ -22,11 +22,12 @@
* sendto */
#include <netinet/in.h> /* htons */
#include <netpacket/packet.h> /* sockaddr_ll, PACKET_OTHERHOST */
#include <stdio.h> /* fprintf, stderr, perror */
#include <stdio.h> /* perror */
#include <string.h> /* memcpy */
#include <unistd.h> /* close */
#include "ipv4.h"
#include "log.h"
#include "transmitter.h"
#include "wrapper.h"
@ -38,8 +39,8 @@ int sock, sock_ipv4;
* Initialize sockets and all needed properties. Should be called only once on
* program startup.
*
* @return 0 for success
* @return 1 for failure
* @return 0 for success
* @return 1 for failure
*/
int transmission_init(void)
{
@ -54,7 +55,7 @@ int transmission_init(void)
/* initialize RAW socket */
if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
fprintf(stderr, "[Error] Couldn't open RAW socket.\n");
log_error("Couldn't open RAW socket.");
perror("socket()");
return 1;
}
@ -62,8 +63,7 @@ int transmission_init(void)
/* bind the socket to the interface */
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &interface,
sizeof(struct ifreq)) == -1) {
fprintf(stderr, "[Error] Couldn't bind the socket to the "
"interface.\n");
log_error("Couldn't bind the socket to the interface.");
perror("setsockopt()");
return 1;
}
@ -76,7 +76,7 @@ int transmission_init(void)
/* initialize RAW IPv4 socket */
if ((sock_ipv4 = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
fprintf(stderr, "[Error] Couldn't open RAW IPv4 socket.\n");
log_error("Couldn't open RAW IPv4 socket.");
perror("socket()");
return 1;
}
@ -84,8 +84,7 @@ int transmission_init(void)
/* we will provide our own IPv4 header */
if (setsockopt(sock_ipv4, IPPROTO_IP, IP_HDRINCL, &on,
sizeof(on)) == -1) {
fprintf(stderr, "[Error] Couldn't apply the socket "
"settings.\n");
log_error("Couldn't apply the socket settings.");
perror("setsockopt()");
return 1;
}
@ -96,15 +95,14 @@ int transmission_init(void)
/**
* Close sockets. Should be called only once on program shutdown.
*
* @return 0 for success
* @return 1 for failure
* @return 0 for success
* @return 1 for failure
*/
int transmission_quit(void)
{
/* close the socket */
if (close(sock) || close(sock_ipv4)) {
fprintf(stderr, "[Error] Couldn't close the transmission "
"sockets.\n");
log_warn("Couldn't close the transmission sockets.");
perror("close()");
return 1;
} else {
@ -118,14 +116,14 @@ int transmission_quit(void)
* @param data Raw packet data, including ethernet header
* @param length Length of the whole packet in bytes
*
* @return 0 for success
* @return 1 for failure
* @return 0 for success
* @return 1 for failure
*/
int transmit_raw(unsigned char *data, unsigned int length)
{
if (sendto(sock, data, length, 0, (struct sockaddr *) &socket_address,
sizeof(struct sockaddr_ll)) != (int) length) {
fprintf(stderr, "[Error] Couldn't send a RAW packet.\n");
log_error("Couldn't send a RAW packet.");
perror("sendto()");
return 1;
}
@ -141,8 +139,8 @@ int transmit_raw(unsigned char *data, unsigned int length)
* including IPv4 header
* @param length Length of the whole packet in bytes
*
* @return 0 for success
* @return 1 for failure
* @return 0 for success
* @return 1 for failure
*/
int transmit_ipv4(struct s_ipv4_addr *ip, unsigned char *data,
unsigned int length)
@ -154,7 +152,7 @@ int transmit_ipv4(struct s_ipv4_addr *ip, unsigned char *data,
if (sendto(sock_ipv4, data, length, 0,
(struct sockaddr *) &socket_address_ipv4,
sizeof(struct sockaddr)) != (int) length) {
fprintf(stderr, "[Error] Couldn't send an IPv4 packet.\n");
log_error("Couldn't send an IPv4 packet.");
perror("sendto()");
return 1;
}

View File

@ -18,7 +18,6 @@
#include <net/ethernet.h> /* ETHERTYPE_* */
#include <netinet/in.h> /* htons */
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* memcpy */
@ -26,6 +25,7 @@
#include "ethernet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "log.h"
#include "nat.h"
#include "transmitter.h"
#include "udp.h"
@ -68,7 +68,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
if (udp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
}
@ -77,7 +77,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
connection = nat_in(nat4_udp, ip4->ip_src, udp->port_src, udp->port_dest);
if (connection == NULL) {
printf("[Debug] Incoming connection wasn't found in NAT\n");
log_debug("Incoming connection wasn't found in NAT");
return 1;
}
@ -85,7 +85,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
if ((packet = (unsigned char *) malloc(sizeof(struct s_ethernet) +
sizeof(struct s_ipv6) +
payload_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
eth6 = (struct s_ethernet *) packet;
@ -168,7 +168,7 @@ int udp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
if (udp->checksum != orig_checksum) {
/* packet is corrupted and shouldn't be processed */
printf("[Debug] Wrong checksum\n");
log_debug("Wrong checksum");
return 1;
}
@ -178,15 +178,14 @@ int udp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
udp->port_src, udp->port_dest);
if (connection == NULL) {
printf("[Debug] Error! Outgoing connection wasn't "
"found/created in NAT!\n");
log_warn("Outgoing connection wasn't found/created in NAT!");
return 1;
}
/* allocate memory for translated packet */
packet_size = sizeof(struct s_ipv4) + htons(ip6->len);
if ((packet = (unsigned char *) malloc(packet_size)) == NULL) {
fprintf(stderr, "[Error] Lack of free memory\n");
log_error("Lack of free memory");
return 1;
}
ip4 = (struct s_ipv4 *) packet;

View File

@ -22,7 +22,6 @@
#include <netpacket/packet.h> /* struct packet_mreq, struct sockaddr_ll */
#include <netinet/in.h> /* htons */
#include <net/ethernet.h> /* ETHERTYPE_* */
#include <stdio.h>
#include <stdlib.h> /* srand */
#include <string.h> /* strncpy */
#include <sys/ioctl.h> /* ioctl, SIOCGIFINDEX */
@ -33,6 +32,7 @@
#include "ethernet.h"
#include "ipv4.h"
#include "ipv6.h"
#include "log.h"
#include "nat.h"
#include "transmitter.h"
#include "wrapper.h"
@ -63,16 +63,18 @@ int main(int argc, char **argv)
int length;
char buffer[MTU];
log_info(PACKAGE_STRING " is starting");
/* initialize the socket for sniffing */
if ((sniff_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
fprintf(stderr, "[Error] Unable to create listening socket\n");
log_error("Unable to create listening socket");
return 1;
}
/* get the interface */
strncpy(interface.ifr_name, INTERFACE, IFNAMSIZ);
if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
fprintf(stderr, "[Error] Unable to get the interface\n");
log_error("Unable to get the interface");
return 1;
}
@ -82,11 +84,11 @@ int main(int argc, char **argv)
/* reinitialize the interface */
if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
fprintf(stderr, "[Error] Unable to reinitialize the interface\n");
log_error("Unable to reinitialize the interface");
return 1;
}
} else {
fprintf(stderr, "[Error] Unable to get the interface's HW address\n");
log_error("Unable to get the interface's HW address");
return 1;
}
@ -95,7 +97,7 @@ int main(int argc, char **argv)
pmr.mr_ifindex = interface.ifr_ifindex;
pmr.mr_type = PACKET_MR_PROMISC;
if (setsockopt(sniff_sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
fprintf(stderr, "[Error] Unable to set the promiscuous mode on the interface\n");
log_error("Unable to set the promiscuous mode on the interface");
return 1;
}
@ -111,7 +113,7 @@ int main(int argc, char **argv)
/* initiate sending socket */
if (transmission_init()) {
fprintf(stderr, "[Error] Unable to initiate sending socket\n");
log_error("Unable to initiate sending socket");
return 1;
}
@ -125,7 +127,7 @@ int main(int argc, char **argv)
for (;;) {
addr_size = sizeof(addr);
if ((length = recv(sniff_sock, buffer, MTU, 0)) == -1) {
fprintf(stderr, "[Error] Unable to retrieve data from socket\n");
log_error("Unable to retrieve data from socket");
return 1;
}
@ -141,7 +143,7 @@ int main(int argc, char **argv)
/* unset the promiscuous mode */
if (setsockopt(sniff_sock, SOL_PACKET, PACKET_DROP_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
fprintf(stderr, "[Error] Unable to unset the promiscuous mode on the interface\n");
log_error("Unable to unset the promiscuous mode on the interface");
/* do not call `return` here as we want to close the socket too */
}
@ -162,16 +164,16 @@ int process(char *packet)
switch (htons(eth->type)) {
case ETHERTYPE_IP:
printf("[Debug] HW Protocol: IPv4\n");
log_debug("HW Protocol: IPv4");
return ipv4(eth, payload);
case ETHERTYPE_IPV6:
printf("[Debug] HW Protocol: IPv6\n");
log_debug("HW Protocol: IPv6");
return ipv6(eth, payload);
case ETHERTYPE_ARP:
printf("[Debug] HW Protocol: ARP\n");
log_debug("HW Protocol: ARP");
return arp(eth, payload);
default:
printf("[Debug] HW Protocol: unknown [%d/0x%04x]\n",
log_debug("HW Protocol: unknown [%d/0x%04x]",
htons(eth->type), htons(eth->type));
return 1;
}