Debian Codegraph @

contrib/ports/unix/proj/minimal/main.c

raw
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 * RT timer modifications by Christiaan Simons
31 */
32
33 #include <unistd.h>
34 #include <getopt.h>
35
36 #include "lwip/init.h"
37
38 #include "lwip/debug.h"
39
40 #include "lwip/mem.h"
41 #include "lwip/memp.h"
42 #include "lwip/sys.h"
43
44 #include "lwip/stats.h"
45
46 #include "lwip/ip.h"
47 #include "lwip/ip_frag.h"
48 #include "lwip/udp.h"
49 #include "lwip/snmp_msg.h"
50 #include "lwip/tcp_impl.h"
51 #include "mintapif.h"
52 #include "netif/etharp.h"
53
54 #include "timer.h"
55 #include <signal.h>
56
57 #include "echo.h"
58 #include "private_mib.h"
59
60 /* (manual) host IP configuration */
61 static ip_addr_t ipaddr, netmask, gw;
62
63 /* SNMP trap destination cmd option */
64 static unsigned char trap_flag;
65 static ip_addr_t trap_addr;
66
67 /* nonstatic debug cmd option, exported in lwipopts.h */
68 unsigned char debug_flags;
69
70 /* 'non-volatile' SNMP settings
71 @todo: make these truly non-volatile */
72 u8_t syscontact_str[255];
73 u8_t syscontact_len = 0;
74 u8_t syslocation_str[255];
75 u8_t syslocation_len = 0;
76 /* enable == 1, disable == 2 */
77 u8_t snmpauthentraps_set = 2;
78
79 static struct option longopts[] = {
80 /* turn on debugging output (if build with LWIP_DEBUG) */
81 {"debug", no_argument, NULL, 'd'},
82 /* help */
83 {"help", no_argument, NULL, 'h'},
84 /* gateway address */
85 {"gateway", required_argument, NULL, 'g'},
86 /* ip address */
87 {"ipaddr", required_argument, NULL, 'i'},
88 /* netmask */
89 {"netmask", required_argument, NULL, 'm'},
90 /* ping destination */
91 {"trap_destination", required_argument, NULL, 't'},
92 /* new command line options go here! */
93 {NULL, 0, NULL, 0}
94 };
95 #define NUM_OPTS ((sizeof(longopts) / sizeof(struct option)) - 1)
96
97 static void usage(void)
98 {
99 unsigned char i;
100
101 printf("options:\n");
102 for (i = 0; i < NUM_OPTS; i++) {
103 printf("-%c --%s\n",longopts[i].val, longopts[i].name);
104 }
105 }
106
107 int
108 main(int argc, char **argv)
109 {
110 struct netif netif;
111 sigset_t mask, oldmask, empty;
112 int ch;
113 char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
114
115 /* startup defaults (may be overridden by one or more opts) */
116 IP4_ADDR(&gw, 192,168,0,1);
117 IP4_ADDR(&ipaddr, 192,168,0,2);
118 IP4_ADDR(&netmask, 255,255,255,0);
119
120 trap_flag = 0;
121 /* use debug flags defined by debug.h */
122 debug_flags = LWIP_DBG_OFF;
123
124 while ((ch = getopt_long(argc, argv, "dhg:i:m:t:", longopts, NULL)) != -1) {
125 switch (ch) {
126 case 'd':
127 debug_flags |= (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT);
128 break;
129 case 'h':
130 usage();
131 exit(0);
132 break;
133 case 'g':
134 ipaddr_aton(optarg, &gw);
135 break;
136 case 'i':
137 ipaddr_aton(optarg, &ipaddr);
138 break;
139 case 'm':
140 ipaddr_aton(optarg, &netmask);
141 break;
142 case 't':
143 trap_flag = !0;
144 /* @todo: remove this authentraps tweak
145 when we have proper SET & non-volatile mem */
146 snmpauthentraps_set = 1;
147 ipaddr_aton(optarg, &trap_addr);
148 strncpy(ip_str, ipaddr_ntoa(&trap_addr),sizeof(ip_str));
149 printf("SNMP trap destination %s\n", ip_str);
150 break;
151 default:
152 usage();
153 break;
154 }
155 }
156 argc -= optind;
157 argv += optind;
158
159 strncpy(ip_str, ipaddr_ntoa(&ipaddr), sizeof(ip_str));
160 strncpy(nm_str, ipaddr_ntoa(&netmask), sizeof(nm_str));
161 strncpy(gw_str, ipaddr_ntoa(&gw), sizeof(gw_str));
162 printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str);
163
164
165 #ifdef PERF
166 perf_init("/tmp/minimal.perf");
167 #endif /* PERF */
168
169 lwip_init();
170
171 printf("TCP/IP initialized.\n");
172
173 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, mintapif_init, ethernet_input);
174 netif_set_default(&netif);
175 netif_set_up(&netif);
176 #if LWIP_IPV6
177 netif_create_ip6_linklocal_address(&netif, 1);
178 #endif
179
180
181 #if SNMP_PRIVATE_MIB != 0
182 /* initialize our private example MIB */
183 lwip_privmib_init();
184 #endif
185 snmp_trap_dst_ip_set(0,&trap_addr);
186 snmp_trap_dst_enable(0,trap_flag);
187 snmp_set_syscontact(syscontact_str,&syscontact_len);
188 snmp_set_syslocation(syslocation_str,&syslocation_len);
189 snmp_set_snmpenableauthentraps(&snmpauthentraps_set);
190 snmp_init();
191
192 echo_init();
193
194 timer_init();
195 timer_set_interval(TIMER_EVT_ETHARPTMR, ARP_TMR_INTERVAL / 10);
196 timer_set_interval(TIMER_EVT_TCPTMR, TCP_TMR_INTERVAL / 10);
197 #if IP_REASSEMBLY
198 timer_set_interval(TIMER_EVT_IPREASSTMR, IP_TMR_INTERVAL / 10);
199 #endif
200
201 printf("Applications started.\n");
202
203
204 while (1) {
205
206 /* poll for input packet and ensure
207 select() or read() arn't interrupted */
208 sigemptyset(&mask);
209 sigaddset(&mask, SIGALRM);
210 sigprocmask(SIG_BLOCK, &mask, &oldmask);
211
212 /* start of critical section,
213 poll netif, pass packet to lwIP */
214 if (mintapif_select(&netif) > 0)
215 {
216 /* work, immediatly end critical section
217 hoping lwIP ended quickly ... */
218 sigprocmask(SIG_SETMASK, &oldmask, NULL);
219 }
220 else
221 {
222 /* no work, wait a little (10 msec) for SIGALRM */
223 sigemptyset(&empty);
224 sigsuspend(&empty);
225 /* ... end critical section */
226 sigprocmask(SIG_SETMASK, &oldmask, NULL);
227 }
228
229 if(timer_testclr_evt(TIMER_EVT_TCPTMR))
230 {
231 tcp_tmr();
232 }
233 #if IP_REASSEMBLY
234 if(timer_testclr_evt(TIMER_EVT_IPREASSTMR))
235 {
236 ip_reass_tmr();
237 }
238 #endif
239 if(timer_testclr_evt(TIMER_EVT_ETHARPTMR))
240 {
241 etharp_tmr();
242 }
243
244 }
245
246 return 0;
247 }