Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Specialized logging functions; can control debug

  • Loading branch information...
commit 34025797b15e19804ca66eebe172f22aa7a73e60 1 parent fbbf20b
Michal Zima authored
27 configure.ac
... ... @@ -1,11 +1,36 @@
1 1 AC_INIT([WrapSix], [0.1.99.0], [xhire@mujmalysvet.cz])
2 2 AM_INIT_AUTOMAKE([-Wall -Werror])
  3 +
  4 +# prevent automatic adding of "-g -O2" to CFLAGS
  5 +OLD_CFLAGS=$CFLAGS
3 6 AC_PROG_CC
  7 +CFLAGS=$OLD_CFLAGS
  8 +
4 9 AC_CONFIG_FILES([
5 10 Makefile
6 11 src/Makefile
7 12 ])
8 13
9   -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"
  14 +###
  15 +# Configuration options
  16 +###
  17 +
  18 +AC_ARG_ENABLE([debug],
  19 + AS_HELP_STRING([--enable-debug], [enable debugging]),
  20 + [debug=$enableval],
  21 + [debug=no])
  22 +
  23 +if test "x$debug" = "xyes"; then
  24 + 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"
  25 + AC_DEFINE([DEBUG])
  26 +else
  27 + AM_CFLAGS="${AM_CFLAGS} -O2"
  28 +fi
  29 +
  30 +###
  31 +# Final commands
  32 +###
  33 +
  34 +AC_SUBST([AM_CFLAGS])
10 35
11 36 AC_OUTPUT
1  src/Makefile.am
@@ -7,6 +7,7 @@ wrapsix_wrapper_SOURCES = \
7 7 icmp.c icmp.h \
8 8 ipv4.c ipv4.h \
9 9 ipv6.c ipv6.h \
  10 + log.c log.h \
10 11 nat.c nat.h \
11 12 radixtree.c radixtree.h \
12 13 tcp.c tcp.h \
10 src/arp.c
@@ -18,11 +18,11 @@
18 18
19 19 #include <net/ethernet.h> /* ETHERTYPE_* */
20 20 #include <netinet/in.h> /* htons */
21   -#include <stdio.h>
22 21 #include <stdlib.h> /* malloc */
23 22 #include <string.h> /* memcmp, memset */
24 23
25 24 #include "arp.h"
  25 +#include "log.h"
26 26 #include "transmitter.h"
27 27 #include "wrapper.h"
28 28
@@ -32,8 +32,8 @@
32 32 * @param ethq Ethernet header of the packet
33 33 * @param payload Data of the packet
34 34 *
35   - * @return 0 for success
36   - * @return 1 for failure
  35 + * @return 0 for success
  36 + * @return 1 for failure
