icmp-udp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*!
  2. * Copyright (C) 2009 by Ole Reinhardt <ole.reinhardt@thermotemp.de>.
  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. * $Id: icmp-udp.c 5079 2013-03-21 19:13:56Z olereinhardt $
  36. */
  37. /*!
  38. * \example icmp-udp/icmp-udp.c
  39. *
  40. * This sample demonstrates the icmp error handling for udp sockets.
  41. * NUT_UDP_ICMP_EXCLUDE should not be set in the configurator to use
  42. * icmp support on UDP sockets.
  43. *
  44. * Connect the RS232 port of the Ethernut with a free COM
  45. * port of your PC and run a terminal emulator at 115200 Baud.
  46. *
  47. * Configure MY_IP, MY_MASK, and MY_GATE approriate. Configure UDP_ECHO_IP
  48. * to the IP of a host running an udp echo server.
  49. *
  50. * The program will try to send some UDP packets to the given
  51. * destination address on port 7 (UDP echo port) and will try to
  52. * read back the sended data.
  53. *
  54. * You can run the udp_echo java program in this directory with admin (root)
  55. * priviledges to have a simple udp echo server on your pc.
  56. *
  57. * Just start the program with 'java udp_echo.class'
  58. * This program will echo back every data packet received on udp port 7.
  59. * The program can be stoped with <ctrl>+<c>
  60. *
  61. * If the udp_echo program (or any other udp echo server) is not running
  62. * you'll receive ICMP destination unreachable messages. Which will
  63. * be noticed from this demo app on the next send or receive call.
  64. */
  65. #define MY_MAC {0x00,0x06,0x98,0x20,0x00,0x00}
  66. #define MY_IP "192.168.1.100"
  67. #define MY_MASK "255.255.255.0"
  68. #define MY_GATE "192.168.1.254"
  69. #define UDP_ECHO_IP "192.168.1.103"
  70. #define UDP_ECHO_PORT 7
  71. #include <string.h>
  72. #include <stdio.h>
  73. #include <io.h>
  74. //#include <inttypes.h>
  75. #include <errno.h>
  76. #include <dev/board.h>
  77. #include <sys/thread.h>
  78. #include <sys/timer.h>
  79. #include <sys/socket.h>
  80. #include <sys/confnet.h>
  81. #include <arpa/inet.h>
  82. #include <net/route.h>
  83. #define UDP_BUFF_SIZE 256
  84. static char send_buffer[UDP_BUFF_SIZE];
  85. static char rcv_buffer[UDP_BUFF_SIZE];
  86. static uint8_t my_mac[] = MY_MAC;
  87. #ifdef DEV_ETHER
  88. /* Print error message */
  89. void print_udp_icmp_error(uint32_t remote_ip, uint16_t remote_port, int error)
  90. {
  91. printf("ICMP destination unreachable received for remote ip: %s\r\nremote port: %d, errno: %d -- ",
  92. inet_ntoa(remote_ip), remote_port, error);
  93. switch (error) {
  94. case ENETUNREACH:
  95. printf("Destination network unreachable\r\n");
  96. break;
  97. case EHOSTUNREACH:
  98. printf("Destination host unreachable\r\n");
  99. break;
  100. case ENOPROTOOPT:
  101. printf("Destination protocol unreachable\r\n");
  102. break;
  103. case ECONNREFUSED:
  104. printf("Destination port unreachable / connection refused\r\n");
  105. break;
  106. case EMSGSIZE:
  107. printf("Fragmentation required, and DF flag set\r\n");
  108. break;
  109. case EOPNOTSUPP:
  110. printf("Source route failed\r\n");
  111. break;
  112. case EHOSTDOWN:
  113. printf("Destination host unknown or down\r\n");
  114. break;
  115. default:
  116. printf("Other error\r\n");
  117. }
  118. }
  119. THREAD(UDPReceiver, arg)
  120. {
  121. uint32_t remote_ip;
  122. uint16_t remote_port;
  123. UDPSOCKET *socket = (UDPSOCKET*) arg;
  124. int rc;
  125. while (1) {
  126. rc = NutUdpReceiveFrom(socket, &remote_ip, &remote_port, rcv_buffer, UDP_BUFF_SIZE, 2000);
  127. if (rc == 0) {
  128. printf("<-- Timeout (2 sec.) on UDP receive\r\n");
  129. } else
  130. if (rc < 0) {
  131. #ifndef NUT_UDP_ICMP_EXCLUDE
  132. int error;
  133. error = NutUdpError(socket, &remote_ip, &remote_port);
  134. print_udp_icmp_error(remote_ip, remote_port, error);
  135. #endif
  136. } else {
  137. printf("<-- Received packet: \"%s\"\r\n", rcv_buffer);
  138. }
  139. }
  140. }
  141. #endif /* DEV_ETHER */
  142. /*
  143. * Main application routine.
  144. *
  145. */
  146. int main(void)
  147. {
  148. uint32_t baud = 115200;
  149. UDPSOCKET *socket;
  150. uint32_t ip_addr;
  151. uint32_t ip_udp_echo;
  152. int rc;
  153. int packet_nr;
  154. uint16_t length;
  155. /*
  156. * Initialize the uart device.
  157. */
  158. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  159. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  160. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  161. puts("Demo for ICMP support in UDP sockets...\r\n");
  162. #ifdef DEV_ETHER
  163. #ifdef NUT_UDP_ICMP_EXCLUDE
  164. #warning ICMP support for UDP sockets not enabled in the configurator, please disable NUT_UDP_ICMP_EXCLUDE
  165. puts("ICMP support for UDP sockets not enabled in the configurator\r\n");
  166. puts("Please disable NUT_UDP_ICMP_EXCLUDE\r\n");
  167. #endif
  168. /*
  169. * Register the network device.
  170. */
  171. puts("Configuring Ethernet interface");
  172. NutRegisterDevice(&DEV_ETHER, 0, 0);
  173. ip_addr = inet_addr(MY_IP);
  174. NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
  175. NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);
  176. printf("%s ready\r\n", inet_ntoa(ip_addr));
  177. socket = NutUdpCreateSocket(UDP_ECHO_PORT);
  178. if (socket == 0) {
  179. printf("Could not create UDP socket in port '%d'\r\n", UDP_ECHO_PORT);
  180. puts("Demo halted...\r\n");
  181. while (1);
  182. } else {
  183. printf("Successfully created UDP socket on port '%d'\r\n", UDP_ECHO_PORT);
  184. length = UDP_BUFF_SIZE;
  185. if (NutUdpSetSockOpt(socket, SO_RCVBUF, &length, sizeof(length))) {;
  186. printf("Could not set UDP receive buffer size (%d)\r\n", length);
  187. puts("Demo halted...\r\n");
  188. while (1);
  189. }
  190. }
  191. if (NutThreadCreate("RCV", UDPReceiver, (void*)socket, 1024) == 0) {
  192. puts("Could not start receiver thread\r\n");
  193. puts("Demo halted...\r\n");
  194. while (1);
  195. } else {
  196. puts("Receiver thread started\r\n");
  197. }
  198. puts("Starting echo test (1 packet / second)\r\n");
  199. ip_udp_echo = inet_addr(UDP_ECHO_IP);
  200. packet_nr = 0;
  201. while (1) {
  202. packet_nr ++;
  203. sprintf(send_buffer, "Packet: %d", packet_nr);
  204. rc = NutUdpSendTo(socket, ip_udp_echo, UDP_ECHO_PORT, send_buffer, length);
  205. printf("--> Sended packet: \"%s\", to %s, rc: %d\r\n", send_buffer, inet_ntoa(ip_udp_echo), rc);
  206. if (rc < 0) {
  207. #ifndef NUT_UDP_ICMP_EXCLUDE
  208. int error;
  209. uint32_t remote_ip;
  210. uint16_t remote_port;
  211. error = NutUdpError(socket, &remote_ip, &remote_port);
  212. print_udp_icmp_error(remote_ip, remote_port, error);
  213. #endif
  214. }
  215. NutSleep(1000);
  216. }
  217. #endif /* DEV_ETHER */
  218. return 0;
  219. }