netdebug.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2001-2003 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. /*!
  36. * \file net/netdebug.c
  37. * \brief Network debug helper functions.
  38. *
  39. * \verbatim
  40. * $Id: netdebug.c 5541 2014-01-10 17:10:49Z olereinhardt $
  41. * \endverbatim
  42. */
  43. #include <arpa/inet.h>
  44. #include <netinet/in.h>
  45. #include <netinet/ip.h>
  46. #include <netinet/icmp.h>
  47. #include <netinet/ip_icmp.h>
  48. #include <netinet/ipcsum.h>
  49. #include <net/netdebug.h>
  50. #include <sys/socket.h>
  51. extern TCPSOCKET *tcpSocketList;
  52. extern UDPSOCKET *udpSocketList;
  53. FILE *__tcp_trs; /*!< \brief TCP trace output stream. */
  54. uint_fast8_t __tcp_trf; /*!< \brief TCP trace flags. */
  55. void NutDumpTcpHeader(FILE * stream, char * ds, TCPSOCKET * sock, NETBUF * nb)
  56. {
  57. static const char fmt[] PROGMEM = "%s%p[%u]-SEQ(%lx)";
  58. TCPHDR *th = (TCPHDR *) nb->nb_tp.vp;
  59. fprintf_P(stream, fmt, ds, sock, (unsigned int)nb->nb_ap.sz, ntohl(th->th_seq));
  60. if (th->th_flags & TH_ACK)
  61. fprintf(stream, "-ACK(%lx)", ntohl(th->th_ack));
  62. if (th->th_flags & TH_FIN)
  63. fputs("-FIN", stream);
  64. if (th->th_flags & TH_SYN)
  65. fputs("-SYN", stream);
  66. if (th->th_flags & TH_RST)
  67. fputs("-RST", stream);
  68. if (th->th_flags & TH_PUSH)
  69. fputs("-PSH", stream);
  70. if (th->th_flags & TH_URG)
  71. fputs("-URG", stream);
  72. fputs("\n", stream);
  73. }
  74. void NutDumpSockState(FILE * stream, uint8_t state, char * lead, char * trail)
  75. {
  76. if (lead)
  77. fputs(lead, stream);
  78. switch (state) {
  79. case TCPS_LISTEN:
  80. fputs("LISTEN", stream);
  81. break;
  82. case TCPS_SYN_SENT:
  83. fputs("SYNSENT", stream);
  84. break;
  85. case TCPS_SYN_RECEIVED:
  86. fputs("SYNRCVD", stream);
  87. break;
  88. case TCPS_ESTABLISHED:
  89. fputs("ESTABL", stream);
  90. break;
  91. case TCPS_FIN_WAIT_1:
  92. fputs("FINWAIT1", stream);
  93. break;
  94. case TCPS_FIN_WAIT_2:
  95. fputs("FINWAIT2", stream);
  96. break;
  97. case TCPS_CLOSE_WAIT:
  98. fputs("CLOSEWAIT", stream);
  99. break;
  100. case TCPS_CLOSING:
  101. fputs("CLOSING", stream);
  102. break;
  103. case TCPS_LAST_ACK:
  104. fputs("LASTACK", stream);
  105. break;
  106. case TCPS_TIME_WAIT:
  107. fputs("TIMEWAIT", stream);
  108. break;
  109. case TCPS_CLOSED:
  110. fputs("CLOSED", stream);
  111. break;
  112. case TCPS_DESTROY:
  113. fputs("DESTROY", stream);
  114. default:
  115. fputs("?UNK?", stream);
  116. break;
  117. }
  118. if (trail)
  119. fputs(trail, stream);
  120. }
  121. void NutDumpSocketList(FILE * stream)
  122. {
  123. TCPSOCKET *ts;
  124. UDPSOCKET *us;
  125. static const char fmt1[] PROGMEM = "%10p TCP %15s:%-6u ";
  126. static const char fmt2[] PROGMEM = "%10p UDP %6u\r\n";
  127. fputs("\r\nSocket Typ Local Remote State\n", stream);
  128. /* 1234567890 123 123456789012345:123456 123456789012345:123456 */
  129. for (ts = tcpSocketList; ts; ts = ts->so_next) {
  130. fprintf_P(stream, fmt1, ts, inet_ntoa(ts->so_local_addr), ntohs(ts->so_local_port));
  131. fprintf(stream, "%15s:%-6u ", inet_ntoa(ts->so_remote_addr), ntohs(ts->so_remote_port));
  132. NutDumpSockState(stream, ts->so_state, 0, "\r\n");
  133. }
  134. for (us = udpSocketList; us; us = us->so_next) {
  135. fprintf_P(stream, fmt2, us, ntohs(us->so_local_port));
  136. }
  137. }
  138. /*!
  139. * \brief Control TCP tracing.
  140. *
  141. * \param stream Pointer to a previously opened stream or null to leave
  142. * it unchanged.
  143. * \param flags Flags to enable specific traces.
  144. */
  145. void NutTraceTcp(FILE * stream, uint8_t flags)
  146. {
  147. if (stream)
  148. __tcp_trs = stream;
  149. if (__tcp_trs)
  150. __tcp_trf = flags;
  151. else
  152. __tcp_trf = 0;
  153. }