2013-08-23 21:39:21 +00:00
|
|
|
/*
|
2014-01-20 19:18:55 +00:00
|
|
|
* TAP-Windows -- A kernel driver to provide virtual tap
|
|
|
|
* device functionality on Windows.
|
|
|
|
*
|
|
|
|
* This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
|
|
|
|
*
|
|
|
|
* This source code is Copyright (C) 2002-2010 OpenVPN Technologies, Inc.,
|
|
|
|
* and is released under the GPL version 2 (see below).
|
|
|
|
*
|
|
|
|
* 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 (see the file COPYING included with this
|
|
|
|
* distribution); if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2013-08-23 21:39:21 +00:00
|
|
|
|
|
|
|
//------------------
|
|
|
|
// Memory Management
|
|
|
|
//------------------
|
|
|
|
|
|
|
|
PVOID
|
2014-01-20 19:18:55 +00:00
|
|
|
MemAlloc (ULONG p_Size, BOOLEAN zero)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
PVOID l_Return = NULL;
|
|
|
|
|
|
|
|
if (p_Size)
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (NdisAllocateMemoryWithTag (&l_Return, p_Size, 'APAT')
|
|
|
|
== NDIS_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
if (zero)
|
|
|
|
NdisZeroMemory (l_Return, p_Size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
l_Return = NULL;
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
l_Return = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l_Return;
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2014-01-20 19:18:55 +00:00
|
|
|
MemFree (PVOID p_Addr, ULONG p_Size)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
if (p_Addr && p_Size)
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
2013-08-23 21:39:21 +00:00
|
|
|
#if DBG
|
2014-01-20 19:18:55 +00:00
|
|
|
NdisZeroMemory (p_Addr, p_Size);
|
2013-08-23 21:39:21 +00:00
|
|
|
#endif
|
2014-01-20 19:18:55 +00:00
|
|
|
NdisFreeMemory (p_Addr, p_Size, 0);
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-01-20 19:18:55 +00:00
|
|
|
* Circular queue management routines.
|
|
|
|
*/
|
2013-08-23 21:39:21 +00:00
|
|
|
|
|
|
|
#define QUEUE_BYTE_ALLOCATION(size) \
|
2014-01-20 19:18:55 +00:00
|
|
|
(sizeof (Queue) + (size * sizeof (PVOID)))
|
2013-08-23 21:39:21 +00:00
|
|
|
|
|
|
|
#define QUEUE_ADD_INDEX(var, inc) \
|
|
|
|
{ \
|
2014-01-20 19:18:55 +00:00
|
|
|
var += inc; \
|
|
|
|
if (var >= q->capacity) \
|
|
|
|
var -= q->capacity; \
|
|
|
|
MYASSERT (var < q->capacity); \
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define QUEUE_SANITY_CHECK() \
|
2014-01-20 19:18:55 +00:00
|
|
|
MYASSERT (q != NULL && q->base < q->capacity && q->size <= q->capacity)
|
2013-08-23 21:39:21 +00:00
|
|
|
|
|
|
|
#define QueueCount(q) (q->size)
|
|
|
|
|
|
|
|
#define UPDATE_MAX_SIZE() \
|
|
|
|
{ \
|
2014-01-20 19:18:55 +00:00
|
|
|
if (q->size > q->max_size) \
|
|
|
|
q->max_size = q->size; \
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Queue *
|
2014-01-20 19:18:55 +00:00
|
|
|
QueueInit (ULONG capacity)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
Queue *q;
|
2013-08-23 21:39:21 +00:00
|
|
|
|
2014-01-20 19:18:55 +00:00
|
|
|
MYASSERT (capacity > 0);
|
|
|
|
q = (Queue *) MemAlloc (QUEUE_BYTE_ALLOCATION (capacity), TRUE);
|
|
|
|
if (!q)
|
|
|
|
return NULL;
|
2013-08-23 21:39:21 +00:00
|
|
|
|
2014-01-20 19:18:55 +00:00
|
|
|
q->base = q->size = 0;
|
|
|
|
q->capacity = capacity;
|
|
|
|
q->max_size = 0;
|
|
|
|
return q;
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2014-01-20 19:18:55 +00:00
|
|
|
QueueFree (Queue *q)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
if (q)
|
|
|
|
{
|
|
|
|
QUEUE_SANITY_CHECK ();
|
|
|
|
MemFree (q, QUEUE_BYTE_ALLOCATION (q->capacity));
|
|
|
|
}
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PVOID
|
2014-01-20 19:18:55 +00:00
|
|
|
QueuePush (Queue *q, PVOID item)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
ULONG dest;
|
|
|
|
QUEUE_SANITY_CHECK ();
|
|
|
|
if (q->size == q->capacity)
|
|
|
|
return NULL;
|
|
|
|
dest = q->base;
|
|
|
|
QUEUE_ADD_INDEX (dest, q->size);
|
|
|
|
q->data[dest] = item;
|
|
|
|
++q->size;
|
|
|
|
UPDATE_MAX_SIZE();
|
|
|
|
return item;
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PVOID
|
2014-01-20 19:18:55 +00:00
|
|
|
QueuePop (Queue *q)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
ULONG oldbase;
|
|
|
|
QUEUE_SANITY_CHECK ();
|
|
|
|
if (!q->size)
|
|
|
|
return NULL;
|
|
|
|
oldbase = q->base;
|
|
|
|
QUEUE_ADD_INDEX (q->base, 1);
|
|
|
|
--q->size;
|
|
|
|
UPDATE_MAX_SIZE();
|
|
|
|
return q->data[oldbase];
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PVOID
|
2014-01-20 19:18:55 +00:00
|
|
|
QueueExtract (Queue *q, PVOID item)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
ULONG src, dest, count, n;
|
|
|
|
QUEUE_SANITY_CHECK ();
|
|
|
|
n = 0;
|
|
|
|
src = dest = q->base;
|
|
|
|
count = q->size;
|
|
|
|
while (count--)
|
2013-08-23 21:39:21 +00:00
|
|
|
{
|
2014-01-20 19:18:55 +00:00
|
|
|
if (item == q->data[src])
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
--q->size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q->data[dest] = q->data[src];
|
|
|
|
QUEUE_ADD_INDEX (dest, 1);
|
|
|
|
}
|
|
|
|
QUEUE_ADD_INDEX (src, 1);
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
2014-01-20 19:18:55 +00:00
|
|
|
if (n)
|
|
|
|
return item;
|
|
|
|
else
|
|
|
|
return NULL;
|
2013-08-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef QUEUE_BYTE_ALLOCATION
|
|
|
|
#undef QUEUE_ADD_INDEX
|
|
|
|
#undef QUEUE_SANITY_CHECK
|
|
|
|
#undef UPDATE_MAX_SIZE
|