⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 winNT技术操作系统,国外开放的原代码和LIUX一样
💻 C
📖 第 1 页 / 共 3 页
字号:
#endif
    return NDIS_STATUS_PENDING;
}


static NDIS_STATUS STDCALL MiniportSetInformation(
    IN  NDIS_HANDLE MiniportAdapterContext,
    IN  NDIS_OID    Oid,
    IN  PVOID       InformationBuffer,
    IN  ULONG       InformationBufferLength,
    OUT PULONG      BytesRead,
    OUT PULONG      BytesNeeded)
/*
 * FUNCTION: Changes state information in the driver
 * ARGUMENTS:
 *     MiniportAdapterContext  = Pointer to adapter context area
 *     Oid                     = OID code designating set operation
 *     InformationBuffer       = Pointer to buffer with state information
 *     InformationBufferLength = Length of InformationBuffer
 *     BytesRead               = Address of buffer to place number of bytes read
 *     BytesNeeded             = Address of buffer to place number of extra bytes
 *                               needed in InformationBuffer for specified OID
 * RETURNS:
 *     Status of operation
 */
{
    ULONG GenericULONG;
    NDIS_STATUS Status   = NDIS_STATUS_SUCCESS;
    PNIC_ADAPTER Adapter = (PNIC_ADAPTER)MiniportAdapterContext;

    NDIS_DbgPrint(MAX_TRACE, ("Called. Oid (0x%X).\n", Oid));

    switch (Oid) {
    case OID_GEN_CURRENT_PACKET_FILTER:
        /* Verify length */
        if (InformationBufferLength < sizeof(ULONG)) {
            *BytesRead   = 0;
            *BytesNeeded = sizeof(ULONG) - InformationBufferLength;
            Status       = NDIS_STATUS_INVALID_LENGTH;
            break;
        }

        NdisMoveMemory(&GenericULONG, InformationBuffer, sizeof(ULONG));
        /* Check for properties the driver don't support */
        if (GenericULONG &
           (NDIS_PACKET_TYPE_ALL_FUNCTIONAL |
            NDIS_PACKET_TYPE_FUNCTIONAL     |
            NDIS_PACKET_TYPE_GROUP          |
            NDIS_PACKET_TYPE_MAC_FRAME      |
            NDIS_PACKET_TYPE_SMT            |
            NDIS_PACKET_TYPE_SOURCE_ROUTING)) {
            *BytesRead   = 4;
            *BytesNeeded = 0;
            Status       = NDIS_STATUS_NOT_SUPPORTED;
            break;
        }

        Adapter->PacketFilter = GenericULONG;

        /* FIXME: Set filter on hardware */

        break;
    case OID_GEN_CURRENT_LOOKAHEAD:
        /* Verify length */
        if (InformationBufferLength < sizeof(ULONG)) {
            *BytesRead   = 0;
            *BytesNeeded = sizeof(ULONG) - InformationBufferLength;
            Status = NDIS_STATUS_INVALID_LENGTH;
            break;
        }

        NdisMoveMemory(&GenericULONG, InformationBuffer, sizeof(ULONG));
        if (GenericULONG > DRIVER_MAXIMUM_LOOKAHEAD)
            Status = NDIS_STATUS_INVALID_LENGTH;
        else
            Adapter->LookaheadSize = GenericULONG;
        break;
    case OID_802_3_MULTICAST_LIST:
        /* Verify length. Must be multiplum of hardware address length */
        if ((InformationBufferLength % DRIVER_LENGTH_OF_ADDRESS) != 0) {
            *BytesRead   = 0;
            *BytesNeeded = 0;
            Status       = NDIS_STATUS_INVALID_LENGTH;
            break;
        }

        /* Set new multicast address list */
        NdisMoveMemory(Adapter->Addresses, InformationBuffer, InformationBufferLength);

        /* FIXME: Update hardware */

        break;
    default:
        NDIS_DbgPrint(MIN_TRACE, ("Invalid object ID (0x%X).\n", Oid));
        *BytesRead   = 0;
        *BytesNeeded = 0;
        Status       = NDIS_STATUS_INVALID_OID;
        break;
    }

    if (Status == NDIS_STATUS_SUCCESS) {
        *BytesRead   = InformationBufferLength;
        *BytesNeeded = 0;
    }

    NDIS_DbgPrint(MAX_TRACE, ("Leaving. Status (0x%X).\n", Status));

    return Status;
}


static NDIS_STATUS STDCALL MiniportTransferData(
    OUT PNDIS_PACKET    Packet,
    OUT PUINT           BytesTransferred,
    IN  NDIS_HANDLE     MiniportAdapterContext,
    IN  NDIS_HANDLE     MiniportReceiveContext,
    IN  UINT            ByteOffset,
    IN  UINT            BytesToTransfer)
