Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

some code reduction

  • Loading branch information...
commit ffd4ff1f6c2abf1229f08a411503894a4ea92962 1 parent 611a03e
Giovanni Pellerano authored
Showing with 81 additions and 114 deletions.
  1. +1 −1  CMakeLists.txt
  2. +3 −3 src/CMakeLists.txt
  3. +48 −84 src/Makefile
  4. +29 −26 src/janus.c
View
2  CMakeLists.txt
@@ -1,4 +1,4 @@
-CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
+CMAKE_MINIMUM_REQUIRED( VERSION 2.4 )
PROJECT( janus )
View
6 src/CMakeLists.txt
@@ -1,10 +1,10 @@
IF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
SET(TUN "tun/tun-linux")
-ELSEIF("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")
+ELSE("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")
SET(TUN "tun/tun-freebsd")
ELSE()
MESSAGE(FATAL_ERROR "${CMAKE_SYSTEM_NAME} system not tested yet")
-ENDIF()
+ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
ADD_EXECUTABLE(
janus
@@ -26,7 +26,7 @@ FOREACH( PACKAGE ${PACKAGELIST} )
MESSAGE( STATUS "${PACKAGE} includes: ${${PACKAGE}_INCLUDE_DIRS}" )
MESSAGE( STATUS "${PACKAGE} libs: ${${PACKAGE}_LIBRARIES}" )
-ENDFOREACH()
+ENDFOREACH(PACKAGE ${PACKAGELIST})
SET( CMAKE_C_FLAGS "-O3 -Wall -pedantic ${CMAKE_C_FLAGS}" )
View
132 src/Makefile
@@ -36,10 +36,10 @@ CMAKE_COMMAND = /usr/bin/cmake
RM = /usr/bin/cmake -E remove -f
# The top-level source directory on which CMake was run.
-CMAKE_SOURCE_DIR = /home/evilaliv3/Desktop/janus
+CMAKE_SOURCE_DIR = /home/evilaliv3/Desktop/a/janus
# The top-level build directory on which CMake was run.
-CMAKE_BINARY_DIR = /home/evilaliv3/Desktop/janus
+CMAKE_BINARY_DIR = /home/evilaliv3/Desktop/a/janus
#=============================================================================
# Targets provided globally by CMake.
@@ -107,14 +107,14 @@ rebuild_cache/fast: rebuild_cache
# The main all target
all: cmake_check_build_system
- cd /home/evilaliv3/Desktop/janus && $(CMAKE_COMMAND) -E cmake_progress_start /home/evilaliv3/Desktop/janus/CMakeFiles /home/evilaliv3/Desktop/janus/src/CMakeFiles/progress.marks
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f CMakeFiles/Makefile2 src/all
- $(CMAKE_COMMAND) -E cmake_progress_start /home/evilaliv3/Desktop/janus/CMakeFiles 0
+ cd /home/evilaliv3/Desktop/a/janus && $(CMAKE_COMMAND) -E cmake_progress_start /home/evilaliv3/Desktop/a/janus/CMakeFiles /home/evilaliv3/Desktop/a/janus/src/CMakeFiles/progress.marks
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f CMakeFiles/Makefile2 src/all
+ $(CMAKE_COMMAND) -E cmake_progress_start /home/evilaliv3/Desktop/a/janus/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f CMakeFiles/Makefile2 src/clean
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f CMakeFiles/Makefile2 src/clean
.PHONY : clean
# The main clean target
@@ -123,22 +123,22 @@ clean/fast: clean
# Prepare targets for installation.
preinstall: all
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f CMakeFiles/Makefile2 src/preinstall
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f CMakeFiles/Makefile2 src/preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f CMakeFiles/Makefile2 src/preinstall
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f CMakeFiles/Makefile2 src/preinstall
.PHONY : preinstall/fast
# clear depends
depend:
- cd /home/evilaliv3/Desktop/janus && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
+ cd /home/evilaliv3/Desktop/a/janus && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
# Convenience name for target.
src/CMakeFiles/janus.dir/rule:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f CMakeFiles/Makefile2 src/CMakeFiles/janus.dir/rule
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f CMakeFiles/Makefile2 src/CMakeFiles/janus.dir/rule
.PHONY : src/CMakeFiles/janus.dir/rule
# Convenience name for target.
@@ -147,104 +147,68 @@ janus: src/CMakeFiles/janus.dir/rule
# fast build rule for target.
janus/fast:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/build
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/build
.PHONY : janus/fast
-janus.o: janus.c.o
-.PHONY : janus.o
-
# target to build an object file
-janus.c.o:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.c.o
-.PHONY : janus.c.o
-
-janus.i: janus.c.i
-.PHONY : janus.i
+janus.o:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.o
+.PHONY : janus.o
# target to preprocess a source file
-janus.c.i:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.c.i
-.PHONY : janus.c.i
-
-janus.s: janus.c.s
-.PHONY : janus.s
+janus.i:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.i
+.PHONY : janus.i
# target to generate assembly for a file
-janus.c.s:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.c.s
-.PHONY : janus.c.s
-
-main.o: main.c.o
-.PHONY : main.o
+janus.s:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/janus.s
+.PHONY : janus.s
# target to build an object file
-main.c.o:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.c.o
-.PHONY : main.c.o
-
-main.i: main.c.i
-.PHONY : main.i
+main.o:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.o
+.PHONY : main.o
# target to preprocess a source file
-main.c.i:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.c.i
-.PHONY : main.c.i
-
-main.s: main.c.s
-.PHONY : main.s
+main.i:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.i
+.PHONY : main.i
# target to generate assembly for a file
-main.c.s:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.c.s
-.PHONY : main.c.s
-
-packet_queue.o: packet_queue.c.o
-.PHONY : packet_queue.o
+main.s:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/main.s
+.PHONY : main.s
# target to build an object file
-packet_queue.c.o:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.c.o
-.PHONY : packet_queue.c.o
-
-packet_queue.i: packet_queue.c.i
-.PHONY : packet_queue.i
+packet_queue.o:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.o
+.PHONY : packet_queue.o
# target to preprocess a source file
-packet_queue.c.i:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.c.i
-.PHONY : packet_queue.c.i
-
-packet_queue.s: packet_queue.c.s
-.PHONY : packet_queue.s
+packet_queue.i:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.i
+.PHONY : packet_queue.i
# target to generate assembly for a file
-packet_queue.c.s:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.c.s
-.PHONY : packet_queue.c.s
-
-tun/tun-linux.o: tun/tun-linux.c.o
-.PHONY : tun/tun-linux.o
+packet_queue.s:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/packet_queue.s
+.PHONY : packet_queue.s
# target to build an object file
-tun/tun-linux.c.o:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.c.o
-.PHONY : tun/tun-linux.c.o
-
-tun/tun-linux.i: tun/tun-linux.c.i
-.PHONY : tun/tun-linux.i
+tun/tun-linux.o:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.o
+.PHONY : tun/tun-linux.o
# target to preprocess a source file
-tun/tun-linux.c.i:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.c.i
-.PHONY : tun/tun-linux.c.i
-
-tun/tun-linux.s: tun/tun-linux.c.s
-.PHONY : tun/tun-linux.s
+tun/tun-linux.i:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.i
+.PHONY : tun/tun-linux.i
# target to generate assembly for a file
-tun/tun-linux.c.s:
- cd /home/evilaliv3/Desktop/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.c.s
-.PHONY : tun/tun-linux.c.s
+tun/tun-linux.s:
+ cd /home/evilaliv3/Desktop/a/janus && $(MAKE) -f src/CMakeFiles/janus.dir/build.make src/CMakeFiles/janus.dir/tun/tun-linux.s
+.PHONY : tun/tun-linux.s
# Help Target
help:
@@ -282,6 +246,6 @@ help:
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
- cd /home/evilaliv3/Desktop/janus && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
+ cd /home/evilaliv3/Desktop/a/janus && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
View
55 src/janus.c
@@ -72,6 +72,7 @@ enum mitm_t
struct janus_config conf;
+static uint16_t pbuf_len;
static struct packets *pbufs;
static pcap_t *capnet;
@@ -79,8 +80,8 @@ static char *macpkt;
static char str[STRINGS_NUM][CONST_JANUS_BUFSIZE];
-static uint8_t netif_send_hdr[ETH_HLEN];
-static uint8_t netif_recv_hdr[ETH_HLEN];
+static char netif_send_hdr[ETH_HLEN];
+static char netif_recv_hdr[ETH_HLEN];
struct mitm_descriptor
{
@@ -128,6 +129,15 @@ static void setflflag(int fd, long flags)
runtime_exception("unable to set fl flags %u on fd %u (F_GETFL/F_SETFL)", fd, flags);
}
+static void parseMAC(const char* str, char* buf)
+{
+ uint8_t i;
+ uint32_t tmp_mac[ETH_ALEN];
+ sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x", &tmp_mac[0], &tmp_mac[1], &tmp_mac[2], &tmp_mac[3], &tmp_mac[4], &tmp_mac[5]);
+ for (i = 0; i < ETH_ALEN; ++i)
+ buf[i] = tmp_mac[i];
+}
+
static struct packet* bufferedRead(struct mitm_descriptor* desc)
{
return queue_pop_front(desc->pqueue, &desc->pbuf_send);
@@ -160,11 +170,10 @@ static ssize_t netif_recv(int sockfd, struct packet* pbuf)
if ((packet != NULL) && !memcmp(packet, netif_recv_hdr, ETH_HLEN))
{
- uint16_t mtu = atoi(str[STR_NET_MTU]);
uint32_t len = header.len - ETH_HLEN;
- len = (len > mtu) ? mtu : len;
+ len = (len > pbuf_len) ? pbuf_len : len;
memcpy(pbuf->buf, packet + ETH_HLEN, len);
- return header.len - ETH_HLEN;
+ return len;
}
errno = EAGAIN;
@@ -183,7 +192,7 @@ static ssize_t netif_send(int sockfd, struct packet* pbuf)
static ssize_t tunif_recv(int sockfd, struct packet* pbuf)
{
- return read(sockfd, pbuf->buf, atoi(str[STR_NET_MTU]));
+ return read(sockfd, pbuf->buf, pbuf_len);
}
static ssize_t tunif_send(int sockfd, struct packet* pbuf)
@@ -374,11 +383,11 @@ static int setupMitmAttach(uint16_t port)
void JANUS_Bootstrap(void)
{
- uint8_t mac[ETH_ALEN];
- uint32_t tmp_mac[ETH_ALEN];
-
uint8_t i, j;
+ char mac[ETH_ALEN];
+ uint32_t tmp_mac[ETH_ALEN];
+
for (i = 0; i < STRINGS_NUM; i++)
str[i][0] = '\0';
@@ -422,33 +431,27 @@ void JANUS_Bootstrap(void)
capnet = NULL;
- sscanf(str[STR_NET_MAC], "%02x:%02x:%02x:%02x:%02x:%02x", &tmp_mac[0], &tmp_mac[1], &tmp_mac[2], &tmp_mac[3], &tmp_mac[4], &tmp_mac[5]);
- for (i = 0; i < ETH_ALEN; ++i)
- mac[i] = tmp_mac[i];
+ parseMAC(str[STR_GW_MAC], netif_send_hdr);
+ parseMAC(str[STR_NET_MAC], &netif_send_hdr[ETH_ALEN]);
- memcpy(netif_recv_hdr, mac, ETH_ALEN);
- memcpy(&netif_send_hdr[ETH_ALEN], mac, ETH_ALEN);
-
- sscanf(str[STR_GW_MAC], "%02x:%02x:%02x:%02x:%02x:%02x", &tmp_mac[0], &tmp_mac[1], &tmp_mac[2], &tmp_mac[3], &tmp_mac[4], &tmp_mac[5]);
- for (i = 0; i < ETH_ALEN; ++i)
- mac[i] = tmp_mac[i];
-
- memcpy(netif_send_hdr, mac, ETH_ALEN);
- memcpy(&netif_recv_hdr[ETH_ALEN], mac, ETH_ALEN);
+ memcpy(netif_recv_hdr, &netif_send_hdr[ETH_ALEN], ETH_ALEN);
+ memcpy(&netif_recv_hdr[ETH_ALEN], netif_send_hdr, ETH_ALEN);
*(uint16_t *)&netif_send_hdr[2 * ETH_ALEN] = htons(ETH_P_IP);
*(uint16_t *)&netif_recv_hdr[2 * ETH_ALEN] = htons(ETH_P_IP);
- macpkt = malloc(ETH_HLEN + atoi(str[STR_NET_MTU]));
+ pbuf_len = atoi(str[STR_NET_MTU]);
+
+ pbufs = pbufs_malloc(conf.pqueue_len, pbuf_len);
+ if (pbufs == NULL)
+ runtime_exception("unable to allocate memory for packet buffers");
+
+ macpkt = malloc(pbuf_len);
if (macpkt == NULL)
runtime_exception("unable to allocate memory for the datalink packet buffer");
memcpy(macpkt, netif_send_hdr, ETH_HLEN);
- pbufs = pbufs_malloc(conf.pqueue_len, atoi(str[STR_NET_MTU]));
- if (pbufs == NULL)
- runtime_exception("unable to allocate memory for packet buffers");
-
memset(&mitm_desc, 0, sizeof (mitm_desc));
mitm_desc[NET].fd_recv = netif_recv;
Please sign in to comment.
Something went wrong with that request. Please try again.