test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright (c) 2001,2002 Florian Schulze.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the authors nor the names of the contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * test.c - This file is part of lwIP test
  31. *
  32. */
  33. /* C runtime includes */
  34. #include <stdio.h>
  35. #include <stdarg.h>
  36. #include <stdlib.h>
  37. #include <time.h>
  38. #include <string.h>
  39. /* lwIP core includes */
  40. #include "lwip/opt.h"
  41. #include "lwip/sys.h"
  42. #include "lwip/timeouts.h"
  43. #include "lwip/debug.h"
  44. #include "lwip/stats.h"
  45. #include "lwip/init.h"
  46. #include "lwip/tcpip.h"
  47. #include "lwip/netif.h"
  48. #include "lwip/api.h"
  49. #include "lwip/tcp.h"
  50. #include "lwip/udp.h"
  51. #include "lwip/dns.h"
  52. #include "lwip/dhcp.h"
  53. #include "lwip/autoip.h"
  54. /* lwIP netif includes */
  55. #include "lwip/etharp.h"
  56. #include "netif/ethernet.h"
  57. /* applications includes */
  58. #include "lwip/apps/netbiosns.h"
  59. #include "lwip/apps/httpd.h"
  60. #include "apps/httpserver/httpserver-netconn.h"
  61. #include "apps/netio/netio.h"
  62. #include "apps/ping/ping.h"
  63. #include "apps/rtp/rtp.h"
  64. #include "apps/chargen/chargen.h"
  65. #include "apps/shell/shell.h"
  66. #include "apps/tcpecho/tcpecho.h"
  67. #include "apps/udpecho/udpecho.h"
  68. #include "apps/tcpecho_raw/tcpecho_raw.h"
  69. #include "apps/socket_examples/socket_examples.h"
  70. #include "examples/lwiperf/lwiperf_example.h"
  71. #include "examples/mdns/mdns_example.h"
  72. #include "examples/snmp/snmp_example.h"
  73. #include "examples/tftp/tftp_example.h"
  74. #include "examples/sntp/sntp_example.h"
  75. #include "examples/mqtt/mqtt_example.h"
  76. #include "examples/httpd/cgi_example/cgi_example.h"
  77. #include "examples/httpd/fs_example/fs_example.h"
  78. #include "examples/httpd/https_example/https_example.h"
  79. #include "examples/httpd/ssi_example/ssi_example.h"
  80. #include "default_netif.h"
  81. #if NO_SYS
  82. /* ... then we need information about the timer intervals: */
  83. #include "lwip/ip4_frag.h"
  84. #include "lwip/igmp.h"
  85. #endif /* NO_SYS */
  86. #include "netif/ppp/ppp_opts.h"
  87. #if PPP_SUPPORT
  88. /* PPP includes */
  89. #include "lwip/sio.h"
  90. #include "netif/ppp/pppapi.h"
  91. #include "netif/ppp/pppos.h"
  92. #include "netif/ppp/pppoe.h"
  93. #if !NO_SYS && !LWIP_PPP_API
  94. #error With NO_SYS==0, LWIP_PPP_API==1 is required.
  95. #endif
  96. #endif /* PPP_SUPPORT */
  97. /* include the port-dependent configuration */
  98. #include "lwipcfg.h"
  99. #ifndef LWIP_EXAMPLE_APP_ABORT
  100. #define LWIP_EXAMPLE_APP_ABORT() 0
  101. #endif
  102. /** Define this to 1 to enable a port-specific ethernet interface as default interface. */
  103. #ifndef USE_DEFAULT_ETH_NETIF
  104. #define USE_DEFAULT_ETH_NETIF 1
  105. #endif
  106. /** Define this to 1 to enable a PPP interface. */
  107. #ifndef USE_PPP
  108. #define USE_PPP 0
  109. #endif
  110. /** Define this to 1 or 2 to support 1 or 2 SLIP interfaces. */
  111. #ifndef USE_SLIPIF
  112. #define USE_SLIPIF 0
  113. #endif
  114. /** Use an ethernet adapter? Default to enabled if port-specific ethernet netif or PPPoE are used. */
  115. #ifndef USE_ETHERNET
  116. #define USE_ETHERNET (USE_DEFAULT_ETH_NETIF || PPPOE_SUPPORT)
  117. #endif
  118. /** Use an ethernet adapter for TCP/IP? By default only if port-specific ethernet netif is used. */
  119. #ifndef USE_ETHERNET_TCPIP
  120. #define USE_ETHERNET_TCPIP (USE_DEFAULT_ETH_NETIF)
  121. #endif
  122. #if USE_SLIPIF
  123. #include <netif/slipif.h>
  124. #endif /* USE_SLIPIF */
  125. #ifndef USE_DHCP
  126. #define USE_DHCP LWIP_DHCP
  127. #endif
  128. #ifndef USE_AUTOIP
  129. #define USE_AUTOIP LWIP_AUTOIP
  130. #endif
  131. /* global variables for netifs */
  132. #if USE_ETHERNET
  133. #if LWIP_DHCP
  134. /* dhcp struct for the ethernet netif */
  135. static struct dhcp netif_dhcp;
  136. #endif /* LWIP_DHCP */
  137. #if LWIP_AUTOIP
  138. /* autoip struct for the ethernet netif */
  139. static struct autoip netif_autoip;
  140. #endif /* LWIP_AUTOIP */
  141. #endif /* USE_ETHERNET */
  142. #if USE_PPP
  143. /* THE PPP PCB */
  144. static ppp_pcb *ppp;
  145. /* THE PPP interface */
  146. static struct netif ppp_netif;
  147. /* THE PPP descriptor */
  148. static u8_t sio_idx = 0;
  149. static sio_fd_t ppp_sio;
  150. #endif /* USE_PPP */
  151. #if USE_SLIPIF
  152. static struct netif slipif1;
  153. #if USE_SLIPIF > 1
  154. static struct netif slipif2;
  155. #endif /* USE_SLIPIF > 1 */
  156. #endif /* USE_SLIPIF */
  157. #if USE_PPP
  158. static void
  159. pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx)
  160. {
  161. struct netif *pppif = ppp_netif(pcb);
  162. LWIP_UNUSED_ARG(ctx);
  163. switch(errCode) {
  164. case PPPERR_NONE: { /* No error. */
  165. printf("pppLinkStatusCallback: PPPERR_NONE\n");
  166. #if LWIP_IPV4
  167. printf(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif)));
  168. printf(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif)));
  169. printf(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif)));
  170. #endif /* LWIP_IPV4 */
  171. #if LWIP_DNS
  172. printf(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0)));
  173. printf(" dns2 = %s\n", ipaddr_ntoa(dns_getserver(1)));
  174. #endif /* LWIP_DNS */
  175. #if PPP_IPV6_SUPPORT
  176. printf(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
  177. #endif /* PPP_IPV6_SUPPORT */
  178. break;
  179. }
  180. case PPPERR_PARAM: { /* Invalid parameter. */
  181. printf("pppLinkStatusCallback: PPPERR_PARAM\n");
  182. break;
  183. }
  184. case PPPERR_OPEN: { /* Unable to open PPP session. */
  185. printf("pppLinkStatusCallback: PPPERR_OPEN\n");
  186. break;
  187. }
  188. case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */
  189. printf("pppLinkStatusCallback: PPPERR_DEVICE\n");
  190. break;
  191. }
  192. case PPPERR_ALLOC: { /* Unable to allocate resources. */
  193. printf("pppLinkStatusCallback: PPPERR_ALLOC\n");
  194. break;
  195. }
  196. case PPPERR_USER: { /* User interrupt. */
  197. printf("pppLinkStatusCallback: PPPERR_USER\n");
  198. break;
  199. }
  200. case PPPERR_CONNECT: { /* Connection lost. */
  201. printf("pppLinkStatusCallback: PPPERR_CONNECT\n");
  202. break;
  203. }
  204. case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */
  205. printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n");
  206. break;
  207. }
  208. case PPPERR_PROTOCOL: { /* Failed to meet protocol. */
  209. printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n");
  210. break;
  211. }
  212. case PPPERR_PEERDEAD: { /* Connection timeout */
  213. printf("pppLinkStatusCallback: PPPERR_PEERDEAD\n");
  214. break;
  215. }
  216. case PPPERR_IDLETIMEOUT: { /* Idle Timeout */
  217. printf("pppLinkStatusCallback: PPPERR_IDLETIMEOUT\n");
  218. break;
  219. }
  220. case PPPERR_CONNECTTIME: { /* Max connect time reached */
  221. printf("pppLinkStatusCallback: PPPERR_CONNECTTIME\n");
  222. break;
  223. }
  224. case PPPERR_LOOPBACK: { /* Loopback detected */
  225. printf("pppLinkStatusCallback: PPPERR_LOOPBACK\n");
  226. break;
  227. }
  228. default: {
  229. printf("pppLinkStatusCallback: unknown errCode %d\n", errCode);
  230. break;
  231. }
  232. }
  233. }
  234. #if PPPOS_SUPPORT
  235. static u32_t
  236. ppp_output_cb(ppp_pcb *pcb, const void *data, u32_t len, void *ctx)
  237. {
  238. LWIP_UNUSED_ARG(pcb);
  239. LWIP_UNUSED_ARG(ctx);
  240. return sio_write(ppp_sio, (const u8_t*)data, len);
  241. }
  242. #endif /* PPPOS_SUPPORT */
  243. #endif /* USE_PPP */
  244. #if LWIP_NETIF_STATUS_CALLBACK
  245. static void
  246. status_callback(struct netif *state_netif)
  247. {
  248. if (netif_is_up(state_netif)) {
  249. #if LWIP_IPV4
  250. printf("status_callback==UP, local interface IP is %s\n", ip4addr_ntoa(netif_ip4_addr(state_netif)));
  251. #else
  252. printf("status_callback==UP\n");
  253. #endif
  254. } else {
  255. printf("status_callback==DOWN\n");
  256. }
  257. }
  258. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  259. #if LWIP_NETIF_LINK_CALLBACK
  260. static void
  261. link_callback(struct netif *state_netif)
  262. {
  263. if (netif_is_link_up(state_netif)) {
  264. printf("link_callback==UP\n");
  265. } else {
  266. printf("link_callback==DOWN\n");
  267. }
  268. }
  269. #endif /* LWIP_NETIF_LINK_CALLBACK */
  270. /* This function initializes all network interfaces */
  271. static void
  272. test_netif_init(void)
  273. {
  274. #if LWIP_IPV4 && USE_ETHERNET
  275. ip4_addr_t ipaddr, netmask, gw;
  276. #endif /* LWIP_IPV4 && USE_ETHERNET */
  277. #if USE_SLIPIF
  278. u8_t num_slip1 = 0;
  279. #if LWIP_IPV4
  280. ip4_addr_t ipaddr_slip1, netmask_slip1, gw_slip1;
  281. #endif
  282. #if USE_SLIPIF > 1
  283. u8_t num_slip2 = 1;
  284. #if LWIP_IPV4
  285. ip4_addr_t ipaddr_slip2, netmask_slip2, gw_slip2;
  286. #endif
  287. #endif /* USE_SLIPIF > 1 */
  288. #endif /* USE_SLIPIF */
  289. #if USE_DHCP || USE_AUTOIP
  290. err_t err;
  291. #endif
  292. #if USE_PPP
  293. const char *username = NULL, *password = NULL;
  294. #ifdef PPP_USERNAME
  295. username = PPP_USERNAME;
  296. #endif
  297. #ifdef PPP_PASSWORD
  298. password = PPP_PASSWORD;
  299. #endif
  300. printf("ppp_connect: COM%d\n", (int)sio_idx);
  301. #if PPPOS_SUPPORT
  302. ppp_sio = sio_open(sio_idx);
  303. if (ppp_sio == NULL) {
  304. printf("sio_open error\n");
  305. } else {
  306. ppp = pppos_create(&ppp_netif, ppp_output_cb, pppLinkStatusCallback, NULL);
  307. if (ppp == NULL) {
  308. printf("pppos_create error\n");
  309. } else {
  310. ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password);
  311. ppp_connect(ppp, 0);
  312. }
  313. }
  314. #endif /* PPPOS_SUPPORT */
  315. #endif /* USE_PPP */
  316. #if USE_ETHERNET
  317. #if LWIP_IPV4
  318. ip4_addr_set_zero(&gw);
  319. ip4_addr_set_zero(&ipaddr);
  320. ip4_addr_set_zero(&netmask);
  321. #if USE_ETHERNET_TCPIP
  322. #if USE_DHCP
  323. printf("Starting lwIP, local interface IP is dhcp-enabled\n");
  324. #elif USE_AUTOIP
  325. printf("Starting lwIP, local interface IP is autoip-enabled\n");
  326. #else /* USE_DHCP */
  327. LWIP_PORT_INIT_GW(&gw);
  328. LWIP_PORT_INIT_IPADDR(&ipaddr);
  329. LWIP_PORT_INIT_NETMASK(&netmask);
  330. printf("Starting lwIP, local interface IP is %s\n", ip4addr_ntoa(&ipaddr));
  331. #endif /* USE_DHCP */
  332. #endif /* USE_ETHERNET_TCPIP */
  333. #else /* LWIP_IPV4 */
  334. printf("Starting lwIP, IPv4 disable\n");
  335. #endif /* LWIP_IPV4 */
  336. #if LWIP_IPV4
  337. init_default_netif(&ipaddr, &netmask, &gw);
  338. #else
  339. init_default_netif();
  340. #endif
  341. #if LWIP_IPV6
  342. netif_create_ip6_linklocal_address(netif_default, 1);
  343. printf("ip6 linklocal address: %s\n", ip6addr_ntoa(netif_ip6_addr(netif_default, 0)));
  344. #endif /* LWIP_IPV6 */
  345. #if LWIP_NETIF_STATUS_CALLBACK
  346. netif_set_status_callback(netif_default, status_callback);
  347. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  348. #if LWIP_NETIF_LINK_CALLBACK
  349. netif_set_link_callback(netif_default, link_callback);
  350. #endif /* LWIP_NETIF_LINK_CALLBACK */
  351. #if USE_ETHERNET_TCPIP
  352. #if LWIP_AUTOIP
  353. autoip_set_struct(netif_default, &netif_autoip);
  354. #endif /* LWIP_AUTOIP */
  355. #if LWIP_DHCP
  356. dhcp_set_struct(netif_default, &netif_dhcp);
  357. #endif /* LWIP_DHCP */
  358. netif_set_up(netif_default);
  359. #if USE_DHCP
  360. err = dhcp_start(netif_default);
  361. LWIP_ASSERT("dhcp_start failed", err == ERR_OK);
  362. #elif USE_AUTOIP
  363. err = autoip_start(netif_default);
  364. LWIP_ASSERT("autoip_start failed", err == ERR_OK);
  365. #endif /* USE_DHCP */
  366. #else /* USE_ETHERNET_TCPIP */
  367. /* Use ethernet for PPPoE only */
  368. netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */
  369. netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */
  370. #endif /* USE_ETHERNET_TCPIP */
  371. #if USE_PPP && PPPOE_SUPPORT
  372. /* start PPPoE after ethernet netif is added! */
  373. ppp = pppoe_create(&ppp_netif, netif_default, NULL, NULL, pppLinkStatusCallback, NULL);
  374. if (ppp == NULL) {
  375. printf("pppoe_create error\n");
  376. } else {
  377. ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password);
  378. ppp_connect(ppp, 0);
  379. }
  380. #endif /* USE_PPP && PPPOE_SUPPORT */
  381. #endif /* USE_ETHERNET */
  382. #if USE_SLIPIF
  383. #if LWIP_IPV4
  384. #define SLIP1_ADDRS &ipaddr_slip1, &netmask_slip1, &gw_slip1,
  385. LWIP_PORT_INIT_SLIP1_IPADDR(&ipaddr_slip1);
  386. LWIP_PORT_INIT_SLIP1_GW(&gw_slip1);
  387. LWIP_PORT_INIT_SLIP1_NETMASK(&netmask_slip1);
  388. printf("Starting lwIP slipif, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip1));
  389. #else
  390. #define SLIP1_ADDRS
  391. printf("Starting lwIP slipif\n");
  392. #endif
  393. #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT
  394. num_slip1++; /* COM ports cannot be 0-based */
  395. #endif
  396. netif_add(&slipif1, SLIP1_ADDRS &num_slip1, slipif_init, ip_input);
  397. #if !USE_ETHERNET
  398. netif_set_default(&slipif1);
  399. #endif /* !USE_ETHERNET */
  400. #if LWIP_IPV6
  401. netif_create_ip6_linklocal_address(&slipif1, 1);
  402. printf("SLIP ip6 linklocal address: %s\n", ip6addr_ntoa(netif_ip6_addr(&slipif1, 0)));
  403. #endif /* LWIP_IPV6 */
  404. #if LWIP_NETIF_STATUS_CALLBACK
  405. netif_set_status_callback(&slipif1, status_callback);
  406. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  407. #if LWIP_NETIF_LINK_CALLBACK
  408. netif_set_link_callback(&slipif1, link_callback);
  409. #endif /* LWIP_NETIF_LINK_CALLBACK */
  410. netif_set_up(&slipif1);
  411. #if USE_SLIPIF > 1
  412. #if LWIP_IPV4
  413. #define SLIP2_ADDRS &ipaddr_slip2, &netmask_slip2, &gw_slip2,
  414. LWIP_PORT_INIT_SLIP2_IPADDR(&ipaddr_slip2);
  415. LWIP_PORT_INIT_SLIP2_GW(&gw_slip2);
  416. LWIP_PORT_INIT_SLIP2_NETMASK(&netmask_slip2);
  417. printf("Starting lwIP SLIP if #2, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip2));
  418. #else
  419. #define SLIP2_ADDRS
  420. printf("Starting lwIP SLIP if #2\n");
  421. #endif
  422. #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT
  423. num_slip2++; /* COM ports cannot be 0-based */
  424. #endif
  425. netif_add(&slipif2, SLIP2_ADDRS &num_slip2, slipif_init, ip_input);
  426. #if LWIP_IPV6
  427. netif_create_ip6_linklocal_address(&slipif1, 1);
  428. printf("SLIP2 ip6 linklocal address: ");
  429. ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif2, 0));
  430. printf("\n");
  431. #endif /* LWIP_IPV6 */
  432. #if LWIP_NETIF_STATUS_CALLBACK
  433. netif_set_status_callback(&slipif2, status_callback);
  434. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  435. #if LWIP_NETIF_LINK_CALLBACK
  436. netif_set_link_callback(&slipif2, link_callback);
  437. #endif /* LWIP_NETIF_LINK_CALLBACK */
  438. netif_set_up(&slipif2);
  439. #endif /* USE_SLIPIF > 1*/
  440. #endif /* USE_SLIPIF */
  441. }
  442. #if LWIP_DNS_APP && LWIP_DNS
  443. static void
  444. dns_found(const char *name, const ip_addr_t *addr, void *arg)
  445. {
  446. LWIP_UNUSED_ARG(arg);
  447. printf("%s: %s\n", name, addr ? ipaddr_ntoa(addr) : "<not found>");
  448. }
  449. static void
  450. dns_dorequest(void *arg)
  451. {
  452. const char* dnsname = "3com.com";
  453. ip_addr_t dnsresp;
  454. LWIP_UNUSED_ARG(arg);
  455. if (dns_gethostbyname(dnsname, &dnsresp, dns_found, NULL) == ERR_OK) {
  456. dns_found(dnsname, &dnsresp, NULL);
  457. }
  458. }
  459. #endif /* LWIP_DNS_APP && LWIP_DNS */
  460. /* This function initializes applications */
  461. static void
  462. apps_init(void)
  463. {
  464. #if LWIP_DNS_APP && LWIP_DNS
  465. /* wait until the netif is up (for dhcp, autoip or ppp) */
  466. sys_timeout(5000, dns_dorequest, NULL);
  467. #endif /* LWIP_DNS_APP && LWIP_DNS */
  468. #if LWIP_CHARGEN_APP && LWIP_SOCKET
  469. chargen_init();
  470. #endif /* LWIP_CHARGEN_APP && LWIP_SOCKET */
  471. #if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP
  472. ping_init(&netif_default->gw);
  473. #endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */
  474. #if LWIP_NETBIOS_APP && LWIP_UDP
  475. netbiosns_init();
  476. #ifndef NETBIOS_LWIP_NAME
  477. #if LWIP_NETIF_HOSTNAME
  478. netbiosns_set_name(netif_default->hostname);
  479. #else
  480. netbiosns_set_name("NETBIOSLWIPDEV");
  481. #endif
  482. #endif
  483. #endif /* LWIP_NETBIOS_APP && LWIP_UDP */
  484. #if LWIP_HTTPD_APP && LWIP_TCP
  485. #ifdef LWIP_HTTPD_APP_NETCONN
  486. http_server_netconn_init();
  487. #else /* LWIP_HTTPD_APP_NETCONN */
  488. #if defined(LWIP_HTTPD_EXAMPLE_CUSTOMFILES) && LWIP_HTTPD_EXAMPLE_CUSTOMFILES && defined(LWIP_HTTPD_EXAMPLE_CUSTOMFILES_ROOTDIR)
  489. fs_ex_init(LWIP_HTTPD_EXAMPLE_CUSTOMFILES_ROOTDIR);
  490. #endif
  491. httpd_init();
  492. #if defined(LWIP_HTTPD_EXAMPLE_SSI_SIMPLE) && LWIP_HTTPD_EXAMPLE_SSI_SIMPLE
  493. ssi_ex_init();
  494. #endif
  495. #if defined(LWIP_HTTPD_EXAMPLE_CGI_SIMPLE) && LWIP_HTTPD_EXAMPLE_CGI_SIMPLE
  496. cgi_ex_init();
  497. #endif
  498. #if defined(LWIP_HTTPD_EXAMPLE_HTTPS) && LWIP_HTTPD_EXAMPLE_HTTPS
  499. https_ex_init();
  500. #endif
  501. #endif /* LWIP_HTTPD_APP_NETCONN */
  502. #endif /* LWIP_HTTPD_APP && LWIP_TCP */
  503. #if LWIP_NETIO_APP && LWIP_TCP
  504. netio_init();
  505. #endif /* LWIP_NETIO_APP && LWIP_TCP */
  506. #if LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP
  507. rtp_init();
  508. #endif /* LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP */
  509. #if LWIP_SHELL_APP && LWIP_NETCONN
  510. shell_init();
  511. #endif /* LWIP_SHELL_APP && LWIP_NETCONN */
  512. #if LWIP_TCPECHO_APP
  513. #if LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN)
  514. tcpecho_init();
  515. #else /* LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) */
  516. tcpecho_raw_init();
  517. #endif
  518. #endif /* LWIP_TCPECHO_APP && LWIP_NETCONN */
  519. #if LWIP_UDPECHO_APP && LWIP_NETCONN
  520. udpecho_init();
  521. #endif /* LWIP_UDPECHO_APP && LWIP_NETCONN */
  522. #if LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET
  523. socket_examples_init();
  524. #endif /* LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET */
  525. #if LWIP_MDNS_APP
  526. mdns_example_init();
  527. #endif
  528. #if LWIP_SNMP_APP
  529. snmp_example_init();
  530. #endif
  531. #if LWIP_SNTP_APP
  532. sntp_example_init();
  533. #endif
  534. #if LWIP_TFTP_APP
  535. tftp_example_init_server();
  536. #endif
  537. #if LWIP_TFTP_CLIENT_APP
  538. tftp_example_init_client();
  539. #endif
  540. #if LWIP_LWIPERF_APP
  541. lwiperf_example_init();
  542. #endif
  543. #if LWIP_MQTT_APP
  544. mqtt_example_init();
  545. #endif
  546. #ifdef LWIP_APP_INIT
  547. LWIP_APP_INIT();
  548. #endif
  549. }
  550. /* This function initializes this lwIP test. When NO_SYS=1, this is done in
  551. * the main_loop context (there is no other one), when NO_SYS=0, this is done
  552. * in the tcpip_thread context */
  553. static void
  554. test_init(void * arg)
  555. { /* remove compiler warning */
  556. #if NO_SYS
  557. LWIP_UNUSED_ARG(arg);
  558. #else /* NO_SYS */
  559. sys_sem_t *init_sem;
  560. LWIP_ASSERT("arg != NULL", arg != NULL);
  561. init_sem = (sys_sem_t*)arg;
  562. #endif /* NO_SYS */
  563. /* init randomizer again (seed per thread) */
  564. srand((unsigned int)time(NULL));
  565. /* init network interfaces */
  566. test_netif_init();
  567. /* init apps */
  568. apps_init();
  569. #if !NO_SYS
  570. sys_sem_signal(init_sem);
  571. #endif /* !NO_SYS */
  572. }
  573. /* This is somewhat different to other ports: we have a main loop here:
  574. * a dedicated task that waits for packets to arrive. This would normally be
  575. * done from interrupt context with embedded hardware, but we don't get an
  576. * interrupt in windows for that :-) */
  577. static void
  578. main_loop(void)
  579. {
  580. #if !NO_SYS
  581. err_t err;
  582. sys_sem_t init_sem;
  583. #endif /* NO_SYS */
  584. #if USE_PPP
  585. #if !USE_ETHERNET
  586. int count;
  587. u8_t rxbuf[1024];
  588. #endif
  589. volatile int callClosePpp = 0;
  590. #endif /* USE_PPP */
  591. /* initialize lwIP stack, network interfaces and applications */
  592. #if NO_SYS
  593. lwip_init();
  594. test_init(NULL);
  595. #else /* NO_SYS */
  596. err = sys_sem_new(&init_sem, 0);
  597. LWIP_ASSERT("failed to create init_sem", err == ERR_OK);
  598. LWIP_UNUSED_ARG(err);
  599. tcpip_init(test_init, &init_sem);
  600. /* we have to wait for initialization to finish before
  601. * calling update_adapter()! */
  602. sys_sem_wait(&init_sem);
  603. sys_sem_free(&init_sem);
  604. #endif /* NO_SYS */
  605. #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
  606. netconn_thread_init();
  607. #endif
  608. /* MAIN LOOP for driver update (and timers if NO_SYS) */
  609. while (!LWIP_EXAMPLE_APP_ABORT()) {
  610. #if NO_SYS
  611. /* handle timers (already done in tcpip.c when NO_SYS=0) */
  612. sys_check_timeouts();
  613. #endif /* NO_SYS */
  614. #if USE_ETHERNET
  615. default_netif_poll();
  616. #else /* USE_ETHERNET */
  617. /* try to read characters from serial line and pass them to PPPoS */
  618. count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024);
  619. if(count > 0) {
  620. pppos_input(ppp, rxbuf, count);
  621. } else {
  622. /* nothing received, give other tasks a chance to run */
  623. sys_msleep(1);
  624. }
  625. #endif /* USE_ETHERNET */
  626. #if USE_SLIPIF
  627. slipif_poll(&slipif1);
  628. #if USE_SLIPIF > 1
  629. slipif_poll(&slipif2);
  630. #endif /* USE_SLIPIF > 1 */
  631. #endif /* USE_SLIPIF */
  632. #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING
  633. /* check for loopback packets on all netifs */
  634. netif_poll_all();
  635. #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  636. #if USE_PPP
  637. {
  638. int do_hup = 0;
  639. if(do_hup) {
  640. ppp_close(ppp, 1);
  641. do_hup = 0;
  642. }
  643. }
  644. if(callClosePpp && ppp) {
  645. /* make sure to disconnect PPP before stopping the program... */
  646. callClosePpp = 0;
  647. #if NO_SYS
  648. ppp_close(ppp, 0);
  649. #else
  650. pppapi_close(ppp, 0);
  651. #endif
  652. ppp = NULL;
  653. }
  654. #endif /* USE_PPP */
  655. }
  656. #if USE_PPP
  657. if(ppp) {
  658. u32_t started;
  659. printf("Closing PPP connection...\n");
  660. /* make sure to disconnect PPP before stopping the program... */
  661. #if NO_SYS
  662. ppp_close(ppp, 0);
  663. #else
  664. pppapi_close(ppp, 0);
  665. #endif
  666. ppp = NULL;
  667. /* Wait for some time to let PPP finish... */
  668. started = sys_now();
  669. do
  670. {
  671. #if USE_ETHERNET
  672. default_netif_poll();
  673. #endif
  674. /* @todo: need a better check here: only wait until PPP is down */
  675. } while(sys_now() - started < 5000);
  676. }
  677. #endif /* USE_PPP */
  678. #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
  679. netconn_thread_cleanup();
  680. #endif
  681. #if USE_ETHERNET
  682. default_netif_shutdown();
  683. #endif /* USE_ETHERNET */
  684. }
  685. #if USE_PPP && PPPOS_SUPPORT
  686. int main(int argc, char **argv)
  687. #else /* USE_PPP && PPPOS_SUPPORT */
  688. int main(void)
  689. #endif /* USE_PPP && PPPOS_SUPPORT */
  690. {
  691. #if USE_PPP && PPPOS_SUPPORT
  692. if(argc > 1) {
  693. sio_idx = (u8_t)atoi(argv[1]);
  694. }
  695. printf("Using serial port %d for PPP\n", sio_idx);
  696. #endif /* USE_PPP && PPPOS_SUPPORT */
  697. /* no stdio-buffering, please! */
  698. setvbuf(stdout, NULL,_IONBF, 0);
  699. main_loop();
  700. return 0;
  701. }
  702. /* This function is only required to prevent arch.h including stdio.h
  703. * (which it does if LWIP_PLATFORM_ASSERT is undefined)
  704. */
  705. void lwip_example_app_platform_assert(const char *msg, int line, const char *file)
  706. {
  707. printf("Assertion \"%s\" failed at line %d in %s\n", msg, line, file);
  708. fflush(NULL);
  709. abort();
  710. }