www.pudn.com > TCPmodbushy.rar > netif.c


/** 
 * @file 
 * 
 * lwIP network interface abstraction 
 */ 
 
/* 
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 * 3. The name of the author may not be used to endorse or promote products 
 *    derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE. 
 * 
 * This file is part of the lwIP TCP/IP stack. 
 * 
 * Author: Adam Dunkels  
 * 
 */ 
 
#include "o:/lwip/opt.h" 
 
#include "o:/lwip/def.h" 
#include "o:/IPV4/lwip/ip_addr.h" 
#include "o:/lwip/netif.h" 
#include "o:/lwip/tcp.h" 
 
//struct netif *netif_list = NULL; 
//struct netif *netif_default = NULL; 
 
/** 
 * Add a network interface to the list of lwIP netifs. 
 * 
 * @param netif a pre-allocated netif structure 
 * @param ipaddr IP address for the new netif 
 * @param netmask network mask for the new netif 
 * @param gw default gateway IP address for the new netif 
 * @param state opaque data passed to the new netif 
 * @param init callback function that initializes the interface 
 * @param input callback function that is called to pass 
 * ingress packets up in the protocol layer stack. 
 * 
 * @return netif, or NULL if failed. 
 */ 
 
struct netif xdata *netif; 
 
static struct netif xdata NetifBody; 
 
void netif_Init( void ) 
{ 
	netif = &NetifBody; 
#if LWIP_DHCP 
  /* netif not under DHCP control by default */ 
  netif->dhcp = NULL; 
#endif 
  /* remember netif specific state information data */ 
  netif->state = NULL; 
  netif_set_ipaddr(IP_ADDR_ANY); 
  netif_set_gw(IP_ADDR_ANY); 
  netif_set_netmask(IP_ADDR_ANY); 
  return; 
} 
 
void netif_set_ipaddr(struct ip_addr xdata *ipaddr) 
{ 
  /* TODO: Handling of obsolete pcbs */ 
  /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */ 
#if LWIP_TCP 
  struct tcp_pcb xdata *pcb; 
 
  /* address is actually being changed? */ 
  if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) 
  { 
    /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */ 
    LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n")); 
    pcb = tcp_active_pcbs; 
    while (pcb != NULL) { 
        /* this connection must be aborted */ 
        struct tcp_pcb xdata *next = pcb->next; 
        LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb)); 
        tcp_abort(pcb); 
        pcb = next; 
    } 
  } 
#endif 
  ip_addr_set(&(netif->ip_addr), ipaddr); 
#if 0 /* only allowed for Ethernet interfaces TODO: how can we check? */ 
  /** For Ethernet network interfaces, we would like to send a 
   *  "gratuitous ARP"; this is an ARP packet sent by a node in order 
   *  to spontaneously cause other nodes to update an entry in their 
   *  ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6. 
   */  
  etharp_query(netif, ipaddr, NULL); 
#endif 
} 
 
void netif_set_gw(struct ip_addr xdata *gw) 
{ 
  ip_addr_set(&(netif->gw), gw); 
} 
 
void netif_set_netmask(struct ip_addr xdata *netmask) 
{ 
  ip_addr_set(&(netif->netmask), netmask); 
} 
 
/** 
 * Bring an interface up, available for processing 
 * traffic. 
 *  
 * @note: Enabling DHCP on a down interface will make it come 
 * up once configured. 
 *  
 * @see dhcp_start() 
 */  
void netif_set_up(void) 
{ 
  netif->flags |= NETIF_FLAG_UP; 
} 
 
/** 
 * Ask if an interface is up 
 */  
u8_t netif_is_up(void) 
{ 
  return (netif->flags & NETIF_FLAG_UP)?1:0; 
} 
 
/** 
 * Bring an interface down, disabling any traffic processing. 
 * 
 * @note: Enabling DHCP on a down interface will make it come 
 * up once configured. 
 *  
 * @see dhcp_start() 
 */  
void netif_set_down(void) 
{ 
  netif->flags &= ~NETIF_FLAG_UP; 
}