37 37 */
38 38 int arp(struct s_ethernet *ethq, char *payload)
39 39 {
@@ -51,7 +51,7 @@ int arp(struct s_ethernet *ethq, char *payload)
51 51
52 52 /* test if this packet belongs to us */
53 53 if (memcmp(&wrapsix_ipv4_addr, &arpq->ip_dest, 4)) {
54   - printf("[Debug] This is unfamiliar packet\n");
  54 + log_debug("This is unfamiliar ARP packet");
55 55 return 1;
56 56 }
57 57
@@ -60,7 +60,7 @@ int arp(struct s_ethernet *ethq, char *payload)
60 60
61 61 /* allocate enough memory */
62 62 if ((packet = (unsigned char *) malloc(ARP_PACKET_SIZE)) == NULL) {
63   - fprintf(stderr, "[Error] Lack of free memory\n");
  63 + log_error("Lack of free memory");
64 64 return 1;
65 65 }
66 66 memset(packet, 0x0, ARP_PACKET_SIZE);
18 src/checksum.c
@@ -17,13 +17,13 @@
17 17 */
18 18
19 19 #include <netinet/in.h> /* htonl */
20   -#include <stdio.h>
21 20 #include <stdlib.h> /* malloc */
22 21 #include <string.h> /* memcpy */
23 22
24 23 #include "checksum.h"
25 24 #include "ipv4.h"
26 25 #include "ipv6.h"
  26 +#include "log.h"
27 27
28 28 /**
29 29 * General checksum computation function
@@ -31,7 +31,7 @@
31 31 * @param data Pointer to data of which to compute the checksum
32 32 * @param length Length of the data (in bytes)
33 33 *
34   - * @return Checksum
  34 + * @return Checksum
35 35 */
36 36 unsigned short checksum(const void *data, int length)
37 37 {
@@ -76,7 +76,7 @@ unsigned short checksum(const void *data, int length)
76 76 * number of octets
77 77 * @param new_len Length of new data
78 78 *
79   - * @return Updated checksum
  79 + * @return Updated checksum
80 80 */
81 81 unsigned short checksum_update(unsigned short old_sum,
82 82 unsigned short *old_data, short old_len,
@@ -116,7 +116,7 @@ unsigned short checksum_update(unsigned short old_sum,
116 116 * @param proto Protocol in the payload
117 117 * @param payload Pointer to payload data
118 118 *
119   - * @return Checksum
  119 + * @return Checksum
120 120 */
121 121 unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
122 122 struct s_ipv4_addr ip_dest,
@@ -128,7 +128,7 @@ unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
128 128 unsigned short sum;
129 129
130 130 if ((buffer = malloc(sizeof(struct s_ipv4_pseudo) + length)) == NULL) {
131   - fprintf(stderr, "[Error] Lack of free memory\n");
  131 + log_error("Lack of free memory");
132 132 return 0;
133 133 }
134 134
@@ -158,7 +158,7 @@ unsigned short checksum_ipv4(struct s_ipv4_addr ip_src,
158 158 * @param proto Protocol in the payload
159 159 * @param payload Pointer to payload data
160 160 *
161   - * @return Checksum
  161 + * @return Checksum
162 162 */
163 163 unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
164 164 struct s_ipv6_addr ip_dest,
@@ -170,7 +170,7 @@ unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
170 170 unsigned short sum;
171 171
172 172 if ((buffer = malloc(sizeof(struct s_ipv6_pseudo) + length)) == NULL) {
173   - fprintf(stderr, "[Error] Lack of free memory\n");
  173 + log_error("Lack of free memory");
174 174 return 0;
175 175 }
176 176
@@ -202,7 +202,7 @@ unsigned short checksum_ipv6(struct s_ipv6_addr ip_src,
202 202 * @param ip4_dest New destination IPv4 address
203 203 * @param new_port New transport layer address (port)
204 204 *
205   - * @return Checksum
  205 + * @return Checksum
206 206 */
207 207 unsigned short checksum_ipv4_update(unsigned short old_sum,
208 208 struct s_ipv6_addr ip6_src,
@@ -241,7 +241,7 @@ unsigned short checksum_ipv4_update(unsigned short old_sum,
241 241 * @param ip6_dest New destination IPv6 address
242 242 * @param new_port New transport layer address (port)
243 243 *
244   - * @return Checksum
  244 + * @return Checksum
245 245 */
246 246 unsigned short checksum_ipv6_update(unsigned short old_sum,
247 247 struct s_ipv4_addr ip4_src,
30 src/icmp.c
@@ -18,13 +18,13 @@
18 18
19 19 #include <net/ethernet.h> /* ETHERTYPE_* */
20 20 #include <netinet/in.h> /* htons */
21   -#include <stdio.h>
22 21 #include <stdlib.h> /* malloc */
23 22 #include <string.h> /* memcpy, memset */
24 23
25 24 #include "checksum.h"
26 25 #include "icmp.h"
27 26 #include "ipv6.h"
  27 +#include "log.h"
28 28 #include "nat.h"
29 29 #include "transmitter.h"
30 30 #include "wrapper.h"
@@ -65,7 +65,7 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
65 65
66 66 if (icmp->checksum != orig_checksum) {
67 67 /* packet is corrupted and shouldn't be processed */
68   - printf("[Debug] Wrong checksum\n");
  68 + log_debug("Wrong checksum");
69 69 return 1;
70 70 }
71 71
@@ -81,8 +81,8 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
81 81 0, echo->id);
82 82
83 83 if (connection == NULL) {
84   - printf("[Debug] Incoming connection wasn't "
85   - "found in NAT\n");
  84 + log_debug("Incoming connection wasn't found in "
  85 + "NAT");
86 86 return 1;
87 87 }
88 88
@@ -94,8 +94,8 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
94 94 break;
95 95
96 96 default:
97   - printf("[Debug] ICMPv4 Type: unknown [%d/0x%x]\n",
98   - icmp->type, icmp->type);
  97 + log_debug("ICMPv4 Type: unknown [%d/0x%x]",
  98 + icmp->type, icmp->type);
99 99 return 1;
100 100 }
101 101
@@ -103,7 +103,7 @@ int icmp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4,
103 103 if ((packet = (unsigned char *) malloc(sizeof(struct s_ethernet) +
104 104 sizeof(struct s_ipv6) +
105 105 payload_size)) == NULL) {
106   - fprintf(stderr, "[Error] Lack of free memory\n");
  106 + log_error("Lack of free memory");
107 107 return 1;
108 108 }
109 109 eth6 = (struct s_ethernet *) packet;
@@ -177,7 +177,7 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
177 177
178 178 if (icmp->checksum != orig_checksum) {
179 179 /* packet is corrupted and shouldn't be processed */
180   - printf("[Debug] Wrong checksum\n");
  180 + log_debug("Wrong checksum");
181 181 return 1;
182 182 }
183 183
@@ -192,8 +192,8 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
192 192 echo->id, 0);
193 193
194 194 if (connection == NULL) {
195   - printf("[Debug] Error! Outgoing connection "
196   - "wasn't found/created in NAT!\n");
  195 + log_warn("Outgoing connection wasn't "
  196 + "found/created in NAT!");
197 197 return 1;
198 198 }
199 199
@@ -213,15 +213,15 @@ int icmp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
213 213 (struct s_icmp_ndp_ns *) icmp_data);
214 214
215 215 default:
216   - printf("[Debug] ICMPv6 Type: unknown [%d/0x%x]\n",
217   - icmp->type, icmp->type);
  216 + log_debug("ICMPv6 Type: unknown [%d/0x%x]",
  217 + icmp->type, icmp->type);
218 218 return 1;
219 219 }
220 220
221 221 /* allocate memory for translated packet */
222 222 if ((packet = (unsigned char *) malloc(sizeof(struct s_ipv4) +
223 223 htons(ip6->len))) == NULL) {
224   - fprintf(stderr, "[Error] Lack of free memory\n");
  224 + log_error("Lack of free memory");
225 225 return 1;
226 226 }
227 227 ip4 = (struct s_ipv4 *) packet;
@@ -280,7 +280,7 @@ int icmp_ndp(struct s_ethernet *ethq, struct s_ipv6 *ipq,
280 280
281 281 /* first check whether the request belongs to us */
282 282 if (memcmp(&wrapsix_ipv6_prefix, &ndp_ns->target, 12) != 0) {
283   - printf("[Debug] [NDP] This is unfamiliar packet\n");
  283 + log_debug("This is unfamiliar NDP packet");
284 284 return 1;
285 285 }
286 286
@@ -290,7 +290,7 @@ int icmp_ndp(struct s_ethernet *ethq, struct s_ipv6 *ipq,
290 290 sizeof(struct s_icmp) + \
291 291 sizeof(struct s_icmp_ndp_na)
292 292 if ((packet = (unsigned char *) malloc(NDP_PACKET_SIZE)) == NULL) {
293   - fprintf(stderr, "[Error] Lack of free memory\n");
  293 + log_error("Lack of free memory");
294 294 return 1;
295 295 }
296 296 memset(packet, 0x0, NDP_PACKET_SIZE);
14 src/ipv4.c
@@ -17,11 +17,11 @@
17 17 */
18 18
19 19 #include <netinet/in.h> /* IPPROTO_* */
20   -#include <stdio.h>
21 20 #include <string.h> /* memcmp */
22 21
23 22 #include "icmp.h"
24 23 #include "ipv4.h"
  24 +#include "log.h"
25 25 #include "tcp.h"
26 26 #include "udp.h"
27 27 #include "wrapper.h"
@@ -47,7 +47,7 @@ int ipv4(struct s_ethernet *eth, char *packet)
47 47
48 48 /* test if this packet belongs to us */
49 49 if (memcmp(&wrapsix_ipv4_addr, &ip->ip_dest, 4) != 0) {
50   - printf("[Debug] [IPv4] This is unfamiliar packet\n");
  50 + log_debug("This is unfamiliar IPv4 packet");
51 51 return 1;
52 52 }
53 53
@@ -58,17 +58,17 @@ int ipv4(struct s_ethernet *eth, char *packet)
58 58
59 59 switch (ip->proto) {
60 60 case IPPROTO_TCP:
61   - printf("[Debug] IPv4 Protocol: TCP\n");
  61 + log_debug("IPv4 Protocol: TCP");
62 62 return tcp_ipv4(eth, ip, payload, data_size);
63 63 case IPPROTO_UDP:
64   - printf("[Debug] IPv4 Protocol: UDP\n");
  64 + log_debug("IPv4 Protocol: UDP");
65 65 return udp_ipv4(eth, ip, payload, data_size);
66 66 case IPPROTO_ICMP:
67   - printf("[Debug] IPv4 Protocol: ICMP\n");
  67 + log_debug("IPv4 Protocol: ICMP");
68 68 return icmp_ipv4(eth, ip, payload, data_size);
69 69 default:
70   - printf("[Debug] IPv4 Protocol: unknown [%d/0x%x]\n",
71   - ip->proto, ip->proto);
  70 + log_debug("IPv4 Protocol: unknown [%d/0x%x]",
  71 + ip->proto, ip->proto);
72 72 return 1;
73 73 }
74 74 }
14 src/ipv6.c
@@ -17,11 +17,11 @@
17 17 */
18 18
19 19 #include <netinet/in.h> /* IPPROTO_* */
20   -#include <stdio.h>
21 20 #include <string.h> /* memcmp */
22 21
23 22 #include "icmp.h"
24 23 #include "ipv6.h"
  24 +#include "log.h"
25 25 #include "tcp.h"
26 26 #include "udp.h"
27 27 #include "wrapper.h"
@@ -47,23 +47,23 @@ int ipv6(struct s_ethernet *eth, char *packet)
47 47 /* test if this packet belongs to us */
48 48 if (memcmp(&wrapsix_ipv6_prefix, &ip->ip_dest, 12) != 0 &&
49 49 memcmp(&ndp_multicast_addr, &ip->ip_dest, 13) != 0) {
50   - printf("[Debug] [IPv6] This is unfamiliar packet\n");
  50 + log_debug("This is unfamiliar IPv6 packet");
51 51 return 1;
52 52 }
53 53
54 54 switch (ip->next_header) {
55 55 case IPPROTO_TCP:
56   - printf("[Debug] IPv6 Protocol: TCP\n");
  56 + log_debug("IPv6 Protocol: TCP");
57 57 return tcp_ipv6(eth, ip, payload);
58 58 case IPPROTO_UDP:
59   - printf("[Debug] IPv6 Protocol: UDP\n");
  59 + log_debug("IPv6 Protocol: UDP");
60 60 return udp_ipv6(eth, ip, payload);
61 61 case IPPROTO_ICMPV6:
62   - printf("[Debug] IPv6 Protocol: ICMP\n");
  62 + log_debug("IPv6 Protocol: ICMP");
63 63 return icmp_ipv6(eth, ip, payload);
64 64 default:
65   - printf("[Debug] IPv6 Protocol: unknown [%d/0x%x]\n",
66   - ip->next_header, ip->next_header);
  65 + log_debug("IPv6 Protocol: unknown [%d/0x%x]",
  66 + ip->next_header, ip->next_header);
67 67 return 1;
68 68 }
69 69 }
100 src/log.c
... ... @@ -0,0 +1,100 @@
  1 +/*
  2 + * WrapSix
  3 + * Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
  4 + *
  5 + * This program is free software: you can redistribute it and/or modify
  6 + * it under the terms of the GNU Affero General Public License as
  7 + * published by the Free Software Foundation, either version 3 of the
  8 + * License, or (at your option) any later version.
  9 + *
  10 + * This program is distributed in the hope that it will be useful,
  11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 + * GNU Affero General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU Affero General Public License
  16 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17 + */
  18 +
  19 +#include <stdarg.h>
  20 +#include <stdio.h>
  21 +
  22 +#include "log.h"
  23 +
  24 +/**
  25 + * Logs debugging stuff to stdout, but only when compiled with debug enabled.
  26 + *
  27 + * @param msg Formatted message
  28 + * @param ... Parameters to be included in the message
  29 + */
  30 +void log_debug(const char *msg, ...)
  31 +{
  32 +#ifdef DEBUG
  33 + va_list args;
  34 +
  35 + fprintf(stdout, "[Debug] ");
  36 +
  37 + va_start(args, msg);
  38 + vfprintf(stdout, msg, args);
  39 + va_end(args);
  40 +
  41 + fprintf(stdout, "\n");
  42 +#endif /* DEBUG */
  43 +}
  44 +
  45 +/**
  46 + * Logs information to stdout.
  47 + *
  48 + * @param msg Formatted message
  49 + * @param ... Parameters to be included in the message
  50 + */
  51 +void log_info(const char *msg, ...)
  52 +{
  53 + va_list args;
  54 +
  55 + fprintf(stdout, "[Info] ");
  56 +
  57 + va_start(args, msg);
  58 + vfprintf(stdout, msg, args);
  59 + va_end(args);
  60 +
  61 + fprintf(stdout, "\n");
  62 +}
  63 +
  64 +/**
  65 + * Logs warnings to stderr.
  66 + *
  67 + * @param msg Formatted message
  68 + * @param ... Parameters to be included in the message
  69 + */
  70 +void log_warn(const char *msg, ...)
  71 +{
  72 + va_list args;
  73 +
  74 + fprintf(stderr, "[Warning] ");
  75 +
  76 + va_start(args, msg);
  77 + vfprintf(stderr, msg, args);
  78 + va_end(args);
  79 +
  80 + fprintf(stderr, "\n");
  81 +}
  82 +
  83 +/**
  84 + * Logs errors to stderr.
  85 + *
  86 + * @param msg Formatted message
  87 + * @param ... Parameters to be included in the message
  88 + */
  89 +void log_error(const char *msg, ...)
  90 +{
  91 + va_list args;
  92 +
  93 + fprintf(stderr, "[Error] ");
  94 +
  95 + va_start(args, msg);
  96 + vfprintf(stderr, msg, args);
  97 + va_end(args);
  98 +
  99 + fprintf(stderr, "\n");
  100 +}
27 src/log.h
... ... @@ -0,0 +1,27 @@
  1 +/*
  2 + * WrapSix
  3 + * Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
  4 + *
  5 + * This program is free software: you can redistribute it and/or modify
  6 + * it under the terms of the GNU Affero General Public License as
  7 + * published by the Free Software Foundation, either version 3 of the
  8 + * License, or (at your option) any later version.
  9 + *
  10 + * This program is distributed in the hope that it will be useful,
  11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 + * GNU Affero General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU Affero General Public License
  16 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17 + */
  18 +
  19 +#ifndef LOG_H
  20 +#define LOG_H
  21 +
  22 +void log_debug(const char *msg, ...);
  23 +void log_info(const char *msg, ...);
  24 +void log_warn(const char *msg, ...);
  25 +void log_error(const char *msg, ...);
  26 +
  27 +#endif /* LOG_H */
6 src/nat.c
@@ -16,13 +16,13 @@
16 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17 */
18 18
19   -#include <stdio.h>
20 19 #include <stdlib.h> /* malloc */
21 20 #include <time.h> /* time */
22 21
23 22 #include "ethernet.h"
24 23 #include "ipv4.h"
25 24 #include "ipv6.h"
  25 +#include "log.h"
26 26 #include "nat.h"
27 27 #include "radixtree.h"
28 28 #include "wrapper.h"
@@ -120,7 +120,7 @@ struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
120 120 /* if no connection is found, let's create one */
121 121 if ((connection =
122 122 (struct s_nat *) malloc(sizeof(struct s_nat))) == NULL) {
123   - fprintf(stderr, "[Error] Lack of free memory\n");
  123 + log_error("Lack of free memory");
124 124 return NULL;
125 125 }
126 126
@@ -198,7 +198,7 @@ struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
198 198 * @param id Fragment identification
199 199 * @param nat Connection to save
200 200 *
201   - * @return Connection
  201 + * @return Connection
202 202 */
203 203 struct s_nat *nat_in_fragments(radixtree_t *nat_proto4,
204 204 struct s_ipv4_addr ipv4_src,
8 src/radixtree.c
@@ -16,10 +16,10 @@
16 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17 */
18 18
19   -#include <stdio.h> /* fprintf */
20 19 #include <stdlib.h> /* free, malloc */
21 20 #include <string.h> /* memcpy */
22 21
  22 +#include "log.h"
23 23 #include "radixtree.h"
24 24
25 25 /**
@@ -32,7 +32,7 @@ radixtree_t *radixtree_create(void)
32 32 radixtree_t *radixtree;
33 33
34 34 if ((radixtree = (radixtree_t *) malloc(sizeof(radixtree_t))) == NULL) {
35   - fprintf(stderr, "[Error] Lack of free memory\n");
  35 + log_error("Lack of free memory");
36 36 return NULL;
37 37 }
38 38 memset(radixtree, 0, sizeof(radixtree_t));
@@ -207,7 +207,7 @@ void *radixtree_lookup(radixtree_t *root,
207 207 * @param count Variable into which is put information about number of
208 208 * chunks produced
209 209 *
210   - * @return Array of chunks
  210 + * @return Array of chunks
211 211 */
212 212 unsigned char *radixtree_chunker(void *data, unsigned char size, unsigned char *count)
213 213 {
@@ -220,7 +220,7 @@ unsigned char *radixtree_chunker(void *data, unsigned char size, unsigned char *
220 220 memcpy(count, &counter, sizeof(unsigned char));
221 221
222 222 if ((chunks = (unsigned char *) malloc(counter * sizeof(unsigned char))) == NULL) {
223   - fprintf(stderr, "[Error] Lack of free memory\n");
  223 + log_error("Lack of free memory");
224 224 return NULL;
225 225 }
226 226
24 src/tcp.c
@@ -18,7 +18,6 @@
18 18
19 19 #include <net/ethernet.h> /* ETHERTYPE_* */
20 20 #include <netinet/in.h> /* htons */
21   -#include <stdio.h>
22 21 #include <stdlib.h> /* malloc */
23 22 #include <string.h> /* memcpy */
24 23
@@ -26,6 +25,7 @@
26 25 #include "ethernet.h"
27 26 #include "ipv4.h"
28 27 #include "ipv6.h"
  28 +#include "log.h"
29 29 #include "nat.h"
30 30 #include "tcp.h"
31 31 #include "transmitter.h"
@@ -78,7 +78,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
78 78 if (tcp->checksum != orig_checksum) {
79 79 /* packet is corrupted and shouldn't be
80 80 * processed */
81   - printf("[Debug] Wrong checksum\n");
  81 + log_debug("Wrong checksum");
82 82 return 1;
83 83 }
84 84 }
@@ -88,8 +88,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
88 88 tcp->port_src, tcp->port_dest);
89 89
90 90 if (connection == NULL) {
91   - printf("[Debug] Incoming connection wasn't found in "
92   - "NAT\n");
  91 + log_debug("Incoming connection wasn't found in NAT");
93 92 return 1;
94 93 }
95 94
@@ -105,7 +104,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
105 104 MTU + sizeof(struct s_ethernet) :
106 105 sizeof(struct s_ethernet) + sizeof(struct s_ipv6) +
107 106 payload_size)) == NULL) {
108   - fprintf(stderr, "[Error] Lack of free memory\n");
  107 + log_error("Lack of free memory");
109 108 return 1;
110 109 }
111 110 eth6 = (struct s_ethernet *) packet;
@@ -201,8 +200,8 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
201 200 ip4->id, NULL);
202 201
203 202 if (connection == NULL) {
204   - printf("[Debug] Incoming connection wasn't found in "
205   - "fragments table\n");
  203 + log_debug("Incoming connection wasn't found in "
  204 + "fragments table");
206 205 return 1;
207 206 }
208 207
@@ -213,7 +212,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
213 212 MTU + sizeof(struct s_ethernet) :
214 213 sizeof(struct s_ethernet) + sizeof(struct s_ipv6) +
215 214 sizeof(struct s_ipv6_fragment) + payload_size)) == NULL) {
216   - fprintf(stderr, "[Error] Lack of free memory\n");
  215 + log_error("Lack of free memory");
217 216 return 1;
218 217 }
219 218 eth6 = (struct s_ethernet *) packet;
@@ -311,7 +310,7 @@ int tcp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
311 310
312 311 /* if this is the last fragment, remove the entry from table */
313 312 if (!(ip4->flags_offset & htons(IPV4_FLAG_MORE_FRAGMENTS))) {
314   - printf("[Debug] Removing fragment entry\n");
  313 + log_debug("Removing fragment entry");
315 314 nat_in_fragments_cleanup(nat4_tcp_fragments,
316 315 ip4->ip_src, ip4->id);
317 316 }
@@ -354,7 +353,7 @@ int tcp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
354 353
355 354 if (tcp->checksum != orig_checksum) {
356 355 /* packet is corrupted and shouldn't be processed */
357   - printf("[Debug] Wrong checksum\n");
  356 + log_debug("Wrong checksum");
358 357 return 1;
359 358 }
360 359
@@ -364,15 +363,14 @@ int tcp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
364 363 tcp->port_src, tcp->port_dest);
365 364
366 365 if (connection == NULL) {
367   - printf("[Debug] Error! Outgoing connection wasn't "
368   - "found/created in NAT!\n");
  366 + log_warn("Outgoing connection wasn't found/created in NAT");
369 367 return 1;
370 368 }
371 369
372 370 /* allocate memory for translated packet */
373 371 if ((packet = (unsigned char *) malloc(sizeof(struct s_ipv4) +
374 372 htons(ip6->len))) == NULL) {
375   - fprintf(stderr, "[Error] Lack of free memory\n");
  373 + log_error("Lack of free memory");
376 374 return 1;
377 375 }
378 376 ip4 = (struct s_ipv4 *) packet;
36 src/transmitter.c
@@ -22,11 +22,12 @@
22 22 * sendto */
23 23 #include <netinet/in.h> /* htons */
24 24 #include <netpacket/packet.h> /* sockaddr_ll, PACKET_OTHERHOST */
25   -#include <stdio.h> /* fprintf, stderr, perror */
  25 +#include <stdio.h> /* perror */
