LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
LinkBack Search this Thread
Old 10-07-2009, 04:56 PM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Rep: Reputation: 22
How to get the lowest/smallest local IP?


Hello,

I need to use the lowest/smallest IP address of the host in my module. Is there an easy way to get a list of the current IP addresses?

Thanks.
 
Old 10-08-2009, 12:37 PM   #2
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
Found some code that supposedly does does this, but have not been able to get it to return the correct address.

Code:
/* eth0 to __u32 IP address */
static inline __u32
getlocalIP(void){
	struct net_device *device;
	struct in_device *in_dev;
	struct in_ifaddr *if_addr;
	char dev_name[20];
	__u8 *addr;

	sprintf(dev_name,"eth%d",0);
	device = dev_get_by_name(dev_name);
	in_dev = (struct in_device *)device->ip_ptr;
	if_addr = in_dev->ifa_list;

	addr = (char *)&if_addr->ifa_local;
	
	printk(KERN_ALERT "Eth0 IP: %lu.",
	(unsigned int long)addr);
	
	return (__u32)addr;
}
 
Old 10-08-2009, 01:17 PM   #3
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
Got it working.

Code:
/* eth0 to __u32 IP address */
static inline __u32
getlocalIP(void){
	struct net_device *device;
	struct in_device *in_dev;
	struct in_ifaddr *if_addr;
	char dev_name[20];
	__u32 localIP;
	__u8 *addr;
	
	localIP = 0;

	sprintf(dev_name,"eth%d",0);
	device = dev_get_by_name(dev_name);
	in_dev = (struct in_device *)device->ip_ptr;
	if_addr = in_dev->ifa_list;
	
	addr = (char *)&if_addr->ifa_local;

	localIP += (addr[0] << 24);
	localIP += (addr[1] << 16);
	localIP += (addr[2] << 8);
	localIP += addr[3];

	return localIP;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lowest level of interrupt handling in kernel ashok449 Linux - Kernel 6 07-28-2008 09:44 AM
Lowest possible ram for Debian? jmite Debian 5 06-19-2008 10:40 AM
Computer with the lowest power consumption Matizek General 24 02-08-2008 12:10 PM
rtorrent 0.7.4 +/- 0-3 priority is 3 the lowest? almost000 Linux - Newbie 0 08-04-2007 04:49 PM
Q3 Extremely slow even on the lowest of settings w0lv3rin3 Linux - Software 0 03-31-2004 05:37 PM


All times are GMT -5. The time now is 09:10 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration