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

13 篇文章 0 订阅
2 篇文章 0 订阅

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值