linux内核IP地址转换函数

来源:互联网 发布:收银软件免费版 编辑:程序博客网 时间:2024/05/17 00:15

http://www.cloudbbs.org/forum.php?mod=viewthread&tid=17856

IP 地址转换(字符串 -> 数值)


#include<linux/inet.h>
iph->saddr = in_aton("1.1.1.1");
打印 IP 地址
#include<linux/kernel.h>
printk("%d.%d.%d.%d\n", NIPQUAD(iph->saddr));

#define NIPQUAD(addr) \
        ((unsignedchar*)&addr)[0],
        ((unsignedchar*)&addr)[1],
        ((unsignedchar*)&addr)[2],
        ((unsignedchar*)&addr)[3]

用 sprintf 可将上面的 IP 数值转换为字符串


======================================================================================================


http://stackoverflow.com/questions/584713/ip-address-from-sk-buff

#define NIPQUAD(addr) \

    ((unsigned char *)&addr)[0], \

    ((unsigned char *)&addr)[1], \

    ((unsigned char *)&addr)[2], \

    ((unsigned char *)&addr)[3]

#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])

    printk(KERN_DEBUG "Received packet from source address: %d.%d.%d.%d!\n",NIPQUAD(iph->saddr));


http://svn.netfilter.org/netfilter/branches/patch-o-matic-ng/linux-2.6.11/ROUTE/linux-2.6/net/ipv6/netfilter/ip6t_ROUTE.c

struct rt6_info *rt = NULL;

struct ipv6hdr *ipv6h = skb->nh.ipv6h;

struct in6_addr *gw = (struct in6_addr*)&route_info->gw;

DEBUGP("ip6t_ROUTE: called with: ");

DEBUGP("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->daddr));

DEBUGP("GATEWAY=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(*gw));

DEBUGP("OUT=%s\n", route_info->oif);


printk实用功能
%pI4 打印IPV4地址
%pI6 打印IPV6地址
%pM  打印MAC地址
   

再较新的内核版本中NIPQUAD和NIP6两个宏被移除了, 可以直接使用printk打印IP地址, 相关信息如下:

http://www.cppblog.com/momoxiao/archive/2011/01/27/139393.html

Using NIPQUAD() with NIPQUAD_FMT, %d.%d.%d.%d or %u.%u.%u.%u

can be replaced with %pI4


-  dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",

-   NIPQUAD(src_ipaddr),

-   NIPQUAD(arpinfo->smsk.s_addr),

-   NIPQUAD(arpinfo->src.s_addr),

+  dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",

+   &src_ipaddr,

+   &arpinfo->smsk.s_addr,

+   &arpinfo->src.s_addr,

    arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");


http://www.oschina.net/code/explore/pf-kernel/fs/cifs/cifs_spnego.c

/** add the server address */

if (server->addr.sockAddr.sin_family == AF_INET)

     sprintf(dp, "ip4=%pI4", &server->addr.sockAddr.sin_addr);

else if (server->addr.sockAddr.sin_family == AF_INET6)

     sprintf(dp, "ip6=%pI6", &server->addr.sockAddr6.sin6_addr);

else



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

IPv4 addresses:

%pI41.2.3.4

%pi4001.002.003.004

%p[Ii][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.

IPv6 addresses:

%pI60001:0002:0003:0004:0005:0006:0007:0008

%pi600010002000300040005000600070008

%pI6c1: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


0 0