inetq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*!
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Log$
  36. * Revision 1.6 2009/02/18 12:18:58 olereinhardt
  37. * 2009-02-18 Ole Reinhardt <ole.reinhardt@thermotemp.de>
  38. *
  39. * Fixed compilier warnings. Especialy signedness of char buffers
  40. * as well as unused code on arm platform and main functions without
  41. * return value
  42. *
  43. * Revision 1.5 2006/07/21 09:07:48 haraldkipp
  44. * Fixed warnings about wrong signedness.
  45. *
  46. * Revision 1.4 2005/11/22 09:14:13 haraldkipp
  47. * Replaced specific device names by generalized macros.
  48. *
  49. * Revision 1.3 2005/04/19 08:53:56 haraldkipp
  50. * Added more detailed description
  51. *
  52. * Revision 1.2 2003/11/04 17:46:52 haraldkipp
  53. * Adapted to Ethernut 2
  54. *
  55. * Revision 1.1 2003/08/05 18:59:05 haraldkipp
  56. * Release 3.3 update
  57. *
  58. * Revision 1.7 2003/05/08 12:00:53 harald
  59. * Changed target to famous spammer
  60. *
  61. * Revision 1.6 2003/02/04 18:19:37 harald
  62. * Version 3 released
  63. *
  64. * Revision 1.5 2003/02/04 16:24:32 harald
  65. * Adapted to version 3
  66. *
  67. * Revision 1.4 2002/06/12 10:55:54 harald
  68. * *** empty log message ***
  69. *
  70. * Revision 1.3 2002/06/04 19:11:29 harald
  71. * *** empty log message ***
  72. *
  73. * Revision 1.2 2002/05/08 16:02:30 harald
  74. * First Imagecraft compilation
  75. *
  76. */
  77. /*!
  78. * \example inetq/inetq.c
  79. *
  80. * Requests an URL from the Internet and transfers the HTML
  81. * source code to the serial device.
  82. *
  83. * Your local Ethernet network must provide Internet access.
  84. * Connect the RS232 port of the Ethernut with a free COM
  85. * port of your PC and run a terminal emulator at 115200 Baud.
  86. *
  87. * If your local network does not support DHCP, it may be
  88. * required to modify the MY_IP, MY_MASK and MY_GATE below.
  89. *
  90. * This sample demonstrates DNS query and default route usage.
  91. */
  92. #define DNSSERVERIP "192.168.192.2"
  93. #define INETSERVER "www.kornet.net"
  94. #define INETSERVERPORT 80
  95. #define INETURL "/"
  96. #define MY_MAC {0x00,0x06,0x98,0x20,0x00,0x00}
  97. #define MY_IP "192.168.192.100"
  98. #define MY_MASK "255.255.255.0"
  99. #define MY_GATE "192.168.192.3"
  100. #include <string.h>
  101. #include <stdio.h>
  102. #include <io.h>
  103. #include <dev/board.h>
  104. #include <sys/heap.h>
  105. #include <sys/thread.h>
  106. #include <sys/timer.h>
  107. #include <sys/socket.h>
  108. #include <sys/confnet.h>
  109. #include <arpa/inet.h>
  110. #include <net/route.h>
  111. #include <netdb.h>
  112. #include <pro/dhcp.h>
  113. static char buff[1024];
  114. static uint8_t my_mac[] = MY_MAC;
  115. /*
  116. * Main application routine.
  117. *
  118. */
  119. int main(void)
  120. {
  121. uint32_t baud = 115200;
  122. TCPSOCKET *sock;
  123. FILE *stream;
  124. uint32_t rip;
  125. uint32_t ip_addr;
  126. int bite;
  127. size_t rc;
  128. size_t len;
  129. uint32_t start_time;
  130. uint32_t total_bytes;
  131. /*
  132. * Initialize the uart device.
  133. */
  134. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  135. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  136. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  137. puts("\nInetQuery 1.0");
  138. #ifdef DEV_ETHER
  139. /*
  140. * Register Realtek controller at address 8300 hex and interrupt 5.
  141. */
  142. puts("Configuring Ethernet interface");
  143. NutRegisterDevice(&DEV_ETHER, 0, 0);
  144. /*
  145. * Try DHCP. First use MAC from EEPROM.
  146. */
  147. if (NutDhcpIfConfig("eth0", 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) {
  148. /*
  149. * No DHCP server available. Use hard coded values.
  150. */
  151. ip_addr = inet_addr(MY_IP);
  152. NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
  153. NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);
  154. NutDnsConfig2(0, 0, inet_addr(DNSSERVERIP), 0);
  155. } else
  156. ip_addr = confnet.cdn_ip_addr;
  157. printf("%s ready\n", inet_ntoa(ip_addr));
  158. /*
  159. * Resolve hostname using DNS.
  160. */
  161. if ((rip = NutDnsGetHostByName((uint8_t*)INETSERVER)) != 0) {
  162. /*
  163. * Let's try a stdio stream first.
  164. */
  165. if ((sock = NutTcpCreateSocket()) != 0) {
  166. /*
  167. * Connect a HTTP server in the Internet.
  168. */
  169. printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
  170. if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
  171. /*
  172. * Assign a stream to our connected socket.
  173. */
  174. if ((stream = _fdopen((int) sock, "r+b")) != 0) {
  175. /*
  176. * Send HTTP request to the server.
  177. */
  178. fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL);
  179. fputs("User-Agent: Ethernut [en] (NutOS)\r\n", stream);
  180. fputs("\r\n", stream);
  181. fflush(stream);
  182. /*
  183. * Init measure values.
  184. */
  185. start_time = NutGetTickCount();
  186. total_bytes = 0;
  187. /*
  188. * Read server response and send it to the UART.
  189. */
  190. while (fgets(buff, sizeof(buff), stream)) {
  191. puts(buff);
  192. total_bytes += strlen(buff);
  193. }
  194. printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
  195. fclose(stream);
  196. } else
  197. puts("Creating stream device failed");
  198. } else {
  199. printf("Bad news, %s refuses the connection.\n", INETSERVER);
  200. }
  201. printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
  202. NutTcpCloseSocket(sock);
  203. }
  204. NutSleep(5000);
  205. /*
  206. * Now let's use native calls.
  207. */
  208. if ((sock = NutTcpCreateSocket()) != 0) {
  209. /*
  210. * Connect a HTTP server in the Internet.
  211. */
  212. printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
  213. if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
  214. /*
  215. * Send HTTP request to the server. NutTcpSend() doesn't
  216. * guarantee to send out all bytes, thus the loop.
  217. */
  218. strcpy(buff, "GET " INETURL " HTTP/1.0\r\nUser-Agent: Ethernut [en] (NutOS)\r\n\r\n");
  219. len = (int) strlen(buff);
  220. for (rc = 0; rc < len; rc += bite)
  221. if ((bite = NutTcpSend(sock, buff + rc, len - rc)) <= 0)
  222. break;
  223. /*
  224. * Init measure values.
  225. */
  226. start_time = NutGetTickCount();
  227. total_bytes = 0;
  228. /*
  229. * Read server response and send it to the UART.
  230. */
  231. while ((bite = NutTcpReceive(sock, buff, sizeof(buff) - 1)) > 0) {
  232. total_bytes += bite;
  233. buff[bite] = 0;
  234. puts(buff);
  235. }
  236. printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
  237. } else {
  238. printf("Bad news, %s refuses the connection.\n", INETSERVER);
  239. }
  240. printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
  241. NutTcpCloseSocket(sock);
  242. }
  243. } else
  244. printf("Great news, %s has been removed!\n", INETSERVER);
  245. #endif
  246. for (;;)
  247. NutSleep(1000);
  248. return 0;
  249. }