几年前写的一个虚拟网卡驱动

2009-05-13 04:14:13来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

晚上整理磁盘,居然翻出了几年前写的虚拟网卡驱动。这个驱动主要的功能是可以在一个真实的物理网卡上,模拟出多个虚拟网卡,虚拟网卡有自己的MAC和IP地址。驱动的实现借鉴了网桥的部分实现方式和其他网卡驱动的实现方式
头文件:
#ifndef _IF_VCN_H_
#define _IF_VCN_H_
#define VCNNAME      "vcn"
#define VCN_MAXUNIT  32          /* maxnum vcn */
#define VCN_MAXIP    10
struct    ifvcn {
    struct    arpcom ifv_ac;   /* make this an interface */
    struct    ifnet *ifv_p;    /* parent interface */
    u_long ip[VCN_MAXIP];      /* the ip array of this interface, just for search faster */
    short ip_num;            /* the ip number */
    LIST_ENTRY(ifvcn) ifv_list;
    struct resource *r_unit;    /* resource allocated for this unit */
};
#define    ifv_if    ifv_ac.ac_if
#ifdef _KERNEL
typedef    int vcn_dispatch_t(struct ifnet *ifp_p, struct ether_header *eh, struct mbuf *m);
extern int vcn_loaded;
extern vcn_dispatch_t *vcn_dispatch_ptr;
#endif /* _KERNEL */
#endif /* !_IF_VCN_H_ */
C文件
#include "opt_inet.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include     /* XXX: Shouldn't really be required! */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef INET
#include
#include
#endif
#include
/* convert a array to a long */
#define A2L(dim) ( dim[0] + (dim[1] if_softc;
    u_long seed;
    microtime(&tv);
    seed = tv.tv_sec ^ tv.tv_usec;
   
    sc->ifv_ac.ac_enaddr[0] = 0x0;
    sc->ifv_ac.ac_enaddr[1] = 0x62;
    sc->ifv_ac.ac_enaddr[2] = 0x6c;
    sc->ifv_ac.ac_enaddr[3] = 0x6d;
    sc->ifv_ac.ac_enaddr[4] = (seed & 0xff);
    sc->ifv_ac.ac_enaddr[5] = ifp->if_unit & 0xff;
}
/* if matched return 1, else return 0 */
static int
vcn_match_eaddr(struct ifnet *ifp, struct ether_header *eh)
{
#define IFP2AC(IFP) ((struct arpcom *)IFP)
    if (((eh->ether_dhost[0] & 1) == 0) &&
         (memcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) == 0)) {
        return 1;
    } else
        return 0;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:第3章 简单的 port -- 3.3 创建校验和文件

下一篇:第3章 简单的 port -- 3.4 测试 port