linux核心中的hook函數詳解

來源:互聯網
上載者:User

標籤:鉤子函數

    在編寫linux核心中的網路模組時,用到了鉤子函數也就是hook函數。現在來看看linux是如何?hook函數的。


    先介紹一個結構體:

    struct nf_hook_ops,這個結構體是實現鉤子函數必須要用到的結構體,其實際的定義為:

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/54/27/wKiom1R5tnqSKXGCAAJM35eYXcA677.jpg" title="struct nf_hook_ops.png" alt="wKiom1R5tnqSKXGCAAJM35eYXcA677.jpg" />

  其中的成員資訊為:

      hook  :是一個函數指標,可以將自訂的函數賦值給它,來實現當有資料包到達是調用你自訂的函數。自訂函數的傳回值為:

     650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/54/27/wKiom1R5uMmzzFylAAEb6hbpt4Q018.jpg" title="hook函數的傳回值.png" alt="wKiom1R5uMmzzFylAAEb6hbpt4Q018.jpg" />


     owner:是模組的所有者,一般owner = THIS_MODULE ;

    pf   :是protocol flags,其取值範圍為:

     650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/27/wKiom1R5uGqAqwcwAADw8I1IpQc003.jpg" title="protocol_flags.png" alt="wKiom1R5uGqAqwcwAADw8I1IpQc003.jpg" />

     

      hooknum :中存放的是使用者自訂的鉤子函數的調用時機,其取值為:

      650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/54/26/wKioL1R5ufPDvJLrAADh0hya4Tc517.jpg" title="hooknum values.png" alt="wKioL1R5ufPDvJLrAADh0hya4Tc517.jpg" />    

      其中的每個值的含義為:

      650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/26/wKioL1R5veixMYuNAAHPQqUD_4A525.jpg" title="Screenshot.png" alt="wKioL1R5veixMYuNAAHPQqUD_4A525.jpg" />

     priority : 為所定義的鉤子函數的優先順序,其取值為份兩種:分別為IPV4 和 IPV6; 

    priority 的IPV4取值為:      

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/26/wKioL1R5wYjTgLozAAJJNM_RELA888.jpg" title="IPV4優先順序.png" alt="wKioL1R5wYjTgLozAAJJNM_RELA888.jpg" />

     priority 的IPV6取值為:

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/54/27/wKiom1R5wN_BUt3EAAHLCDG7A-c778.jpg" title="Screenshot-2.png" alt="wKiom1R5wN_BUt3EAAHLCDG7A-c778.jpg" />



    以上是對struct nf_hook_ops結構體中的每個欄位的詳解;

    現在舉例:

    struct nf_hook_ops  my_hook = {            .hook = myfunction,                        .owner = THIS_MODULE ,                        .pf = NFPROTO_IPV4,                        .hooknum = NET_INET_FORWARD ,                        .priority = NF_IP4_PRI_FIRST };                unsigned int myfunction( unsigend int hooknum,  struct sk_buff *skb,                             const struct net_device *in,                             const struct net_device *out,                             int (*okfn)(struct sk_buff *) )    {        }


    如上面的代碼一樣,當定義一個struct nf_hook_ops結構體,並且對其完成了初始化以後,需要將這個結構體進行註冊,之後這個結構體以及其中的自訂函數才會其作用。

   

    註冊一個struct nf_hook_ops需要用到的函數為:

    int  nf_register_hook( struct nf_hook_ops *reg )

   其實這個 int nf_register_hook()函數在核心中的實現也沒有多麼的複雜,

來看看它是如何?的:

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/54/26/wKioL1R5x4mCyyDqAAN56MgIAzU730.jpg" title="Screenshot-3.png" alt="wKioL1R5x4mCyyDqAAN56MgIAzU730.jpg" />


     當不再需要使用這個struct nf_hook_ops時,需要登出這個結構體,其可用的函數為:

     void nf_unregister_hook( struct nf_hook_ops *reg )

   650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/27/wKiom1R5x9-iJSlNAAFPhYJpukY147.jpg" title="Screenshot-4.png" alt="wKiom1R5x9-iJSlNAAFPhYJpukY147.jpg" />


    當一次需要註冊多個struct nf_hook_ops結構體,如:

     struct nf_hook_ops myhooks[n]時,使用:

    int nf_register_hooks( struct nf_hook_ops *regs, unsigned int n );

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/26/wKioL1R5yQDisY-SAAHWOnMosAY902.jpg" title="Screenshot-6.png" alt="wKioL1R5yQDisY-SAAHWOnMosAY902.jpg" />


     同樣,當一次需要登出多個struct nf_hook_ops結構體是,使用:

     void nf_unregister_hoos( struct nf_hook_ops *regs, unsigned int n );

     650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/26/wKioL1R5ybiBKmdyAAEjb5oY2m8888.jpg" title="Screenshot-7.png" alt="wKioL1R5ybiBKmdyAAEjb5oY2m8888.jpg" />



     總結:

           struct nf_hook_ops

           int nf_register_hook( struct nf_hook_ops *reg );

           void nf_unregister_hook( struct nf_hook_ops *reg );

           int nf_register_hooks( struct nf_hook_ops *regs, unsigend int n );

           void nf_unregister_hooks( struct nf_hook_ops *regs, unsigned int n );

本文出自 “阿輝仔” 部落格,請務必保留此出處http://weiguozhihui.blog.51cto.com/3060615/1584577

linux核心中的hook函數詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.