-void route_outgoing(vpn_packet_t *packet)
-{
- unsigned short int type;
- avl_tree_t *node;
- connection_t *cl;
-cp
- /* FIXME: multicast? */
-
- switch(routing_mode)
- {
- case RMODE_ROUTER:
- type = ntohs(*((unsigned short*)(&packet->data[12])));
- switch(type)
- {
- case 0x0800:
- cl = route_ipv4(packet);
- break;
- case 0x86DD:
- cl = route_ipv6(packet);
- break;
- default:
- if(debug_lvl >= DEBUG_TRAFFIC)
- {
- syslog(LOG_WARNING, _("Cannot route packet: unknown type %hx"), type);
- }
- return;
- }
- send_packet(cl, packet);
- break;
-
- case RMODE_SWITCH:
- cl = route_mac(packet);
- if(cl)
- send_packet(cl, packet);
- break;
-
- case RMODE_HUB:
- for(node = connection_tree->head; node; node = node->next)
- {
- cl = (connection_t *)node->data;
- if(cl->status.active)
- send_packet(cl, packet);
- }
- break;
- }
-}
+ //Check Length
+ if(!checklength(source, packet, sizeof(*hdr))) {
+ return;
+ }
+
+ if(dest == NULL) {
+ //Lookup failed
+ logger(mesh, MESHLINK_WARNING, "Can't lookup the destination of a packet in the route() function. This should never happen!\n");
+ logger(mesh, MESHLINK_WARNING, "Destination was: %s\n", hdr->destination);
+ return;
+ }
+
+ if(dest == mesh->self) {
+ const void *payload = packet->data + sizeof(*hdr);
+ size_t len = packet->len - sizeof(*hdr);
+
+ char hex[len * 2 + 1];
+
+ if(mesh->log_level <= MESHLINK_DEBUG) {
+ bin2hex(payload, hex, len); // don't do this unless it's going to be logged
+ }
+
+ logger(mesh, MESHLINK_DEBUG, "I received a packet for me with payload: %s\n", hex);
+
+ if(mesh->receive_cb) {
+ mesh->receive_cb(mesh, (meshlink_node_t *)source, payload, len);
+ }
+
+ return;
+ }
+
+ if(!dest->status.reachable) {
+ //TODO: check what to do here, not just print a warning
+ logger(mesh, MESHLINK_WARNING, "The destination of a packet in the route() function is unreachable. Dropping packet.\n");
+ return;
+ }
+
+ if(dest == source) {
+ logger(mesh, MESHLINK_ERROR, "Routing loop for packet from %s!", source->name);
+ return;
+ }