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

180 lines
4.2 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"
2012-10-20 18:06:11 +00:00
#include "addrcache.h"
2008-12-09 21:36:07 +00:00
extern const char *version_string;
extern const char *compile_time;
struct ipaddr_list {
uint32_t ipaddr;
struct ipaddr_list *next;
};
2008-12-09 21:36:07 +00:00
ipx_socket *sockets = NULL;
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
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;
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
}
}
2008-12-09 21:36:07 +00:00
BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) {
if(why == DLL_PROCESS_ATTACH) {
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);
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();
min_log_level = main_config.log_level;
2012-10-21 10:26:52 +00:00
2012-10-20 18:06:11 +00:00
addr_cache_init();
ipx_interfaces_init();
if(!rclient_init(&g_rclient)) {
return FALSE;
}
2012-10-20 18:06:11 +00:00
init_cs(&sockets_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;
}
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) {
2011-11-16 21:32:59 +00:00
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) {
2011-11-16 21:32:59 +00:00
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);
2008-12-09 21:36:07 +00:00
2011-09-08 00:20:34 +00:00
WSACleanup();
2012-10-20 18:06:11 +00:00
DeleteCriticalSection(&sockets_cs);
2011-08-29 13:15:10 +00:00
ipx_interfaces_cleanup();
2012-10-20 18:06:11 +00:00
addr_cache_cleanup();
unload_dlls();
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
*
* 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
}