From a4e0add8c7adace82bdb69121e1fe0601c26e63b Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Sun, 24 Apr 2011 00:24:10 +0000 Subject: [PATCH] Small amount of code cleanup --- src/ipxwrapper.c | 151 +++++++++++++++++++++++------------------------ src/ipxwrapper.h | 20 +++++++ 2 files changed, 95 insertions(+), 76 deletions(-) diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index f05b683..4655b32 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -53,6 +53,7 @@ static HMODULE load_sysdll(char const *name); 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) { @@ -81,84 +82,10 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { debug("Could not open registry: %s", w32_error(reg_err)); } - IP_ADAPTER_INFO *ifroot = get_nics(); - IP_ADAPTER_INFO *ifptr = ifroot; - ipx_nic *enic = NULL; - - if(!ifptr) { - debug("No NICs: %s", w32_error(WSAGetLastError())); + if(!load_nics()) { + return FALSE; } - while(ifptr) { - struct reg_value rv; - int got_rv = 0; - - if(regkey) { - char vname[18]; - - sprintf( - vname, - "%02X:%02X:%02X:%02X:%02X:%02X", - ifptr->Address[0], - ifptr->Address[1], - ifptr->Address[2], - ifptr->Address[3], - ifptr->Address[4], - ifptr->Address[5] - ); - - DWORD rv_size = sizeof(rv); - - int reg_err = RegQueryValueEx(regkey, vname, NULL, NULL, (BYTE*)&rv, &rv_size); - if(reg_err == ERROR_SUCCESS && rv_size == 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 = ntohl(inet_addr(ifptr->IpAddressList.IpAddress.String)); - nnic->bcast = nnic->ipaddr | ~ntohl(inet_addr(ifptr->IpAddressList.IpMask.String)); - - 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; - }else if(enic) { - enic->next = nnic; - enic = nnic; - }else{ - enic = nics = nnic; - } - - ifptr = ifptr->Next; - } - - free(ifroot); - mutex = CreateMutex(NULL, FALSE, NULL); if(!mutex) { debug("Failed to create mutex"); @@ -504,3 +431,75 @@ 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) { + debug("No NICs: %s", w32_error(WSAGetLastError())); + } + + while(ifptr) { + struct reg_value rv; + int got_rv = 0; + + if(regkey) { + char vname[18]; + NODE_TO_STRING(vname, ifptr->Address); + + DWORD rv_size = sizeof(rv); + + int reg_err = RegQueryValueEx(regkey, vname, NULL, NULL, (BYTE*)&rv, &rv_size); + if(reg_err == ERROR_SUCCESS && rv_size == 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 = ntohl(inet_addr(ifptr->IpAddressList.IpAddress.String)); + nnic->bcast = nnic->ipaddr | ~ntohl(inet_addr(ifptr->IpAddressList.IpMask.String)); + + 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; + }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 1625900..20404f1 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -78,6 +78,26 @@ SetLastError(errnum);\ return __VA_ARGS__; +#define NET_TO_STRING(s, net) \ + sprintf( \ + s, "%02X:%02X:%02X:%02X", \ + (unsigned int)net[0], \ + (unsigned int)net[1], \ + (unsigned int)net[2], \ + (unsigned int)net[3] \ + ) + +#define NODE_TO_STRING(s, node) \ + sprintf( \ + s, "%02X:%02X:%02X:%02X:%02X:%02X", \ + (unsigned int)node[0], \ + (unsigned int)node[1], \ + (unsigned int)node[2], \ + (unsigned int)node[3], \ + (unsigned int)node[4], \ + (unsigned int)node[5] \ + ) + typedef struct ipx_socket ipx_socket; typedef struct ipx_packet ipx_packet; typedef struct ipx_nic ipx_nic;