main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "uip.h"
  38. #include "uip_arp.h"
  39. #include "tapdev.h"
  40. #include "timer.h"
  41. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  42. #ifndef NULL
  43. #define NULL (void *)0
  44. #endif /* NULL */
  45. /*---------------------------------------------------------------------------*/
  46. int
  47. main(void)
  48. {
  49. int i;
  50. uip_ipaddr_t ipaddr;
  51. struct timer periodic_timer, arp_timer;
  52. timer_set(&periodic_timer, CLOCK_SECOND / 2);
  53. timer_set(&arp_timer, CLOCK_SECOND * 10);
  54. tapdev_init();
  55. uip_init();
  56. uip_ipaddr(ipaddr, 192,168,0,2);
  57. uip_sethostaddr(ipaddr);
  58. uip_ipaddr(ipaddr, 192,168,0,1);
  59. uip_setdraddr(ipaddr);
  60. uip_ipaddr(ipaddr, 255,255,255,0);
  61. uip_setnetmask(ipaddr);
  62. httpd_init();
  63. /* telnetd_init();*/
  64. /* hello_world_init();*/
  65. /* {
  66. u8_t mac[6] = {1,2,3,4,5,6};
  67. dhcpc_init(&mac, 6);
  68. }*/
  69. /*uip_ipaddr(ipaddr, 127,0,0,1);
  70. smtp_configure("localhost", ipaddr);
  71. SMTP_SEND("adam@sics.se", NULL, "uip-testing@example.com",
  72. "Testing SMTP from uIP",
  73. "Test message sent by uIP\r\n");*/
  74. /*
  75. webclient_init();
  76. resolv_init();
  77. uip_ipaddr(ipaddr, 195,54,122,204);
  78. resolv_conf(ipaddr);
  79. resolv_query("www.sics.se");*/
  80. while(1) {
  81. uip_len = tapdev_read();
  82. if(uip_len > 0) {
  83. if(BUF->type == htons(UIP_ETHTYPE_IP)) {
  84. uip_arp_ipin();
  85. uip_input();
  86. /* If the above function invocation resulted in data that
  87. should be sent out on the network, the global variable
  88. uip_len is set to a value > 0. */
  89. if(uip_len > 0) {
  90. uip_arp_out();
  91. tapdev_send();
  92. }
  93. } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
  94. uip_arp_arpin();
  95. /* If the above function invocation resulted in data that
  96. should be sent out on the network, the global variable
  97. uip_len is set to a value > 0. */
  98. if(uip_len > 0) {
  99. tapdev_send();
  100. }
  101. }
  102. } else if(timer_expired(&periodic_timer)) {
  103. timer_reset(&periodic_timer);
  104. for(i = 0; i < UIP_CONNS; i++) {
  105. uip_periodic(i);
  106. /* If the above function invocation resulted in data that
  107. should be sent out on the network, the global variable
  108. uip_len is set to a value > 0. */
  109. if(uip_len > 0) {
  110. uip_arp_out();
  111. tapdev_send();
  112. }
  113. }
  114. #if UIP_UDP
  115. for(i = 0; i < UIP_UDP_CONNS; i++) {
  116. uip_udp_periodic(i);
  117. /* If the above function invocation resulted in data that
  118. should be sent out on the network, the global variable
  119. uip_len is set to a value > 0. */
  120. if(uip_len > 0) {
  121. uip_arp_out();
  122. tapdev_send();
  123. }
  124. }
  125. #endif /* UIP_UDP */
  126. /* Call the ARP timer function every 10 seconds. */
  127. if(timer_expired(&arp_timer)) {
  128. timer_reset(&arp_timer);
  129. uip_arp_timer();
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. /*---------------------------------------------------------------------------*/
  136. void
  137. uip_log(char *m)
  138. {
  139. printf("uIP log message: %s\n", m);
  140. }
  141. void
  142. resolv_found(char *name, u16_t *ipaddr)
  143. {
  144. u16_t *ipaddr2;
  145. if(ipaddr == NULL) {
  146. printf("Host '%s' not found.\n", name);
  147. } else {
  148. printf("Found name '%s' = %d.%d.%d.%d\n", name,
  149. htons(ipaddr[0]) >> 8,
  150. htons(ipaddr[0]) & 0xff,
  151. htons(ipaddr[1]) >> 8,
  152. htons(ipaddr[1]) & 0xff);
  153. /* webclient_get("www.sics.se", 80, "/~adam/uip");*/
  154. }
  155. }
  156. #ifdef __DHCPC_H__
  157. void
  158. dhcpc_configured(const struct dhcpc_state *s)
  159. {
  160. uip_sethostaddr(s->ipaddr);
  161. uip_setnetmask(s->netmask);
  162. uip_setdraddr(s->default_router);
  163. resolv_conf(s->dnsaddr);
  164. }
  165. #endif /* __DHCPC_H__ */
  166. void
  167. smtp_done(unsigned char code)
  168. {
  169. printf("SMTP done with code %d\n", code);
  170. }
  171. void
  172. webclient_closed(void)
  173. {
  174. printf("Webclient: connection closed\n");
  175. }
  176. void
  177. webclient_aborted(void)
  178. {
  179. printf("Webclient: connection aborted\n");
  180. }
  181. void
  182. webclient_timedout(void)
  183. {
  184. printf("Webclient: connection timed out\n");
  185. }
  186. void
  187. webclient_connected(void)
  188. {
  189. printf("Webclient: connected, waiting for data...\n");
  190. }
  191. void
  192. webclient_datahandler(char *data, u16_t len)
  193. {
  194. printf("Webclient: got %d bytes of data.\n", len);
  195. }
  196. /*---------------------------------------------------------------------------*/