telnet.c

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - Testapplication
00004 
00005 File        : telnet.c
00006 
00007 Description : Simple Telnet Application to test TCP
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2005 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 
00029 #############################################################################*/
00030 
00031 
00032 #include "telnet.h"
00033 #include "dispatcher.h"
00034 #include "inet.h"
00035 #include "os_core.h"
00036 #include "nip_error.h"
00037 #include "net/net_if.h"
00038 #include "net/tcp.h"
00039 #include "net/numbers.h"
00040 #include "app/ports.h"
00041 
00042 #if TELNET_ENABLE == 1
00043 
00044 nip_tcp_sock_id_t        tel_listensock;
00045 nip_tcp_sock_id_t        tel_opensock = NIP_TCP_NO_SOCKET;
00046 nip_tcp_sock_id_t        tel_tmp_sock = NIP_TCP_NO_SOCKET;
00047 
00048 
00049 void telnet_start( void )
00050 {
00051         struct nip_tcp_sock_addr sockaddr;
00052 
00053         // open tcp socket
00054         nip_memset( &sockaddr, 0, sizeof( struct nip_tcp_sock_addr ) );
00055         sockaddr.port = NIP_TCP_PORT_TELNET;
00056         tel_listensock = nip_tcp_socket( &sockaddr, NIP_TCP_SOCK_FLG_LISTENING );
00057 }
00058 
00059 void telnet_check( void )
00060 {
00061         nip_mem_size_t     size;
00062         uint8_t            buf[10];
00063         uint8_t            *msg;
00064 
00065         // wait for incoming connection
00066         if ( tel_tmp_sock != NIP_TCP_NO_SOCKET )
00067         {
00068                 // wait for socket to be closed
00069                 if ( nip_tcp_read( tel_tmp_sock, buf, 10 ) == 0 && nip_error == NIP_E_EOF )
00070                 {
00071                         tel_tmp_sock = NIP_TCP_NO_SOCKET;
00072                 }
00073         }
00074         else
00075         {
00076                 tel_tmp_sock = nip_tcp_accept( tel_listensock );
00077                 if ( tel_tmp_sock != NIP_TCP_NO_SOCKET )
00078                 {
00079                         nip_tcp_write( tel_tmp_sock, (uint8_t*)"\r\n ~'\\._]  S u P   M o F u  [_./`~\r\n\r\n>", 38 );
00080 
00081                         if ( tel_opensock == NIP_TCP_NO_SOCKET )
00082                         {
00083                                 tel_opensock = tel_tmp_sock;
00084                                 tel_tmp_sock = NIP_TCP_NO_SOCKET;
00085                         }
00086                         else
00087                         {
00088                                 msg = (uint8_t*)"-out of connection space. SoRRy. :(\r\n";
00089                                 if ( 0 == nip_tcp_write( tel_tmp_sock, msg, 38 ) )
00090                                 {
00091                                         nip_tcp_write( tel_tmp_sock, msg, 38 );
00092                                 }
00093                                 nip_tcp_close( tel_tmp_sock );
00094                         }
00095                 }
00096         }
00097         if ( tel_opensock != NIP_TCP_NO_SOCKET )
00098         {
00099                 // check for incoming data
00100                 size = nip_tcp_read( tel_opensock, buf, 10 );
00101 
00102                 if ( size == 0 && nip_error == NIP_E_EOF )
00103                 {
00104                         // close connectin
00105                         nip_tcp_close( tel_opensock );
00106                         tel_opensock = NIP_TCP_NO_SOCKET;
00107                 }
00108                 else if ( size > 0 )
00109                 {
00110                         if ( size > 4 && nip_memcmp( buf, "quit", 4 ) == 0 )
00111                         {
00112                                 // user wants to close :(
00113                                 nip_tcp_write( tel_opensock, (uint8_t*)"ooohhhh.... :(\r\nbye\r\n", 21 );
00114                                 nip_tcp_close( tel_opensock );
00115                         }
00116                         else
00117                         {
00118                                 // yay.. we received something. :)
00119                                 nip_tcp_write( tel_opensock, (uint8_t*)"YaY. I received something.\r\n>", 29 );
00120                         }
00121                 }
00122         }
00123 
00124 }
00125 
00126 #endif /* NIP_TELNET_ENABLE */

Generated on Thu Jul 10 01:09:30 2008 for NIP by  doxygen 1.5.5