Permalink
Browse files

Specialized logging functions; can control debug

  • Loading branch information...
1 parent fbbf20b commit 34025797b15e19804ca66eebe172f22aa7a73e60 @xHire committed Jul 4, 2012
Showing with 254 additions and 104 deletions.
  1. +26 −1 configure.ac
  2. +1 −0 src/Makefile.am
  3. +5 −5 src/arp.c
  4. +9 −9 src/checksum.c
  5. +15 −15 src/icmp.c
  6. +7 −7 src/ipv4.c
  7. +7 −7 src/ipv6.c
  8. +100 −0 src/log.c
  9. +27 −0 src/log.h
  10. +3 −3 src/nat.c
  11. +4 −4 src/radixtree.c
  12. +11 −13 src/tcp.c
  13. +17 −19 src/transmitter.c
  14. +7 −8 src/udp.c
  15. +15 −13 src/wrapper.c
View
@@ -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
@@ -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
@@ -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
@@ -17,21 +17,21 @@
*/
#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
*
* @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
@@ -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,16 +94,16 @@ 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;
}
/* allocate memory for translated packet */
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
@@ -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
@@ -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;
}
}
Oops, something went wrong.

0 comments on commit 3402579

Please sign in to comment.