/* 
   Unix SMB/CIFS implementation.
   multiple interface handling
   Copyright (C) Andrew Tridgell 1992-2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see .
*/
#include "includes.h"
#include "system/network.h"
#include "lib/socket/netif.h"
#include "../lib/util/dlinklist.h"
/** used for network interfaces */
struct interface {
	struct interface *next, *prev;
	struct in_addr ip;
	struct in_addr nmask;
	const char *ip_s;
	const char *bcast_s;
	const char *nmask_s;
};
#define ALLONES  ((uint32_t)0xFFFFFFFF)
/*
  address construction based on a patch from fred@datalync.com
*/
#define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
#define MKNETADDR(_IP, _NM) (_IP & _NM)
/****************************************************************************
Try and find an interface that matches an ip. If we cannot, return NULL
  **************************************************************************/
static struct interface *iface_find(struct interface *interfaces, 
				    struct in_addr ip, bool CheckMask)
{
	struct interface *i;
	if (is_zero_ip_v4(ip)) return interfaces;
	for (i=interfaces;i;i=i->next)
		if (CheckMask) {
			if (same_net_v4(i->ip,ip,i->nmask)) return i;
		} else if (i->ip.s_addr == ip.s_addr) return i;
	return NULL;
}
/****************************************************************************
add an interface to the linked list of interfaces
****************************************************************************/
static void add_interface(TALLOC_CTX *mem_ctx, struct in_addr ip, struct in_addr nmask, struct interface **interfaces)
{
	struct interface *iface;
	struct in_addr bcast;
	if (iface_find(*interfaces, ip, false)) {
		DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip)));
		return;
	}
	iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
	if (iface == NULL) 
		return;
	
	ZERO_STRUCTPN(iface);
	iface->ip = ip;
	iface->nmask = nmask;
	bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr);
	/* keep string versions too, to avoid people tripping over the implied
	   static in inet_ntoa() */
	iface->ip_s = talloc_strdup(iface, inet_ntoa(iface->ip));
	iface->nmask_s = talloc_strdup(iface, inet_ntoa(iface->nmask));
	
	if (nmask.s_addr != ~0) {
		iface->bcast_s = talloc_strdup(iface, inet_ntoa(bcast));
	}
	DLIST_ADD_END(*interfaces, iface, struct interface *);
	DEBUG(2,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s));
}
/**
interpret a single element from a interfaces= config line 
This handles the following different forms:
1) wildcard interface name
2) DNS name
3) IP/masklen
4) ip/mask
5) bcast/mask
**/
static void interpret_interface(TALLOC_CTX *mem_ctx, 
				const char *token, 
				struct iface_struct *probed_ifaces, 
				int total_probed,
				struct interface **local_interfaces)
{
	struct in_addr ip, nmask;
	char *p;
	char *address;
	int i, added=0;
	ip.s_addr = 0;
	nmask.s_addr = 0;
	
	/* first check if it is an interface name */
	for (i=0;i 2) {
		nmask.s_addr = interpret_addr2(p).s_addr;
	} else {
		nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
	}
	/* maybe the first component was a broadcast address */
	if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) ||
	    ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) {
		for (i=0;inext)
		ret++;
	return ret;
}
/**
  return IP of the Nth interface
  **/
const char *iface_n_ip(struct interface *ifaces, int n)
{
	struct interface *i;
  
	for (i=ifaces;i && n;i=i->next)
		n--;
	if (i) {
		return i->ip_s;
	}
	return NULL;
}
/**
  return bcast of the Nth interface
  **/
const char *iface_n_bcast(struct interface *ifaces, int n)
{
	struct interface *i;
  
	for (i=ifaces;i && n;i=i->next)
		n--;
	if (i) {
		return i->bcast_s;
	}
	return NULL;
}
/**
  return netmask of the Nth interface
  **/
const char *iface_n_netmask(struct interface *ifaces, int n)
{
	struct interface *i;
  
	for (i=ifaces;i && n;i=i->next)
		n--;
	if (i) {
		return i->nmask_s;
	}
	return NULL;
}
/**
  return the local IP address that best matches a destination IP, or
  our first interface if none match
*/
const char *iface_best_ip(struct interface *ifaces, const char *dest)
{
	struct interface *iface;
	struct in_addr ip;
	ip.s_addr = interpret_addr(dest);
	iface = iface_find(ifaces, ip, true);
	if (iface) {
		return iface->ip_s;
	}
	return iface_n_ip(ifaces, 0);
}
/**
  return true if an IP is one one of our local networks
*/
bool iface_is_local(struct interface *ifaces, const char *dest)
{
	struct in_addr ip;
	ip.s_addr = interpret_addr(dest);
	if (iface_find(ifaces, ip, true)) {
		return true;
	}
	return false;
}
/**
  return true if a IP matches a IP/netmask pair
*/
bool iface_same_net(const char *ip1, const char *ip2, const char *netmask)
{
	return same_net_v4(interpret_addr2(ip1),
			interpret_addr2(ip2),
			interpret_addr2(netmask));
}