/*
 * FUNCTION: Transfers data from a received frame into an NDIS packet
 * ARGUMENTS:
 *     Packet                 = Address of packet to copy received data into
 *     BytesTransferred       = Address of buffer to place number of bytes transmitted
 *     MiniportAdapterContext = Pointer to adapter context area
 *     MiniportReceiveContext = Pointer to receive context area (actually NULL)
 *     ByteOffset             = Offset within received packet to begin copying
 *     BytesToTransfer        = Number of bytes to copy into packet
 * RETURNS:
 *     Status of operation
 */
{
    PNDIS_BUFFER DstBuffer;
    UINT BytesCopied, BytesToCopy, DstSize;
    ULONG SrcData;
    PUCHAR DstData;
    UINT RecvStart;
    UINT RecvStop;
    PNIC_ADAPTER Adapter = (PNIC_ADAPTER)MiniportAdapterContext;

    NDIS_DbgPrint(MAX_TRACE, ("Called. Packet (0x%X)  ByteOffset (0x%X)  BytesToTransfer (%d).\n",
        Packet, ByteOffset, BytesToTransfer));

    if (BytesToTransfer == 0) {
        *BytesTransferred = 0;
        return NDIS_STATUS_SUCCESS;
    }

    RecvStart = Adapter->PageStart * DRIVER_BLOCK_SIZE;
    RecvStop  = Adapter->PageStop  * DRIVER_BLOCK_SIZE;

    NdisQueryPacket(Packet, NULL, NULL, &DstBuffer, NULL);
    NdisQueryBuffer(DstBuffer, (PVOID)&DstData, &DstSize);

    SrcData = Adapter->PacketOffset + sizeof(DISCARD_HEADER) + ByteOffset;
    if (ByteOffset + sizeof(DISCARD_HEADER) + BytesToTransfer >
	Adapter->PacketHeader.PacketLength)
        BytesToTransfer = Adapter->PacketHeader.PacketLength -
	    sizeof(DISCARD_HEADER) - ByteOffset;

    /* Start copying the data */
    BytesCopied = 0;
    for (;;) {
        BytesToCopy = (DstSize < BytesToTransfer) ? DstSize : BytesToTransfer;
        if (SrcData + BytesToCopy > RecvStop)
            BytesToCopy = (RecvStop - SrcData);

        NICReadData(Adapter, DstData, SrcData, BytesToCopy);

        BytesCopied     += BytesToCopy;
        SrcData         += BytesToCopy;
        DstData          = (PUCHAR)((ULONG_PTR) DstData + BytesToCopy);
        BytesToTransfer -= BytesToCopy;
        if (BytesToTransfer == 0)
            break;

        DstSize -= BytesToCopy;
        if (DstSize == 0) {
            /* No more bytes in destination buffer. Proceed to
               the next buffer in the destination buffer chain */
            NdisGetNextBuffer(DstBuffer, &DstBuffer);
            if (!DstBuffer)
                break;

            NdisQueryBuffer(DstBuffer, (PVOID)&DstData, &DstSize);
        }

        if (SrcData == RecvStop)
            SrcData = RecvStart;
    }

    NDIS_DbgPrint(MID_TRACE, ("Transferred (%d) bytes.\n", BytesToTransfer));

    *BytesTransferred = BytesCopied;

    return NDIS_STATUS_SUCCESS;
}


NTSTATUS
#ifndef _MSC_VER
STDCALL
#endif
DriverEntry(
    PDRIVER_OBJECT DriverObject,
    PUNICODE_STRING RegistryPath)
