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;
|
|
|
|
ipx_host *hosts = NULL;
|
2011-09-09 19:03:19 +00:00
|
|
|
SOCKET send_fd = -1;
|
2011-04-24 18:44:31 +00:00
|
|
|
struct reg_global global_conf;
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-09-09 18:36:52 +00:00
|
|
|
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) {
|
2011-07-13 22:56:19 +00:00
|
|
|
log_open();
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-09-09 18:36:52 +00:00
|
|
|
if(!rclient_init(&g_rclient)) {
|
|
|
|
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-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) {
|
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-09 18:36:52 +00:00
|
|
|
if(!rclient_start(&g_rclient)) {
|
2008-12-09 21:36:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-09 19:03:19 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2011-04-24 23:49:58 +00:00
|
|
|
}else if(why == DLL_PROCESS_DETACH) {
|
2011-09-09 19:03:19 +00:00
|
|
|
if(send_fd != -1 && !g_rclient.router) {
|
|
|
|
closesocket(send_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
send_fd = -1;
|
|
|
|
|
2011-09-09 18:36:52 +00:00
|
|
|
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-08-28 21:27:06 +00:00
|
|
|
reg_close();
|
2011-04-23 19:49:53 +00:00
|
|
|
|
2011-09-11 13:28:41 +00:00
|
|
|
unload_dlls();
|
2011-07-13 22:56:19 +00:00
|
|
|
|
|
|
|
log_close();
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
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
|
|
|
}
|
|
|
|
|
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) {
|
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) {
|
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);
|
|
|
|
|
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);
|
|
|
|
|
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;
|
2011-09-08 00:20:34 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&hosts_cs);
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the hosts list */
|
2011-04-23 23:42:14 +00:00
|
|
|
ipx_host *find_host(const unsigned char *net, const unsigned char *node) {
|
2011-09-08 00:20:34 +00:00
|
|
|
EnterCriticalSection(&hosts_cs);
|
|
|
|
|
2011-04-23 23:42:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-08 00:20:34 +00:00
|
|
|
hptr = NULL;
|
2011-04-23 23:42:14 +00:00
|
|
|
}
|
2011-09-08 00:20:34 +00:00
|
|
|
|
|
|
|
break;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-08 00:20:34 +00:00
|
|
|
LeaveCriticalSection(&hosts_cs);
|
|
|
|
return hptr;
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|