xref: /nrf52832-nimble/rt-thread/components/net/uip/unix/main.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Copyright (c) 2001, Adam Dunkels.
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Adam Dunkels.
16  * 4. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file is part of the uIP TCP/IP stack.
33  *
34  * $Id: main.c,v 1.16 2006/06/11 21:55:03 adam Exp $
35  *
36  */
37 
38 
39 #include "uip.h"
40 #include "uip_arp.h"
41 #include "tapdev.h"
42 
43 #include "timer.h"
44 
45 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
46 
47 #ifndef NULL
48 #define NULL (void *)0
49 #endif /* NULL */
50 
51 /*---------------------------------------------------------------------------*/
52 int
53 main(void)
54 {
55   int i;
56   uip_ipaddr_t ipaddr;
57   struct timer periodic_timer, arp_timer;
58 
59   timer_set(&periodic_timer, CLOCK_SECOND / 2);
60   timer_set(&arp_timer, CLOCK_SECOND * 10);
61 
62   tapdev_init();
63   uip_init();
64 
65   uip_ipaddr(ipaddr, 192,168,0,2);
66   uip_sethostaddr(ipaddr);
67   uip_ipaddr(ipaddr, 192,168,0,1);
68   uip_setdraddr(ipaddr);
69   uip_ipaddr(ipaddr, 255,255,255,0);
70   uip_setnetmask(ipaddr);
71 
72   httpd_init();
73 
74   /*  telnetd_init();*/
75 
76   /*  hello_world_init();*/
77 
78   /*  {
79       u8_t mac[6] = {1,2,3,4,5,6};
80       dhcpc_init(&mac, 6);
81       }*/
82 
83   /*uip_ipaddr(ipaddr, 127,0,0,1);
84   smtp_configure("localhost", ipaddr);
85   SMTP_SEND("[email protected]", NULL, "[email protected]",
86 	    "Testing SMTP from uIP",
87 	    "Test message sent by uIP\r\n");*/
88 
89   /*
90     webclient_init();
91     resolv_init();
92     uip_ipaddr(ipaddr, 195,54,122,204);
93     resolv_conf(ipaddr);
94     resolv_query("www.sics.se");*/
95 
96 
97 
98   while(1) {
99     uip_len = tapdev_read();
100     if(uip_len > 0) {
101       if(BUF->type == htons(UIP_ETHTYPE_IP)) {
102 	uip_arp_ipin();
103 	uip_input();
104 	/* If the above function invocation resulted in data that
105 	   should be sent out on the network, the global variable
106 	   uip_len is set to a value > 0. */
107 	if(uip_len > 0) {
108 	  uip_arp_out();
109 	  tapdev_send();
110 	}
111       } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
112 	uip_arp_arpin();
113 	/* If the above function invocation resulted in data that
114 	   should be sent out on the network, the global variable
115 	   uip_len is set to a value > 0. */
116 	if(uip_len > 0) {
117 	  tapdev_send();
118 	}
119       }
120 
121     } else if(timer_expired(&periodic_timer)) {
122       timer_reset(&periodic_timer);
123       for(i = 0; i < UIP_CONNS; i++) {
124 	uip_periodic(i);
125 	/* If the above function invocation resulted in data that
126 	   should be sent out on the network, the global variable
127 	   uip_len is set to a value > 0. */
128 	if(uip_len > 0) {
129 	  uip_arp_out();
130 	  tapdev_send();
131 	}
132       }
133 
134 #if UIP_UDP
135       for(i = 0; i < UIP_UDP_CONNS; i++) {
136 	uip_udp_periodic(i);
137 	/* If the above function invocation resulted in data that
138 	   should be sent out on the network, the global variable
139 	   uip_len is set to a value > 0. */
140 	if(uip_len > 0) {
141 	  uip_arp_out();
142 	  tapdev_send();
143 	}
144       }
145 #endif /* UIP_UDP */
146 
147       /* Call the ARP timer function every 10 seconds. */
148       if(timer_expired(&arp_timer)) {
149 	timer_reset(&arp_timer);
150 	uip_arp_timer();
151       }
152     }
153   }
154   return 0;
155 }
156 /*---------------------------------------------------------------------------*/
157 void
158 uip_log(char *m)
159 {
160   printf("uIP log message: %s\n", m);
161 }
162 void
163 resolv_found(char *name, u16_t *ipaddr)
164 {
165   u16_t *ipaddr2;
166 
167   if(ipaddr == NULL) {
168     printf("Host '%s' not found.\n", name);
169   } else {
170     printf("Found name '%s' = %d.%d.%d.%d\n", name,
171 	   htons(ipaddr[0]) >> 8,
172 	   htons(ipaddr[0]) & 0xff,
173 	   htons(ipaddr[1]) >> 8,
174 	   htons(ipaddr[1]) & 0xff);
175     /*    webclient_get("www.sics.se", 80, "/~adam/uip");*/
176   }
177 }
178 #ifdef __DHCPC_H__
179 void
180 dhcpc_configured(const struct dhcpc_state *s)
181 {
182   uip_sethostaddr(s->ipaddr);
183   uip_setnetmask(s->netmask);
184   uip_setdraddr(s->default_router);
185   resolv_conf(s->dnsaddr);
186 }
187 #endif /* __DHCPC_H__ */
188 void
189 smtp_done(unsigned char code)
190 {
191   printf("SMTP done with code %d\n", code);
192 }
193 void
194 webclient_closed(void)
195 {
196   printf("Webclient: connection closed\n");
197 }
198 void
199 webclient_aborted(void)
200 {
201   printf("Webclient: connection aborted\n");
202 }
203 void
204 webclient_timedout(void)
205 {
206   printf("Webclient: connection timed out\n");
207 }
208 void
209 webclient_connected(void)
210 {
211   printf("Webclient: connected, waiting for data...\n");
212 }
213 void
214 webclient_datahandler(char *data, u16_t len)
215 {
216   printf("Webclient: got %d bytes of data.\n", len);
217 }
218 /*---------------------------------------------------------------------------*/
219