/*
 * FUNCTION: Main driver entry point
 * ARGUMENTS:
 *     DriverObject = Pointer to a driver object for this driver
 *     RegistryPath = Registry node for configuration parameters
 * RETURNS:
 *     Status of driver initialization
 */
{
    NDIS_STATUS Status;
    NDIS_HANDLE NdisWrapperHandle;
    NDIS_MINIPORT_CHARACTERISTICS Miniport;

    NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));

    NdisZeroMemory(&Miniport, sizeof(Miniport));
    Miniport.MajorNdisVersion           = DRIVER_NDIS_MAJOR_VERSION;
    Miniport.MinorNdisVersion           = DRIVER_NDIS_MINOR_VERSION;
    Miniport.CheckForHangHandler        = NULL; //MiniportCheckForHang;
    Miniport.DisableInterruptHandler    = MiniportDisableInterrupt;
    Miniport.EnableInterruptHandler     = MiniportEnableInterrupt;
    Miniport.HaltHandler                = MiniportHalt;
    Miniport.HandleInterruptHandler     = MiniportHandleInterrupt;
    Miniport.InitializeHandler          = MiniportInitialize;
    Miniport.ISRHandler                 = MiniportISR;
    Miniport.QueryInformationHandler    = MiniportQueryInformation;
    Miniport.ReconfigureHandler         = MiniportReconfigure;
    Miniport.ResetHandler               = MiniportReset;
    Miniport.SendHandler                = MiniportSend;
    Miniport.SetInformationHandler      = MiniportSetInformation;
    Miniport.TransferDataHandler        = MiniportTransferData;

    NdisMInitializeWrapper(&NdisWrapperHandle,
                           DriverObject,
                           RegistryPath,
                           NULL);

    DriverInfo.NdisWrapperHandle = NdisWrapperHandle;
    DriverInfo.NdisMacHandle     = NULL;
    InitializeListHead(&DriverInfo.AdapterListHead);

    Status = NdisMRegisterMiniport(NdisWrapperHandle,
                                   &Miniport,
                                   sizeof(NDIS_MINIPORT_CHARACTERISTICS));
    if (Status != NDIS_STATUS_SUCCESS) {
        NDIS_DbgPrint(MIN_TRACE, ("NdisMRegisterMiniport() failed with status code (0x%X).\n", Status));
        NdisTerminateWrapper(NdisWrapperHandle, NULL);
        return STATUS_UNSUCCESSFUL;
    }

    return STATUS_SUCCESS;
}

#if 0
        /* while i'm here - some basic registry sanity checks */
        {
            /* write tests */
            NDIS_CONFIGURATION_PARAMETER ParameterValue;

            ParameterValue.ParameterType = NdisParameterInteger;
            ParameterValue.ParameterData.IntegerData = 0x12345678;
            NdisInitUnicodeString(&Keyword, L"DwordTest");
            NdisWriteConfiguration(&Status, ConfigurationHandle, &Keyword, &ParameterValue);

            if(Status != NDIS_STATUS_SUCCESS)
            {
                DbgPrint("ne2000!MiniportInitialize: failed to set DwordTest: 0x%x\n", Status);
                KeBugCheck(0);
            }

            DbgPrint("ne2000!MiniportInitialize: DwordTest successfully set\n");

            NdisInitUnicodeString(&Keyword, L"StringTest");
            ParameterValue.ParameterType = NdisParameterString;
            NdisInitUnicodeString(&ParameterValue.ParameterData.StringData, L"Testing123");

            NdisWriteConfiguration(&Status, ConfigurationHandle, &Keyword, &ParameterValue);

            if(Status != NDIS_STATUS_SUCCESS)
            {
                DbgPrint("ne2000!MiniportInitialize: failed to set StringTest: 0x%x\n", Status);
                KeBugCheck(0);
            }

            DbgPrint("ne2000!MiniportInitialize: StringTest successfully set\n");
        }

        {
            /* read back the test values */
            NDIS_CONFIGURATION_PARAMETER *ParameterValue = 0;

            NdisInitUnicodeString(&Keyword, L"DwordTest");
            NdisReadConfiguration(&Status, &ParameterValue, ConfigurationHandle, &Keyword, NdisParameterInteger);

            if(Status != NDIS_STATUS_SUCCESS)
            {
                DbgPrint("ne2000!MiniportInitialize: failed to read DwordTest: 0x%x\n", Status);
                KeBugCheck(0);
            }

            if(ParameterValue->ParameterData.IntegerData != 0x12345678)
            {
                DbgPrint("ne2000!MiniportInitialize: DwordTest value is wrong: 0x%x\n",
                    ParameterValue->ParameterData.IntegerData);
                KeBugCheck(0);
            }

            DbgPrint("ne2000!MiniportInitialize: DwordTest value was correctly read\n");

            NdisInitUnicodeString(&Keyword, L"StringTest");
            NdisReadConfiguration(&Status, &ParameterValue, ConfigurationHandle, &Keyword, NdisParameterString);

            if(Status != NDIS_STATUS_SUCCESS)
            {
                DbgPrint("ne2000!MiniportInitialize: failed to read StringTest: 0x%x\n", Status);
                KeBugCheck(0);
            }

            if(wcsncmp(ParameterValue->ParameterData.StringData.Buffer, L"Testing123",
                    wcslen(L"Testing123")))
            {
                DbgPrint("ne2000!MiniportInitialize: StringTest value is wrong: %wZ\n",
                    &ParameterValue->ParameterData.StringData);
                KeBugCheck(0);
            }

            DbgPrint("ne2000!MiniportInitialize: StringTest value was correctly read\n");
        }

#endif
/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -