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

📄 ndis2.c

📁 Windows XP下的抓包程序实现
💻 C
📖 第 1 页 / 共 2 页
字号:
   */
  for (i = 0; i < rxBufDescr->rxDataCount; ++i)
  {
    struct _RxBufDescrRec *rxDescr = &rxBufDescr->rxBufDescrRec[i];

    memcpy (pktBuf->buffer + pktBuf->packetLength,
            rxDescr->rxDataPtr, rxDescr->rxDataLen);
    pktBuf->packetLength += rxDescr->rxDataLen;
  }

  EnquePktBuf (pktBuf);

  ARGSUSED (frameSize);
  ARGSUSED (reqHandle);
  ARGSUSED (indicate);
  ARGSUSED (protDS);   

  /* This frees up the buffer for the MAC to use
   */
  return (ERR_SUCCESS);
}

CALLBACK (NdisStatusProc (WORD macId,  WORD param1, BYTE *indicate,
                          WORD opcode, WORD protDS))
{
  switch (opcode)
  {
    case STATUS_RING_STATUS:
	 break;
    case STATUS_ADAPTER_CHECK:
	 break;
    case STATUS_START_RESET:
	 break;
    case STATUS_INTERRUPT:
	 break;
    case STATUS_END_RESET:
	 break;
    default:
	 break;
  }
  ARGSUSED (macId);
  ARGSUSED (param1);
  ARGSUSED (indicate);
  ARGSUSED (opcode);
  ARGSUSED (protDS);

  /* We don't need to do anything about this stuff yet
   */
  return (ERR_SUCCESS);
}

/*
 * Tell the NDIS driver to start the delivery of the packet
 */
int NdisSendPacket (struct _PktBuf *pktBuf, int macId)
{ 
  struct _TxBufDescr txBufDescr;
  int     result;

  xmitPending++;
  txBufPending = pktBuf;    /* we only have 1 pending Tx at a time */

  txBufDescr.txImmedLen  = 0;
  txBufDescr.txImmedPtr  = NULL;
  txBufDescr.txDataCount = 1;
  txBufDescr.txBufDescrRec[0].txPtrType = NDIS_PTR_PHYSICAL;
  txBufDescr.txBufDescrRec[0].dummy     = 0;
  txBufDescr.txBufDescrRec[0].txDataLen = pktBuf->packetLength;
  txBufDescr.txBufDescrRec[0].txDataPtr = pktBuf->buffer;

  result = MAC_DISPATCH(handle)->transmitChain (common.moduleId,
                                                pktBuf->handle,
                                                &txBufDescr,
                                                handle->common->moduleDS);
  switch (result)
  {
    case ERR_OUT_OF_RESOURCE:
         /* Note that this should not happen but if it does there is not
          * much we can do about it
          */
         printf ("ERROR: transmit queue overflowed\n");
         return (0);

    case ERR_SUCCESS:
         /* Everything was hunky dory and synchronous. Free up the 
          * packet buffer
          */
         xmitPending--;
         FreePktBuf (pktBuf);
         return (1);

    case ERR_REQUEST_QUEUED:
         /* Everything was hunky dory and asynchronous. Do nothing
          */
         return (1);

    default:
         printf ("Tx fail, code = %04X\n", result);
         return (0);
  }
}



static int ndis_nerr = sizeof(ndis_errlist) / sizeof(ndis_errlist[0]);

static char *Ndis_strerror (WORD errorCode)
{
  static char buf[30];
  int    i;

  for (i = 0; i < ndis_nerr; i++)
      if (errorCode == ndis_errlist[i].err_num)
         return (ndis_errlist[i].err_text);

  sprintf (buf,"unknown error %d",errorCode);
  return (buf);
}


char *NdisLastError (void)
{
  char *errStr = lastErr;
  lastErr = NULL;
  return (errStr);
}

int NdisOpen (void)
{
  struct _ReqBlock reqBlock;
  int     result;
  int     ndisFd = open (NDIS_PATH, O_RDONLY);

  if (ndisFd < 0)
  {
    printf ("Could not open NDIS Protocol Manager device.\n");
    return (0);
  }

  memset (&reqBlock, 0, sizeof(ReqBlock));

  reqBlock.opcode = PM_GET_PROTOCOL_MANAGER_LINKAGE;

  result = NdisGetLinkage (ndisFd, (char*)&reqBlock, sizeof(ReqBlock));
  if (result != 0)
  {
    printf ("Could not get Protocol Manager linkage.\n");
    close (ndisFd);
    return (0);
  }

  close (ndisFd);
  protManEntry = (ProtMan) reqBlock.pointer1;
  protManDS    = reqBlock.word1;

  DEBUG2 ("Entry Point = %04X:%04X\n", FP_SEG(protManEntry),FP_OFF(protManEntry));
  DEBUG1 ("ProtMan DS  = %04X\n", protManDS);
  return (1);
}


int NdisRegisterAndBind (int promis)
{
  struct _ReqBlock reqBlock;
  WORD    result;

  memset (&common,0,sizeof(common));

  common.tableSize = sizeof (common);

  common.majorNdisVersion   = 2;
  common.minorNdisVersion   = 0;
  common.majorModuleVersion = 2;
  common.minorModuleVersion = 0;

  /* Indicates binding from below and dynamically loaded
   */
  common.moduleFlags = 0x00000006L;

  strcpy (common.moduleName, "PCAP");

  common.protocolLevelUpper = 0xFF;
  common.protocolLevelLower = 1;
  common.interfaceLower     = 1;
#ifdef __DJGPP__
  common.moduleDS           = _dos_ds; /* the callback data segment */
#else
  common.moduleDS           = _DS;
#endif

  common.systemRequest      = (SystemRequest) systemRequestGlue;
  common.serviceChars       = (BYTE*) &protChars;
  common.serviceStatus      = NULL;
  common.upperDispatchTable = NULL;
  common.lowerDispatchTable = (BYTE*) &lowerTable;

  protChars.length  = sizeof (protChars);
  protChars.name[0] = 0;
  protChars.type    = 0;

  lowerTable.backPointer        = &common;
  lowerTable.requestConfirm     = requestConfirmGlue;
  lowerTable.transmitConfirm    = transmitConfirmGlue;
  lowerTable.receiveLookahead   = receiveLookaheadGlue;
  lowerTable.indicationComplete = indicationCompleteGlue;
  lowerTable.receiveChain       = receiveChainGlue;
  lowerTable.status             = statusGlue;
  lowerTable.flags              = 3;
  if (promis)
     lowerTable.flags |= 4;   /* promiscous mode (receive everything) */

  bindings.numBindings = 1;
  strcpy (bindings.moduleName[0], handle->moduleName);

  /* Register ourselves with NDIS
   */
  reqBlock.opcode   = PM_REGISTER_MODULE;
  reqBlock.pointer1 = (BYTE FAR*) &common;
  reqBlock.pointer2 = (BYTE FAR*) &bindings;

  result = (*protManEntry) (&reqBlock, protManDS);
  if (result)
  {
    printf ("Protman registering failed: %s\n", Ndis_strerror(result));
    return (0);
  }

  /* Start the binding process
   */
  reqBlock.opcode   = PM_BIND_AND_START;
  reqBlock.pointer1 = (BYTE FAR*) &failingModules;

  result = (*protManEntry) (&reqBlock, protManDS);
  if (result)
  {
    printf ("Start binding failed: %s\n", Ndis_strerror(result));
    return (0);
  }
  return (1);
}

static int CheckMacFeatures (CardHandle *card)
{
  DWORD      serviceFlags;
  BYTE _far *mediaString;
  BYTE _far *mac_addr;

  DEBUG2 ("checking card features\n"
          "common table address = %08lX, macId = %d\n",
          card->common, card->common->moduleId);

  serviceFlags = MAC_CHAR (handle)->serviceFlags;

  if ((serviceFlags & SF_PROMISCUOUS) == 0)
  {
    printf ("The MAC %s does not support promiscuous mode.\n",
            card->moduleName);
    return (0);
  }

  mediaString = MAC_CHAR (handle)->macName;

  DEBUG1 ("media type = %s\n",mediaString);

  /* Get the media type. And set the header size
   */
  if (!strncmp(mediaString,"802.3",5) ||
      !strncmp(mediaString,"DIX",3)   ||
      !strncmp(mediaString,"DIX+802.3",9))
       headerSize = sizeof (EthernetIIHeader);

  else if (!strncmp(mediaString,"FDDI",4))
       headerSize = sizeof (FddiHeader) +
                    sizeof (Ieee802Dot2SnapHeader);
  else
  {
    printf ("Unsupported MAC type: `%s'\n", mediaString);
    return (0);
  }

  frameSize = MAC_CHAR (handle)->maxFrameSize;
  mac_addr  = MAC_CHAR (handle)->currentAddress;

  printf ("Hardware address: %02X:%02X:%02X:%02X:%02X:%02X\n",
          mac_addr[0], mac_addr[1], mac_addr[2],
          mac_addr[3], mac_addr[4], mac_addr[5]);
  return (1);
}

static int NdisStartMac (CardHandle *card)
{
  WORD result;

  /* Set the lookahead length
   */
  result = MAC_DISPATCH(handle)->request (common.moduleId, 0,
                                          headerSize, 0,
                                          REQ_SET_LOOKAHEAD,
                                          card->common->moduleDS);

  /* We assume that if we got INVALID PARAMETER then either this
   * is not supported or will work anyway. NE2000 does this.
   */
  if (result != ERR_SUCCESS && result != ERR_INVALID_PARAMETER)
  {
    DEBUG1 ("Set lookahead failed: %s\n", Ndis_strerror(result));
    return (0);
  }

  /* Set the packet filter. Note that for some medias and drivers we
   * must specify all three flags or the card(s) will not operate correctly.
   */
  result = MAC_DISPATCH(handle)->request (common.moduleId, 0,
                      /* all packets */   FILTER_PROMISCUOUS |
                      /* packets to us */ FILTER_DIRECTED    |
                      /* broadcasts */    FILTER_BROADCAST,
                                          0, REQ_SET_PACKET_FILTER,
                                          card->common->moduleDS);
  if (result != ERR_SUCCESS)
  {
    DEBUG1 ("Set packet filter failed: %s\n", Ndis_strerror(result));
    return (0);
  }

  /* If OPEN/CLOSE supported then open the adapter
   */
  if (MAC_CHAR(handle)->serviceFlags & SF_OPEN_CLOSE)
  {
    result = MAC_DISPATCH(handle)->request (common.moduleId, 0, 0, NULL,
                                            REQ_OPEN_ADAPTER,
                                            card->common->moduleDS);
    if (result != ERR_SUCCESS)
    {
      DEBUG1 ("Opening the MAC failed: %s\n", Ndis_strerror(result));
      return (0);
    }
  }
  return (1);
}

void NdisShutdown (void)
{
  struct _ReqBlock reqBlock;
  int     result, i;

  if (!handle)
     return;

  /* If the adapters support open and are open then close them
   */
  if ((MAC_CHAR(handle)->serviceFlags & SF_OPEN_CLOSE) &&
      (MAC_STATUS(handle)->macStatus & MAC_OPEN))
  {
    result = MAC_DISPATCH(handle)->request (common.moduleId, 0, 0, 0,
                                            REQ_CLOSE_ADAPTER,
                                            handle->common->moduleDS);
    if (result != ERR_SUCCESS)
    {
      printf ("Closing the MAC failed: %s\n", Ndis_strerror(result));
      return;
    }
  }

  /* Tell the Protocol Manager to unbind and stop
   */
  reqBlock.opcode   = PM_UNBIND_AND_STOP;
  reqBlock.pointer1 = (BYTE FAR*) &failingModules;
  reqBlock.pointer2 = NULL;

  result = (*protManEntry) (&reqBlock, protManDS);
  if (result)
     printf ("Unbind failed: %s\n",  Ndis_strerror(result));

  for (i = 0; i < STACK_POOL_SIZE; ++i)
     free (freeStacks[i] - STACK_SIZE);

  handle = NULL;
}

int NdisInit (int promis)
{
  int i, result;

  /* Allocate the real mode stacks used for NDIS callbacks
   */
  for (i = 0; i < STACK_POOL_SIZE; ++i)
  {
    freeStacks[i] = malloc (STACK_SIZE);
    if (!freeStacks[i])
       return (0);
    freeStacks[i] += STACK_SIZE;
  }

  if (!NdisOpen())
     return (0);

  if (!NdisRegisterAndBind(promis))
     return (0);

  DEBUG1 ("My module id: %d\n", common.moduleId);
  DEBUG1 ("Handle id;    %d\n", handle->common->moduleId);
  DEBUG1 ("MAC card:     %-16s - ", handle->moduleName);

  atexit (NdisShutdown);

  if (!CheckMacFeatures(&handle))
     return (0);

  switch (mediaType)
  {
    case MEDIA_FDDI:
         DEBUG0 ("Media type: FDDI");
	 break;
    case MEDIA_ETHERNET:
         DEBUG0 ("Media type: ETHERNET");
	 break;
    default:
         DEBUG0 ("Unsupported media.\n");
         return (0);
  }

  DEBUG1 (" - Frame size: %d\n", frameSize);

  if (!NdisStartMac(&handle))
     return (0);
  return (1);
}
#endif  /* USE_NDIS2 */

⌨️ 快捷键说明

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