IP-address from sk_buff

前端 未结 6 1507
说谎
说谎 2021-02-02 17:43

I am writing a kernel module which registers a netfilter hook. I am trying to get the ip address of the caller by using the sk_buff->saddr member. Is there a way

6条回答
  •  -上瘾入骨i
    2021-02-02 18:16

    printk can handle this directly:

    IPv4 addresses:

    %pI4    1.2.3.4
    %pi4    001.002.003.004
    %p[Ii]4[hnbl]
    
    For printing IPv4 dot-separated decimal addresses. The 'I4' and 'i4'
    specifiers result in a printed address with ('i4') or without ('I4')
    leading zeros.
    
    The additional 'h', 'n', 'b', and 'l' specifiers are used to specify
    host, network, big or little endian order addresses respectively. Where
    no specifier is provided the default network/big endian order is used.
    
    Passed by reference.
    

    IPv6 addresses:

    %pI6    0001:0002:0003:0004:0005:0006:0007:0008
    %pi6    00010002000300040005000600070008
    %pI6c   1:2:3:4:5:6:7:8
    
    For printing IPv6 network-order 16-bit hex addresses. The 'I6' and 'i6'
    specifiers result in a printed address with ('I6') or without ('i6')
    colon-separators. Leading zeros are always used.
    
    The additional 'c' specifier can be used with the 'I' specifier to
    print a compressed IPv6 address as described by
    http://tools.ietf.org/html/rfc5952
    
    Passed by reference.
    

    Reference: https://www.kernel.org/doc/Documentation/printk-formats.txt

提交回复
热议问题