From 2674e3356f5168102589a537ab8435eb9f8be1aa Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Mon, 29 Aug 2011 13:21:18 +0000 Subject: [PATCH] More code cleanup and a couple of files I forgot to add last commit. --- src/common.h | 17 ------ src/interface.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++ src/interface.h | 39 ++++++++++++++ src/ipxwrapper.c | 1 + src/ipxwrapper.h | 1 - src/winsock.c | 1 + 6 files changed, 178 insertions(+), 18 deletions(-) create mode 100644 src/interface.c create mode 100644 src/interface.h diff --git a/src/common.h b/src/common.h index 700ddd3..538a421 100644 --- a/src/common.h +++ b/src/common.h @@ -42,19 +42,6 @@ (unsigned int)(unsigned char)(node[5]) \ ) -struct ipx_interface { - uint32_t ipaddr; - uint32_t netmask; - uint32_t bcast; - - unsigned char hwaddr[6]; - - unsigned char ipx_net[4]; - unsigned char ipx_node[6]; - - struct ipx_interface *next; -}; - extern HKEY regkey; void log_printf(const char *fmt, ...); @@ -69,8 +56,4 @@ 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); - #endif /* !IPXWRAPPER_COMMON_H */ diff --git a/src/interface.c b/src/interface.c new file mode 100644 index 0000000..b27ab46 --- /dev/null +++ b/src/interface.c @@ -0,0 +1,137 @@ +/* IPXWrapper - Interface functions + * Copyright (C) 2011 Daniel Collins + * + * 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 +#include + +#include "interface.h" +#include "common.h" +#include "config.h" + +/* 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/interface.h b/src/interface.h new file mode 100644 index 0000000..7d382d3 --- /dev/null +++ b/src/interface.h @@ -0,0 +1,39 @@ +/* IPXWrapper - Interface header + * Copyright (C) 2011 Daniel Collins + * + * 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. +*/ + +#ifndef IPXWRAPPER_INTERFACE_H +#define IPXWRAPPER_INTERFACE_H + +#include + +struct ipx_interface { + uint32_t ipaddr; + uint32_t netmask; + uint32_t bcast; + + unsigned char hwaddr[6]; + + unsigned char ipx_net[4]; + unsigned char ipx_node[6]; + + struct ipx_interface *next; +}; + +struct ipx_interface *get_interfaces(int ifnum); +void free_interfaces(struct ipx_interface *iface); + +#endif /* !IPXWRAPPER_INTERFACE_H */ diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index 86da07c..bb293a5 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -28,6 +28,7 @@ #include "ipxwrapper.h" #include "common.h" +#include "interface.h" #define DLL_UNLOAD(dll) \ if(dll) {\ diff --git a/src/ipxwrapper.h b/src/ipxwrapper.h index 80158e8..eea2f1b 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -55,7 +55,6 @@ typedef struct ipx_socket ipx_socket; typedef struct ipx_packet ipx_packet; -typedef struct ipx_nic ipx_nic; typedef struct ipx_host ipx_host; struct ipx_socket { diff --git a/src/winsock.c b/src/winsock.c index b37b450..e51d6c4 100644 --- a/src/winsock.c +++ b/src/winsock.c @@ -25,6 +25,7 @@ #include "ipxwrapper.h" #include "common.h" +#include "interface.h" INT APIENTRY EnumProtocolsA(LPINT protocols, LPVOID buf, LPDWORD bsptr) { int bufsize = *bsptr, rval, i, want_ipx = 0;