linux 内核配置ip地址,linux内核IP地址转换函数

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

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

#include

iph->saddr = in_aton("1.1.1.1");

打印 IP 地址

#include

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值