network_utilities.c

来自「91c111芯片的网络模块的原理图以及和ep1c6fpga连线相关的例子程序」· C语言 代码 · 共 482 行 · 第 1/2 页

C
482
字号
  }#else  ret_code = ERR_IF;  perror( "Not an Altera board.\n");  perror( "You need to modify the function get_mac_addr() \n");  perror( "to set a MAC address for your board");#endif /* (Altera Nios Dev board definition) */  return ret_code;}/* * get_ip_addr() *  * This routine is called by LWIP to obtain an IP address for the specified  * network adapter. Like the MAC address, obtaining an IP address is very * system-dependant and therefore this function is exported for the developer * to control. *  * In our system, we are either attempting DHCP auto-negotiation of IP address, * or we are setting our own static IP, Gateway, and Subnet Mask addresses our * self. This routine is where that happens. */int get_ip_addr(alt_lwip_dev* lwip_dev, struct ip_addr* ipaddr,                 struct ip_addr* netmask, struct ip_addr* gw,                int* use_dhcp){  int ret_code = 0;  if (!strcmp(lwip_dev->name, "/dev/" LWIP_DEFAULT_IF))  {#if LWIP_DHCP == 1    *use_dhcp = 1;    /*     * If we are telling LWIP to attempt DHCP, all that is needed is to save      * the the LWIP device in question to a the global netif "adapter"; the      * DHCP timeout task will then querry this device to see whether an IP     * address has been set.     *      * If we are not attempting DHCP, static network settings are assigned here     * and we go on our merry way.     */    adapter = lwip_dev->netif;#ifdef LCD_DISPLAY_NAME      /* Clear the LCD screen */    fprintf(lcdDevice, "\x1b");    fprintf(lcdDevice, "[2J");    fprintf(lcdDevice, "Using DHCP to\nfind IP Addr");#endif /* LCD_DISPLAY_NAME */    printf("Using DHCP to find an IP Address\n");#else    IP4_ADDR(ipaddr, IPADDR0,IPADDR1,IPADDR2,IPADDR3);    IP4_ADDR(gw, GWADDR0,GWADDR1,GWADDR2,GWADDR3);    IP4_ADDR(netmask, MSKADDR0,MSKADDR1,MSKADDR2,MSKADDR3);    *use_dhcp = 0;    printf("Static IP Address is %d.%d.%d.%d\n",         ip4_addr1(ipaddr),        ip4_addr2(ipaddr),        ip4_addr3(ipaddr),        ip4_addr4(ipaddr));#ifdef LCD_DISPLAY_NAME        /* Clear the LCD screen */      fprintf(lcdDevice, "\x1b");      fprintf(lcdDevice, "[2J");      fprintf(lcdDevice, "Static IP Addr\n %d.%d.%d.%d",        ip4_addr1(ipaddr),        ip4_addr2(ipaddr),        ip4_addr3(ipaddr),        ip4_addr4(ipaddr));#endif /* LCD_DISPLAY_NAME */#endif /* LWIP_DHCP */    ret_code = 1;  }  return ret_code;}/* * dhcp_timeout_task() *  * This LWIP-called routine is responsible for checking to see whether DHCP  * was sucessful. Here we check to see whether a timeout (120 seconds in this * case) has elapsed. If so, display an error, assign a static address, and  * bail out. If not, continue checking for DHCP success and bail out once * a DHCP address assignment is complete. *  * This routine also displays the assigned IP address to the user (via STDOUT  * and, if equipped, LCD screen. *  * Note: LCD_DISPLAY_NAME is defined in user.h to be the LCD interface for *       the "standard" example design. */#if LWIP_DHCP == 1void dhcp_timeout_task(){  int have_address = 0, timeout_dhcp = 0;  struct ip_addr ipaddr, netmask, gw;  INT8U return_code = OS_NO_ERR; #ifdef LCD_DISPLAY_NAME  lcd_sem = OSSemCreate(1);#endif /* LCD_DISPLAY_NAME */  OSSemPend(attained_ip_address_sem, 0, &return_code);  #ifdef LCD_DISPLAY_NAME  OSSemPend(lcd_sem, 0, &return_code);#endif /* LCD_DISPLAY_NAME */  #ifdef LCD_DISPLAY_NAME   lcdDevice = fopen(LCD_DISPLAY_NAME, "w");#endif /* LCD_DISPLAY_NAME */  while(1)  {    OSTimeDlyHMSM(0,0,0,100);    /*      * For demo purposes we need to use DHCP to allocate an IP Address     * and LWIP had no mechanism to tell us one is allocated. Wait two      * minutes if we don't have one we use a static IP address     */    if((timeout_dhcp < DHCP_TIMEOUT) && (have_address == 0))    {      timeout_dhcp++;      if (!ip_addr_isany(&adapter->ip_addr))      {        have_address = 1;        printf("Assigned IP Address is %d.%d.%d.%d\n\n",                ip4_addr1(&adapter->ip_addr),                ip4_addr2(&adapter->ip_addr),                ip4_addr3(&adapter->ip_addr),                ip4_addr4(&adapter->ip_addr));#ifdef LCD_DISPLAY_NAME                /* Clear screen */        fprintf(lcdDevice, "\x1b");        fprintf(lcdDevice, "[2J");        fprintf(lcdDevice, "DHCP IP Addr is \n %d.%d.%d.%d",                ip4_addr1(&adapter->ip_addr),                ip4_addr2(&adapter->ip_addr),                ip4_addr3(&adapter->ip_addr),                ip4_addr4(&adapter->ip_addr));#endif /* LCD_DISPLAY_NAME */      }    }    /*     * You're in a bit of trouble if this happens, you could assign a static     * IP address but how do we choose it?     */    if (timeout_dhcp ==  DHCP_TIMEOUT)    {      dhcp_stop(adapter);      IP4_ADDR(&ipaddr, IPADDR0,IPADDR1,IPADDR2,IPADDR3);      IP4_ADDR(&gw, GWADDR0,GWADDR1,GWADDR2,GWADDR3);      IP4_ADDR(&netmask, MSKADDR0,MSKADDR1,MSKADDR2,MSKADDR3);            netif_set_addr( adapter,&ipaddr, &netmask, &gw);      netif_set_up(adapter);            printf("DHCP Failed to assign an IP Address\n\n");       printf("\nUsing static address: %d.%d.%d.%d\n\n\n",        ip4_addr1(&adapter->ip_addr),        ip4_addr2(&adapter->ip_addr),        ip4_addr3(&adapter->ip_addr),        ip4_addr4(&adapter->ip_addr));#ifdef LCD_DISPLAY_NAME        /* Clear the LCD screen */      fprintf(lcdDevice, "\x1b");      fprintf(lcdDevice, "[2J");      fprintf(lcdDevice, "DHCP Failed using\n %d.%d.%d.%d",        ip4_addr1(&adapter->ip_addr),        ip4_addr2(&adapter->ip_addr),        ip4_addr3(&adapter->ip_addr),        ip4_addr4(&adapter->ip_addr));#endif /* LCD_DISPLAY_NAME */      have_address = 1;    }        if (have_address == 1)    {      /*        * We have an IP address this task has served it's purpose - kill it.       */#ifdef LCD_DISPLAY_NAME               fclose(lcdDevice);#endif /* LCD_DISPLAY_NAME */       OSSemPost(attained_ip_address_sem);#ifdef LCD_DISPLAY_NAME               OSSemPost(lcd_sem);#endif /* LCD_DISPLAY_NAME */       OSTaskDel(OS_PRIO_SELF);    }  }}#endif /* LWIP_DHCP *//*******************************************************************************                                                                             ** License Agreement                                                           **                                                                             ** Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           ** All rights reserved.                                                        **                                                                             ** Permission is hereby granted, free of charge, to any person obtaining a     ** copy of this software and associated documentation files (the "Software"),  ** to deal in the Software without restriction, including without limitation   ** the rights to use, copy, modify, merge, publish, distribute, sublicense,    ** and/or sell copies of the Software, and to permit persons to whom the       ** Software is furnished to do so, subject to the following conditions:        **                                                                             ** The above copyright notice and this permission notice shall be included in  ** all copies or substantial portions of the Software.                         **                                                                             ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     ** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         ** DEALINGS IN THE SOFTWARE.                                                   **                                                                             ** This agreement shall be governed in all respects by the laws of the State   ** of California and by the laws of the United States of America.              ** Altera does not recommend, suggest or require that this reference design    ** file be used in conjunction or combination with any other product.          *******************************************************************************/

⌨️ 快捷键说明

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