00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00066 if ( tel_tmp_sock != NIP_TCP_NO_SOCKET )
00067 {
00068
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
00100 size = nip_tcp_read( tel_opensock, buf, 10 );
00101
00102 if ( size == 0 && nip_error == NIP_E_EOF )
00103 {
00104
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
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
00119 nip_tcp_write( tel_opensock, (uint8_t*)"YaY. I received something.\r\n>", 29 );
00120 }
00121 }
00122 }
00123
00124 }
00125
00126 #endif