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

286 lines
6.4 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"
#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 send_fd = -1;
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;
struct rclient g_rclient;
2008-12-09 21:36:07 +00:00
2011-09-08 00:20:34 +00:00
static CRITICAL_SECTION sockets_cs;
static CRITICAL_SECTION hosts_cs;
#define INIT_CS(cs) if(!init_cs(cs, &initialised_cs)) { return FALSE; }
static BOOL init_cs(CRITICAL_SECTION *cs, int *counter) {
2011-09-09 00:45:16 +00:00
if(!InitializeCriticalSectionAndSpinCount(cs, 0x80000000)) {
2011-09-08 00:20:34 +00:00
log_printf("Failed to initialise critical section: %s", w32_error(GetLastError()));
return FALSE;
}
(*counter)++;
return TRUE;
}
2008-12-09 21:36:07 +00:00
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
2011-09-08 00:20:34 +00:00
static int initialised_cs = 0;
2008-12-09 21:36:07 +00:00
if(why == DLL_PROCESS_ATTACH) {
log_open();
2008-12-09 21:36:07 +00:00
if(!rclient_init(&g_rclient)) {
return FALSE;
}
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-08-29 13:15:10 +00:00
nics = get_interfaces(-1);
2008-12-09 21:36:07 +00:00
2011-09-08 00:20:34 +00:00
INIT_CS(&sockets_cs);
INIT_CS(&hosts_cs);
2008-12-09 21:36:07 +00:00
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(!rclient_start(&g_rclient)) {
2008-12-09 21:36:07 +00:00
return FALSE;
}
if(g_rclient.router) {
send_fd = g_rclient.router->udp_sock;
}else{
/* Create UDP socket for sending packets if not using a private router */
if((send_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
log_printf("Failed to create UDP socket: %s", w32_error(WSAGetLastError()));
return FALSE;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 0;
if(bind(send_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
log_printf("Failed to bind UDP socket (send_fd): %s", w32_error(WSAGetLastError()));
return FALSE;
}
}
}else if(why == DLL_PROCESS_DETACH) {
if(send_fd != -1 && !g_rclient.router) {
closesocket(send_fd);
}
send_fd = -1;
rclient_stop(&g_rclient);
2008-12-09 21:36:07 +00:00
2011-09-08 00:20:34 +00:00
WSACleanup();
switch(initialised_cs) {
case 2: DeleteCriticalSection(&hosts_cs);
case 1: DeleteCriticalSection(&sockets_cs);
default: break;
2008-12-09 21:36:07 +00:00
}
2011-09-08 00:20:34 +00:00
initialised_cs = 0;
2011-08-29 13:15:10 +00:00
2011-09-08 00:20:34 +00:00
free_interfaces(nics);
2008-12-09 21:36:07 +00:00
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) {
2011-09-08 00:20:34 +00:00
lock_sockets();
2008-12-09 21:36:07 +00:00
ipx_socket *ptr = sockets;
while(ptr) {
if(ptr->fd == fd) {
break;
}
ptr = ptr->next;
}
if(!ptr) {
2011-09-08 00:20:34 +00:00
unlock_sockets();
2008-12-09 21:36:07 +00:00
}
return ptr;
}
/* Lock the mutex */
2011-09-08 00:20:34 +00:00
void lock_sockets(void) {
EnterCriticalSection(&sockets_cs);
2008-12-09 21:36:07 +00:00
}
/* Unlock the mutex */
2011-09-08 00:20:34 +00:00
void unlock_sockets(void) {
LeaveCriticalSection(&sockets_cs);
2008-12-09 21:36:07 +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) {
2011-09-08 00:20:34 +00:00
EnterCriticalSection(&hosts_cs);
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);
2011-09-08 00:20:34 +00:00
LeaveCriticalSection(&hosts_cs);
2008-12-09 21:36:07 +00:00
return;
}
hptr = hptr->next;
}
hptr = malloc(sizeof(ipx_host));
if(!hptr) {
2011-09-08 00:20:34 +00:00
LeaveCriticalSection(&hosts_cs);
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;
2011-09-08 00:20:34 +00:00
LeaveCriticalSection(&hosts_cs);
2008-12-09 21:36:07 +00:00
}
/* Search the hosts list */
ipx_host *find_host(const unsigned char *net, const unsigned char *node) {
2011-09-08 00:20:34 +00:00
EnterCriticalSection(&hosts_cs);
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);
}
2011-09-08 00:20:34 +00:00
hptr = NULL;
}
2011-09-08 00:20:34 +00:00
break;
2008-12-09 21:36:07 +00:00
}
pptr = hptr;
2008-12-09 21:36:07 +00:00
hptr = hptr->next;
}
2011-09-08 00:20:34 +00:00
LeaveCriticalSection(&hosts_cs);
return hptr;
2008-12-09 21:36:07 +00:00
}