Skip to content
Browse files

Redeveloped TUNMessageType.

  • Loading branch information...
1 parent 41b4716 commit 5dff7dbfa9903bf76fbbee5627998d46b8bf16ac Caleb James DeLisle committed Dec 10, 2012
View
11 interface/CMakeLists.txt
@@ -29,6 +29,7 @@ add_library(interface
${ethif}
${tunif}
TUNConfigurator_${SYSTEM}.c
+ TUNMessageType.c
InterfaceConnector.c
SessionManager.c
ICMP6Generator.c
@@ -42,16 +43,6 @@ target_link_libraries(interface
cjdns-util-platform-socket
)
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/TUNMessageType_${SYSTEM}.c)
- set(TUNMessageType TUNMessageType_${SYSTEM}.c)
-else()
- set(TUNMessageType TUNMessageType.c)
-endif()
-
-add_library(cjdns-interface-tun-messagetype
- ${TUNMessageType}
-)
-
add_library(cjdns-interface-pipeinterface
PipeInterface.c
)
View
56 interface/TUNMessageType.c
@@ -13,19 +13,71 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "interface/TUNMessageType.h"
+#include "util/Assert.h"
+#include "wire/Ethernet.h"
#include "wire/Message.h"
#include <stdint.h>
+#include <netinet/in.h>
-void TUNMessageType_push(struct Message* message, uint16_t ethertype)
+
+/**
+ * Simple and sane, the 4 bytes prior to the message contain flags and the
+ * big endian ethertype. Flags only contain a single flag which we don't use.
+ */
+void TUNMessageType_push_Linux(struct Message* message, uint16_t ethertype)
{
Message_shift(message, 4);
((uint16_t*) message->bytes)[0] = 0;
((uint16_t*) message->bytes)[1] = ethertype;
}
-uint16_t TUNMessageType_pop(struct Message* message)
+uint16_t TUNMessageType_pop_Linux(struct Message* message)
{
Message_shift(message, -4);
return ((uint16_t*) message->bytes)[-1];
}
+
+
+/**
+ * OSX and BSD is expect you to send the platform dependent
+ * address family type rather than the ethertype.
+ */
+#define AF_INET6_BE Endian_hostToBigEndian16(AF_INET6)
+#define AF_INET_BE Endian_hostToBigEndian16(AF_INET)
+
+void TUNMessageType_push_BSD(struct Message* message, uint16_t ethertype)
+{
+ uint16_t afType_be = (ethertype == Ethernet_TYPE_IP6) ? AF_INET6_BE : AF_INET_BE;
+
+ Message_shift(message, 4);
+ ((uint16_t*) message->bytes)[0] = 0;
+ ((uint16_t*) message->bytes)[1] = afType_be;
+}
+uint16_t TUNMessageType_pop_BSD(struct Message* message)
+{
+ Message_shift(message, -4);
+ uint16_t afType_be = ((uint16_t*) message->bytes)[-1];
+
+ return (afType_be == AF_INET6_BE) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
+}
+
+
+/**
+ * Illumos has no concept of packet info, it only supports IPv4 and IPv6
+ * through TUN devices and it detects it by reading the version byte.
+ */
+static inline uint16_t ethertypeForPacketType(uint8_t highByte)
+{
+ return ((highByte >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
+}
+
+void TUNMessageType_push_Illumos(struct Message* message, uint16_t ethertype)
+{
+ Assert_true(ethertype == ethertypeForPacketType(message->bytes[0]));
+}
+
+uint16_t TUNMessageType_pop_Illumos(struct Message* message)
+{
+ return ethertypeForPacketType(message->bytes[0]);
+}
View
14 interface/TUNMessageType.h
@@ -19,6 +19,20 @@
#include <stdint.h>
+#if defined(BSD) || defined(OSX)
+ #define TUNMessageType_push TUNMessageType_push_BSD
+ #define TUNMessageType_pop TUNMessageType_pop_BSD
+#elif defined(Illumos)
+ #define TUNMessageType_push TUNMessageType_push_Illumos
+ #define TUNMessageType_pop TUNMessageType_pop_Illumos
+#elif defined(Linux) || defined(WIN32)
+ // We'll assume that when the WIN32 implementation is written, it works like linux.
+ #define TUNMessageType_push TUNMessageType_push_Linux
+ #define TUNMessageType_pop TUNMessageType_pop_Linux
+#else
+ #error No TUNMessageType_push() and TUNMessageType_pop() implementations for your system.
+#endif
+
void TUNMessageType_push(struct Message* message, uint16_t ethertype);
uint16_t TUNMessageType_pop(struct Message* message);
View
39 interface/TUNMessageType_Illumos.c
@@ -1,39 +0,0 @@
-/* vim: set expandtab ts=4 sw=4: */
-/*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#include "wire/Message.h"
-#include "interface/TUNMessageType.h"
-#include "wire/Ethernet.h"
-
-#include <stdint.h>
-
-/**
- * Illumos has no consept of packet info, it only supports IPv4 and IPv6
- * through TUN devices and it detects it by reading the version byte.
- */
-
-static inline uint16_t ethertypeForPacketType(uint8_t highByte)
-{
- return ((highByte >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
-}
-
-void TUNMessageType_push(struct Message* message, uint16_t ethertype)
-{
- Assert_true(ethertype == ethertypeForPacketType(message->bytes[0]));
-}
-
-uint16_t TUNMessageType_pop(struct Message* message)
-{
- return ethertypeForPacketType(message->bytes[0]);
-}
View
45 interface/TUNMessageType_OSX.c
@@ -1,45 +0,0 @@
-/* vim: set expandtab ts=4 sw=4: */
-/*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#include "wire/Message.h"
-#include "interface/TUNMessageType.h"
-#include "wire/Ethernet.h"
-
-#include <stdint.h>
-#include <netinet/in.h>
-
-#define AF_INET6_BE Endian_hostToBigEndian16(AF_INET6)
-#define AF_INET_BE Endian_hostToBigEndian16(AF_INET)
-
-/**
- * OSX is broken and expects you to send the platform dependent
- * address family type rather than the ethertype.
- */
-
-void TUNMessageType_push(struct Message* message, uint16_t ethertype)
-{
- uint16_t afType_be = (ethertype == Ethernet_TYPE_IP6) ? AF_INET6_BE : AF_INET_BE;
-
- Message_shift(message, 4);
- ((uint16_t*) message->bytes)[0] = 0;
- ((uint16_t*) message->bytes)[1] = afType_be;
-}
-
-uint16_t TUNMessageType_pop(struct Message* message)
-{
- Message_shift(message, -4);
- uint16_t afType_be = ((uint16_t*) message->bytes)[-1];
-
- return (afType_be == AF_INET6_BE) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
-}
View
1 interface/test/CMakeLists.txt
@@ -23,7 +23,6 @@ set(Test_LIBRARIES
cjdns-admin
cjdns-admin-client
cjdns-memory-canary
- cjdns-interface-tun-messagetype
cjdns-interface-pipeinterface
cjdadmintest
interface
View
1 net/CMakeLists.txt
@@ -18,7 +18,6 @@ add_library(cjdnet
)
target_link_libraries(cjdnet
- cjdns-interface-tun-messagetype
cjdbenc
crypto
interface
View
2 tunnel/CMakeLists.txt
@@ -17,7 +17,7 @@ add_library(cjdns-tunnel-iptunnel
target_link_libraries(cjdns-tunnel-iptunnel
cjdbenc
- cjdns-interface-tun-messagetype
+ interface
)
enable_testing()

0 comments on commit 5dff7db

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