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>
|
2011-04-23 23:42:14 +00:00
|
|
|
#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"
|
2011-08-29 13:21:18 +00:00
|
|
|
#include "interface.h"
|
2011-09-07 20:03:16 +00:00
|
|
|
#include "router.h"
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
#define DLL_UNLOAD(dll) \
|
|
|
|
if(dll) {\
|
|
|
|
FreeModule(dll);\
|
|
|
|
dll = NULL;\
|
|
|
|
}
|
|
|
|
|
|
|
|
ipx_socket *sockets = NULL;
|
2011-08-29 13:15:10 +00:00
|
|
|
struct ipx_interface *nics = NULL;
|
2008-12-09 21:36:07 +00:00
|
|
|
ipx_host *hosts = NULL;
|
|
|
|
SOCKET net_fd = -1;
|
2011-04-24 18:44:31 +00:00
|
|
|
struct reg_global global_conf;
|
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;
|
2011-09-07 20:03:16 +00:00
|
|
|
struct router_vars *router = NULL;
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
static BOOL start_router(void);
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
|
|
|
|
if(why == DLL_PROCESS_ATTACH) {
|
2011-07-13 22:56:19 +00:00
|
|
|
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-04-23 19:49:53 +00:00
|
|
|
|
2011-08-28 21:27:06 +00:00
|
|
|
if(reg_get_bin("global", &global_conf, sizeof(global_conf)) != sizeof(global_conf)) {
|
2011-04-24 18:44:31 +00:00
|
|
|
global_conf.udp_port = DEFAULT_PORT;
|
|
|
|
global_conf.w95_bug = 1;
|
2011-04-24 21:55:57 +00:00
|
|
|
global_conf.bcast_all = 0;
|
2011-04-24 23:32:10 +00:00
|
|
|
global_conf.filter = 1;
|
2011-04-24 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
2011-08-29 13:15:10 +00:00
|
|
|
nics = get_interfaces(-1);
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
mutex = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
if(!mutex) {
|
2011-07-13 22:56:19 +00:00
|
|
|
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) {
|
2011-07-13 22:56:19 +00:00
|
|
|
log_printf("Failed to initialize winsock: %s", w32_error(err));
|
2008-12-09 21:36:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
if(!start_router()) {
|
2008-12-09 21:36:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-04-24 23:49:58 +00:00
|
|
|
}else if(why == DLL_PROCESS_DETACH) {
|
2011-09-07 20:03:16 +00:00
|
|
|
if(router_thread) {
|
|
|
|
EnterCriticalSection(&(router->crit_sec));
|
|
|
|
|
|
|
|
router->running = FALSE;
|
|
|
|
SetEvent(router->wsa_event);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&(router->crit_sec));
|
|
|
|
|
|
|
|
WaitForSingleObject(router_thread, INFINITE);
|
2008-12-09 21:36:07 +00:00
|
|
|
router_thread = NULL;
|
2011-09-07 20:03:16 +00:00
|
|
|
|
|
|
|
router_destroy(router);
|
|
|
|
router = NULL;
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mutex) {
|
|
|
|
CloseHandle(mutex);
|
|
|
|
mutex = NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-29 13:15:10 +00:00
|
|
|
free_interfaces(nics);
|
|
|
|
|
2008-12-09 21:36:07 +00:00
|
|
|
WSACleanup();
|
|
|
|
|
2011-08-28 21:27:06 +00:00
|
|
|
reg_close();
|
2011-04-23 19:49:53 +00:00
|
|
|
|
2008-12-09 21:36:07 +00:00
|
|
|
DLL_UNLOAD(winsock2_dll);
|
|
|
|
DLL_UNLOAD(mswsock_dll);
|
|
|
|
DLL_UNLOAD(wsock32_dll);
|
2011-07-13 22:56:19 +00:00
|
|
|
|
|
|
|
log_close();
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-01-25 17:06:29 +00:00
|
|
|
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) {
|
2011-07-13 22:56:19 +00:00
|
|
|
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
|
2011-04-24 01:23:10 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and start the router thread */
|
2011-09-07 20:03:16 +00:00
|
|
|
static BOOL start_router(void) {
|
|
|
|
if(!(router = router_init(FALSE))) {
|
|
|
|
return FALSE;
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
router_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&router_main, router, 0, NULL);
|
2008-12-09 21:36:07 +00:00
|
|
|
if(!router_thread) {
|
2011-09-07 20:03:16 +00:00
|
|
|
log_printf("Failed to create router thread: %s", w32_error(GetLastError()));
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
router_destroy(router);
|
|
|
|
router = NULL;
|
2011-04-24 23:32:10 +00:00
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
return FALSE;
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:03:16 +00:00
|
|
|
net_fd = router->udp_sock;
|
|
|
|
|
|
|
|
return TRUE;
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
2011-04-23 23:42:14 +00:00
|
|
|
/* Add a host to the hosts list or update an existing one */
|
2011-08-29 13:41:10 +00:00
|
|
|
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) {
|
2011-04-23 23:42:14 +00:00
|
|
|
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;
|
2011-04-23 23:42:14 +00:00
|
|
|
hptr->last_packet = time(NULL);
|
|
|
|
|
2008-12-09 21:36:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hptr = hptr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
hptr = malloc(sizeof(ipx_host));
|
|
|
|
if(!hptr) {
|
2011-07-13 22:56:19 +00:00
|
|
|
log_printf("No memory for hosts list entry");
|
2008-12-09 21:36:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-23 23:42:14 +00:00
|
|
|
memcpy(hptr->ipx_net, net, 4);
|
|
|
|
memcpy(hptr->ipx_node, node, 6);
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
hptr->ipaddr = ipaddr;
|
2011-04-23 23:42:14 +00:00
|
|
|
hptr->last_packet = time(NULL);
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
hptr->next = hosts;
|
|
|
|
hosts = hptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the hosts list */
|
2011-04-23 23:42:14 +00:00
|
|
|
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) {
|
2011-04-23 23:42:14 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-04-23 23:42:14 +00:00
|
|
|
pptr = hptr;
|
2008-12-09 21:36:07 +00:00
|
|
|
hptr = hptr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|