Nut/OS  4.10.3
API Reference
netdebug.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  * Revision 1.11  2009/01/17 11:26:51  haraldkipp
00037  * Getting rid of two remaining BSD types in favor of stdint.
00038  * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
00039  *
00040  * Revision 1.10  2008/08/11 07:00:31  haraldkipp
00041  * BSD types replaced by stdint types (feature request #1282721).
00042  *
00043  * Revision 1.9  2008/04/18 13:32:00  haraldkipp
00044  * Changed size parameter from u_short to int, which is easier to handle
00045  * for 32-bit targets. You need to recompile your ARM code. No impact on
00046  * AVR expected
00047  * I changed u_int to int at some places to avoid some warnings during
00048  * compilation of Nut/Net.
00049  * libs.
00050  *
00051  * Revision 1.8  2007/07/17 18:31:44  haraldkipp
00052  * Output strings must be signed characters. Fixed provided by Michael Mueller.
00053  *
00054  * Revision 1.7  2006/03/16 15:25:35  haraldkipp
00055  * Changed human readable strings from u_char to char to stop GCC 4 from
00056  * nagging about signedness.
00057  *
00058  * Revision 1.6  2005/01/13 18:48:53  haraldkipp
00059  * Compiler warnings avoided.
00060  *
00061  * Revision 1.5  2005/01/02 10:07:10  haraldkipp
00062  * Replaced platform dependant formats in debug outputs.
00063  *
00064  * Revision 1.4  2004/04/07 12:13:58  haraldkipp
00065  * Matthias Ringwald's *nix emulation added
00066  *
00067  * Revision 1.3  2004/03/19 09:05:08  jdubiec
00068  * Fixed format strings declarations for AVR.
00069  *
00070  * Revision 1.2  2004/03/16 16:48:45  haraldkipp
00071  * Added Jan Dubiec's H8/300 port.
00072  *
00073  * Revision 1.1.1.1  2003/05/09 14:41:34  haraldkipp
00074  * Initial using 3.2.1
00075  *
00076  * Revision 1.8  2003/05/06 18:16:14  harald
00077  * Separate PPP debug module added.
00078  *
00079  * Revision 1.7  2003/02/04 18:14:57  harald
00080  * Version 3 released
00081  *
00082  * Revision 1.6  2002/10/29 15:32:24  harald
00083  * PPP support
00084  *
00085  * Revision 1.5  2002/06/26 17:29:36  harald
00086  * First pre-release with 2.4 stack
00087  *
00088  */
00089 
00090 #include <arpa/inet.h>
00091 #include <netinet/in.h>
00092 #include <netinet/ip.h>
00093 #include <netinet/icmp.h>
00094 #include <netinet/ip_icmp.h>
00095 #include <netinet/ipcsum.h>
00096 #include <net/netdebug.h>
00097 #include <sys/socket.h>
00098 
00099 extern TCPSOCKET *tcpSocketList;
00100 extern UDPSOCKET *udpSocketList;
00101 
00102 FILE *__tcp_trs;                
00103 uint_fast8_t __tcp_trf;               
00105 void NutDumpTcpHeader(FILE * stream, char * ds, TCPSOCKET * sock, NETBUF * nb)
00106 {
00107     static prog_char fmt[] = "%s%p[%u]-SEQ(%lx)";
00108     TCPHDR *th = (TCPHDR *) nb->nb_tp.vp;
00109 
00110     fprintf_P(stream, fmt, ds, sock, (unsigned int)nb->nb_ap.sz, ntohl(th->th_seq));
00111     if (th->th_flags & TH_ACK)
00112         fprintf(stream, "-ACK(%lx)", ntohl(th->th_ack));
00113     if (th->th_flags & TH_FIN)
00114         fputs("-FIN", stream);
00115     if (th->th_flags & TH_SYN)
00116         fputs("-SYN", stream);
00117     if (th->th_flags & TH_RST)
00118         fputs("-RST", stream);
00119     if (th->th_flags & TH_PUSH)
00120         fputs("-PSH", stream);
00121     if (th->th_flags & TH_URG)
00122         fputs("-URG", stream);
00123     fputs("\n", stream);
00124 }
00125 
00126 void NutDumpSockState(FILE * stream, uint8_t state, char * lead, char * trail)
00127 {
00128     if (lead)
00129         fputs(lead, stream);
00130     switch (state) {
00131     case TCPS_LISTEN:
00132         fputs("LISTEN", stream);
00133         break;
00134     case TCPS_SYN_SENT:
00135         fputs("SYNSENT", stream);
00136         break;
00137     case TCPS_SYN_RECEIVED:
00138         fputs("SYNRCVD", stream);
00139         break;
00140     case TCPS_ESTABLISHED:
00141         fputs("ESTABL", stream);
00142         break;
00143     case TCPS_FIN_WAIT_1:
00144         fputs("FINWAIT1", stream);
00145         break;
00146     case TCPS_FIN_WAIT_2:
00147         fputs("FINWAIT2", stream);
00148         break;
00149     case TCPS_CLOSE_WAIT:
00150         fputs("CLOSEWAIT", stream);
00151         break;
00152     case TCPS_CLOSING:
00153         fputs("CLOSING", stream);
00154         break;
00155     case TCPS_LAST_ACK:
00156         fputs("LASTACK", stream);
00157         break;
00158     case TCPS_TIME_WAIT:
00159         fputs("TIMEWAIT", stream);
00160         break;
00161     case TCPS_CLOSED:
00162         fputs("CLOSED", stream);
00163         break;
00164     default:
00165         fputs("?UNK?", stream);
00166         break;
00167     }
00168     if (trail)
00169         fputs(trail, stream);
00170 }
00171 
00172 
00173 void NutDumpSocketList(FILE * stream)
00174 {
00175     TCPSOCKET *ts;
00176     UDPSOCKET *us;
00177 
00178     static prog_char fmt1[] = "%10p TCP %15s:%-6u ";
00179     static prog_char fmt2[] = "%10p UDP %6u\r\n";
00180 
00181     fputs("\r\nSocket     Typ Local                  Remote                 State\n", stream);
00182     /*         1234567890 123 123456789012345:123456 123456789012345:123456 */
00183 
00184     for (ts = tcpSocketList; ts; ts = ts->so_next) {
00185         fprintf_P(stream, fmt1, ts, inet_ntoa(ts->so_local_addr), ntohs(ts->so_local_port));
00186         fprintf(stream, "%15s:%-6u ", inet_ntoa(ts->so_remote_addr), ntohs(ts->so_remote_port));
00187         NutDumpSockState(stream, ts->so_state, 0, "\r\n");
00188     }
00189     for (us = udpSocketList; us; us = us->so_next) {
00190         fprintf_P(stream, fmt2, us, ntohs(us->so_local_port));
00191     }
00192 }
00193 
00194 
00202 void NutTraceTcp(FILE * stream, uint8_t flags)
00203 {
00204     if (stream)
00205         __tcp_trs = stream;
00206     if (__tcp_trs)
00207         __tcp_trf = flags;
00208     else
00209         __tcp_trf = 0;
00210 }