vxworks下MUX接口的使用

来源:互联网 发布:宏投网络 编辑:程序博客网 时间:2024/05/17 01:26

http://blog.csdn.net/at91rm9200/article/details/1438115


1. END驱动与MUX接口概述

VxWorks下增强型网络驱动-END(EnhancedNetworkDriver)是一个数据链路层驱动程序,增强型网络驱动(END)是OSI模型中数据链路层的实现,通过MUX函数与网络协议层通讯。END驱动程序是基于MUX模式,网络驱动程序被划分为协议组件和硬件组件。MUX作为数据链路层和网络层之间的接口,它管理网络协议接口和低层硬件接口之间的交互,将硬件从网络协议的细节中隔离出来;删除使用输入钩例程来过滤接收从协议来的数据包,和删除了使用输出钩例程来过滤协议包的发送;并且链路层上的驱动程序需要访问网络层(IP或其他协议)时,也会调用相关的MUX例程。值得注意的是,网络层协议和数据链路层驱动程序不能直接通讯,它们必须通过MUX。MUX的目的是提供一个接口,隔离网络接口驱动和网络服务,MUX支持两种网络驱动接口类型:
– END: Enhanced Network Driver,一种面向帧结构的驱动
– NPT: Network Protocol Toolkit,一种面向分组的驱动,所有链路层信息被去除。


2. 用MUX-API实现网络协议

(1) 网络协议是OSI模型中网络层和传输层的实现.在基于END驱动vxWorks中,网络协议通过MUX接口与数据链路层进行通信,所有与具体网络接口相关的事务都在数据链路层驱动中进行处理,如发送和接收数据等。


2. 实现过程详解
 

*Step 1: 使用muxBind()函数将协议类型与特定的网络接口绑定

muxBind()函数原型如下:

void * muxBind
    (
    char * pName,             /* interface name, for example, ln, ei,... */
    int    unit,              /* unit number */
    BOOL (* stackRcvRtn) (void* , long, M_BLK_ID, LL_HDR_INFO * , void* ),
                              /* receive function to be called. */
    STATUS (* stackShutdownRtn) (void* , void* ),
                              /* routine to call to shutdown the stack */
    STATUS (* stackTxRestartRtn) (void* , void* ),
                              /* routine to tell the stack it can transmit */
    void (* stackErrorRtn) (END_OBJ* , END_ERR* , void* ),
                              /* routine to call on an error. */
    long   type,              /* protocol type from RFC1700 and many */
                              /* other sources (for example, 0x800 is IP) */ 
    char * pProtoName,        /* string name for protocol */
    void * pSpare             /* per protocol spare pointer */
    )

其中: pName为网络接口的名字, unit为接口号, stackRcvRtn为协议数据处理函数,负责对接收的数据(或者发送的数据,当协议类型为MUX_PROTO_OUTPUT时), stackShutdownRtn,stackTxRestartRtn,stackErrorRtn分别为关闭,重启和错误处理函数, 若不用则设置为NULL, type为指定的协议类型, pProtoName为协议名称, pSpare为每个协议的备用指针
协议类型及含义:
MUX_PROTO_SNARF: 在所有标准协议接收之前调用 stackRcvRtn()处理函数;
MUX_PROTO_PROMISC: 在所有标准协议接收之后调用 stackRcvRtn(0处理函数;
MUX_PROTO_OUTPUT: 在数据被送到物理层(DRIVER)之前调用 stackRcvRtn()函数.

在用muxSend()发送数据时,需要从网络数据缓冲池中申请内存,这里需要用到END_OBJ指针,
取得 END_OBJ 的方法

第一种方法:
END_OBJ * endFindByName
(
    char * pName,             /* device name to search for */
    int    unit
)
第二种方法:
利用muxBind()的返回值(void类型的指针),将此指针强制转换为MUX_ID类型(声明在系统头文件 "private/muxlibP.h"中),其中的pEnd成员即我们需要的END_OBJ指针.如下:

pEnd = ((MUX_ID)pSendCookie)->pEnd;

 

/* 接收(或者发送)处理函数[也可以说是过滤函数] */
BOOL stackRcvRtn
(
void * pCookie, /* returned by muxBind() */
long type, /* from RFC1700, or user-defined */
M_BLK_ID pNetBuff, /* packet with link-level info */
LL_HDR_INFO * pLinkHdr, /* link-level header info structure */
void * pSpare /* defined on a per-protocol basis */
)
{
/*process code here*/
return(FALSE/*or TRUE*/);
}

若stackRcvRtn()返回TRUE,则该数据包将不再被传递至上层协议或底层DRIVER
(当为MUX_PROTO_OUTPUT类型时);否则该数据包将继续向上或向下传递.


/*muxSend()发送数据包*/

 

/******/
示例代码: 在vxWorks下利用MUX接口实现UDP广播

说明: 在vxworks下利用socket发送UDP广播时,虽然将广播地址设置为255.255.255.255,但在底层
vxWorks用子网掩码进行处理后,际的广播地址为本网段的广播地址(如10.10.156.255).下面这个程序利用MUX接口,自己构造UDP数据包,用muxSend()实现对整个网络的UDP广播(255.255.255.255).

/* 头文件 */

#include "vxWorks.h"
#include "netinet/in.h"
#include "netinet/ip.h"
#include "netinet/if_ether.h"
#include "netinet/udp.h"

#define ARRAY_BROADCAST_PORT  2020
#define ARRAY_ACQ_PORT        2021

#define EH_SIZE                 14
#define IPHL                    20
#define UDPHL                   8

/*=============================================================*/
struct myIpFrame        /* ip frame struct which has no 14 bytes ethernet header */
{
 struct ip *iph;
 struct udphdr *udph;
 char   *pdata;
};

struct ps_udph          /* pseudo udp header for checksum */
{
    struct in_addr      srcip;
    struct in_addr      dstip;
    char                zero;
    char                prto;
    short               ulen;
};

struct myBuffer
{
 char *buf;
        u_short  size;
};

/* 源程序 */

#include "private/muxlibP.h"
#include "setupArray.h"

/****************************************************/
BOOL stackRcvRtn
(
void * pCookie, /* returned by muxBind() */
long type, /* from RFC1700, or user-defined */
M_BLK_ID pNetBuff, /* packet with link-level info */
LL_HDR_INFO * pLinkHdr, /* link-level header info structure */
void * pSpare /* defined on a per-protocol basis */
)
{
return(FALSE);
}

/*****************************************************/
#define MAX_DATA_LEN    500

STATUS myUdpBroadcast(char *pName, int unit, char *udp_data,int size)
{

END_OBJ *pEnd;
M_BLK_ID  pMblk;
void *pSendCookie;

struct ps_udph pudph;
struct ether_header eth_head;
struct myIpFrame myframe;
struct myBuffer pbuf; 
char *pData;
int length;

const char dst_mac_addr[6]={0xff,0xff,0xff,0xff,0xff,0xff};
const char src_mac_addr[6]={0x00,0x30,0x40,0x10,0x00,0x40};

  if(udp_data == NULL || size<=0) return (ERROR);
   
  if((pSendCookie = muxBind(pName,unit,(FUNCPTR)stackRcvRtn,NULL,NULL,NULL,
                          MUX_PROTO_PROMISC,"ArraySetup",NULL)) == NULL)
  {
  perror("muxBind");
  return(ERROR);
  }
  
  if((pEnd = ((MUX_ID)pSendCookie)->pEnd) == NULL)
  {
  perror("pEnd");
  muxUnbind(pSendCookie,MUX_PROTO_PROMISC,(FUNCPTR)stackRcvRtn);
  return(ERROR);
  }

  if ((pMblk = netTupleGet (pEnd->pNetPool, 1514, M_DONTWAIT, MT_DATA,
                            FALSE)) == NULL)
  {
    perror("pMblk");
    muxUnbind(pSendCookie,MUX_PROTO_PROMISC,(FUNCPTR)stackRcvRtn);
    return(ERROR);
  }

  pMblk->mBlkHdr.mFlags |= M_PKTHDR;
  pMblk->mBlkHdr.mLen  = 0;
  pMblk->mBlkHdr.mData = pMblk->pClBlk->clNode.pClBuf;
  
  length = size;
  if(length > MAX_DATA_LEN) 
     length = MAX_DATA_LEN;
  pbuf.size = IPHL + UDPHL + length;
  if((pbuf.buf = (char *) memalign (sizeof(long),pbuf.size))==NULL)
  {
    perror("pbuf");
    muxUnbind(pSendCookie,MUX_PROTO_PROMISC,(FUNCPTR)stackRcvRtn);
    return(ERROR);
  }   
  bzero(pbuf.buf,pbuf.size);
  
  pData = pbuf.buf;
  myframe.iph  = (struct ip*)(pData);
  myframe.udph = (struct udphdr*)(pData+IPHL);
  myframe.pdata = (char *)(pData+UDPHL+IPHL);

/*************DATA FIELE*******************/
  bcopy(udp_data, myframe.pdata, length);

/************ ETH HEAD ***************/
  bcopy(dst_mac_addr, eth_head.ether_dhost,6);
  bcopy(src_mac_addr, eth_head.ether_shost,6);
  eth_head.ether_type = htons(ETHERTYPE_IP);

/********* UDP HEADER ***************/
  myframe.udph->uh_sport = htons(ARRAY_BROADCAST_PORT);
  myframe.udph->uh_dport = htons(ARRAY_ACQ_PORT);
  myframe.udph->uh_ulen  = htons (length + UDPHL);
  myframe.udph->uh_sum = 0;

  pudph.srcip.s_addr = INADDR_ANY;
  pudph.dstip.s_addr = INADDR_BROADCAST;
  pudph.zero = 0;
  pudph.prto = IPPROTO_UDP;
  pudph.ulen = myframe.udph->uh_ulen;
  myframe.udph->uh_sum = udp_cksum (&pudph, (char *) myframe.udph,
                                     ntohs (pudph.ulen));

/********** IP HEADER **********/
  myframe.iph->ip_v = IPVERSION;
  myframe.iph->ip_hl = IPHL >> 2;
  myframe.iph->ip_tos = 0;
  myframe.iph->ip_len = htons (UDPHL + IPHL + length);
  myframe.iph->ip_id  = myframe.udph->uh_sum;
  myframe.iph->ip_off = htons (IP_DF);    /* XXX */
  myframe.iph->ip_ttl = 0x20;            /* XXX */
  myframe.iph->ip_p = IPPROTO_UDP;

  myframe.iph->ip_src.s_addr = INADDR_ANY;
  myframe.iph->ip_dst.s_addr = INADDR_BROADCAST;
  myframe.iph->ip_sum = 0;
  myframe.iph->ip_sum = checksum ((u_short *)myframe.iph,
                                  (myframe.iph->ip_hl) << 2);

  pData = pMblk->mBlkHdr.mData;
  bcopy((char*)&eth_head,pData,EH_SIZE);
  bcopy(pbuf.buf,pData+EH_SIZE,pbuf.size);

  pMblk->mBlkHdr.mLen  = EH_SIZE + pbuf.size;
  pMblk->mBlkPktHdr.len  = pMblk->mBlkHdr.mLen;
    
  if(muxSend(pSendCookie,pMblk)!=OK)
  {
     perror("muxSend"); 
     netMblkClChainFree(pMblk);
     cfree(pbuf.buf);
     muxUnbind(pSendCookie,MUX_PROTO_PROMISC,(FUNCPTR)stackRcvRtn);
     return (ERROR);
  }
/********************************/
  cfree(pbuf.buf);
  muxUnbind(pSendCookie,MUX_PROTO_PROMISC,(FUNCPTR)stackRcvRtn);
  return(OK);
}
 

原创粉丝点击