netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Function to determine if IP exists on a net-device?
@ 2003-11-19  9:25 Ben Greear
       [not found] ` <20031119173103.1938bc51.davem@redhat.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Ben Greear @ 2003-11-19  9:25 UTC (permalink / raw)
  To: 'netdev@oss.sgi.com'

Is there a method already written that will tell me if an IP exists on
a netdevice?

If not, I am quite sure the answer lies in the in_ifaddr
list, but which field holds the IP address for the device?

Thanks,
Ben

-- 
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc  http://www.candelatech.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Function to determine if IP exists on a net-device?
       [not found] ` <20031119173103.1938bc51.davem@redhat.com>
@ 2003-11-20  2:05   ` Ben Greear
       [not found]     ` <20031119181529.4c2b861a.davem@redhat.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Ben Greear @ 2003-11-20  2:05 UTC (permalink / raw)
  To: David S. Miller; +Cc: netdev

David S. Miller wrote:
> On Wed, 19 Nov 2003 01:25:06 -0800
> Ben Greear <greearb@candelatech.com> wrote:
> 
> 
>>Is there a method already written that will tell me if an IP exists on
>>a netdevice?
>>
>>If not, I am quite sure the answer lies in the in_ifaddr
>>list, but which field holds the IP address for the device?
> 
> 
> Call getifaddrs(), walk the list finding the each and every list entry
> with the 'ifa_name' name matching the device you want.  If there are
> multiple addresses attached to an interface there will be multiple
> in_ifaddr list entries with the 'ifa_name' of your interface.
> 
> In each of those entry, check the sockaddr pointed to by the 'ifa_addr'
> field.


Is there not a more direct access if I already have the netdevice in question?
ie, can I get at the list by looking at dev->ip_ptr struct?


Thanks,
Ben


-- 
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc  http://www.candelatech.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Function to determine if IP exists on a net-device?
       [not found]     ` <20031119181529.4c2b861a.davem@redhat.com>
@ 2003-11-20  8:30       ` Ben Greear
  0 siblings, 0 replies; 3+ messages in thread
From: Ben Greear @ 2003-11-20  8:30 UTC (permalink / raw)
  To: netdev

David S. Miller wrote:
> On Wed, 19 Nov 2003 18:05:15 -0800
> Ben Greear <greearb@candelatech.com> wrote:
> 
> 
>>Is there not a more direct access if I already have the netdevice in question?
>>ie, can I get at the list by looking at dev->ip_ptr struct?
> 
> 
> Yes, using dev->ip_ptr as a "struct in_device *in_dev"
> do something like this:
> 
> 	struct in_ifaddr *ifa;
> 
> 	read_lock(&in_dev->lock);
> 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
> 		if (inet_ifa_match(my_addr, ifa)) {
> 			/* match */
> 		}
> 	}
> 	read_unlock(&in_dev->lock);
> 
> should work...
> 


The inet_ifa_match thing uses a mask, and was matching everything on the subnet,
or something...

This seems to work though:

static int is_ip_on_dev(struct net_device* dev, __u32 ip) {
	int rv = 0;
	struct in_device* in_dev = in_dev_get(dev);
	if (in_dev) {
		struct in_ifaddr *ifa;

		read_lock(&in_dev->lock);
		for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
			if (ifa->ifa_address == ip) {
				/* match */
				rv = 1;
				break;
			}
		}
		read_unlock(&in_dev->lock);
		in_dev_put(in_dev);
	}
	return rv;
}


-- 
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc  http://www.candelatech.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-11-20  8:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-19  9:25 Function to determine if IP exists on a net-device? Ben Greear
     [not found] ` <20031119173103.1938bc51.davem@redhat.com>
2003-11-20  2:05   ` Ben Greear
     [not found]     ` <20031119181529.4c2b861a.davem@redhat.com>
2003-11-20  8:30       ` Ben Greear

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).