📄 test.c
字号:
/*
* Copyright (c) 2001,2002 Florian Schulze.
* 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. Neither the name of the authors nor the names of the contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``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 AUTHORS OR CONTRIBUTORS 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.
*
* test.c - This file is part of lwIP test
*
*/
/* C runtime includes */
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <conio.h>
/* lwIP core includes */
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/timers.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "lwip/netif.h"
#include "lwip/tcp.h"
#include "lwip/udp.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
/* lwIP netif includes */
#include "netif/etharp.h"
/* applications includes */
#include "apps/httpserver_raw/httpd.h"
#include "apps/httpserver/httpserver-netconn.h"
#include "apps/netio/netio.h"
#include "apps/netbios/netbios.h"
#include "apps/ping/ping.h"
#include "apps/rtp/rtp.h"
#include "apps/sntp/sntp.h"
#include "apps/chargen/chargen.h"
#include "apps/shell/shell.h"
#include "apps/tcpecho/tcpecho.h"
#include "apps/udpecho/udpecho.h"
#include "apps/tcpecho_raw/echo.h"
#include "apps/socket_examples/socket_examples.h"
#if NO_SYS
/* ... then we need information about the timer intervals: */
#include "lwip/ip_frag.h"
#include "lwip/igmp.h"
#endif /* NO_SYS */
#if PPP_SUPPORT
/* PPP includes */
#include "../netif/ppp/ppp.h"
#include "../netif/ppp/lcp.h"
#include "lwip/sio.h"
#include "netif/ppp_oe.h"
#endif /* PPP_SUPPORT */
#include "pcapif.h"
/* include the port-dependent configuration */
#include "lwipcfg_msvc.h"
/** Use an ethernet adapter? By default only if PPP is not used. */
#ifndef USE_ETHERNET
#define USE_ETHERNET (!PPP_SUPPORT || PPPOE_SUPPORT)
#endif
/** Use an ethernet adapter for TCP/IP? By default only if PPP is not used. */
#ifndef USE_ETHERNET_TCPIP
#define USE_ETHERNET_TCPIP !PPP_SUPPORT
#endif
/* globales variables for netifs */
#if USE_ETHERNET
/* THE ethernet interface */
struct netif netif;
#if LWIP_DHCP
/* dhcp struct for the ethernet netif */
struct dhcp netif_dhcp;
#endif /* LWIP_DHCP */
#if LWIP_AUTOIP
/* autoip struct for the ethernet netif */
struct autoip netif_autoip;
#endif /* LWIP_AUTOIP */
#endif /* USE_ETHERNET */
#if PPP_SUPPORT
/* THE PPP descriptor */
int ppp_desc = -1;
u8_t sio_idx = 0;
sio_fd_t ppp_sio;
#endif /* PPP_SUPPORT */
#if PPP_SUPPORT
void
pppLinkStatusCallback(void *ctx, int errCode, void *arg)
{
LWIP_UNUSED_ARG(ctx);
switch(errCode) {
case PPPERR_NONE: { /* No error. */
struct ppp_addrs *ppp_addrs = arg;
printf("pppLinkStatusCallback: PPPERR_NONE\n");
printf(" our_ipaddr=%s\n", ip_ntoa(&ppp_addrs->our_ipaddr));
printf(" his_ipaddr=%s\n", ip_ntoa(&ppp_addrs->his_ipaddr));
printf(" netmask =%s\n", ip_ntoa(&ppp_addrs->netmask));
printf(" dns1 =%s\n", ip_ntoa(&ppp_addrs->dns1));
printf(" dns2 =%s\n", ip_ntoa(&ppp_addrs->dns2));
break;
}
case PPPERR_PARAM: { /* Invalid parameter. */
printf("pppLinkStatusCallback: PPPERR_PARAM\n");
break;
}
case PPPERR_OPEN: { /* Unable to open PPP session. */
printf("pppLinkStatusCallback: PPPERR_OPEN\n");
break;
}
case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */
printf("pppLinkStatusCallback: PPPERR_DEVICE\n");
break;
}
case PPPERR_ALLOC: { /* Unable to allocate resources. */
printf("pppLinkStatusCallback: PPPERR_ALLOC\n");
break;
}
case PPPERR_USER: { /* User interrupt. */
printf("pppLinkStatusCallback: PPPERR_USER\n");
break;
}
case PPPERR_CONNECT: { /* Connection lost. */
printf("pppLinkStatusCallback: PPPERR_CONNECT\n");
break;
}
case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */
printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n");
break;
}
case PPPERR_PROTOCOL: { /* Failed to meet protocol. */
printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n");
break;
}
default: {
printf("pppLinkStatusCallback: unknown errCode %d\n", errCode);
break;
}
}
}
#endif /* PPP_SUPPORT */
#if LWIP_NETIF_STATUS_CALLBACK
void status_callback(struct netif *netif)
{
if (netif_is_up(netif)) {
printf("status_callback==UP, local interface IP is %s\n", ip_ntoa(&netif->ip_addr));
} else {
printf("status_callback==DOWN\n");
}
}
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
void link_callback(struct netif *netif)
{
if (netif_is_link_up(netif)) {
printf("link_callback==UP\n");
#if LWIP_DHCP
if (netif->dhcp != NULL) {
dhcp_renew(netif);
}
#endif /* LWIP_DHCP */
} else {
printf("link_callback==DOWN\n");
}
}
#endif /* LWIP_NETIF_LINK_CALLBACK */
/* This function initializes all network interfaces */
static void
msvc_netif_init()
{
#if USE_ETHERNET
ip_addr_t ipaddr, netmask, gw;
#endif /* USE_ETHERNET */
#if PPP_SUPPORT
const char *username = NULL, *password = NULL;
#ifdef PPP_USERNAME
username = PPP_USERNAME;
#endif
#ifdef PPP_PASSWORD
password = PPP_PASSWORD;
#endif
printf("pppInit\n");
pppInit();
pppSetAuth(PPPAUTHTYPE_ANY, username, password);
printf("pppOpen: COM%d\n", (int)sio_idx);
#if PPPOS_SUPPORT
ppp_sio = sio_open(sio_idx);
if (ppp_sio == NULL) {
printf("sio_open error\n");
} else {
ppp_desc = pppOpen(ppp_sio, pppLinkStatusCallback, NULL);
}
#endif /* PPPOS_SUPPORT */
#endif /* PPP_SUPPORT */
#if USE_ETHERNET
ip_addr_set_zero(&gw);
ip_addr_set_zero(&ipaddr);
ip_addr_set_zero(&netmask);
#if USE_ETHERNET_TCPIP
#if LWIP_DHCP
printf("Starting lwIP, local interface IP is dhcp-enabled\n");
#elif LWIP_AUTOIP
printf("Starting lwIP, local interface IP is autoip-enabled\n");
#else /* LWIP_AUTOIP */
LWIP_PORT_INIT_GW(&gw);
LWIP_PORT_INIT_IPADDR(&ipaddr);
LWIP_PORT_INIT_NETMASK(&netmask);
printf("Starting lwIP, local interface IP is %s\n", ip_ntoa(&ipaddr));
#endif /* LWIP_DHCP */
#endif /* USE_ETHERNET_TCPIP */
#if NO_SYS
#if LWIP_ARP
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ethernet_input));
#else /* LWIP_ARP */
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, ip_input));
#endif /* LWIP_ARP */
#else /* NO_SYS */
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input));
#endif /* NO_SYS */
#if LWIP_NETIF_STATUS_CALLBACK
netif_set_status_callback(&netif, status_callback);
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
netif_set_link_callback(&netif, link_callback);
#endif /* LWIP_NETIF_LINK_CALLBACK */
#if USE_ETHERNET_TCPIP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -