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

📄 pktdrv.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2005 Dennis Kuschel
 * Copyright (c) 2001,2002 Florian Schulze.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of the contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * pktif.c - This file is part of lwIPtest
 *
 ****************************************************************************
 *
 * This file is derived from an example in lwIP with the following license:
 *
 * Copyright (c) 2001, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*  - CHANGE LOG -
 *
 *  o  Now the user can choose the adapter by pressing a key.
 *     Dennis Kuschel, 2005-01-22
 *  o  Now the driver description is also printed
 *     (This is very helpful for users running Windows XP)
 *     Dennis Kuschel, 2005-01-29
 *  o  The driver is now able to emulate a real hardware NIC
 *     (from the view of pico]OS). You need to set the define NIC_EMU.
 *     Dennis Kuschel, 2005-02-10
 */

#define WIN32_LEAN_AND_MEAN
/* get the windows definitions of the following 4 functions out of the way */
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <packet32.h>
#include <ntddndis.h>

#define NIC_EMU  /* set this define if you wish a real hw NIC emulation */

LPADAPTER  lpAdapter;
LPPACKET   lpPacket;
char buffer[256000];  // buffer to hold the data coming from the driver
unsigned char *cur_packet;
int cur_length;
unsigned char ethaddr[6];

/*-----------------------------------------------------------------------------------*/
int init_adapter(int adapter_num)
{
  #define Max_Num_Adapter 10

  void *AdapterList[Max_Num_Adapter];
  char *AdapterDesc[Max_Num_Adapter];
  char prbuf[100];
  int i, l;
  char c;
  DWORD dwVersion;
  DWORD dwWindowsMajorVersion;

  //unicode strings (winnt)
  wchar_t     AdapterName[8192]; // string that contains a list of the network adapters
  wchar_t     *temp,*temp1;

  //ascii strings (win95)
  char        AdapterNamea[8192]; // string that contains a list of the network adapters
  char        *tempa,*temp1a;

  int         AdapterNum=0;
  ULONG       AdapterLength;

  PPACKET_OID_DATA ppacket_oid_data;

  // obtain the name of the adapters installed on this machine
  AdapterLength=4096;

  memset(AdapterList,0,sizeof(AdapterList));

  if (adapter_num < 0) {
    printf("\nList of found adapters:\n\n");
  }

  i=0;

  // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
  // We have to check the os on which we are running
  dwVersion=GetVersion();
  dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
  if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
  {  // Windows NT
    if (PacketGetAdapterNames((char *)AdapterName,&AdapterLength)==FALSE){
        printf("Unable to retrieve the list of the adapters!\n");
          return -1;
    }
    temp=AdapterName;
    temp1=AdapterName;
    while ((*temp!='\0')||(*(temp-1)!='\0'))
    {
      if (*temp=='\0')
      {
        AdapterList[i] = temp1;
        temp1=temp+1;
        i++;
      }

      temp++;
    }

    AdapterNum=i;

    tempa = (char*) (temp + 1);
    for (i=0; i<AdapterNum; i++)
    {
      AdapterDesc[i] = tempa;
      tempa += strlen(tempa) + 1;
    }

    for (i=0; i<AdapterNum; i++)
    {
      if (*AdapterDesc[i] != 0)
      {
        memset(prbuf, 0, 100);
        strncpy(prbuf, AdapterDesc[i], 75);
        printf("%2i: %s\n", i, prbuf);
      }
      else
      {
        wprintf(L"%2i: %s\n", i, AdapterList[i]);
      }
    }
  }

  else    //windows 95
  {
    if (PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){
      printf("Unable to retrieve the list of the adapters!\n");
      return -1;
    }
    tempa=AdapterNamea;
    temp1a=AdapterNamea;

    while ((*tempa!='\0')||(*(tempa-1)!='\0'))
    {
      if (*tempa=='\0')
      {
        AdapterList[i] = temp1a;
        temp1a=tempa+1;
        i++;
      }
      tempa++;
    }

    AdapterNum=i;

    tempa = tempa + 1;
    for (i=0; i<AdapterNum; i++)
    {
      AdapterDesc[i] = tempa;
      tempa += strlen(tempa) + 1;
    }

    for (i=0; i<AdapterNum; i++)
    {
      l = printf("%2i: %s", i, (char*)AdapterList[i]);
      memset(prbuf, 0, 100);
      if ((l > 0) && (l < 77))
      {
        strncpy(prbuf, AdapterDesc[i], (80 - 3) - l);
      }
      printf("  %s\n", prbuf);
    }
  }

  if (AdapterNum<=0)
    return -1;
  if ((adapter_num < 0) || (adapter_num >= AdapterNum))
  {
    printf("\nPlease choose an adapter: ");
    do {
      do {
        c = _getch();
      } while ((c < '0') || (c > '9'));
      adapter_num = c - '0';
    } while (adapter_num >= AdapterNum);
    printf("%i\n\n", adapter_num);
  }
  if (adapter_num >= AdapterNum)
    return -1;
  ppacket_oid_data=malloc(sizeof(PACKET_OID_DATA)+6);
  lpAdapter=PacketOpenAdapter(AdapterList[adapter_num]);
  if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
    return -1;
  ppacket_oid_data->Oid=OID_802_3_PERMANENT_ADDRESS;
  ppacket_oid_data->Length=6;
  if (!PacketRequest(lpAdapter,FALSE,ppacket_oid_data))
    return -1;
  memcpy(&ethaddr,ppacket_oid_data->Data,6);
  free(ppacket_oid_data);
  printf("-->%2i: MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",
         adapter_num, ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]);
  PacketSetBuff(lpAdapter,512000);
  PacketSetReadTimeout(lpAdapter,1);
  PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_ALL_LOCAL);
  if ((lpPacket = PacketAllocatePacket())==NULL){
    return (-1);
  }
  PacketInitPacket(lpPacket,(char*)buffer,256000);

  return adapter_num;
}

void shutdown_adapter(void)
{
  PacketFreePacket(lpPacket);
  PacketCloseAdapter(lpAdapter);
}

int packet_send(void *buffer, int len)
{
  LPPACKET lpPacket;

  if ((lpPacket = PacketAllocatePacket())==NULL)
    return -1;
  PacketInitPacket(lpPacket,buffer,len);
  if (!PacketSendPacket(lpAdapter,lpPacket,TRUE))
    return -1;
  PacketFreePacket(lpPacket);

  return 0;
}

#ifdef NIC_EMU
static
#else
extern
#endif
void process_input(void);

static void ProcessPackets(LPPACKET lpPacket)
{
  ULONG   ulLines, ulBytesReceived;
  char    *base;
  char    *buf;
  u_int off=0;
  u_int tlen,tlen1;
  struct bpf_hdr *hdr;

  ulBytesReceived = lpPacket->ulBytesReceived;

⌨️ 快捷键说明

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