Permalink
Browse files

Workaround non-standardized TUN message types.

Also minor refactoring.
  • Loading branch information...
1 parent f96202e commit b0472e8b223317883516ebd97b2a142afb2b3256 Caleb James DeLisle committed Dec 10, 2012
View
@@ -21,12 +21,8 @@
#include "memory/Allocator.h"
#include "util/log/Log.h"
#include "util/UniqueName.h"
-
-#ifdef FreeBSD
- #include <netinet/in.h>
-#endif
-
#include "util/events/EventBase.h"
+
#include <stdbool.h>
#define Admin_FUNCTION(name) void (* name)(Dict* input, void* context, String* txid)
View
@@ -256,7 +256,7 @@ int Core_main(int argc, char** argv)
SerializationModule_register(registry, logger, alloc);
- struct IpTunnel* ipTun = IpTunnel_new(logger, eventBase, alloc, rand);
+ struct IpTunnel* ipTun = IpTunnel_new(logger, eventBase, alloc, rand, admin);
struct Ducttape* dt = Ducttape_register(privateKey,
registry,
@@ -42,6 +42,16 @@ 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
)
@@ -48,17 +48,6 @@ static void handleEvent(void* vcontext)
}
msg->length = length;
- #ifdef OSX
- // OSX tags the message with the AF type (AF_INET or AF_INET6) rather than the ethertype.
- // pop it and scrap it then tag it the same as we do for Illumos.
- TUNInterface_popMessageType(msg);
- #endif
- #if defined(Illumos) || defined(OSX)
- // Illumos does not send packet info, it only supports ip4 and ip6 over tun.
- uint16_t ethertype = ((msg->bytes[0] >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
- TUNInterface_pushMessageType(msg, ethertype);
- #endif
-
struct Interface* iface = &tun->pub.iface;
if (iface->receiveMessage) {
iface->receiveMessage(msg, iface);
@@ -67,11 +56,6 @@ static void handleEvent(void* vcontext)
static uint8_t sendMessage(struct Message* message, struct Interface* iface)
{
- #ifdef Illumos
- // Illumos does not support packet info.
- Message_shift(message, -4);
- #endif
-
struct TUNInterface_pvt* tun = Identity_cast((struct TUNInterface_pvt*) iface);
ssize_t ret = write(tun->tunSocket, message->bytes, message->length);
@@ -15,11 +15,9 @@
#ifndef TUNInterface_H
#define TUNInterface_H
-#include "benc/String.h"
#include "interface/Interface.h"
#include "memory/Allocator.h"
#include "util/events/EventBase.h"
-#include "wire/Message.h"
/**
* An interface which connects to the TUN/TAP device for
@@ -39,21 +37,6 @@ struct TUNInterface
* @return a TUNInterface structure.
*/
struct TUNInterface* TUNInterface_new(void* tunSocket,
- struct event_base* base,
+ struct EventBase* base,
struct Allocator* allocator);
-
-
-static inline void TUNInterface_pushMessageType(struct Message* message, uint16_t ethertype)
-{
- Message_shift(message, 4);
- ((uint16_t*) message->bytes)[0] = 0;
- ((uint16_t*) message->bytes)[1] = ethertype;
-}
-
-static inline uint16_t TUNInterface_popMessageType(struct Message* message)
-{
- Message_shift(message, -4);
- return ((uint16_t*) message->bytes)[-1];
-}
-
#endif
@@ -0,0 +1,31 @@
+/* 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 "interface/TUNMessageType.h"
+#include "wire/Message.h"
+
+#include <stdint.h>
+
+void TUNMessageType_push(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)
+{
+ Message_shift(message, -4);
+ return ((uint16_t*) message->bytes)[-1];
+}
@@ -0,0 +1,26 @@
+/* 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/>.
+ */
+#ifndef TUNMessageType_H
+#define TUNMessageType_H
+
+#include "wire/Message.h"
+
+#include <stdint.h>
+
+void TUNMessageType_push(struct Message* message, uint16_t ethertype);
+
+uint16_t TUNMessageType_pop(struct Message* message);
+
+#endif
@@ -0,0 +1,39 @@
+/* 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 ethertypeForPacketType(uint8_t highByte)
+{
+ return ((message->bytes[0] >> 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]);
+}
@@ -0,0 +1,42 @@
+/* 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>
+
+/**
+ * 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 = (ethertype == Ethernet_TYPE_IP6) ? AF_INET6 : AF_INET;
+
+ Message_shift(message, 4);
+ ((uint16_t*) message->bytes)[0] = 0;
+ ((uint16_t*) message->bytes)[1] = afType;
+}
+
+uint16_t TUNMessageType_pop(struct Message* message)
+{
+ Message_shift(message, -4);
+ uint16_t afType = ((uint16_t*) message->bytes)[-1];
+
+ return (afType == AF_INET6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
+}
@@ -23,6 +23,7 @@ set(Test_LIBRARIES
cjdns-admin
cjdns-admin-client
cjdns-memory-canary
+ cjdns-interface-tun-messagetype
cjdns-interface-pipeinterface
cjdadmintest
interface
@@ -22,6 +22,7 @@
#include "benc/Int.h"
#include "interface/UDPInterface_pvt.h"
#include "interface/TUNInterface.h"
+#include "interface/TUNMessageType.h"
#include "interface/TUNConfigurator.h"
#include "memory/Allocator.h"
#include "memory/MallocAllocator.h"
@@ -63,7 +64,7 @@ static int receivedMessageTUNCount = 0;
static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
{
receivedMessageTUNCount++;
- uint16_t ethertype = TUNInterface_popMessageType(msg);
+ uint16_t ethertype = TUNMessageType_pop(msg);
if (ethertype != Ethernet_TYPE_IP4) {
printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
return 0;
@@ -79,7 +80,7 @@ static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
Bits_memcpyConst(header->destAddr, testAddrA, 4);
Bits_memcpyConst(header->sourceAddr, testAddrB, 4);
- TUNInterface_pushMessageType(msg, ethertype);
+ TUNMessageType_push(msg, ethertype);
return iface->sendMessage(msg, iface);
}
@@ -22,6 +22,7 @@
#include "benc/Int.h"
#include "interface/UDPInterface_pvt.h"
#include "interface/TUNInterface.h"
+#include "interface/TUNMessageType.h"
#include "interface/TUNConfigurator.h"
#include "memory/Allocator.h"
#include "memory/MallocAllocator.h"
@@ -62,7 +63,7 @@ static int receivedMessageTUNCount = 0;
static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
{
receivedMessageTUNCount++;
- uint16_t ethertype = TUNInterface_popMessageType(msg);
+ uint16_t ethertype = TUNMessageType_pop(msg);
if (ethertype != Ethernet_TYPE_IP6) {
printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
return 0;
@@ -82,7 +83,7 @@ static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
Bits_memcpyConst(header->destinationAddr, testAddrA, 16);
Bits_memcpyConst(header->sourceAddr, testAddrB, 16);
- TUNInterface_pushMessageType(msg, ethertype);
+ TUNMessageType_push(msg, ethertype);
return iface->sendMessage(msg, iface);
}
View
@@ -18,6 +18,7 @@ add_library(cjdnet
)
target_link_libraries(cjdnet
+ cjdns-interface-tun-messagetype
cjdbenc
crypto
interface
View
@@ -21,7 +21,7 @@
#include "dht/DHTModuleRegistry.h"
#include "dht/dhtcore/Node.h"
#include "dht/dhtcore/RouterModule.h"
-#include "interface/TUNInterface.h"
+#include "interface/TUNMessageType.h"
#include "interface/Interface.h"
#include "interface/SessionManager.h"
#include "util/log/Log.h"
@@ -299,7 +299,7 @@ static inline uint8_t incomingForMe(struct Message* message,
Bits_memmoveConst(message->bytes, context->ip6Header, Headers_IP6Header_SIZE);
}
- TUNInterface_pushMessageType(message, Ethernet_TYPE_IP6);
+ TUNMessageType_push(message, Ethernet_TYPE_IP6);
context->userIf->sendMessage(message, context->userIf);
return Error_NONE;
@@ -407,7 +407,7 @@ static inline uint8_t incomingFromTun(struct Message* message,
{
struct Ducttape_pvt* context = Identity_cast((struct Ducttape_pvt*) iface->receiverContext);
- uint16_t ethertype = TUNInterface_popMessageType(message);
+ uint16_t ethertype = TUNMessageType_pop(message);
struct Headers_IP6Header* header = (struct Headers_IP6Header*) message->bytes;
@@ -72,7 +72,7 @@ struct TestFramework* TestFramework_setUp(char* privateKey,
SerializationModule_register(registry, logger, allocator);
- struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand);
+ struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand, NULL);
struct Ducttape* dt =
Ducttape_register((uint8_t*)privateKey, registry, routerModule,
@@ -27,7 +27,7 @@
#include "net/Ducttape_pvt.h"
#include "wire/Headers.h"
#include "wire/Ethernet.h"
-#include "interface/TUNInterface.h"
+#include "interface/TUNMessageType.h"
#include <stdio.h>
@@ -36,23 +36,23 @@
#define TUNA 1
uint8_t incomingTunC(struct Message* msg, struct Interface* iface)
{
- Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6);
+ Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE);
printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length);
*((int*)iface->senderContext) = TUNC;
return 0;
}
uint8_t incomingTunB(struct Message* msg, struct Interface* iface)
{
- Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6);
+ Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE);
printf("Message from TUN in node B [%s]\n", msg->bytes);
*((int*)iface->senderContext) = TUNB;
return 0;
}
uint8_t incomingTunA(struct Message* msg, struct Interface* iface)
{
- Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6);
+ Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE);
uint8_t buff[1024];
Hex_encode(buff, 1024, msg->bytes, msg->length);
@@ -164,7 +164,7 @@ void sendMessage(struct ThreeNodes* tn,
Assert_always(false);
}
- TUNInterface_pushMessageType(msg, Ethernet_TYPE_IP6);
+ TUNMessageType_push(msg, Ethernet_TYPE_IP6);
fromIf->receiveMessage(msg, fromIf);
if (to == tn->nodeA) {
Oops, something went wrong. Retry.

0 comments on commit b0472e8

Please sign in to comment.