Skip to contentSkip to conversation

Function to read IP address in dot format

DISCUSSION linux lover 3 participants 3 messages Apr 11, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,
what is the function to read IP address in
kernel code as human readable dot quad format? IP address are unsigned int's in ip.h file as

        __u32   saddr;
        __u32   daddr;
         In ip_build_xmit ip header gets IP addresses

from rtable struct as
iph->saddr=rt->rt_src;
iph->daddr=rt->rt_dst;
Now if i want to know to which ip current
packet is being sent i am using printks but it prints iph->saddr in int format not in user readable format. how to do that?
regards,
linux_lover

__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail

#1 lore raw

On Sun, 10 Apr 2005 23:05:10 -0700 (PDT) linux lover wrote:

| 
| Hello,
|           what is the function to read IP address in
| kernel code as human readable dot quad format? IP
| address are unsigned int's in ip.h file as
|         __u32   saddr;
|         __u32   daddr;
|          In ip_build_xmit ip header gets IP addresses
| from rtable struct as
| iph->saddr=rt->rt_src;
| iph->daddr=rt->rt_dst;
|            Now if i want to know to which ip current
| packet is being sent i am using printks but it prints
| iph->saddr in int format not in user readable format.
| how to do that?

Look at the NIPQUAD() macro in include/linux/kernel.h and at some example uses of it.

---
~Randy

#2 lore raw
In reply to linux lover
Quoted 8 lines · linux lover

Hello,
what is the function to read IP address in
kernel code as human readable dot quad format? IP address are unsigned int's in ip.h file as

        __u32   saddr;
        __u32   daddr;
         In ip_build_xmit ip header gets IP addresses
Quoted 8 lines

from rtable struct as
iph->saddr=rt->rt_src;
iph->daddr=rt->rt_dst;
Now if i want to know to which ip current
packet is being sent i am using printks but it prints iph->saddr in int format not in user readable format. how to do that?
regards,

Use NIPQUAD, as in the example below:

        printk(KERN_DEBUG "dccp rx: packet=%s, "
                          "source=%u.%u.%u.%u@%d, "
                          "dest=%u.%u.%u.%u@%d\n",
               dccp_packet_name(dh->dccph_type),
               NIPQUAD(skb->nh.iph->saddr),
               ntohs(dh->dccph_sport),
               NIPQUAD(skb->nh.iph->daddr),
               ntohs(dh->dccph_dport));

- Arnaldo