From 8b1c53e0bd71be84982bb348fe5e7971e51221d2 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Mon, 29 Aug 2011 13:15:10 +0000 Subject: [PATCH] More code cleanup --- Makefile | 7 +-- src/common.c | 114 ----------------------------------------------- src/common.h | 1 + src/ipxwrapper.c | 110 +++------------------------------------------ src/ipxwrapper.h | 20 ++------- src/winsock.c | 8 ++-- 6 files changed, 17 insertions(+), 243 deletions(-) diff --git a/Makefile b/Makefile index 1314dfa..f447733 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,8 @@ CFLAGS := -Wall -I./include/ CXXFLAGS := -Wall -I./include/ -IPXWRAPPER_DEPS := src/ipxwrapper.o src/winsock.o src/ipxwrapper_stubs.o src/log.o src/common.o src/ipxwrapper.def +IPXWRAPPER_DEPS := src/ipxwrapper.o src/winsock.o src/ipxwrapper_stubs.o src/log.o src/common.o \ + src/interface.o src/ipxwrapper.def BIN_FILES := changes.txt license.txt readme.txt ipxwrapper.dll mswsock.dll wsock32.dll ipxconfig.exe SRC_FILES := changes.txt license.txt Makefile mkstubs.pl readme.txt src/config.h src/ipxconfig.cpp \ @@ -53,7 +54,7 @@ ipxconfig.exe: src/ipxconfig.cpp $(CXX) $(CXXFLAGS) -static-libgcc -static-libstdc++ -Wl,-s -D_WIN32_IE=0x0400 -mwindows -o ipxconfig.exe src/ipxconfig.cpp -liphlpapi dpwsockx.dll: src/directplay.o src/log.o src/dpwsockx_stubs.o src/common.o ipxwrapper.dll - $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o dpwsockx.dll src/directplay.o src/log.o src/common.o src/dpwsockx_stubs.o src/dpwsockx.def -L. -lipxwrapper -lwsock32 -liphlpapi + $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o dpwsockx.dll src/directplay.o src/log.o src/common.o src/dpwsockx_stubs.o src/dpwsockx.def -L. -lipxwrapper -lwsock32 src/ipxwrapper_stubs.s: src/ipxwrapper_stubs.txt perl mkstubs.pl src/ipxwrapper_stubs.txt src/ipxwrapper_stubs.s @@ -68,7 +69,7 @@ src/dpwsockx_stubs.s: src/dpwsockx_stubs.txt perl mkstubs.pl src/dpwsockx_stubs.txt src/dpwsockx_stubs.s dpwsockx.dll %.dll: src/stubdll.o src/%_stubs.o src/log.o src/common.o src/%.def - $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o $@ $^ -liphlpapi + $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o $@ $^ src/%_stubs.o: src/%_stubs.s nasm -f win32 -o $@ $< diff --git a/src/common.c b/src/common.c index b4da98f..5ea9c6e 100644 --- a/src/common.c +++ b/src/common.c @@ -106,117 +106,3 @@ HMODULE load_sysdll(const char *name) { return dll; } - -/* Get virtual IPX interfaces - * Select a single interface by setting ifnum >= 0 -*/ -struct ipx_interface *get_interfaces(int ifnum) { - IP_ADAPTER_INFO *ifroot, tbuf; - ULONG bufsize = sizeof(IP_ADAPTER_INFO); - - int err = GetAdaptersInfo(&tbuf, &bufsize); - if(err == ERROR_NO_DATA) { - log_printf("No network interfaces detected!"); - return NULL; - }else if(err != ERROR_SUCCESS && err != ERROR_BUFFER_OVERFLOW) { - log_printf("Error fetching network interfaces: %s", w32_error(err)); - return NULL; - } - - if(!(ifroot = malloc(bufsize))) { - log_printf("Out of memory! (Tried to allocate %u bytes)", (unsigned int)bufsize); - return NULL; - } - - err = GetAdaptersInfo(ifroot, &bufsize); - if(err != ERROR_SUCCESS) { - log_printf("Error fetching network interfaces: %s", w32_error(err)); - - free(ifroot); - return NULL; - } - - struct ipx_interface *nics = NULL, *enic = NULL; - - IP_ADAPTER_INFO *ifptr = ifroot; - int this_ifnum = 0; - - while(ifptr) { - if(ifnum >= 0 && this_ifnum++ != ifnum) { - ifptr = ifptr->Next; - continue; - } - - struct reg_value rv; - int got_rv = 0; - - char vname[18]; - NODE_TO_STRING(vname, ifptr->Address); - - if(reg_get_bin(vname, &rv, sizeof(rv)) == sizeof(rv)) { - got_rv = 1; - } - - if(got_rv && !rv.enabled) { - /* Interface has been disabled, don't add it */ - ifptr = ifptr->Next; - continue; - } - - struct ipx_interface *nnic = malloc(sizeof(struct ipx_interface)); - if(!nnic) { - log_printf("Out of memory! (Tried to allocate %u bytes)", (unsigned int)sizeof(struct ipx_interface)); - - free_interfaces(nics); - return NULL; - } - - nnic->ipaddr = inet_addr(ifptr->IpAddressList.IpAddress.String); - nnic->netmask = inet_addr(ifptr->IpAddressList.IpMask.String); - nnic->bcast = nnic->ipaddr | ~nnic->netmask; - - memcpy(nnic->hwaddr, ifptr->Address, 6); - - if(got_rv) { - memcpy(nnic->ipx_net, rv.ipx_net, 4); - memcpy(nnic->ipx_node, rv.ipx_node, 6); - }else{ - unsigned char net[] = {0,0,0,1}; - - memcpy(nnic->ipx_net, net, 4); - memcpy(nnic->ipx_node, nnic->hwaddr, 6); - } - - nnic->next = NULL; - - if(got_rv && rv.primary) { - /* Force primary flag set, insert at start of NIC list */ - nnic->next = nics; - nics = nnic; - - if(!enic) { - enic = nnic; - } - }else if(enic) { - enic->next = nnic; - enic = nnic; - }else{ - enic = nics = nnic; - } - - ifptr = ifptr->Next; - } - - free(ifroot); - - return nics; -} - -void free_interfaces(struct ipx_interface *iface) { - while(iface) { - struct ipx_interface *del = iface; - iface = iface->next; - - free(del); - } -} diff --git a/src/common.h b/src/common.h index ad88ce3..700ddd3 100644 --- a/src/common.h +++ b/src/common.h @@ -69,6 +69,7 @@ DWORD reg_get_bin(const char *val_name, void *buf, DWORD size); HMODULE load_sysdll(const char *name); +/* interface.c functions */ struct ipx_interface *get_interfaces(int ifnum); void free_interfaces(struct ipx_interface *iface); diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index dfff327..86da07c 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -36,7 +36,7 @@ } ipx_socket *sockets = NULL; -ipx_nic *nics = NULL; +struct ipx_interface *nics = NULL; ipx_host *hosts = NULL; SOCKET net_fd = -1; struct reg_global global_conf; @@ -53,7 +53,6 @@ static DWORD router_tid = 0; static int init_router(void); static DWORD WINAPI router_main(LPVOID argp); static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr); -static BOOL load_nics(void); BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { if(why == DLL_PROCESS_ATTACH) { @@ -76,9 +75,7 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { global_conf.filter = 1; } - if(!load_nics()) { - return FALSE; - } + nics = get_interfaces(-1); mutex = CreateMutex(NULL, FALSE, NULL); if(!mutex) { @@ -115,6 +112,8 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { mutex = NULL; } + free_interfaces(nics); + WSACleanup(); reg_close(); @@ -182,33 +181,6 @@ void unlock_mutex(void) { while(ReleaseMutex(mutex)) {} } -IP_ADAPTER_INFO *get_nics(void) { - IP_ADAPTER_INFO *buf, tbuf; - ULONG bufsize = sizeof(IP_ADAPTER_INFO); - - int rval = GetAdaptersInfo(&tbuf, &bufsize); - if(rval != ERROR_SUCCESS && rval != ERROR_BUFFER_OVERFLOW) { - WSASetLastError(rval); - return NULL; - } - - buf = malloc(bufsize); - if(!buf) { - WSASetLastError(ERROR_OUTOFMEMORY); - return NULL; - } - - rval = GetAdaptersInfo(buf, &bufsize); - if(rval != ERROR_SUCCESS) { - WSASetLastError(rval); - free(buf); - - return NULL; - } - - return buf; -} - /* Initialize and start the router thread */ static int init_router(void) { net_fd = r_socket(AF_INET, SOCK_DGRAM, 0); @@ -270,7 +242,7 @@ static DWORD WINAPI router_main(LPVOID notused) { } if(global_conf.filter) { - ipx_nic *nic = nics; + struct ipx_interface *nic = nics; while(nic) { if((nic->ipaddr & nic->netmask) == (addr.sin_addr.s_addr & nic->netmask)) { @@ -409,75 +381,3 @@ ipx_host *find_host(const unsigned char *net, const unsigned char *node) { return NULL; } - -static BOOL load_nics(void) { - IP_ADAPTER_INFO *ifroot = get_nics(); - IP_ADAPTER_INFO *ifptr = ifroot; - ipx_nic *enic = NULL; - - if(!ifptr) { - log_printf("No NICs: %s", w32_error(WSAGetLastError())); - } - - while(ifptr) { - struct reg_value rv; - int got_rv = 0; - - char vname[18]; - NODE_TO_STRING(vname, ifptr->Address); - - if(reg_get_bin(vname, &rv, sizeof(rv)) == sizeof(rv)) { - got_rv = 1; - } - - if(got_rv && !rv.enabled) { - /* Interface has been disabled, don't add it */ - ifptr = ifptr->Next; - continue; - } - - ipx_nic *nnic = malloc(sizeof(ipx_nic)); - if(!nnic) { - return FALSE; - } - - nnic->ipaddr = inet_addr(ifptr->IpAddressList.IpAddress.String); - nnic->netmask = inet_addr(ifptr->IpAddressList.IpMask.String); - nnic->bcast = nnic->ipaddr | ~nnic->netmask; - - memcpy(nnic->hwaddr, ifptr->Address, 6); - - if(got_rv) { - memcpy(nnic->ipx_net, rv.ipx_net, 4); - memcpy(nnic->ipx_node, rv.ipx_node, 6); - }else{ - unsigned char net[] = {0,0,0,1}; - - memcpy(nnic->ipx_net, net, 4); - memcpy(nnic->ipx_node, nnic->hwaddr, 6); - } - - nnic->next = NULL; - - if(got_rv && rv.primary) { - /* Force primary flag set, insert at start of NIC list */ - nnic->next = nics; - nics = nnic; - - if(!enic) { - enic = nnic; - } - }else if(enic) { - enic->next = nnic; - enic = nnic; - }else{ - enic = nics = nnic; - } - - ifptr = ifptr->Next; - } - - free(ifroot); - - return TRUE; -} diff --git a/src/ipxwrapper.h b/src/ipxwrapper.h index 48e4338..80158e8 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -66,13 +66,13 @@ struct ipx_socket { uint8_t f_ptype; /* Undefined when IPX_FILTER isn't set */ /* The following values are undefined when IPX_BOUND is not set */ - ipx_nic *nic; + struct ipx_interface *nic; uint16_t socket; /* Stored in NETWORK BYTE ORDER */ /* Extra bind address, only used for receiving packets. * Only defined when IPX_EX_BOUND is set. */ - ipx_nic *ex_nic; + struct ipx_interface *ex_nic; uint16_t ex_socket; ipx_socket *next; @@ -93,19 +93,6 @@ struct ipx_packet { char data[1]; } __attribute__((__packed__)); -struct ipx_nic { - uint32_t ipaddr; - uint32_t netmask; - uint32_t bcast; - - unsigned char hwaddr[6]; - - unsigned char ipx_net[4]; - unsigned char ipx_node[6]; - - ipx_nic *next; -}; - struct ipx_host { unsigned char ipx_net[4]; unsigned char ipx_node[6]; @@ -117,7 +104,7 @@ struct ipx_host { }; extern ipx_socket *sockets; -extern ipx_nic *nics; +extern struct ipx_interface *nics; extern ipx_host *hosts; extern SOCKET net_fd; extern struct reg_global global_conf; @@ -130,7 +117,6 @@ void __stdcall *find_sym(char const *sym); ipx_socket *get_socket(SOCKET fd); void lock_mutex(void); void unlock_mutex(void); -IP_ADAPTER_INFO *get_nics(void); ipx_host *find_host(const unsigned char *net, const unsigned char *node); void log_open(); diff --git a/src/winsock.c b/src/winsock.c index cdf0667..b37b450 100644 --- a/src/winsock.c +++ b/src/winsock.c @@ -227,7 +227,7 @@ int WSAAPI bind(SOCKET fd, const struct sockaddr *addr, int addrlen) { */ unsigned char z6[] = {0,0,0,0,0,0}; - ipx_nic *nic = nics; + struct ipx_interface *nic = nics; while(nic) { if( @@ -348,7 +348,7 @@ int ipx_ex_bind(SOCKET fd, const struct sockaddr_ipx *ipxaddr) { } unsigned char z6[] = {0,0,0,0,0,0}; - ipx_nic *nic = nics; + struct ipx_interface *nic = nics; while(nic) { if( @@ -560,7 +560,7 @@ int WSAAPI getsockopt(SOCKET fd, int level, int optname, char FAR *optval, int F IPX_ADDRESS_DATA *ipxdata = (IPX_ADDRESS_DATA*)optval; - ipx_nic *nic = nics; + struct ipx_interface *nic = nics; int i = 0; while(nic && i < ipxdata->adapternum) { @@ -595,7 +595,7 @@ int WSAAPI getsockopt(SOCKET fd, int level, int optname, char FAR *optval, int F *intval = 0; - ipx_nic *nic = nics; + struct ipx_interface *nic = nics; while(nic) { (*intval)++; nic = nic->next;