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"
|
2012-10-20 18:06:11 +00:00
|
|
|
#include "addrcache.h"
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-11-14 02:21:29 +00:00
|
|
|
extern const char *version_string;
|
|
|
|
extern const char *compile_time;
|
|
|
|
|
2011-10-03 11:13:45 +00:00
|
|
|
struct ipaddr_list {
|
|
|
|
uint32_t ipaddr;
|
|
|
|
struct ipaddr_list *next;
|
|
|
|
};
|
|
|
|
|
2008-12-09 21:36:07 +00:00
|
|
|
ipx_socket *sockets = NULL;
|
2011-09-09 19:03:19 +00:00
|
|
|
SOCKET send_fd = -1;
|
2012-10-21 10:26:52 +00:00
|
|
|
main_config_t main_config;
|
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;
|
2011-10-03 11:13:45 +00:00
|
|
|
static CRITICAL_SECTION addrs_cs;
|
|
|
|
|
2011-10-03 11:17:05 +00:00
|
|
|
/* List of local IP addresses with associated IPX interfaces */
|
2011-10-03 11:13:45 +00:00
|
|
|
static struct ipaddr_list *local_addrs = NULL;
|
|
|
|
static time_t local_updated = 0;
|
2011-09-08 00:20:34 +00:00
|
|
|
|
2012-10-20 18:06:11 +00:00
|
|
|
static void init_cs(CRITICAL_SECTION *cs)
|
|
|
|
{
|
|
|
|
if(!InitializeCriticalSectionAndSpinCount(cs, 0x80000000))
|
|
|
|
{
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_ERROR, "Failed to initialise critical section: %s", w32_error(GetLastError()));
|
2012-10-20 18:06:11 +00:00
|
|
|
abort();
|
2011-09-08 00:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 11:13:45 +00:00
|
|
|
static void free_local_ips(void);
|
|
|
|
|
2008-12-09 21:36:07 +00:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
|
|
|
|
if(why == DLL_PROCESS_ATTACH) {
|
2011-09-18 14:36:24 +00:00
|
|
|
log_open("ipxwrapper.log");
|
2008-12-09 21:36:07 +00:00
|
|
|
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_INFO, "IPXWrapper %s", version_string);
|
|
|
|
log_printf(LOG_INFO, "Compiled at %s", compile_time);
|
2011-11-14 02:21:29 +00:00
|
|
|
|
2012-05-10 01:18:25 +00:00
|
|
|
if(!getenv("SystemRoot")) {
|
|
|
|
log_printf(LOG_WARNING, "SystemRoot is not set in the environment");
|
|
|
|
|
|
|
|
char env[268] = "SystemRoot=";
|
|
|
|
GetSystemWindowsDirectory(env+11, 256);
|
|
|
|
|
|
|
|
log_printf(LOG_INFO, "Setting SystemRoot to '%s'", env+11);
|
|
|
|
_putenv(env);
|
|
|
|
}
|
|
|
|
|
2012-10-21 10:26:52 +00:00
|
|
|
main_config = get_main_config();
|
|
|
|
|
2012-10-20 18:06:11 +00:00
|
|
|
addr_cache_init();
|
|
|
|
|
2011-09-09 18:36:52 +00:00
|
|
|
if(!rclient_init(&g_rclient)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-20 18:06:11 +00:00
|
|
|
init_cs(&sockets_cs);
|
|
|
|
init_cs(&addrs_cs);
|
2008-12-09 21:36:07 +00:00
|
|
|
|
|
|
|
WSADATA wsdata;
|
|
|
|
int err = WSAStartup(MAKEWORD(1,1), &wsdata);
|
|
|
|
if(err) {
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_ERROR, "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) {
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_ERROR, "Failed to create UDP socket: %s", w32_error(WSAGetLastError()));
|
2011-09-09 19:03:19 +00:00
|
|
|
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) {
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_ERROR, "Failed to bind UDP socket (send_fd): %s", w32_error(WSAGetLastError()));
|
2011-09-09 19:03:19 +00:00
|
|
|
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-10-03 11:13:45 +00:00
|
|
|
free_local_ips();
|
|
|
|
|
2011-09-08 00:20:34 +00:00
|
|
|
WSACleanup();
|
|
|
|
|
2012-10-20 18:06:11 +00:00
|
|
|
DeleteCriticalSection(&addrs_cs);
|
|
|
|
DeleteCriticalSection(&sockets_cs);
|
2011-08-29 13:15:10 +00:00
|
|
|
|
2012-10-20 18:06:11 +00:00
|
|
|
addr_cache_cleanup();
|
|
|
|
|
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-10-03 11:13:45 +00:00
|
|
|
/* Check if supplied IP (network byte order) is a local address */
|
|
|
|
BOOL ip_is_local(uint32_t ipaddr) {
|
|
|
|
EnterCriticalSection(&addrs_cs);
|
|
|
|
|
2012-10-21 10:33:20 +00:00
|
|
|
if(local_updated + main_config.iface_ttl < time(NULL)) {
|
2011-10-03 11:13:45 +00:00
|
|
|
/* TODO: Use all local IPs rather than just the ones with associated IPX addresses? */
|
|
|
|
|
|
|
|
struct ipx_interface *ifaces = get_interfaces(-1);
|
|
|
|
struct ipx_interface *i = ifaces;
|
|
|
|
|
|
|
|
while(i) {
|
|
|
|
struct ipaddr_list *nn = malloc(sizeof(struct ipaddr_list));
|
|
|
|
if(!nn) {
|
2011-11-16 21:32:59 +00:00
|
|
|
log_printf(LOG_ERROR, "Out of memory! Can't allocate ipaddr_list structure!");
|
2011-10-03 11:13:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nn->ipaddr = i->ipaddr;
|
|
|
|
nn->next = local_addrs;
|
|
|
|
local_addrs = nn;
|
|
|
|
|
|
|
|
i = i->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_interfaces(ifaces);
|
|
|
|
|
|
|
|
local_updated = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ipaddr_list *p = local_addrs;
|
|
|
|
|
|
|
|
while(p) {
|
|
|
|
if(p->ipaddr == ipaddr) {
|
|
|
|
LeaveCriticalSection(&addrs_cs);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&addrs_cs);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free local IP address list */
|
|
|
|
static void free_local_ips(void) {
|
|
|
|
struct ipaddr_list *p = local_addrs, *d;
|
|
|
|
|
|
|
|
while(p) {
|
|
|
|
d = p;
|
|
|
|
p = p->next;
|
|
|
|
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
local_addrs = NULL;
|
|
|
|
}
|