| 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 |
|
| 34 |
/* C runtime includes */ |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <time.h> |
| 38 |
#include <string.h> |
| 39 |
#include <conio.h> |
| 40 |
|
| 41 |
/* lwIP core includes */ |
| 42 |
#include "lwip/opt.h" |
| 43 |
|
| 44 |
#include "lwip/debug.h" |
| 45 |
#include "lwip/stats.h" |
| 46 |
#include "lwip/init.h" |
| 47 |
#include "lwip/tcpip.h" |
| 48 |
|
| 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 |
|
| 55 |
/* lwIP netif includes */ |
| 56 |
#include "netif/loopif.h" |
| 57 |
#include "netif/etharp.h" |
| 58 |
|
| 59 |
/* applications includes */ |
| 60 |
#include "apps/httpserver_raw/httpd.h" |
| 61 |
#include "apps/netio/netio.h" |
| 62 |
#include "apps/netbios/netbios.h" |
| 63 |
#include "apps/ping/ping.h" |
| 64 |
#include "apps/rtp/rtp.h" |
| 65 |
#include "apps/sntp/sntp.h" |
| 66 |
|
| 67 |
#if NO_SYS |
| 68 |
/* ... then we need information about the timer intervals: */ |
| 69 |
#include "lwip/ip_frag.h" |
| 70 |
#include "lwip/igmp.h" |
| 71 |
#endif /* NO_SYS */ |
| 72 |
|
| 73 |
#if PPP_SUPPORT |
| 74 |
/* PPP includes */ |
| 75 |
#include "../netif/ppp/ppp.h" |
| 76 |
#include "lwip/sio.h" |
| 77 |
#endif /* PPP_SUPPORT */ |
| 78 |
|
| 79 |
#include "pktif.h" |
| 80 |
|
| 81 |
/* include the port-dependent configuration */ |
| 82 |
#include "lwipcfg_msvc.h" |
| 83 |
|
| 84 |
|
| 85 |
#if NO_SYS |
| 86 |
/* port-defined functions used for timer execution */ |
| 87 |
void sys_init_timing(); |
| 88 |
u32_t sys_now(); |
| 89 |
#endif /* NO_SYS */ |
| 90 |
|
| 91 |
/* globales variables for netifs */ |
| 92 |
/* THE ethernet interface */ |
| 93 |
struct netif netif; |
| 94 |
#if LWIP_HAVE_LOOPIF |
| 95 |
/* THE loopback interface */ |
| 96 |
struct netif loop_netif; |
| 97 |
#endif /* LWIP_HAVE_LOOPIF */ |
| 98 |
|
| 99 |
|
| 100 |
#if NO_SYS |
| 101 |
/* special functions used for NO_SYS=1 only */ |
| 102 |
typedef struct _timers_infos { |
| 103 |
int timer; |
| 104 |
int timer_interval; |
| 105 |
void (*timer_func)(void); |
| 106 |
}timers_infos; |
| 107 |
|
| 108 |
static timers_infos timers_table[] = { |
| 109 |
#if LWIP_TCP |
| 110 |
{ 0, TCP_FAST_INTERVAL, tcp_fasttmr}, |
| 111 |
{ 0, TCP_SLOW_INTERVAL, tcp_slowtmr}, |
| 112 |
#endif /* LWIP_TCP */ |
| 113 |
#if LWIP_ARP |
| 114 |
{ 0, ARP_TMR_INTERVAL, etharp_tmr}, |
| 115 |
#endif /* LWIP_ARP */ |
| 116 |
#if LWIP_DHCP |
| 117 |
{ 0, DHCP_FINE_TIMER_MSECS, dhcp_fine_tmr}, |
| 118 |
{ 0, DHCP_COARSE_TIMER_MSECS, dhcp_coarse_tmr}, |
| 119 |
#endif /* LWIP_DHCP */ |
| 120 |
#if IP_REASSEMBLY |
| 121 |
{ 0, IP_TMR_INTERVAL, ip_reass_tmr}, |
| 122 |
#endif /* IP_REASSEMBLY */ |
| 123 |
#if LWIP_AUTOIP |
| 124 |
{ 0, AUTOIP_TMR_INTERVAL, autoip_tmr}, |
| 125 |
#endif /* LWIP_AUTOIP */ |
| 126 |
#if LWIP_IGMP |
| 127 |
{ 0, IGMP_TMR_INTERVAL, igmp_tmr}, |
| 128 |
#endif /* LWIP_IGMP */ |
| 129 |
#if LWIP_DNS |
| 130 |
{ 0, DNS_TMR_INTERVAL, dns_tmr}, |
| 131 |
#endif /* LWIP_DNS */ |
| 132 |
}; |
| 133 |
|
| 134 |
/* initialize stack when NO_SYS=1 */ |
| 135 |
static void |
| 136 |
nosys_init() |
| 137 |
{ |
| 138 |
sys_init_timing(); |
| 139 |
lwip_init(); |
| 140 |
} |
| 141 |
|
| 142 |
/* get the current time and see if any timer has expired */ |
| 143 |
static void |
| 144 |
timers_update() |
| 145 |
{ |
| 146 |
/* static variables for timer execution, initialized to zero! */ |
| 147 |
static int last_time; |
| 148 |
|
| 149 |
int cur_time, time_diff, idxtimer; |
| 150 |
|
| 151 |
cur_time = sys_now(); |
| 152 |
time_diff = cur_time - last_time; |
| 153 |
|
| 154 |
/* the '> 0' is an easy wrap-around check: the big gap at |
| 155 |
* the wraparound step is simply ignored... */ |
| 156 |
if (time_diff > 0) { |
| 157 |
last_time = cur_time; |
| 158 |
for( idxtimer=0; idxtimer<(sizeof(timers_table)/sizeof(timers_infos)); idxtimer++) { |
| 159 |
timers_table[idxtimer].timer += time_diff; |
| 160 |
|
| 161 |
if (timers_table[idxtimer].timer > timers_table[idxtimer].timer_interval) { |
| 162 |
timers_table[idxtimer].timer_func(); |
| 163 |
timers_table[idxtimer].timer -= timers_table[idxtimer].timer_interval; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
#endif /* NO_SYS */ |
| 169 |
|
| 170 |
#if PPP_SUPPORT |
| 171 |
void |
| 172 |
pppLinkStatusCallback(void *ctx, int errCode, void *arg) |
| 173 |
{ |
| 174 |
LWIP_UNUSED_ARG(ctx); |
| 175 |
|
| 176 |
switch(errCode) { |
| 177 |
case PPPERR_NONE: { /* No error. */ |
| 178 |
struct ppp_addrs *ppp_addrs = arg; |
| 179 |
|
| 180 |
printf("pppLinkStatusCallback: PPPERR_NONE\n"); |
| 181 |
printf(" our_ipaddr=%s\n", inet_ntoa(*(struct in_addr*)&(ppp_addrs->our_ipaddr.addr))); |
| 182 |
printf(" his_ipaddr=%s\n", inet_ntoa(*(struct in_addr*)&(ppp_addrs->his_ipaddr.addr))); |
| 183 |
printf(" netmask =%s\n", inet_ntoa(*(struct in_addr*)&(ppp_addrs->netmask.addr))); |
| 184 |
printf(" dns1 =%s\n", inet_ntoa(*(struct in_addr*)&(ppp_addrs->dns1.addr))); |
| 185 |
printf(" dns2 =%s\n", inet_ntoa(*(struct in_addr*)&(ppp_addrs->dns2.addr))); |
| 186 |
break; |
| 187 |
} |
| 188 |
case PPPERR_PARAM: { /* Invalid parameter. */ |
| 189 |
printf("pppLinkStatusCallback: PPPERR_PARAM\n"); |
| 190 |
break; |
| 191 |
} |
| 192 |
case PPPERR_OPEN: { /* Unable to open PPP session. */ |
| 193 |
printf("pppLinkStatusCallback: PPPERR_OPEN\n"); |
| 194 |
break; |
| 195 |
} |
| 196 |
case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */ |
| 197 |
printf("pppLinkStatusCallback: PPPERR_DEVICE\n"); |
| 198 |
break; |
| 199 |
} |
| 200 |
case PPPERR_ALLOC: { /* Unable to allocate resources. */ |
| 201 |
printf("pppLinkStatusCallback: PPPERR_ALLOC\n"); |
| 202 |
break; |
| 203 |
} |
| 204 |
case PPPERR_USER: { /* User interrupt. */ |
| 205 |
printf("pppLinkStatusCallback: PPPERR_USER\n"); |
| 206 |
break; |
| 207 |
} |
| 208 |
case PPPERR_CONNECT: { /* Connection lost. */ |
| 209 |
printf("pppLinkStatusCallback: PPPERR_CONNECT\n"); |
| 210 |
break; |
| 211 |
} |
| 212 |
case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */ |
| 213 |
printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n"); |
| 214 |
break; |
| 215 |
} |
| 216 |
case PPPERR_PROTOCOL: { /* Failed to meet protocol. */ |
| 217 |
printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n"); |
| 218 |
break; |
| 219 |
} |
| 220 |
default: { |
| 221 |
printf("pppLinkStatusCallback: unknown errCode %d\n", errCode); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
#endif /* PPP_SUPPORT */ |
| 227 |
|
| 228 |
#if LWIP_NETIF_STATUS_CALLBACK |
| 229 |
void status_callback(struct netif *netif) |
| 230 |
{ if (netif_is_up(netif)) { |
| 231 |
printf("status_callback==UP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&(netif->ip_addr))); |
| 232 |
} else { |
| 233 |
printf("status_callback==DOWN\n"); |
| 234 |
} |
| 235 |
} |
| 236 |
#endif /* LWIP_NETIF_STATUS_CALLBACK */ |
| 237 |
|
| 238 |
#if LWIP_NETIF_LINK_CALLBACK |
| 239 |
void link_callback(struct netif *netif) |
| 240 |
{ if (netif_is_link_up(netif)) { |
| 241 |
printf("link_callback==UP\n"); |
| 242 |
#if LWIP_DHCP |
| 243 |
if (netif->dhcp != NULL) { |
| 244 |
dhcp_renew(netif); |
| 245 |
} |
| 246 |
#endif /* LWIP_DHCP */ |
| 247 |
} else { |
| 248 |
printf("link_callback==DOWN\n"); |
| 249 |
} |
| 250 |
} |
| 251 |
#endif /* LWIP_NETIF_LINK_CALLBACK */ |
| 252 |
|
| 253 |
/* This function initializes all network interfaces */ |
| 254 |
static void |
| 255 |
msvc_netif_init() |
| 256 |
{ |
| 257 |
#if PPP_SUPPORT |
| 258 |
sio_fd_t ppp_sio; |
| 259 |
ppp_sio = sio_open(0); |
| 260 |
if (ppp_sio != NULL) { |
| 261 |
printf("pppInit\n"); |
| 262 |
pppInit(); |
| 263 |
pppSetAuth(PPPAUTHTYPE_ANY, "", ""); |
| 264 |
printf("pppOpen\n"); |
| 265 |
pppOpen(ppp_sio, pppLinkStatusCallback, NULL); |
| 266 |
} else { |
| 267 |
printf("sio_open error\n"); |
| 268 |
} |
| 269 |
#else /* PPP_SUPPORT */ |
| 270 |
struct ip_addr ipaddr, netmask, gw; |
| 271 |
#if LWIP_HAVE_LOOPIF |
| 272 |
struct ip_addr loop_ipaddr, loop_netmask, loop_gw; |
| 273 |
|
| 274 |
IP4_ADDR(&loop_gw, 127,0,0,1); |
| 275 |
IP4_ADDR(&loop_ipaddr, 127,0,0,1); |
| 276 |
IP4_ADDR(&loop_netmask, 255,0,0,0); |
| 277 |
printf("Starting lwIP, loopback interface IP is %s\n", inet_ntoa(*(struct in_addr*)&loop_ipaddr)); |
| 278 |
|
| 279 |
#if NO_SYS |
| 280 |
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, ip_input); |
| 281 |
#else /* NO_SYS */ |
| 282 |
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, loopif_init, tcpip_input); |
| 283 |
#endif /* NO_SYS */ |
| 284 |
netif_set_up(&loop_netif); |
| 285 |
#endif /* LWIP_HAVE_LOOPIF */ |
| 286 |
|
| 287 |
#if LWIP_DHCP |
| 288 |
gw.addr = 0; |
| 289 |
ipaddr.addr = 0; |
| 290 |
netmask.addr = 0; |
| 291 |
printf("Starting lwIP, local interface IP is dhcp-enabled\n"); |
| 292 |
#elif LWIP_AUTOIP |
| 293 |
gw.addr = 0; |
| 294 |
ipaddr.addr = 0; |
| 295 |
netmask.addr = 0; |
| 296 |
printf("Starting lwIP, local interface IP is autoip-enabled\n"); |
| 297 |
#else /* LWIP_AUTOIP */ |
| 298 |
LWIP_PORT_INIT_GW(&gw); |
| 299 |
LWIP_PORT_INIT_IPADDR(&ipaddr); |
| 300 |
LWIP_PORT_INIT_NETMASK(&netmask); |
| 301 |
printf("Starting lwIP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&ipaddr)); |
| 302 |
#endif /* LWIP_DHCP */ |
| 303 |
|
| 304 |
#if NO_SYS |
| 305 |
#if LWIP_ARP |
| 306 |
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ethernet_input)); |
| 307 |
#else /* LWIP_ARP */ |
| 308 |
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input)); |
| 309 |
#endif /* LWIP_ARP */ |
| 310 |
#else /* NO_SYS */ |
| 311 |
netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input)); |
| 312 |
#endif /* NO_SYS */ |
| 313 |
#if LWIP_NETIF_STATUS_CALLBACK |
| 314 |
netif_set_status_callback(&netif, status_callback); |
| 315 |
#endif /* LWIP_NETIF_STATUS_CALLBACK */ |
| 316 |
#if LWIP_NETIF_LINK_CALLBACK |
| 317 |
netif_set_link_callback(&netif, link_callback); |
| 318 |
#endif /* LWIP_NETIF_LINK_CALLBACK */ |
| 319 |
|
| 320 |
#if LWIP_DHCP |
| 321 |
dhcp_start(&netif); |
| 322 |
#elif LWIP_AUTOIP |
| 323 |
autoip_start(&netif); |
| 324 |
#else /* LWIP_DHCP */ |
| 325 |
netif_set_up(&netif); |
| 326 |
#endif /* LWIP_DHCP */ |
| 327 |
|
| 328 |
#endif /* PPP_SUPPORT */ |
| 329 |
} |
| 330 |
|
| 331 |
void dns_found(const char *name, struct ip_addr *addr, void *arg) |
| 332 |
{ |
| 333 |
LWIP_UNUSED_ARG(arg); |
| 334 |
printf("%s: %s\n", name, addr?inet_ntoa(*(struct in_addr*)addr):"<not found>"); |
| 335 |
} |
| 336 |
|
| 337 |
/* This function initializes applications */ |
| 338 |
static void |
| 339 |
apps_init() |
| 340 |
{ |
| 341 |
#if LWIP_DNS_APP && LWIP_DNS |
| 342 |
char* dnsname="3com.com"; |
| 343 |
struct ip_addr dnsresp; |
| 344 |
if (dns_gethostbyname(dnsname, &dnsresp, dns_found, 0) == ERR_OK) { |
| 345 |
dns_found(dnsname, &dnsresp, 0); |
| 346 |
} |
| 347 |
#endif /* LWIP_DNS_APP && LWIP_DNS */ |
| 348 |
|
| 349 |
#if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP |
| 350 |
ping_init(); |
| 351 |
#endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */ |
| 352 |
|
| 353 |
#if LWIP_NETBIOS_APP && LWIP_UDP |
| 354 |
netbios_init(); |
| 355 |
#endif /* LWIP_NETBIOS_APP && LWIP_UDP */ |
| 356 |
|
| 357 |
#if LWIP_HTTPD_APP && LWIP_TCP |
| 358 |
httpd_init(); |
| 359 |
#endif /* LWIP_HTTPD_APP && LWIP_TCP */ |
| 360 |
|
| 361 |
#if LWIP_NETIO_APP && LWIP_TCP |
| 362 |
netio_init(); |
| 363 |
#endif /* LWIP_NETIO_APP && LWIP_TCP */ |
| 364 |
|
| 365 |
#if LWIP_RTP_APP && LWIP_SOCKET |
| 366 |
rtp_init(); |
| 367 |
#endif /* LWIP_RTP_APP && LWIP_SOCKET */ |
| 368 |
|
| 369 |
#if LWIP_SNTP_APP && LWIP_SOCKET |
| 370 |
sntp_init(); |
| 371 |
#endif /* LWIP_SNTP_APP && LWIP_SOCKET */ |
| 372 |
} |
| 373 |
|
| 374 |
/* This function initializes this lwIP test. When NO_SYS=1, this is done in |
| 375 |
* the main_loop context (there is no other one), when NO_SYS=0, this is done |
| 376 |
* in the tcpip_thread context */ |
| 377 |
static void |
| 378 |
test_init(void * arg) |
| 379 |
{ /* remove compiler warning */ |
| 380 |
#if NO_SYS |
| 381 |
LWIP_UNUSED_ARG(arg); |
| 382 |
#else /* NO_SYS */ |
| 383 |
sys_sem_t init_sem; |
| 384 |
LWIP_ASSERT("arg != NULL", arg != NULL); |
| 385 |
init_sem = (sys_sem_t)arg; |
| 386 |
#endif /* NO_SYS */ |
| 387 |
|
| 388 |
/* init network interfaces */ |
| 389 |
msvc_netif_init(); |
| 390 |
|
| 391 |
/* init apps */ |
| 392 |
apps_init(); |
| 393 |
|
| 394 |
#if !NO_SYS |
| 395 |
sys_sem_signal(init_sem); |
| 396 |
#endif /* !NO_SYS */ |
| 397 |
} |
| 398 |
|
| 399 |
/* This is somewhat different to other ports: we have a main loop here: |
| 400 |
* a dedicated task that waits for packets to arrive. This would normally be |
| 401 |
* done from interrupt context with embedded hardware, but we don't get an |
| 402 |
* interrupt in windows for that :-) */ |
| 403 |
void main_loop() |
| 404 |
{ |
| 405 |
#if !NO_SYS |
| 406 |
sys_sem_t init_sem; |
| 407 |
#endif /* NO_SYS */ |
| 408 |
|
| 409 |
/* initialize lwIP stack, network interfaces and applications */ |
| 410 |
#if NO_SYS |
| 411 |
nosys_init(); |
| 412 |
test_init(NULL); |
| 413 |
#else /* NO_SYS */ |
| 414 |
init_sem = sys_sem_new(0); |
| 415 |
tcpip_init(test_init, init_sem); |
| 416 |
/* we have to wait for initialization to finish before |
| 417 |
* calling update_adapter()! */ |
| 418 |
sys_sem_wait(init_sem); |
| 419 |
sys_sem_free(init_sem); |
| 420 |
#endif /* NO_SYS */ |
| 421 |
|
| 422 |
/* MAIN LOOP for driver update (and timers if NO_SYS) */ |
| 423 |
while (!_kbhit()) { |
| 424 |
#if NO_SYS |
| 425 |
/* handle timers (already done in tcpip.c when NO_SYS=0) */ |
| 426 |
timers_update(); |
| 427 |
#endif /* NO_SYS */ |
| 428 |
|
| 429 |
/* check for packets and link status*/ |
| 430 |
ethernetif_poll(&netif); |
| 431 |
#if !LWIP_NETIF_LOOPBACK_MULTITHREADING |
| 432 |
/* check for loopback packets on all netifs */ |
| 433 |
netif_poll_all(); |
| 434 |
#endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */ |
| 435 |
} |
| 436 |
|
| 437 |
/* release the pcap library... */ |
| 438 |
ethernetif_shutdown(&netif); |
| 439 |
} |
| 440 |
|
| 441 |
int main(void) |
| 442 |
{ |
| 443 |
/* no stdio-buffering, please! */ |
| 444 |
setvbuf(stdout, NULL,_IONBF, 0); |
| 445 |
|
| 446 |
main_loop(); |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |