如何用C语言函数指针实现C语言弹性编码,TCP/IP协议中的经典例子

来源:互联网 发布:h5页面制作工具 知乎 编辑:程序博客网 时间:2024/06/04 21:09

The first define a protocolstructure and it can cover TCP, UDP and RAWIP protocol

struct inet_protosw {
    struct list_head        list;

    unsigned short          type;
   int                     protocol;
    struct proto            *prot;
    const struct 
proto_ops --*ops;    
   int                     capability; 

   char                    no_check; 
    unsignedchar            flags; 
};

 

The second, define a protocolarray to store different protocol

 

/usr/src/linux/net/ipv4/af_inet.c
static struct inet_protosw 
inetsw_array[] =
{
    {
       .type       = SOCK_STREAM,
       .protocol   = IPPROTO_TCP,
       .prot       =&tcp_prot,            //与协议类型相关的操作集
       .ops        =&inet_stream_ops,      //
与服务类型相关的操作集
       .capability = -1,

       .no_check   = 0,
       .flags      = INET_PROTOSW_PERMANENT |INET_PROTOSW_ICSK,
    },

    {
       .type       = SOCK_DGRAM,
       .protocol   = IPPROTO_UDP,
       .prot       = &udp_prot,
       .ops        = &inet_dgram_ops,
       .capability = -1,
       .no_check   = UDP_CSUM_DEFAULT,
       .flags      = INET_PROTOSW_PERMANENT,
    },


    {
       .type       = SOCK_RAW,
        .protocol  = IPPROTO_IP,   /* wild card */
       .prot       = &raw_prot,
       .ops        = &inet_sockraw_ops,
       .capability = CAP_NET_RAW,
       .no_check   = UDP_CSUM_DEFAULT,
       .flags      = INET_PROTOSW_REUSE,
    }
};

 

The third, when we specify a protocol tocommunicate, we can chose right protocol in this array, and use this interfacedin socket layer.

 

 

In testmac system, we also do it same, wedefine a structure to store different function between 9131 and 8157, before weinit system, we run command

Cat /proc/cpuinfo, get platform information

 

root@bsc913x:~# cat /proc/cpuinfo

processor       :0

cpu            : e500v2

clock          : 1000.000000MHz

revision       : 5.1 (pvr 8021 2251)

bogomips       : 125.00

timebase       : 62500000

platform       : BSC9131 RDB

model          : fsl,bsc9131rdb

Memory         : 880 MB

 

And init those function in structure, use thisstructure in below code.

原创粉丝点击