Skip to content

Commit

Permalink
Fixes for #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
perlun committed Aug 24, 2015
1 parent 01b81d8 commit 9ee092a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 90 deletions.
83 changes: 26 additions & 57 deletions servers/network/ipv4/forward.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
/* $Id$ */
/* Abstract: Packet forwarding code. */
/* Author: Per Lundberg <per@halleluja.nu> */

/* Copyright 2000 chaos development. */

/* This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
// Abstract: Packet forwarding code.
// Author: Per Lundberg <per@halleluja.nu>
//
// © Copyright 2000 chaos development
// © Copyright 2015 chaos development

#include "arp.h"
#include "forward.h"
#include "ipv4.h"
#include "route.h"

/* Set this to FALSE if you don't want packet forwarding turned on. */

// Set this to FALSE if you don't want packet forwarding turned on.
bool forwarding = TRUE;

void forward_packet(ipv4_ethernet_header_type *ethernet_header,
unsigned int length)
void forward_packet(ipv4_ethernet_header_type *ethernet_header, unsigned int length)
{
ipv4_header_type *ipv4_header =
(ipv4_header_type *) &ethernet_header->data;
ipv4_header_type *ipv4_header = (ipv4_header_type *) &ethernet_header->data;
ipv4_interface_type *interface;
bool direct;
u8 ethernet_address[IPV4_ETHERNET_ADDRESS_LENGTH];
Expand All @@ -41,60 +23,47 @@ void forward_packet(ipv4_ethernet_header_type *ethernet_header,

if (!forwarding)
{
log_print(&log_structure, LOG_URGENCY_INFORMATIVE,
"Someone tried to route a packet through me, but I'm not acting under the role of a router");
log_print(&log_structure, LOG_URGENCY_INFORMATIVE, "Someone tried to route a packet through me, but I'm not acting under the role of a router");
return;
}

/* Find the interface to use for this packet. */

if (!route_find(ipv4_header->destination_address, &interface, &direct,
&ethernet_structure))
// Find the interface to use for this packet.
if (!route_find(ipv4_header->destination_address, &interface, &direct, &ethernet_structure))
{
char address[16];

/* We found no way to handle this packet. */

// We found no way to handle this packet.
ipv4_address_to_string(address, ipv4_header->destination_address);
log_print_formatted(&log_structure, LOG_URGENCY_INFORMATIVE,
"Couldn't route packet to %s: No applicable route found. Dropping.\n",
address);
return;
}

/* Now, do the routing. If the 'direct' flag is set, the target is
located on this subnet. Otherwise, its router is. */
// Now, do the routing. If the 'direct' flag is set, the target is located on this subnet. Otherwise, its router is.
char from_address[16];
char to_address[16];

{
char from_address[16];
char to_address[16];

ipv4_address_to_string(from_address, ipv4_header->source_address);
ipv4_address_to_string(to_address, ipv4_header->destination_address);
log_print_formatted(&log_structure, LOG_URGENCY_DEBUG,
"Routing packet: %s -> %s, via %s",
from_address, to_address,
interface->identification);
}
ipv4_address_to_string(from_address, ipv4_header->source_address);
ipv4_address_to_string(to_address, ipv4_header->destination_address);
log_print_formatted(&log_structure, LOG_URGENCY_DEBUG,
"Routing packet: %s -> %s, via %s",
from_address, to_address,
interface->identification);

while (!arp_ip_to_ethernet_address(ipv4_header->destination_address,
ethernet_address))
while (!arp_ip_to_ethernet_address(ipv4_header->destination_address, ethernet_address))
{
log_print_formatted(&log_structure, LOG_URGENCY_DEBUG,
"Sending ARP who-has for %lX on %s.",
ipv4_header->destination_address,
interface->identification);
arp_who_has(ipv4_header->destination_address, interface,
ethernet_structure);
arp_who_has(ipv4_header->destination_address, interface, ethernet_structure);
system_sleep(500);
}

/* Send the packet. */

memory_copy(ethernet_header->destination_address, ethernet_address,
IPV4_ETHERNET_ADDRESS_LENGTH);
memory_copy(ethernet_header->source_address, interface->hardware_address,
IPV4_ETHERNET_ADDRESS_LENGTH);
// Send the packet.
memory_copy(ethernet_header->destination_address, ethernet_address, IPV4_ETHERNET_ADDRESS_LENGTH);
memory_copy(ethernet_header->source_address, interface->hardware_address, IPV4_ETHERNET_ADDRESS_LENGTH);
message_parameter.data = ethernet_header;
message_parameter.message_class = IPC_ETHERNET_PACKET_SEND;
message_parameter.protocol = IPC_PROTOCOL_ETHERNET;
Expand Down
41 changes: 8 additions & 33 deletions servers/network/ipv4/forward.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
/* $Id$ */
/* Abstract: Function prototypes for the packet forwarding code. */
/* Author: Per Lundberg <per@halleluja.nu> */
// Abstract: Function prototypes for the packet forwarding code.
// Author: Per Lundberg <per@halleluja.nu>
//
// © Copyright 2000 chaos development
// © Copyright 2015 chaos development

/* Copyright 2000 chaos development. */

/* This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */

#ifndef __FORWARD_H__
#define __FORWARD_H__

#include "config.h"

/* Function prototypes. */

extern void forward_packet(ipv4_ethernet_header_type *ethernet_header,
unsigned int length);

/* Globals. */
// Function prototypes.
extern void forward_packet(ipv4_ethernet_header_type *ethernet_header, unsigned int length);

// Globals.
extern bool forwarding;

#endif /* !__FORWARD_H__ */

0 comments on commit 9ee092a

Please sign in to comment.