Debian Codegraph @

contrib/ports/unix/proj/unixsim/simhost.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <[email protected]>
30 *
31 */
32
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <getopt.h>
36
37 #include "lwip/opt.h"
38
39 #include "lwip/init.h"
40
41 #include "lwip/mem.h"
42 #include "lwip/memp.h"
43 #include "lwip/sys.h"
44
45 #include "lwip/stats.h"
46
47 #include "lwip/tcp_impl.h"
48 #include "lwip/inet_chksum.h"
49
50 #include "lwip/tcpip.h"
51 #include "lwip/sockets.h"
52
53 #include "netif/tapif.h"
54 #include "netif/tunif.h"
55
56 #include "netif/unixif.h"
57 #include "netif/dropif.h"
58 #include "netif/pcapif.h"
59
60 #include "netif/tcpdump.h"
61
62 #if PPP_SUPPORT
63 #include "netif/ppp/ppp.h"
64 #define PPP_PTY_TEST 1
65 #include <termios.h>
66 #endif
67
68 #include "lwip/ip_addr.h"
69 #include "arch/perf.h"
70
71 #include "httpd.h"
72 #include "udpecho.h"
73 #include "tcpecho.h"
74 #include "shell.h"
75
76 #if LWIP_RAW
77 #include "lwip/icmp.h"
78 #include "lwip/raw.h"
79 #endif
80
81 /* (manual) host IP configuration */
82 static ip_addr_t ipaddr, netmask, gw;
83
84 /* ping out destination cmd option */
85 static unsigned char ping_flag;
86 static ip_addr_t ping_addr;
87
88 /* nonstatic debug cmd option, exported in lwipopts.h */
89 unsigned char debug_flags;
90
91 /** @todo add options for selecting netif, starting DHCP client etc */
92 static struct option longopts[] = {
93 /* turn on debugging output (if build with LWIP_DEBUG) */
94 {"debug", no_argument, NULL, 'd'},
95 /* help */
96 {"help", no_argument, NULL, 'h'},
97 /* gateway address */
98 {"gateway", required_argument, NULL, 'g'},
99 /* ip address */
100 {"ipaddr", required_argument, NULL, 'i'},
101 /* netmask */
102 {"netmask", required_argument, NULL, 'm'},
103 /* ping destination */
104 {"ping", required_argument, NULL, 'p'},
105 /* new command line options go here! */
106 {NULL, 0, NULL, 0}
107 };
108 #define NUM_OPTS ((sizeof(longopts) / sizeof(struct option)) - 1)
109
110 static void init_netifs(void);
111
112 static void usage(void)
113 {
114 unsigned char i;
115
116 printf("options:\n");
117 for (i = 0; i < NUM_OPTS; i++) {
118 printf("-%c --%s\n",longopts[i].val, longopts[i].name);
119 }
120 }
121
122 #if 0
123 static void
124 tcp_debug_timeout(void *data)
125 {
126 LWIP_UNUSED_ARG(data);
127 #if TCP_DEBUG
128 tcp_debug_print_pcbs();
129 #endif /* TCP_DEBUG */
130 sys_timeout(5000, tcp_debug_timeout, NULL);
131 }
132 #endif
133
134 static void
135 tcpip_init_done(void *arg)
136 {
137 sys_sem_t *sem;
138 sem = (sys_sem_t *)arg;
139
140 init_netifs();
141
142 sys_sem_signal(sem);
143 }
144
145 #if PPP_SUPPORT
146 void
147 pppLinkStatusCallback(void *ctx, int errCode, void *arg)
148 {
149 switch(errCode) {
150 case PPPERR_NONE: /* No error. */
151 {
152 struct ppp_addrs *ppp_addrs = arg;
153
154 printf("pppLinkStatusCallback: PPPERR_NONE");
155 printf(" our_ipaddr=%s", _inet_ntoa(ppp_addrs->our_ipaddr.addr));
156 printf(" his_ipaddr=%s", _inet_ntoa(ppp_addrs->his_ipaddr.addr));
157 printf(" netmask=%s", _inet_ntoa(ppp_addrs->netmask.addr));
158 printf(" dns1=%s", _inet_ntoa(ppp_addrs->dns1.addr));
159 printf(" dns2=%s\n", _inet_ntoa(ppp_addrs->dns2.addr));
160 }
161 break;
162
163 case PPPERR_PARAM: /* Invalid parameter. */
164 printf("pppLinkStatusCallback: PPPERR_PARAM\n");
165 break;
166
167 case PPPERR_OPEN: /* Unable to open PPP session. */
168 printf("pppLinkStatusCallback: PPPERR_OPEN\n");
169 break;
170
171 case PPPERR_DEVICE: /* Invalid I/O device for PPP. */
172 printf("pppLinkStatusCallback: PPPERR_DEVICE\n");
173 break;
174
175 case PPPERR_ALLOC: /* Unable to allocate resources. */
176 printf("pppLinkStatusCallback: PPPERR_ALLOC\n");
177 break;
178
179 case PPPERR_USER: /* User interrupt. */
180 printf("pppLinkStatusCallback: PPPERR_USER\n");
181 break;
182
183 case PPPERR_CONNECT: /* Connection lost. */
184 printf("pppLinkStatusCallback: PPPERR_CONNECT\n");
185 break;
186
187 case PPPERR_AUTHFAIL: /* Failed authentication challenge. */
188 printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n");
189 break;
190
191 case PPPERR_PROTOCOL: /* Failed to meet protocol. */
192 printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n");
193 break;
194
195 default:
196 printf("pppLinkStatusCallback: unknown errCode %d\n", errCode);
197 break;
198 }
199 }
200 #endif
201
202 /*-----------------------------------------------------------------------------------*/
203 /*-----------------------------------------------------------------------------------*/
204 #if LWIP_RAW
205
206 static int seq_num;
207
208 #if 0
209 /* Ping using the raw api */
210 static int
211 ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr)
212 {
213 printf("ping recv\n");
214 return 1; /* eat the event */
215 }
216
217 static void
218 ping_send(struct raw_pcb *raw, ip_addr_t *addr)
219 {
220 struct pbuf *p;
221 struct icmp_echo_hdr *iecho;
222
223 p = pbuf_alloc(PBUF_IP,sizeof(struct icmp_echo_hdr),PBUF_RAM);
224 if (!p) return;
225
226 iecho = p->payload;
227 ICMPH_TYPE_SET(iecho,ICMP_ECHO);
228 iecho->chksum = 0;
229 iecho->seqno = htons(seq_num);
230
231 iecho->chksum = inet_chksum(iecho, p->len);
232 raw_send_to(raw,p,addr);
233
234 pbuf_free(p);
235
236 seq_num++;
237 }
238
239 static void
240 ping_thread(void *arg)
241 {
242 struct raw_pcb *raw;
243
244 if (!(raw = raw_new(IP_PROTO_ICMP))) return;
245
246 raw_recv(raw,ping_recv,NULL);
247
248 while (1)
249 {
250 printf("ping send\n");
251 ping_send(raw,&ping_addr);
252 sleep(1);
253 }
254 /* Never reaches this */
255 raw_remove(raw);
256 }
257 #else
258 /* Ping using the socket api */
259
260 static void
261 ping_send(int s, ip_addr_t *addr)
262 {
263 struct icmp_echo_hdr *iecho;
264 struct sockaddr_in to;
265
266 if (!(iecho = (struct icmp_echo_hdr *)malloc(sizeof(struct icmp_echo_hdr))))
267 return;
268
269 ICMPH_TYPE_SET(iecho,ICMP_ECHO);
270 iecho->chksum = 0;
271 iecho->seqno = htons(seq_num);
272 iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
273
274 to.sin_len = sizeof(to);
275 to.sin_family = AF_INET;
276 to.sin_addr.s_addr = addr->addr;
277
278 lwip_sendto(s,iecho,sizeof(*iecho),0,(struct sockaddr*)&to,sizeof(to));
279
280 free(iecho);
281 seq_num++;
282 }
283
284 static void
285 ping_recv(int s, ip_addr_t *addr)
286 {
287 char buf[200];
288 socklen_t fromlen;
289 int len;
290 struct sockaddr_in from;
291 LWIP_UNUSED_ARG(addr);
292
293 len = lwip_recvfrom(s, buf,sizeof(buf),0,(struct sockaddr*)&from,&fromlen);
294
295 printf("Received %d bytes from %x\n",len,ntohl(from.sin_addr.s_addr));
296 }
297
298 static void
299 ping_thread(void *arg)
300 {
301 int s;
302 LWIP_UNUSED_ARG(arg);
303
304 if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
305 return;
306 }
307
308 while (1) {
309 printf("sending ping\n");
310 ping_send(s,&ping_addr);
311 ping_recv(s,&ping_addr);
312 sleep(1);
313 }
314 }
315 #endif
316
317 #endif
318
319 struct netif netif;
320
321 static void
322 init_netifs(void)
323 {
324 #if PPP_SUPPORT
325 pppInit();
326 #if PPP_PTY_TEST
327 ppp_sio = sio_open(2);
328 #else
329 ppp_sio = sio_open(0);
330 #endif
331 if(!ppp_sio)
332 {
333 perror("Error opening device: ");
334 exit(1);
335 }
336
337 #ifdef LWIP_PPP_CHAP_TEST
338 pppSetAuth(PPPAUTHTYPE_CHAP, "lwip", "mysecret");
339 #endif
340
341 pppOpen(ppp_sio, pppLinkStatusCallback, NULL);
342 #endif /* PPP_SUPPORT */
343
344 #if LWIP_DHCP
345 {
346 IP4_ADDR(&gw, 0,0,0,0);
347 IP4_ADDR(&ipaddr, 0,0,0,0);
348 IP4_ADDR(&netmask, 0,0,0,0);
349
350 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init,
351 tcpip_input);
352 netif_set_default(&netif);
353 dhcp_start(&netif);
354 }
355 #else
356
357 netif_set_default(netif_add(&netif,&ipaddr, &netmask, &gw, NULL, tapif_init,
358 tcpip_input));
359 netif_set_up(&netif);
360
361 #endif
362 #if LWIP_IPV6
363 netif_create_ip6_linklocal_address(&netif, 1);
364 #endif
365
366 #if 0
367 /* Only used for testing purposes: */
368 netif_add(&ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input);
369 #endif
370
371 #if LWIP_TCP
372 tcpecho_init();
373 shell_init();
374 httpd_init();
375 #endif
376 #if LWIP_UDP
377 udpecho_init();
378 #endif
379 /* sys_timeout(5000, tcp_debug_timeout, NULL);*/
380 }
381
382 /*-----------------------------------------------------------------------------------*/
383 static void
384 main_thread(void *arg)
385 {
386 sys_sem_t sem;
387 #if PPP_SUPPORT
388 sio_fd_t ppp_sio;
389 #endif
390 LWIP_UNUSED_ARG(arg);
391
392 if(sys_sem_new(&sem, 0) != ERR_OK) {
393 LWIP_ASSERT("Failed to create semaphore", 0);
394 }
395 tcpip_init(tcpip_init_done, &sem);
396 sys_sem_wait(&sem);
397 printf("TCP/IP initialized.\n");
398
399 #if LWIP_RAW
400 /** @todo remove dependency on RAW PCB support */
401 if(ping_flag) {
402 sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
403 }
404 #endif
405
406 printf("Applications started.\n");
407
408
409 #ifdef MEM_PERF
410 mem_perf_init("/tmp/memstats.client");
411 #endif /* MEM_PERF */
412 #if 0
413 stats_display();
414 #endif
415 /* Block forever. */
416 sys_sem_wait(&sem);
417 }
418 /*-----------------------------------------------------------------------------------*/
419 int
420 main(int argc, char **argv)
421 {
422 struct in_addr inaddr;
423 int ch;
424 char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
425
426 /* startup defaults (may be overridden by one or more opts) */
427 IP4_ADDR(&gw, 192,168,0,1);
428 IP4_ADDR(&netmask, 255,255,255,0);
429 IP4_ADDR(&ipaddr, 192,168,0,2);
430
431 ping_flag = 0;
432 /* use debug flags defined by debug.h */
433 debug_flags = LWIP_DBG_OFF;
434
435 while ((ch = getopt_long(argc, argv, "dhg:i:m:p:", longopts, NULL)) != -1) {
436 switch (ch) {
437 case 'd':
438 debug_flags |= (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT);
439 break;
440 case 'h':
441 usage();
442 exit(0);
443 break;
444 case 'g':
445 inet_aton(optarg, &inaddr);
446 gw.addr = inaddr.s_addr;
447 break;
448 case 'i':
449 inet_aton(optarg, &inaddr);
450 ipaddr.addr = inaddr.s_addr;
451 break;
452 case 'm':
453 inet_aton(optarg, &inaddr);
454 netmask.addr = inaddr.s_addr;
455 break;
456 case 'p':
457 ping_flag = !0;
458 inet_aton(optarg, &inaddr);
459 /* lwip inet.h oddity workaround */
460 ping_addr.addr = inaddr.s_addr;
461 strncpy(ip_str,inet_ntoa(inaddr),sizeof(ip_str));
462 printf("Using %s to ping\n", ip_str);
463 break;
464 default:
465 usage();
466 break;
467 }
468 }
469 argc -= optind;
470 argv += optind;
471
472 inaddr.s_addr = ipaddr.addr;
473 strncpy(ip_str,inet_ntoa(inaddr),sizeof(ip_str));
474 inaddr.s_addr = netmask.addr;
475 strncpy(nm_str,inet_ntoa(inaddr),sizeof(nm_str));
476 inaddr.s_addr = gw.addr;
477 strncpy(gw_str,inet_ntoa(inaddr),sizeof(gw_str));
478 printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str);
479
480 #ifdef PERF
481 perf_init("/tmp/simhost.perf");
482 #endif /* PERF */
483
484 printf("System initialized.\n");
485
486 sys_thread_new("main_thread", main_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
487 pause();
488 return 0;
489 }
490 /*-----------------------------------------------------------------------------------*/
491
492
493
494
495
496
497
498