// Create a new lock NEO_LOCK *NeoNewLock() { NDIS_SPIN_LOCK *spin_lock; // Memory allocation NEO_LOCK *lock = NeoZeroMalloc(sizeof(NEO_LOCK)); if (lock == NULL) { return NULL; } // Initialize spin lock spin_lock = &lock->spin_lock; NdisAllocateSpinLock(spin_lock); return lock; }
NEO_EVENT *NeoNewEvent(char *name) { UNICODE *unicode_name; NEO_EVENT *event; // Validate arguments if (name == NULL) { return NULL; } // Convert the name to Unicode unicode_name = NewUnicode(name); if (unicode_name == NULL) { return NULL; } // Memory allocation event = NeoZeroMalloc(sizeof(NEO_EVENT)); if (event == NULL) { FreeUnicode(unicode_name); return NULL; } // Create an Event event->event = IoCreateNotificationEvent(GetUnicode(unicode_name), &event->event_handle); if (event->event == NULL) { NeoFree(event); FreeUnicode(unicode_name); return NULL; } // Initialize the event KeInitializeEvent(event->event, NotificationEvent, FALSE); KeClearEvent(event->event); // Release a string FreeUnicode(unicode_name); return event; }
NEO_EVENT *NeoCreateWin9xEvent(DWORD h) { NEO_EVENT *event; // Validate arguments if (h == NULL) { return NULL; } // Memory allocation event = NeoZeroMalloc(sizeof(NEO_EVENT)); if (event == NULL) { return NULL; } event->win32_event = h; return event; }
// Create a new Unicode string UNICODE *NewUnicode(char *str) { UNICODE *u; // Validate arguments if (str == NULL) { return NULL; } // Memory allocation u = NeoZeroMalloc(sizeof(UNICODE)); if (u == NULL) { return NULL; } // String initialization _NdisInitializeString(&u->String, str); return u; }
// Create a packet buffer PACKET_BUFFER *NeoNewPacketBuffer() { PACKET_BUFFER *p; NDIS_STATUS ret; // Memory allocation p = NeoZeroMalloc(sizeof(PACKET_BUFFER)); // Memory allocation for packet p->Buf = NeoMalloc(NEO_MAX_PACKET_SIZE); // Allocate the buffer pool NdisAllocateBufferPool(&ret, &p->BufferPool, 1); // Allocate the buffer NdisAllocateBuffer(&ret, &p->NdisBuffer, p->BufferPool, p->Buf, NEO_MAX_PACKET_SIZE); // Secure the packet pool NdisAllocatePacketPool(&ret, &p->PacketPool, 1, PROTOCOL_RESERVED_SIZE_IN_PACKET); // Secure the packet NdisAllocatePacket(&ret, &p->NdisPacket, p->PacketPool); NDIS_SET_PACKET_HEADER_SIZE(p->NdisPacket, NEO_PACKET_HEADER_SIZE); // Attach the buffer to the packet NdisChainBufferAtFront(p->NdisPacket, p->NdisBuffer); return p; }
// Create a packet buffer PACKET_BUFFER *NeoNewPacketBuffer() { PACKET_BUFFER *p; NET_BUFFER_LIST_POOL_PARAMETERS p1; // Memory allocation p = NeoZeroMalloc(sizeof(PACKET_BUFFER)); // Create a NET_BUFFER_LIST pool NeoZero(&p1, sizeof(p1)); p1.Header.Type = NDIS_OBJECT_TYPE_DEFAULT; p1.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1; p1.Header.Size = NDIS_SIZEOF_NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1; p1.ProtocolId = NDIS_PROTOCOL_ID_DEFAULT; p1.fAllocateNetBuffer = TRUE; p1.DataSize = NEO_MAX_PACKET_SIZE; p->NetBufferListPool = NdisAllocateNetBufferListPool(NULL, &p1); // Create a NET_BUFFER_LIST p->NetBufferList = NdisAllocateNetBufferList(p->NetBufferListPool, 0, 0); return p; }
// Read the information from the registry BOOL NeoLoadRegistory() { void *buf; NDIS_STATUS ret; UINT size; NDIS_HANDLE config; NDIS_CONFIGURATION_PARAMETER *param; UNICODE *name; ANSI_STRING ansi; UNICODE_STRING *unicode; UINT speed; BOOL keep; // Get the config handle NdisOpenConfiguration(&ret, &config, ctx->NdisConfig); if (NG(ret)) { // Failure return FALSE; } // Read the MAC address NdisReadNetworkAddress(&ret, &buf, &size, config); if (NG(ret)) { // Failure NdisCloseConfiguration(config); return FALSE; } // Copy the MAC address if (size != NEO_MAC_ADDRESS_SIZE) { // Invalid size NdisCloseConfiguration(config); return FALSE; } NeoCopy(ctx->MacAddress, buf, NEO_MAC_ADDRESS_SIZE); if (ctx->MacAddress[0] == 0x00 && ctx->MacAddress[1] == 0x00 && ctx->MacAddress[2] == 0x01 && ctx->MacAddress[3] == 0x00 && ctx->MacAddress[4] == 0x00 && ctx->MacAddress[5] == 0x01) { // Special MAC address UINT ptr32 = (UINT)((UINT64)ctx); ctx->MacAddress[0] = 0x00; ctx->MacAddress[1] = 0xAD; ctx->MacAddress[2] = ((UCHAR *)(&ptr32))[0]; ctx->MacAddress[3] = ((UCHAR *)(&ptr32))[1]; ctx->MacAddress[4] = ((UCHAR *)(&ptr32))[2]; ctx->MacAddress[5] = ((UCHAR *)(&ptr32))[3]; } // Initialize the key name of the device name name = NewUnicode("MatchingDeviceId"); // Read the hardware ID NdisReadConfiguration(&ret, ¶m, config, GetUnicode(name), NdisParameterString); FreeUnicode(name); if (NG(ret)) { // Failure NdisCloseConfiguration(config); return FALSE; } // Type checking if (param->ParameterType != NdisParameterString) { // Failure NdisCloseConfiguration(config); return FALSE; } unicode = ¶m->ParameterData.StringData; // Prepare a buffer for ANSI string NeoZero(&ansi, sizeof(ANSI_STRING)); ansi.MaximumLength = MAX_SIZE - 1; ansi.Buffer = NeoZeroMalloc(MAX_SIZE); // Convert to ANSI string NdisUnicodeStringToAnsiString(&ansi, unicode); // Copy strcpy(ctx->HardwareID, ansi.Buffer); strcpy(ctx->HardwareID_Raw, ctx->HardwareID); // Convert to upper case _strupr(ctx->HardwareID); // Release the memory NeoFree(ansi.Buffer); // Read the bit rate name = NewUnicode("MaxSpeed"); NdisReadConfiguration(&ret, ¶m, config, GetUnicode(name), NdisParameterInteger); FreeUnicode(name); if (NG(ret) || param->ParameterType != NdisParameterInteger) { speed = NEO_MAX_SPEED_DEFAULT; } else { speed = param->ParameterData.IntegerData * 10000; } max_speed = speed; // Read the link keeping flag name = NewUnicode("KeepLink"); NdisReadConfiguration(&ret, ¶m, config, GetUnicode(name), NdisParameterInteger); FreeUnicode(name); if (NG(ret) || param->ParameterType != NdisParameterInteger) { keep = false; } else { keep = (param->ParameterData.IntegerData == 0 ? false : true); } keep_link = keep; // Close the Config handle NdisCloseConfiguration(config); return TRUE; }