int NInstances (VOID) { int i, n = 0; if (g_InstanceHash) { for (i = 0; i < N_INSTANCE_BUCKETS; ++i) { BOOLEAN got_lock; INSTANCE_BUCKET *ib = &g_InstanceHash->buckets[i]; ACQUIRE_MUTEX_ADAPTIVE (&ib->lock, got_lock); if (got_lock) { INSTANCE *current; for (current = ib->list; current != NULL; current = current->next) ++n; RELEASE_MUTEX (&ib->lock); } else return -1; } } return n; }
TapAdapterPointer LookupAdapterInInstanceList (PDEVICE_OBJECT p_DeviceObject) { BOOLEAN got_lock; TapAdapterPointer ret = NULL; INSTANCE_BUCKET *ib = &g_InstanceHash->buckets[InstanceHashValue((PVOID)p_DeviceObject)]; ACQUIRE_MUTEX_ADAPTIVE (&ib->lock, got_lock); if (got_lock) { INSTANCE *current, *prev=NULL; for (current = ib->list; current != NULL; current = current->next) { if (p_DeviceObject == INSTANCE_KEY (current->m_Adapter)) // found match { // move it to head of list if (prev) { prev->next = current->next; current->next = ib->list; ib->list = current; } ret = ib->list->m_Adapter; break; } prev = current; } RELEASE_MUTEX (&ib->lock); } return ret; }
BOOLEAN RemoveAdapterFromInstanceList (TapAdapterPointer p_Adapter) { BOOLEAN got_lock; BOOLEAN ret = FALSE; INSTANCE_BUCKET *ib = &g_InstanceHash->buckets[InstanceHashValue(INSTANCE_KEY(p_Adapter))]; ACQUIRE_MUTEX_ADAPTIVE (&ib->lock, got_lock); if (got_lock) { INSTANCE *current, *prev=NULL; for (current = ib->list; current != NULL; current = current->next) { if (current->m_Adapter == p_Adapter) // found match { if (prev) prev->next = current->next; else ib->list = current->next; MemFree (current->m_Adapter, sizeof (TapAdapter)); MemFree (current, sizeof (INSTANCE)); ret = TRUE; break; } prev = current; } RELEASE_MUTEX (&ib->lock); } return ret; }
BOOLEAN AddAdapterToInstanceList (TapAdapterPointer p_Adapter) { BOOLEAN got_lock; BOOLEAN ret = FALSE; const int hash = InstanceHashValue(INSTANCE_KEY(p_Adapter)); INSTANCE_BUCKET *ib = &g_InstanceHash->buckets[hash]; DEBUGP (("[TAP] AddAdapterToInstanceList hash=%d\n", hash)); ACQUIRE_MUTEX_ADAPTIVE (&ib->lock, got_lock); if (got_lock) { INSTANCE *i = MemAlloc (sizeof (INSTANCE), FALSE); if (i) { MYASSERT (p_Adapter); i->m_Adapter = p_Adapter; i->next = ib->list; ib->list = i; ret = TRUE; } RELEASE_MUTEX (&ib->lock); } return ret; }
BOOLEAN GetDebugLine (char *buf, const int len) { static const char *truncated = "[OUTPUT TRUNCATED]\n"; BOOLEAN ret = FALSE; NdisZeroMemory (buf, len); if (g_Debug.text && g_Debug.capacity > 0) { BOOLEAN owned; ACQUIRE_MUTEX_ADAPTIVE (&g_Debug.lock, owned); if (owned) { int i = 0; if (g_Debug.error || NewlineExists (g_Debug.text + g_Debug.in, (int)g_Debug.out - (int)g_Debug.in)) { while (i < (len - 1) && g_Debug.in < g_Debug.out) { const char c = g_Debug.text[g_Debug.in++]; if (c == '\n') break; buf[i++] = c; } if (i < len) buf[i] = '\0'; } if (!i) { if (g_Debug.in == g_Debug.out) { g_Debug.in = g_Debug.out = 0; if (g_Debug.error) { const unsigned int tlen = strlen (truncated); if (tlen < g_Debug.capacity) { NdisMoveMemory (g_Debug.text, truncated, tlen+1); g_Debug.out = tlen; } g_Debug.error = FALSE; } } } else ret = TRUE; RELEASE_MUTEX (&g_Debug.lock); } } return ret; }
VOID MyDebugPrint (const unsigned char* format, ...) { if (g_Debug.text && g_Debug.capacity > 0 && CAN_WE_PRINT) { BOOLEAN owned; ACQUIRE_MUTEX_ADAPTIVE (&g_Debug.lock, owned); if (owned) { const int remaining = (int)g_Debug.capacity - (int)g_Debug.out; if (remaining > 0) { va_list args; NTSTATUS status; char *end; #ifdef DBG_PRINT va_start (args, format); vDbgPrintEx (DPFLTR_IHVNETWORK_ID, DPFLTR_INFO_LEVEL, format, args); va_end (args); #endif va_start (args, format); status = RtlStringCchVPrintfExA (g_Debug.text + g_Debug.out, remaining, &end, NULL, STRSAFE_NO_TRUNCATION | STRSAFE_IGNORE_NULLS, format, args); va_end (args); va_start (args, format); vDbgPrintEx(DPFLTR_IHVDRIVER_ID , 1, format, args); va_end (args); if (status == STATUS_SUCCESS) g_Debug.out = (unsigned int) (end - g_Debug.text); else g_Debug.error = TRUE; } else g_Debug.error = TRUE; RELEASE_MUTEX (&g_Debug.lock); } else g_Debug.error = TRUE; } }