User Tools

Site Tools


mywiki:linux:ip_kernel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
mywiki:linux:ip_kernel [2014/09/12 14:11] shaoguohmywiki:linux:ip_kernel [2022/04/02 17:30] (current) – external edit 127.0.0.1
Line 12: Line 12:
 </file> </file>
  
-====== Example ======+====== Example 1======
  
 <file c get_ip.c> <file c get_ip.c>
Line 61: Line 61:
 module_init(test_startup); module_init(test_startup);
 module_exit(test_exit);  module_exit(test_exit); 
 +</file>
 +
 +====== Example 2 ======
 +<file get_ip2.c>
 + int get_ifaddr_by_name(const char *ifname, __u32 * addr)
 +  {
 +         struct net_device *pnet_device;
 +         struct in_device *pin_device;
 +         struct in_ifaddr* inet_ifaddr;
 +
 +         read_lock_bh(&dev_base_lock);
 +  #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
 +         pnet_device = dev_base;
 +  #else
 +         pnet_device = first_net_device();
 +  #endif
 +         while (pnet_device != NULL)
 +         {
 +                 if ((netif_running(pnet_device))
 +                                 && (pnet_device-ip_ptr != NULL)
 +                                 && (strcmp(pnet_device-name, ifname) == 0))
 +                 {
 +                         pin_device =
 +                                 (struct in_device *) pnet_device-ip_ptr;
 +                         inet_ifaddr = pin_device-ifa_list;
 +                         if(inet_ifaddr == NULL)
 +                         {
 +                                 printk("ifa_list is null!\n");
 +                                 break;
 +                         }
 +                         /* ifa_local: ifa_address is the remote point in ppp */
 +                         *addr = (inet_ifaddr-ifa_local);
 +                           read_unlock_bh(&dev_base_lock);
 +                         return 1;
 +                 }
 +  #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
 +                 pnet_device = pnet_device-next;
 +  #else
 +                 pnet_device = next_net_device(pnet_device);
 +  #endif
 +
 +         }
 +
 +         read_unlock_bh(&dev_base_lock);
 +         return -1;              /* address not found! */
 +  }
 +
 </file> </file>
mywiki/linux/ip_kernel.1410502309.txt.gz · Last modified: (external edit)