mirror of
https://github.com/solemnwarning/ipxwrapper
synced 2024-12-30 16:45:37 +01:00
180 lines
4.2 KiB
C
180 lines
4.2 KiB
C
/* 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>
|
|
|
|
#include "ipxwrapper.h"
|
|
#include "common.h"
|
|
#include "interface.h"
|
|
#include "router.h"
|
|
#include "addrcache.h"
|
|
|
|
extern const char *version_string;
|
|
extern const char *compile_time;
|
|
|
|
struct ipaddr_list {
|
|
uint32_t ipaddr;
|
|
struct ipaddr_list *next;
|
|
};
|
|
|
|
ipx_socket *sockets = NULL;
|
|
SOCKET send_fd = -1;
|
|
main_config_t main_config;
|
|
|
|
struct rclient g_rclient;
|
|
|
|
static CRITICAL_SECTION sockets_cs;
|
|
|
|
static void init_cs(CRITICAL_SECTION *cs)
|
|
{
|
|
if(!InitializeCriticalSectionAndSpinCount(cs, 0x80000000))
|
|
{
|
|
log_printf(LOG_ERROR, "Failed to initialise critical section: %s", w32_error(GetLastError()));
|
|
abort();
|
|
}
|
|
}
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
|
|
if(why == DLL_PROCESS_ATTACH) {
|
|
log_open("ipxwrapper.log");
|
|
|
|
log_printf(LOG_INFO, "IPXWrapper %s", version_string);
|
|
log_printf(LOG_INFO, "Compiled at %s", compile_time);
|
|
|
|
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);
|
|
}
|
|
|
|
main_config = get_main_config();
|
|
min_log_level = main_config.log_level;
|
|
|
|
addr_cache_init();
|
|
|
|
ipx_interfaces_init();
|
|
|
|
if(!rclient_init(&g_rclient)) {
|
|
return FALSE;
|
|
}
|
|
|
|
init_cs(&sockets_cs);
|
|
|
|
WSADATA wsdata;
|
|
int err = WSAStartup(MAKEWORD(1,1), &wsdata);
|
|
if(err) {
|
|
log_printf(LOG_ERROR, "Failed to initialize winsock: %s", w32_error(err));
|
|
return FALSE;
|
|
}
|
|
|
|
if(!rclient_start(&g_rclient)) {
|
|
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(LOG_ERROR, "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(LOG_ERROR, "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);
|
|
|
|
WSACleanup();
|
|
|
|
DeleteCriticalSection(&sockets_cs);
|
|
|
|
ipx_interfaces_cleanup();
|
|
|
|
addr_cache_cleanup();
|
|
|
|
unload_dlls();
|
|
|
|
log_close();
|
|
}
|
|
|
|
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
|
|
*
|
|
* TODO: Change this behaviour. It is almost as bad as the BKL.
|
|
*/
|
|
ipx_socket *get_socket(SOCKET fd) {
|
|
lock_sockets();
|
|
|
|
ipx_socket *ptr = sockets;
|
|
|
|
while(ptr) {
|
|
if(ptr->fd == fd) {
|
|
break;
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
}
|
|
|
|
if(!ptr) {
|
|
unlock_sockets();
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
/* Lock the mutex */
|
|
void lock_sockets(void) {
|
|
EnterCriticalSection(&sockets_cs);
|
|
}
|
|
|
|
/* Unlock the mutex */
|
|
void unlock_sockets(void) {
|
|
LeaveCriticalSection(&sockets_cs);
|
|
}
|