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

📄 tcp_ni.c

📁 用于嵌入式系统的TCP/IP协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
  if ((ni->poll == NULL) || (ni->transmit == NULL) ||      (!(ni->flags & NIF_P2P) && (ni->broadcast == NULL)))  {    NetError(NULL, EINVAL);    return -1;  }  /*-------------------------------------------------------------------*/  /* Ensure improper flags are not set.                                */  /*-------------------------------------------------------------------*/  if (ni->flags & (NIF_UP | NIF_BORROWED))  {    NetError(NULL, EINVAL);    return -1;  }  /*-------------------------------------------------------------------*/  /* Acquire access to protocol internals.                             */  /*-------------------------------------------------------------------*/  semPend(Net.IntSem, WAIT_FOREVER);  /*-------------------------------------------------------------------*/  /* Clear statistics fields in network interface structure.           */  /*-------------------------------------------------------------------*/  ni->ipkts = ni->ierrs = ni->opkts = ni->oerrs = 0;  /*-------------------------------------------------------------------*/  /* Ensure interface is not already installed.                        */  /*-------------------------------------------------------------------*/  for (np = Net.Local.next; np; np = np->next)    if (np == ni)    {      NetError(NULL, EINVAL);      rc = -1;      goto add_err;    }  /*-------------------------------------------------------------------*/  /* Determine address assignment method and call appropriate routine. */  /*-------------------------------------------------------------------*/  if (ni->flags & NIF_USE_DHCP)    rc = DhcpAdd(ni);  else if (ni->flags & NIF_USE_RARP)    rc = RarpAdd(ni);  else    rc = NiRtAdd(ni);  /*-------------------------------------------------------------------*/  /* If successful, add to linked list of network interfaces.          */  /*-------------------------------------------------------------------*/  if (rc == 0)  {    ni->next = Net.Local.next;    Net.Local.next = ni;  }  /*-------------------------------------------------------------------*/  /* Release access to protocol internals and return status.           */  /*-------------------------------------------------------------------*/add_err:  semPost(Net.IntSem);  return rc;}/***********************************************************************//*    tcpDelNi: Delete a network interface                             *//*                                                                     *//*       Input: ni = pointer to network interface structure            *//*                                                                     *//*     Returns: -1 if errors, else 0                                   *//*                                                                     *//***********************************************************************/int tcpDelNi(Ni *ni){  Ni *np, *prev;  /*-------------------------------------------------------------------*/  /* Verify protocol has been initialized.                             */  /*-------------------------------------------------------------------*/  if (!Net.Initialized)  {    NetError(NULL, ENETDOWN);    return -1;  }  /*-------------------------------------------------------------------*/  /* Acquire access to protocol internals.                             */  /*-------------------------------------------------------------------*/  semPend(Net.IntSem, WAIT_FOREVER);  /*-------------------------------------------------------------------*/  /* Search linked list, starting at local interface.                  */  /*-------------------------------------------------------------------*/  for (np = &Net.Local;;)  {    /*-----------------------------------------------------------------*/    /* Advance, remembering the previous interface.                    */    /*-----------------------------------------------------------------*/    prev = np;    np = np->next;    /*-----------------------------------------------------------------*/    /* If interface found, remove from linked list and break.          */    /*-----------------------------------------------------------------*/    if (np == ni)    {      prev->next = ni->next;      break;    }    /*-----------------------------------------------------------------*/    /* If end reached, interface was not installed.                    */    /*-----------------------------------------------------------------*/    if (np == NULL)    {      NetError(NULL, EINVAL);      semPost(Net.IntSem);      return -1;    }  }  /*-------------------------------------------------------------------*/  /* Stop DHCP state machine if using DHCP.                            */  /*-------------------------------------------------------------------*/  if (ni->flags & NIF_USE_DHCP)    DhcpDel(ni);  /*-------------------------------------------------------------------*/  /* Delete all routes associated with the interface.                  */  /*-------------------------------------------------------------------*/  NiRtDel(ni);  /*-------------------------------------------------------------------*/  /* Check if IP address has been assigned and is not been borrowed.   */  /*-------------------------------------------------------------------*/  if (ni->ip_addr && ((ni->flags & NIF_BORROWED) == FALSE))  {    /*-----------------------------------------------------------------*/    /* See if another interface "owns" this address also.              */    /*-----------------------------------------------------------------*/    for (np = Net.Local.next; np; np = np->next)    {      if ((np->ip_addr == ni->ip_addr) && !(np->flags & NIF_BORROWED))        break;    }    /*-----------------------------------------------------------------*/    /* If this was only owner, must bring borrowers down.              */    /*-----------------------------------------------------------------*/    if (np == NULL)    {      for (np = Net.Local.next; np; np = np->next)        if ((np->ip_addr == ni->ip_addr) && (np->flags & NIF_BORROWED))          NiRtDel(np);    }  }  /*-------------------------------------------------------------------*/  /* Delete any queued buffers associated with the interface.          */  /*-------------------------------------------------------------------*/  NiBufFree(ni);  /*-------------------------------------------------------------------*/  /* Release access to protocol internals and return success.          */  /*-------------------------------------------------------------------*/  semPost(Net.IntSem);  return 0;}/***********************************************************************//*   tcpStatNi: Read network interface status                          *//*                                                                     *//*       Input: ni = pointer to network interface structure            *//*                                                                     *//*     Returns: TRUE if interface is up, 0 if down, -1 if errors       *//*                                                                     *//***********************************************************************/int tcpStatNi(const char *name){  Ni *ni;  /*-------------------------------------------------------------------*/  /* Verify protocol has been initialized.                             */  /*-------------------------------------------------------------------*/  if (!Net.Initialized)  {    NetError(NULL, ENETDOWN);    return -1;  }  /*-------------------------------------------------------------------*/  /* Acquire access to protocol internals.                             */  /*-------------------------------------------------------------------*/  semPend(Net.IntSem, WAIT_FOREVER);  /*-------------------------------------------------------------------*/  /* Search through list of network interfaces.                        */  /*-------------------------------------------------------------------*/  for (ni = &Net.Local; ni; ni = ni->next)  {    /*-----------------------------------------------------------------*/    /* Read state and break if named interface is found.               */    /*-----------------------------------------------------------------*/    if (!strcmp(name, ni->name))      break;  }  /*-------------------------------------------------------------------*/  /* Release access to protocol internals and return status.           */  /*-------------------------------------------------------------------*/  semPost(Net.IntSem);  return (ni != NULL) && (ni->flags & NIF_UP);}

⌨️ 快捷键说明

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