26 26 #include <string.h> /* memcpy */
27 27 #include <unistd.h> /* close */
28 28
29 29 #include "ipv4.h"
  30 +#include "log.h"
30 31 #include "transmitter.h"
31 32 #include "wrapper.h"
32 33
@@ -38,8 +39,8 @@ int sock, sock_ipv4;
38 39 * Initialize sockets and all needed properties. Should be called only once on
39 40 * program startup.
40 41 *
41   - * @return 0 for success
42   - * @return 1 for failure
  42 + * @return 0 for success
  43 + * @return 1 for failure
43 44 */
44 45 int transmission_init(void)
45 46 {
@@ -54,7 +55,7 @@ int transmission_init(void)
54 55
55 56 /* initialize RAW socket */
56 57 if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
57   - fprintf(stderr, "[Error] Couldn't open RAW socket.\n");
  58 + log_error("Couldn't open RAW socket.");
58 59 perror("socket()");
59 60 return 1;
60 61 }
@@ -62,8 +63,7 @@ int transmission_init(void)
62 63 /* bind the socket to the interface */
63 64 if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &interface,
64 65 sizeof(struct ifreq)) == -1) {
65   - fprintf(stderr, "[Error] Couldn't bind the socket to the "
66   - "interface.\n");
  66 + log_error("Couldn't bind the socket to the interface.");
67 67 perror("setsockopt()");
68 68 return 1;
69 69 }
@@ -76,7 +76,7 @@ int transmission_init(void)
76 76
77 77 /* initialize RAW IPv4 socket */
78 78 if ((sock_ipv4 = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
79   - fprintf(stderr, "[Error] Couldn't open RAW IPv4 socket.\n");
  79 + log_error("Couldn't open RAW IPv4 socket.");
80 80 perror("socket()");
81 81 return 1;
82 82 }
@@ -84,8 +84,7 @@ int transmission_init(void)
84 84 /* we will provide our own IPv4 header */
85 85 if (setsockopt(sock_ipv4, IPPROTO_IP, IP_HDRINCL, &on,
86 86 sizeof(on)) == -1) {
87   - fprintf(stderr, "[Error] Couldn't apply the socket "
88   - "settings.\n");
  87 + log_error("Couldn't apply the socket settings.");
89 88 perror("setsockopt()");
90 89 return 1;
91 90 }
@@ -96,15 +95,14 @@ int transmission_init(void)
96 95 /**
97 96 * Close sockets. Should be called only once on program shutdown.
98 97 *
99   - * @return 0 for success
100   - * @return 1 for failure
  98 + * @return 0 for success
  99 + * @return 1 for failure
101 100 */
102 101 int transmission_quit(void)
103 102 {
104 103 /* close the socket */
105 104 if (close(sock) || close(sock_ipv4)) {
106   - fprintf(stderr, "[Error] Couldn't close the transmission "
107   - "sockets.\n");
  105 + log_warn("Couldn't close the transmission sockets.");
108 106 perror("close()");
109 107 return 1;
110 108 } else {
@@ -118,14 +116,14 @@ int transmission_quit(void)
118 116 * @param data Raw packet data, including ethernet header
119 117 * @param length Length of the whole packet in bytes
120 118 *
121   - * @return 0 for success
122   - * @return 1 for failure
  119 + * @return 0 for success
  120 + * @return 1 for failure
123 121 */
124 122 int transmit_raw(unsigned char *data, unsigned int length)
125 123 {
126 124 if (sendto(sock, data, length, 0, (struct sockaddr *) &socket_address,
127 125 sizeof(struct sockaddr_ll)) != (int) length) {
128   - fprintf(stderr, "[Error] Couldn't send a RAW packet.\n");
  126 + log_error("Couldn't send a RAW packet.");
129 127 perror("sendto()");
130 128 return 1;
131 129 }
@@ -141,8 +139,8 @@ int transmit_raw(unsigned char *data, unsigned int length)
141 139 * including IPv4 header
142 140 * @param length Length of the whole packet in bytes
143 141 *
144   - * @return 0 for success
145   - * @return 1 for failure
  142 + * @return 0 for success
  143 + * @return 1 for failure
146 144 */
147 145 int transmit_ipv4(struct s_ipv4_addr *ip, unsigned char *data,
148 146 unsigned int length)
@@ -154,7 +152,7 @@ int transmit_ipv4(struct s_ipv4_addr *ip, unsigned char *data,
154 152 if (sendto(sock_ipv4, data, length, 0,
155 153 (struct sockaddr *) &socket_address_ipv4,
156 154 sizeof(struct sockaddr)) != (int) length) {
157   - fprintf(stderr, "[Error] Couldn't send an IPv4 packet.\n");
  155 + log_error("Couldn't send an IPv4 packet.");
158 156 perror("sendto()");
159 157 return 1;
160 158 }
15 src/udp.c
@@ -18,7 +18,6 @@
18 18
19 19 #include <net/ethernet.h> /* ETHERTYPE_* */
20 20 #include <netinet/in.h> /* htons */
21   -#include <stdio.h>
22 21 #include <stdlib.h> /* malloc */
23 22 #include <string.h> /* memcpy */
24 23
@@ -26,6 +25,7 @@
26 25 #include "ethernet.h"
27 26 #include "ipv4.h"
28 27 #include "ipv6.h"
  28 +#include "log.h"
29 29 #include "nat.h"
30 30 #include "transmitter.h"
31 31 #include "udp.h"
@@ -68,7 +68,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
68 68
69 69 if (udp->checksum != orig_checksum) {
70 70 /* packet is corrupted and shouldn't be processed */
71   - printf("[Debug] Wrong checksum\n");
  71 + log_debug("Wrong checksum");
72 72 return 1;
73 73 }
74 74 }
@@ -77,7 +77,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
77 77 connection = nat_in(nat4_udp, ip4->ip_src, udp->port_src, udp->port_dest);
78 78
79 79 if (connection == NULL) {
80   - printf("[Debug] Incoming connection wasn't found in NAT\n");
  80 + log_debug("Incoming connection wasn't found in NAT");
81 81 return 1;
82 82 }
83 83
@@ -85,7 +85,7 @@ int udp_ipv4(struct s_ethernet *eth4, struct s_ipv4 *ip4, char *payload,
85 85 if ((packet = (unsigned char *) malloc(sizeof(struct s_ethernet) +
86 86 sizeof(struct s_ipv6) +
87 87 payload_size)) == NULL) {
88   - fprintf(stderr, "[Error] Lack of free memory\n");
  88 + log_error("Lack of free memory");
89 89 return 1;
90 90 }
91 91 eth6 = (struct s_ethernet *) packet;
@@ -168,7 +168,7 @@ int udp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
168 168
169 169 if (udp->checksum != orig_checksum) {
170 170 /* packet is corrupted and shouldn't be processed */
171   - printf("[Debug] Wrong checksum\n");
  171 + log_debug("Wrong checksum");
172 172 return 1;
173 173 }
174 174
@@ -178,15 +178,14 @@ int udp_ipv6(struct s_ethernet *eth6, struct s_ipv6 *ip6, char *payload)
178 178 udp->port_src, udp->port_dest);
179 179
180 180 if (connection == NULL) {
181   - printf("[Debug] Error! Outgoing connection wasn't "
182   - "found/created in NAT!\n");
  181 + log_warn("Outgoing connection wasn't found/created in NAT!");
183 182 return 1;
184 183 }
185 184
186 185 /* allocate memory for translated packet */
187 186 packet_size = sizeof(struct s_ipv4) + htons(ip6->len);
188 187 if ((packet = (unsigned char *) malloc(packet_size)) == NULL) {
189   - fprintf(stderr, "[Error] Lack of free memory\n");
  188 + log_error("Lack of free memory");
190 189 return 1;
191 190 }
192 191 ip4 = (struct s_ipv4 *) packet;
28 src/wrapper.c
@@ -22,7 +22,6 @@
22 22 #include <netpacket/packet.h> /* struct packet_mreq, struct sockaddr_ll */
23 23 #include <netinet/in.h> /* htons */
24 24 #include <net/ethernet.h> /* ETHERTYPE_* */
25   -#include <stdio.h>
26 25 #include <stdlib.h> /* srand */
27 26 #include <string.h> /* strncpy */
28 27 #include <sys/ioctl.h> /* ioctl, SIOCGIFINDEX */
@@ -33,6 +32,7 @@
33 32 #include "ethernet.h"
34 33 #include "ipv4.h"
35 34 #include "ipv6.h"
  35 +#include "log.h"
36 36 #include "nat.h"
37 37 #include "transmitter.h"
38 38 #include "wrapper.h"
@@ -63,16 +63,18 @@ int main(int argc, char **argv)
63 63 int length;
64 64 char buffer[MTU];
65 65
  66 + log_info(PACKAGE_STRING " is starting");
  67 +
66 68 /* initialize the socket for sniffing */
67 69 if ((sniff_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
68   - fprintf(stderr, "[Error] Unable to create listening socket\n");
  70 + log_error("Unable to create listening socket");
69 71 return 1;
70 72 }
71 73
72 74 /* get the interface */
73 75 strncpy(interface.ifr_name, INTERFACE, IFNAMSIZ);
74 76 if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
75   - fprintf(stderr, "[Error] Unable to get the interface\n");
  77 + log_error("Unable to get the interface");
76 78 return 1;
77 79 }
78 80
@@ -82,11 +84,11 @@ int main(int argc, char **argv)
82 84
83 85 /* reinitialize the interface */
84 86 if (ioctl(sniff_sock, SIOCGIFINDEX, &interface) == -1) {
85   - fprintf(stderr, "[Error] Unable to reinitialize the interface\n");
  87 + log_error("Unable to reinitialize the interface");
86 88 return 1;
87 89 }
88 90 } else {
89   - fprintf(stderr, "[Error] Unable to get the interface's HW address\n");
  91 + log_error("Unable to get the interface's HW address");
90 92 return 1;
91 93 }
92 94
@@ -95,7 +97,7 @@ int main(int argc, char **argv)
95 97 pmr.mr_ifindex = interface.ifr_ifindex;
96 98 pmr.mr_type = PACKET_MR_PROMISC;
97 99 if (setsockopt(sniff_sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
98   - fprintf(stderr, "[Error] Unable to set the promiscuous mode on the interface\n");
  100 + log_error("Unable to set the promiscuous mode on the interface");
99 101 return 1;
100 102 }
101 103
@@ -111,7 +113,7 @@ int main(int argc, char **argv)
111 113
112 114 /* initiate sending socket */
113 115 if (transmission_init()) {
114   - fprintf(stderr, "[Error] Unable to initiate sending socket\n");
  116 + log_error("Unable to initiate sending socket");
115 117 return 1;
116 118 }
117 119
@@ -125,7 +127,7 @@ int main(int argc, char **argv)
125 127 for (;;) {
126 128 addr_size = sizeof(addr);
127 129 if ((length = recv(sniff_sock, buffer, MTU, 0)) == -1) {
128   - fprintf(stderr, "[Error] Unable to retrieve data from socket\n");
  130 + log_error("Unable to retrieve data from socket");
129 131 return 1;
130 132 }
131 133
@@ -141,7 +143,7 @@ int main(int argc, char **argv)
141 143
142 144 /* unset the promiscuous mode */
143 145 if (setsockopt(sniff_sock, SOL_PACKET, PACKET_DROP_MEMBERSHIP, (char *) &pmr, sizeof(pmr)) == -1) {
144   - fprintf(stderr, "[Error] Unable to unset the promiscuous mode on the interface\n");
  146 + log_error("Unable to unset the promiscuous mode on the interface");
145 147 /* do not call `return` here as we want to close the socket too */
146 148 }
147 149
@@ -162,16 +164,16 @@ int process(char *packet)
162 164
163 165 switch (htons(eth->type)) {
164 166 case ETHERTYPE_IP:
165   - printf("[Debug] HW Protocol: IPv4\n");
  167 + log_debug("HW Protocol: IPv4");
166 168 return ipv4(eth, payload);
167 169 case ETHERTYPE_IPV6:
168   - printf("[Debug] HW Protocol: IPv6\n");
  170 + log_debug("HW Protocol: IPv6");
169 171 return ipv6(eth, payload);
170 172 case ETHERTYPE_ARP:
171   - printf("[Debug] HW Protocol: ARP\n");
  173 + log_debug("HW Protocol: ARP");
172 174 return arp(eth, payload);
173 175 default:
174   - printf("[Debug] HW Protocol: unknown [%d/0x%04x]\n",
  176 + log_debug("HW Protocol: unknown [%d/0x%04x]",
175 177 htons(eth->type), htons(eth->type));
176 178 return 1;
177 179 }

0 comments on commit 3402579

Please sign in to comment.
Something went wrong with that request. Please try again.