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

Refactored address table code.

- Use process+socket as key rather than local port number.

- Store socket protocol in table.

- Removed ADDR_TABLE_ENTRY_REUSE flag.
This commit is contained in:
Daniel Collins 2014-01-05 19:45:45 +00:00
parent 1f733315d6
commit 0819a324c1
3 changed files with 53 additions and 33 deletions

View File

@ -130,7 +130,7 @@ void addr_table_cleanup(void)
{ {
if(s->flags & IPX_BOUND) if(s->flags & IPX_BOUND)
{ {
addr_table_remove(s->port); addr_table_remove(s);
} }
} }
} }
@ -299,7 +299,7 @@ uint16_t addr_table_auto_socket(void)
* Performs no conflict checking. Take the address table lock in the caller and * Performs no conflict checking. Take the address table lock in the caller and
* use addr_table_check() before calling this. * use addr_table_check() before calling this.
*/ */
void addr_table_add(const struct sockaddr_ipx *addr, uint16_t port) void addr_table_add(const ipx_socket *sock)
{ {
if(!addr_table_base) if(!addr_table_base)
{ {
@ -322,14 +322,27 @@ void addr_table_add(const struct sockaddr_ipx *addr, uint16_t port)
if(entry < end) if(entry < end)
{ {
entry->netnum = addr32_in(addr->sa_netnum); entry->netnum = addr32_in(sock->addr.sa_netnum);
entry->nodenum = addr48_in(addr->sa_nodenum); entry->nodenum = addr48_in(sock->addr.sa_nodenum);
entry->socket = addr->sa_socket; entry->socket = sock->addr.sa_socket;
entry->flags = ADDR_TABLE_ENTRY_VALID | ADDR_TABLE_ENTRY_REUSE; if(sock->flags & IPX_IS_SPXII)
{
entry->type = ADDR_TABLE_TYPE_SPXII;
}
else if(sock->flags & IPX_IS_SPX)
{
entry->type = ADDR_TABLE_TYPE_SPX;
}
else{
entry->type = ADDR_TABLE_TYPE_IPX;
}
entry->port = port; entry->process = GetCurrentProcessId();
entry->time = time(NULL); entry->sock = sock->fd;
entry->flags = ADDR_TABLE_ENTRY_VALID;
entry->time = time(NULL);
} }
else{ else{
log_printf(LOG_ERROR, "Out of address table slots, not appending!"); log_printf(LOG_ERROR, "Out of address table slots, not appending!");
@ -339,7 +352,7 @@ void addr_table_add(const struct sockaddr_ipx *addr, uint16_t port)
} }
/* Remove an entry from the address table. */ /* Remove an entry from the address table. */
void addr_table_remove(uint16_t port) void addr_table_remove(const ipx_socket *sock)
{ {
if(!addr_table_base) if(!addr_table_base)
{ {
@ -353,20 +366,19 @@ void addr_table_remove(uint16_t port)
addr_table_entry_t *entry = addr_table_base; addr_table_entry_t *entry = addr_table_base;
addr_table_entry_t *end = addr_table_base + ADDR_TABLE_MAX_ENTRIES; addr_table_entry_t *end = addr_table_base + ADDR_TABLE_MAX_ENTRIES;
while(entry < end && (entry->flags & ADDR_TABLE_ENTRY_VALID) && entry->port != port) while(entry < end && (entry->flags & ADDR_TABLE_ENTRY_VALID))
{ {
entry++; if(entry->process == GetCurrentProcessId() && entry->sock == sock->fd)
} {
addr_table_entry_t *last = _last_entry();
addr_table_entry_t *last = _last_entry();
*entry = *last;
/* Replace entry with the last entry and mark the latter as invalid. */ last->flags &= ~ADDR_TABLE_ENTRY_VALID;
if(entry < end) break;
{ }
*entry = *last;
last->flags &= ~ADDR_TABLE_ENTRY_VALID; ++entry;
} }
addr_table_unlock(); addr_table_unlock();
@ -415,7 +427,7 @@ void addr_table_update(void)
for(entry = addr_table_base; entry < end && (entry->flags & ADDR_TABLE_ENTRY_VALID); entry++) for(entry = addr_table_base; entry < end && (entry->flags & ADDR_TABLE_ENTRY_VALID); entry++)
{ {
if(entry->port == sock->port) if(entry->process == GetCurrentProcessId() && entry->sock == sock->fd)
{ {
entry->time = time(NULL); entry->time = time(NULL);
break; break;

View File

@ -23,6 +23,7 @@
#include <time.h> #include <time.h>
#include "addr.h" #include "addr.h"
#include "ipxwrapper.h"
#define ADDR_TABLE_MAX_ENTRIES 512 #define ADDR_TABLE_MAX_ENTRIES 512
#define ADDR_TABLE_ENTRY_TIMEOUT 10 #define ADDR_TABLE_ENTRY_TIMEOUT 10
@ -30,7 +31,7 @@
#define ADDR_TABLE_MUTEX "IPXWrapper_addr_table_mutex" #define ADDR_TABLE_MUTEX "IPXWrapper_addr_table_mutex"
#define ADDR_TABLE_NAME "IPXWrapper_addr_table" #define ADDR_TABLE_NAME "IPXWrapper_addr_table"
#define ADDR_TABLE_VERSION 1 #define ADDR_TABLE_VERSION 2
typedef struct addr_table_header addr_table_header_t; typedef struct addr_table_header addr_table_header_t;
@ -40,7 +41,6 @@ struct addr_table_header
}; };
#define ADDR_TABLE_ENTRY_VALID ((int)(1<<0)) #define ADDR_TABLE_ENTRY_VALID ((int)(1<<0))
#define ADDR_TABLE_ENTRY_REUSE ((int)(1<<1))
typedef struct addr_table_entry addr_table_entry_t; typedef struct addr_table_entry addr_table_entry_t;
@ -50,8 +50,16 @@ struct addr_table_entry
addr48_t nodenum; addr48_t nodenum;
uint16_t socket; uint16_t socket;
enum {
ADDR_TABLE_TYPE_IPX,
ADDR_TABLE_TYPE_SPX,
ADDR_TABLE_TYPE_SPXII
} type;
DWORD process;
int sock;
int flags; int flags;
uint16_t port;
time_t time; time_t time;
}; };
@ -64,8 +72,8 @@ void addr_table_unlock(void);
bool addr_table_check(const struct sockaddr_ipx *addr); bool addr_table_check(const struct sockaddr_ipx *addr);
uint16_t addr_table_auto_socket(void); uint16_t addr_table_auto_socket(void);
void addr_table_add(const struct sockaddr_ipx *addr, uint16_t port); void addr_table_add(const ipx_socket *sock);
void addr_table_remove(uint16_t port); void addr_table_remove(const ipx_socket *sock);
void addr_table_update(void); void addr_table_update(void);

View File

@ -263,7 +263,7 @@ int WSAAPI closesocket(SOCKET sockfd)
if(sock->flags & IPX_BOUND) if(sock->flags & IPX_BOUND)
{ {
addr_table_remove(sock->port); addr_table_remove(sock);
} }
HASH_DEL(sockets, sock); HASH_DEL(sockets, sock);
@ -448,15 +448,15 @@ int WSAAPI bind(SOCKET fd, const struct sockaddr *addr, int addrlen)
log_printf(LOG_DEBUG, "Bound to local UDP port %hu", ntohs(sock->port)); log_printf(LOG_DEBUG, "Bound to local UDP port %hu", ntohs(sock->port));
/* Add to the address table. */ /* Mark the IPX socket as bound and insert it into the address
* table.
addr_table_add(&ipxaddr, sock->port); */
/* Mark the IPX socket as bound. */
memcpy(&(sock->addr), &ipxaddr, sizeof(ipxaddr)); memcpy(&(sock->addr), &ipxaddr, sizeof(ipxaddr));
sock->flags |= IPX_BOUND; sock->flags |= IPX_BOUND;
addr_table_add(sock);
addr_table_unlock(); addr_table_unlock();
unlock_sockets(); unlock_sockets();