1
0
mirror of https://github.com/solemnwarning/ipxwrapper synced 2024-12-30 16:45:37 +01:00
ipxwrapper/src/ipxwrapper.c

484 lines
11 KiB
C
Raw Normal View History

2008-12-09 21:36:07 +00:00
/* ipxwrapper - Library functions
* Copyright (C) 2008 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <windows.h>
#include <winsock2.h>
#include <wsipx.h>
#include <nspapi.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <time.h>
2008-12-09 21:36:07 +00:00
#include "ipxwrapper.h"
2011-08-28 21:27:06 +00:00
#include "common.h"
2008-12-09 21:36:07 +00:00
#define DLL_UNLOAD(dll) \
if(dll) {\
FreeModule(dll);\
dll = NULL;\
}
ipx_socket *sockets = NULL;
ipx_nic *nics = NULL;
ipx_host *hosts = NULL;
SOCKET net_fd = -1;
struct reg_global global_conf;
static void *router_buf = NULL;
2008-12-09 21:36:07 +00:00
HMODULE winsock2_dll = NULL;
HMODULE mswsock_dll = NULL;
HMODULE wsock32_dll = NULL;
static HANDLE mutex = NULL;
static HANDLE router_thread = NULL;
static DWORD router_tid = 0;
static int init_router(void);
static DWORD WINAPI router_main(LPVOID argp);
static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr);
2011-04-24 00:24:10 +00:00
static BOOL load_nics(void);
2008-12-09 21:36:07 +00:00
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
if(why == DLL_PROCESS_ATTACH) {
log_open();
2008-12-09 21:36:07 +00:00
winsock2_dll = load_sysdll("ws2_32.dll");
mswsock_dll = load_sysdll("mswsock.dll");
wsock32_dll = load_sysdll("wsock32.dll");
if(!winsock2_dll || !mswsock_dll || !wsock32_dll) {
return FALSE;
}
2011-08-28 21:27:06 +00:00
reg_open(KEY_QUERY_VALUE);
2011-08-28 21:27:06 +00:00
if(reg_get_bin("global", &global_conf, sizeof(global_conf)) != sizeof(global_conf)) {
global_conf.udp_port = DEFAULT_PORT;
global_conf.w95_bug = 1;
global_conf.bcast_all = 0;
global_conf.filter = 1;
}
2011-04-24 00:24:10 +00:00
if(!load_nics()) {
return FALSE;
2008-12-09 21:36:07 +00:00
}
mutex = CreateMutex(NULL, FALSE, NULL);
if(!mutex) {
log_printf("Failed to create mutex");
2008-12-09 21:36:07 +00:00
return FALSE;
}
WSADATA wsdata;
int err = WSAStartup(MAKEWORD(1,1), &wsdata);
if(err) {
log_printf("Failed to initialize winsock: %s", w32_error(err));
2008-12-09 21:36:07 +00:00
return FALSE;
}
if(!init_router()) {
return FALSE;
}
}else if(why == DLL_PROCESS_DETACH) {
2008-12-09 21:36:07 +00:00
if(router_thread && GetCurrentThreadId() != router_tid) {
TerminateThread(router_thread, 0);
router_thread = NULL;
}
free(router_buf);
router_buf = NULL;
2008-12-09 21:36:07 +00:00
if(net_fd >= 0) {
closesocket(net_fd);
net_fd = -1;
}
if(mutex) {
CloseHandle(mutex);
mutex = NULL;
}
WSACleanup();
2011-08-28 21:27:06 +00:00
reg_close();
2008-12-09 21:36:07 +00:00
DLL_UNLOAD(winsock2_dll);
DLL_UNLOAD(mswsock_dll);
DLL_UNLOAD(wsock32_dll);
log_close();
2008-12-09 21:36:07 +00:00
}
return TRUE;
}
void __stdcall *find_sym(char const *symbol) {
2008-12-09 21:36:07 +00:00
void *addr = GetProcAddress(winsock2_dll, symbol);
if(!addr) {
addr = GetProcAddress(mswsock_dll, symbol);
}
if(!addr) {
addr = GetProcAddress(wsock32_dll, symbol);
}
if(!addr) {
log_printf("Unknown symbol: %s", symbol);
2008-12-09 21:36:07 +00:00
abort();
}
return addr;
}
/* Lock the mutex and search the sockets list for an ipx_socket structure with
* the requested fd, if no matching fd is found, unlock the mutex
*
* TODO: Change this behaviour. It is almost as bad as the BKL.
2008-12-09 21:36:07 +00:00
*/
ipx_socket *get_socket(SOCKET fd) {
lock_mutex();
ipx_socket *ptr = sockets;
while(ptr) {
if(ptr->fd == fd) {
break;
}
ptr = ptr->next;
}
if(!ptr) {
unlock_mutex();
}
return ptr;
}
/* Lock the mutex */
void lock_mutex(void) {
WaitForSingleObject(mutex, INFINITE);
}
/* Unlock the mutex */
void unlock_mutex(void) {
2008-12-11 22:00:58 +00:00
while(ReleaseMutex(mutex)) {}
2008-12-09 21:36:07 +00:00
}
IP_ADAPTER_INFO *get_nics(void) {
IP_ADAPTER_INFO *buf, tbuf;
ULONG bufsize = sizeof(IP_ADAPTER_INFO);
int rval = GetAdaptersInfo(&tbuf, &bufsize);
2008-12-09 23:49:48 +00:00
if(rval != ERROR_SUCCESS && rval != ERROR_BUFFER_OVERFLOW) {
2008-12-09 21:36:07 +00:00
WSASetLastError(rval);
return NULL;
}
buf = malloc(bufsize);
if(!buf) {
WSASetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
rval = GetAdaptersInfo(buf, &bufsize);
if(rval != ERROR_SUCCESS) {
WSASetLastError(rval);
free(buf);
return NULL;
}
return buf;
}
/* Initialize and start the router thread */
static int init_router(void) {
net_fd = r_socket(AF_INET, SOCK_DGRAM, 0);
2008-12-09 21:36:07 +00:00
if(net_fd == -1) {
log_printf("Failed to create network socket: %s", w32_error(WSAGetLastError()));
2008-12-09 21:36:07 +00:00
return 0;
}
struct sockaddr_in bind_addr;
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = INADDR_ANY;
bind_addr.sin_port = htons(global_conf.udp_port);
2008-12-09 21:36:07 +00:00
if(r_bind(net_fd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) == -1) {
log_printf("Failed to bind network socket: %s", w32_error(WSAGetLastError()));
2008-12-09 21:36:07 +00:00
return 0;
}
BOOL broadcast = TRUE;
int bufsize = 524288; /* 512KiB */
r_setsockopt(net_fd, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(BOOL));
r_setsockopt(net_fd, SOL_SOCKET, SO_RCVBUF, (char*)&bufsize, sizeof(int));
r_setsockopt(net_fd, SOL_SOCKET, SO_SNDBUF, (char*)&bufsize, sizeof(int));
2008-12-09 21:36:07 +00:00
router_buf = malloc(PACKET_BUF_SIZE);
if(!router_buf) {
log_printf("Not enough memory for router buffer (64KiB)");
2008-12-09 21:36:07 +00:00
return 0;
}
router_thread = CreateThread(NULL, 0, &router_main, NULL, 0, &router_tid);
2008-12-09 21:36:07 +00:00
if(!router_thread) {
log_printf("Failed to create router thread");
2008-12-09 21:36:07 +00:00
return 0;
}
return 1;
}
/* Router thread main function
*
* The router thread recieves packets from the listening port and forwards them
* to the UDP sockets which emulate IPX.
*/
static DWORD WINAPI router_main(LPVOID notused) {
ipx_packet *packet = router_buf;
2008-12-09 21:36:07 +00:00
int addrlen, rval, sval;
unsigned char f6[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
struct sockaddr_in addr;
ipx_socket *sockptr;
while(1) {
addrlen = sizeof(addr);
rval = r_recvfrom(net_fd, (char*)packet, PACKET_BUF_SIZE, 0, (struct sockaddr*)&addr, &addrlen);
2008-12-09 21:36:07 +00:00
if(rval <= 0) {
log_printf("Error recieving packet: %s", w32_error(WSAGetLastError()));
2008-12-09 21:36:07 +00:00
continue;
}
if(global_conf.filter) {
ipx_nic *nic = nics;
while(nic) {
if((nic->ipaddr & nic->netmask) == (addr.sin_addr.s_addr & nic->netmask)) {
break;
}
nic = nic->next;
}
if(!nic) {
/* Packet not recieved from subnet of an enabled interface */
continue;
}
}
2008-12-09 21:36:07 +00:00
packet->size = ntohs(packet->size);
if(packet->size > MAX_PACKET_SIZE || packet->size+sizeof(ipx_packet)-1 != rval) {
log_printf("Recieved packet with incorrect size field, discarding");
2008-12-09 21:36:07 +00:00
continue;
}
lock_mutex();
add_host(packet->src_net, packet->src_node, addr.sin_addr.s_addr);
2008-12-09 21:36:07 +00:00
for(sockptr = sockets; sockptr; sockptr = sockptr->next) {
if(
sockptr->flags & IPX_RECV &&
(
!(sockptr->flags & IPX_FILTER) ||
packet->ptype == sockptr->f_ptype
) && ((
sockptr->flags & IPX_BOUND &&
packet->dest_socket == sockptr->socket &&
2008-12-09 21:36:07 +00:00
(
memcmp(packet->dest_net, sockptr->nic->ipx_net, 4) == 0 ||
(
memcmp(packet->dest_net, f6, 4) == 0 &&
(!global_conf.w95_bug || sockptr->flags & IPX_BROADCAST)
)
) && (
memcmp(packet->dest_node, sockptr->nic->ipx_node, 6) == 0 ||
(
memcmp(packet->dest_node, f6, 6) == 0 &&
(!global_conf.w95_bug || sockptr->flags & IPX_BROADCAST)
)
2008-12-09 21:36:07 +00:00
)
) || (
sockptr->flags & IPX_EX_BOUND &&
packet->dest_socket == sockptr->ex_socket &&
2008-12-09 21:36:07 +00:00
(
memcmp(packet->dest_net, sockptr->ex_nic->ipx_net, 4) == 0 ||
memcmp(packet->dest_net, f6, 4) == 0
) && (
memcmp(packet->dest_node, sockptr->ex_nic->ipx_node, 6) == 0 ||
memcmp(packet->dest_node, f6, 6) == 0
2008-12-09 21:36:07 +00:00
)
))
2008-12-09 21:36:07 +00:00
) {
addrlen = sizeof(addr);
if(r_getsockname(sockptr->fd, (struct sockaddr*)&addr, &addrlen) == -1) {
continue;
}
sval = r_sendto(sockptr->fd, (char*)packet, rval, 0, (struct sockaddr*)&addr, addrlen);
2008-12-09 21:36:07 +00:00
if(sval == -1) {
log_printf("Error relaying packet: %s", w32_error(WSAGetLastError()));
2008-12-09 21:36:07 +00:00
}
}
}
unlock_mutex();
}
return 0;
}
/* Add a host to the hosts list or update an existing one */
static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr) {
2008-12-09 21:36:07 +00:00
ipx_host *hptr = hosts;
while(hptr) {
if(memcmp(hptr->ipx_net, net, 4) == 0 && memcmp(hptr->ipx_node, node, 6) == 0) {
2008-12-09 21:36:07 +00:00
hptr->ipaddr = ipaddr;
hptr->last_packet = time(NULL);
2008-12-09 21:36:07 +00:00
return;
}
hptr = hptr->next;
}
hptr = malloc(sizeof(ipx_host));
if(!hptr) {
log_printf("No memory for hosts list entry");
2008-12-09 21:36:07 +00:00
return;
}
memcpy(hptr->ipx_net, net, 4);
memcpy(hptr->ipx_node, node, 6);
2008-12-09 21:36:07 +00:00
hptr->ipaddr = ipaddr;
hptr->last_packet = time(NULL);
2008-12-09 21:36:07 +00:00
hptr->next = hosts;
hosts = hptr;
}
/* Search the hosts list */
ipx_host *find_host(const unsigned char *net, const unsigned char *node) {
ipx_host *hptr = hosts, *pptr = NULL;
2008-12-09 21:36:07 +00:00
while(hptr) {
if(memcmp(hptr->ipx_net, net, 4) == 0 && memcmp(hptr->ipx_node, node, 6) == 0) {
if(hptr->last_packet+TTL < time(NULL)) {
/* Host record has expired, delete */
if(pptr) {
pptr->next = hptr->next;
free(hptr);
}else{
hosts = hptr->next;
free(hptr);
}
return NULL;
}else{
return hptr;
}
2008-12-09 21:36:07 +00:00
}
pptr = hptr;
2008-12-09 21:36:07 +00:00
hptr = hptr->next;
}
return NULL;
}
2011-04-24 00:24:10 +00:00
static BOOL load_nics(void) {
IP_ADAPTER_INFO *ifroot = get_nics();
IP_ADAPTER_INFO *ifptr = ifroot;
ipx_nic *enic = NULL;
if(!ifptr) {
log_printf("No NICs: %s", w32_error(WSAGetLastError()));
2011-04-24 00:24:10 +00:00
}
while(ifptr) {
struct reg_value rv;
int got_rv = 0;
2011-08-28 21:27:06 +00:00
char vname[18];
NODE_TO_STRING(vname, ifptr->Address);
if(reg_get_bin(vname, &rv, sizeof(rv)) == sizeof(rv)) {
got_rv = 1;
2011-04-24 00:24:10 +00:00
}
if(got_rv && !rv.enabled) {
/* Interface has been disabled, don't add it */
ifptr = ifptr->Next;
continue;
}
ipx_nic *nnic = malloc(sizeof(ipx_nic));
if(!nnic) {
return FALSE;
}
nnic->ipaddr = inet_addr(ifptr->IpAddressList.IpAddress.String);
nnic->netmask = inet_addr(ifptr->IpAddressList.IpMask.String);
nnic->bcast = nnic->ipaddr | ~nnic->netmask;
2011-04-24 00:24:10 +00:00
memcpy(nnic->hwaddr, ifptr->Address, 6);
if(got_rv) {
memcpy(nnic->ipx_net, rv.ipx_net, 4);
memcpy(nnic->ipx_node, rv.ipx_node, 6);
}else{
unsigned char net[] = {0,0,0,1};
memcpy(nnic->ipx_net, net, 4);
memcpy(nnic->ipx_node, nnic->hwaddr, 6);
}
nnic->next = NULL;
if(got_rv && rv.primary) {
/* Force primary flag set, insert at start of NIC list */
nnic->next = nics;
nics = nnic;
if(!enic) {
enic = nnic;
}
2011-04-24 00:24:10 +00:00
}else if(enic) {
enic->next = nnic;
enic = nnic;
}else{
enic = nics = nnic;
}
ifptr = ifptr->Next;
}
free(ifroot);
return TRUE;
}