Linux内核printk打印ip地址

Linux内核printk打印ip地址

https://www.kernel.org/doc/Documentation/printk-formats.txt http://elixir.free-electrons.com/linux/v4.14.1/source/Documentation/printk-formats.txt


打印时,还可以选择字节大小端顺序的比较方便

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 , , , and  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  specifier can be used with the  specifier to
print a compressed IPv6 address as described by
http://tools.ietf.org/html/rfc5952

Passed by reference.

IPv4/IPv6 addresses (generic, with port, flowinfo, scope)
=========================================================

::

    %pIS    1.2.3.4     or 0001:0002:0003:0004:0005:0006:0007:0008
    %piS    001.002.003.004 or 00010002000300040005000600070008
    %pISc   1.2.3.4     or 1:2:3:4:5:6:7:8
    %pISpc  1.2.3.4:12345   or [1:2:3:4:5:6:7:8]:12345
    %p[Ii]S[pfschnbl]

For printing an IP address without the need to distinguish whether it
of type AF_INET or AF_INET6, a pointer to a valid  sockaddr,
specified through  or , can be passed to this format specifier.

The additional , , and  specifiers are used to specify port
(IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a : prefix,
flowinfo a / and scope a %, each followed by the actual value.

In case of an IPv6 address the compressed IPv6 address as described by
http://tools.ietf.org/html/rfc5952 is being used if the additional
specifier  is given. The IPv6 address is surrounded by [, ] in
case of additional specifiers ,  or  as suggested by
https://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07

In case of IPv4 addresses, the additional , , , and 
specifiers can be used as well and are ignored in case of an IPv6
address.

Passed by reference.

Further examples::

    %pISfc      1.2.3.4     or [1:2:3:4:5:6:7:8]/123456789
    %pISsc      1.2.3.4     or [1:2:3:4:5:6:7:8]%1234567890
    %pISpfc     1.2.3.4:12345   or [1:2:3:4:5:6:7:8]:12345/123456789

这个打印格式pI4的处理在 source/lib/vsprintf.c 文件里面

但旧版的内核不支持这个参数,需要使用这几个宏, 使用 NIPQUAD_FMT 来格式化

<span class="cm">/*
 *      Display an IP address in readable format.

 */

<span class="cp">#define NIPQUAD(addr) \
    ((unsigned char *)&addr)[0], \
    ((unsigned char *)&addr)[1], \
    ((unsigned char *)&addr)[2], \
    ((unsigned char *)&addr)[3]
#define NIPQUAD_FMT "%u.%u.%u.%u"

<span class="cp">#define NIP6(addr) \
    ntohs((addr).s6_addr16[0]), \
    ntohs((addr).s6_addr16[1]), \
    ntohs((addr).s6_addr16[2]), \
    ntohs((addr).s6_addr16[3]), \
    ntohs((addr).s6_addr16[4]), \
    ntohs((addr).s6_addr16[5]), \
    ntohs((addr).s6_addr16[6]), \
    ntohs((addr).s6_addr16[7])
#define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
#define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"

<span class="cp">#if defined(__LITTLE_ENDIAN)
#define HIPQUAD(addr) \
    ((unsigned char *)&addr)[3], \
    ((unsigned char *)&addr)[2], \
    ((unsigned char *)&addr)[1], \
    ((unsigned char *)&addr)[0]
#elif defined(__BIG_ENDIAN)
#define HIPQUAD NIPQUAD
#else
#error "Please fix asm/byteorder.h"
#endif <span class="cm">/* __LITTLE_ENDIAN */</span></span></span></span></span>

Original: https://www.cnblogs.com/fire909090/p/16280066.html
Author: fire909090
Title: Linux内核printk打印ip地址

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/10353/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总