⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_ni.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************//*                                                                     *//*   Module:  tcp_ni.c                                                 *//*   Release: 2001.3                                                   *//*   Version: 2000.0                                                   *//*   Purpose: tcpAddNi()/tcpDelNi() implementations                    *//*                                                                     *//*---------------------------------------------------------------------*//*                                                                     *//*               Copyright 2000, Blunk Microsystems                    *//*                      ALL RIGHTS RESERVED                            *//*                                                                     *//*   Licensees have the non-exclusive right to use, modify, or extract *//*   this computer program for software development at a single site.  *//*   This program may be resold or disseminated in executable format   *//*   only. The source code may not be redistributed or resold.         *//*                                                                     *//*                                                                     *//***********************************************************************/#include "tcp_ipp.h"#include "ip/ip.h"#include <string.h>/***********************************************************************//* Local Function Definitions                                          *//***********************************************************************//***********************************************************************//* NiBorrowAddr: Searches for available address to borrow              *//*                                                                     *//*     Returns: Either 0 or a borrowed IP address                      *//*                                                                     *//***********************************************************************/ui32 NiBorrowAddr(void){  Ni *ni;  /*-------------------------------------------------------------------*/  /* Search network interface list, skipping local interface.          */  /*-------------------------------------------------------------------*/  for (ni = Net.Local.next; ni; ni = ni->next)  {    /*-----------------------------------------------------------------*/    /* If address assigned and not borrowed, return its value.         */    /*-----------------------------------------------------------------*/    if (ni->ip_addr && ((ni->flags & NIF_BORROWED) == FALSE))      return ni->ip_addr;  }  /*-------------------------------------------------------------------*/  /* Return 0 if no address available to borrow.                       */  /*-------------------------------------------------------------------*/  return 0;}/***********************************************************************//*     NiRtAdd: Add routes for a network interface                     *//*                                                                     *//*       Input: ni = pointer to network interface structure            *//*                                                                     *//*        Note: Must be called with internals semaphore held           *//*     Returns: -1 if errors, else 0                                   *//*                                                                     *//***********************************************************************/int NiRtAdd(Ni *ni){  ui32 subnet, subbrc;  int rc;  /*-------------------------------------------------------------------*/  /* Check if adding a point-to-point interface.                       */  /*-------------------------------------------------------------------*/  if (ni->flags & NIF_P2P)  {    /*-----------------------------------------------------------------*/    /* If local address is defined, add host route.                    */    /*-----------------------------------------------------------------*/    if (ni->ip_addr)    {      rc = RtAdd(htonl(INADDR_LOOPBACK), 0xFFFFFFFF, ni->ip_addr,                 &Net.Local, 0);      if (rc) return -1;    }    /*-----------------------------------------------------------------*/    /* Else, if available, borrow address from another NI.             */    /*-----------------------------------------------------------------*/    else    {      ui32 addr = NiBorrowAddr();      /*---------------------------------------------------------------*/      /* If address available, use it. Else return error.              */      /*---------------------------------------------------------------*/      if (addr)      {        ni->flags |= NIF_BORROWED;        ni->ip_addr = addr;        ni->ip_mask = 0xFFFFFFFF;      }      else      {        NetError(NULL, EINVAL);        return -1;      }    }    /*-----------------------------------------------------------------*/    /* If remote address supplied, add host route to it.               */    /*-----------------------------------------------------------------*/    if (ni->remote_addr)    {      rc = RtAdd(ni->ip_addr, 0xFFFFFFFF, ni->remote_addr, ni, 0);      if (rc) goto man_err;    }    /*-----------------------------------------------------------------*/    /* Else ensure default route option is requested.                  */    /*-----------------------------------------------------------------*/    else if ((ni->flags & NIF_DEF_GW) == FALSE)    {      NetError(NULL, EINVAL);      goto man_err;    }    /*-----------------------------------------------------------------*/    /* If requested, add default route to remote address.              */    /*-----------------------------------------------------------------*/    if (ni->flags & NIF_DEF_GW)    {      rc = RtAdd(ni->remote_addr, 0, htonl(INADDR_ANY), ni, RT_GATEWAY);      if (rc) goto man_err;    }  }  /*-------------------------------------------------------------------*/  /* Else adding a broadcast interface.                                */  /*-------------------------------------------------------------------*/  else  {    /*-----------------------------------------------------------------*/    /* Error if no address supplied.                                   */    /*-----------------------------------------------------------------*/    if (ni->ip_addr == 0)    {      NetError(NULL, EINVAL);      return -1;    }    /*-----------------------------------------------------------------*/    /* Add host route.                                                 */    /*-----------------------------------------------------------------*/    rc = RtAdd(htonl(INADDR_LOOPBACK), 0xFFFFFFFF, ni->ip_addr,               &Net.Local, 0);    if (rc) return -1;    /*-----------------------------------------------------------------*/    /* Add network route.                                              */    /*-----------------------------------------------------------------*/    subnet = ni->ip_addr & ni->ip_mask;    rc = RtAdd(ni->ip_addr, ni->ip_mask, subnet, ni, 0);    if (rc)      goto man_err;    /*-----------------------------------------------------------------*/    /* Add subnet broadcast address.                                   */    /*-----------------------------------------------------------------*/    subbrc = ni->ip_addr | ~ni->ip_mask;    rc = RtAdd(ni->ip_addr, 0xFFFFFFFF, subbrc, ni, RT_BRDCST);    if (rc)      goto man_err;  }  /*-------------------------------------------------------------------*/  /* Set "up" flag and, if installed, call event callback function.    */  /*-------------------------------------------------------------------*/  ni->flags |= NIF_UP;  if (ni->report)    ni->report(ni, NIE_UP);  return 0;man_err:  NiRtDel(ni);  return -1;}/***********************************************************************//* Global Function Definitions                                         *//***********************************************************************//***********************************************************************//*    tcpAddNi: Add a network interface                                *//*                                                                     *//*       Input: ni = pointer to network interface structure            *//*                                                                     *//*     Returns: -1 if errors, else 0                                   *//*                                                                     *//***********************************************************************/int tcpAddNi(Ni *ni){  int rc;  Ni *np;  /*-------------------------------------------------------------------*/  /* Verify protocol has been initialized.                             */  /*-------------------------------------------------------------------*/  if (!Net.Initialized)  {    NetError(NULL, ENETDOWN);    return -1;  }  /*-------------------------------------------------------------------*/  /* Ensure all manditory functions are provided.                      */  /*-------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -