2
* router.cpp - ip router
4
* Basilisk II (C) 1997-2001 Christian Bauer
6
* Windows platform specific code copyright (C) Lauri Pesonen
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* This could be implemented by writing three (9x,nt,2k)
25
* NDIS filter drivers. No thanks.
26
* But this is not easy either.
31
#define WIN32_LEAN_AND_MEAN
35
#include "cpu_emulation.h"
37
#include "ether_windows.h"
40
#include "router_types.h"
41
#include "dynsockets.h"
49
#include "mib/interfaces.h"
54
#pragma optimize("",off)
60
uint16 next_ip_ident_number = 1;
61
uint32 macos_ip_address = 0;
62
const uint8 router_mac_addr[6] = { '4', '2', '6', '7', '7', '9' };
63
uint32 router_ip_address = 0;
64
bool raw_sockets_available = false;
69
CRITICAL_SECTION router_section;
70
bool is_router_shutting_down = false;
71
static HANDLE r_handle = 0;
72
static unsigned int rh_tid = 0;
75
static void write_ip4( ip_t *ip, int len )
77
if(len < sizeof(ip_t)) {
78
D(bug("Too small ip packet(%d), dropped\r\n", len));
80
uint8 proto = ip->proto;
82
// This is a router, decrement the hop count
83
if( --ip->ttl == 0 ) {
84
// Most likely this is some Mac traceroute app
85
D(bug("ip packet ttl expired, proto=%d.\r\n", proto));
86
error_winsock_2_icmp( WSAETTLEXCEEDED, ip, len );
90
write_icmp( (icmp_t *)ip, len );
93
write_tcp( (tcp_t *)ip, len );
96
write_udp( (udp_t *)ip, len );
99
D(bug("write_ip4() len=%d, proto=%d\r\n", len, proto));
106
bool router_write_packet(uint8 *packet, int len)
111
switch( ntohs( ((mac_t *)packet)->type ) ) {
113
write_ip4( (ip_t *)packet, len );
117
D(bug("write_ip6() len=%d; unsupported.\r\n", len));
121
result = write_arp( (arp_t *)packet, len );
128
bool router_read_packet(uint8 *packet, int len)
133
switch( ntohs( ((mac_t *)packet)->type ) ) {
145
This has nothing to do with TCP TIME_WAITs or CLOSE_WAITs,
146
the thread is needed to close down expired udp sockets.
147
Arguably an ugly hack, but needed since there is no way to
148
listen to all ports w/o writing another ndis filter driver
150
static WINAPI unsigned int router_expire_thread(void *arg)
152
while(!is_router_shutting_down) {
159
bool router_init(void)
161
InitializeCriticalSection( &router_section );
163
if(dynsockets_init()) {
165
if( _gethostname(me, sizeof(me)) == SOCKET_ERROR ) {
166
D(bug("gethostname() failed, error = %d\r\n", _WSAGetLastError()));
168
struct hostent *hent = _gethostbyname(me);
170
D(bug("gethostbyname() failed, error = %d\r\n", _WSAGetLastError()));
172
struct in_addr *ina = (struct in_addr *) *hent->h_addr_list;
173
router_ip_address = ntohl(ina->s_addr);
174
D(bug("router protocol address seems to be %s (used only in icmp error messages)\r\n", _inet_ntoa(*ina)));
177
is_router_shutting_down = false;
178
r_handle = (HANDLE)_beginthreadex( 0, 0, router_expire_thread, 0, 0, &rh_tid );
189
void router_final(void)
195
is_router_shutting_down = true;
196
WaitForSingleObject( r_handle, INFINITE );
201
DeleteCriticalSection( &router_section );