Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
TelnetServer.cpp
00001 /* 00002 Original autor: Francisco 00003 Modificacao do uso da rede: rebonatto 00004 */ 00005 00006 00007 #include "TelnetServer.h" 00008 00009 #include "Split.h" 00010 00011 #define OPT_WILL 251 00012 #define OPT_WONT 252 00013 #define OPT_DO 253 00014 #define OPT_DONT 254 00015 00016 char* welcome_msg = "Protegemed - welcome to telnet\r\nType 'help' for a list of commands\r\n"; 00017 char* prompt_msg = "\r\n$ "; 00018 char iac_will_echo[] = {0xFF,0xFB,0x01}; 00019 char erase_cmd[] = { 0x1B,0x5B,0x44,0x1B,0x5B,0x4B}; 00020 00021 const struct telnet_cmd_handler TelnetServer::cmds[] = { 00022 {"exit",(cmd_function)-1}, 00023 {"help",TelnetServer::HelpCommand}, 00024 {"reset",TelnetServer::ResetCommand}, 00025 {"remove",TelnetServer::RemoveCommand}, 00026 {"listparam",TelnetServer::ListParamCommand}, 00027 {"getparam",TelnetServer::GetParamCommand}, 00028 {"setparam",TelnetServer::SetParamCommand}, 00029 {"version",TelnetServer::VersionCommand}, 00030 {"update",TelnetServer::UpdateCommand}, 00031 {"",(cmd_function)0} 00032 }; 00033 00034 void TelnetServer::TelnetServer_Thread(void const* arg) 00035 { 00036 int r=0; 00037 printf("Telnet Thread starting... \r\nCommands up to 100 chars\n"); 00038 00039 TCPSocketServer server; 00040 00041 server.bind(23); 00042 server.listen(); 00043 00044 while(1) 00045 { 00046 TCPSocketConnection* conn ; 00047 TCPSocketConnection aux; 00048 r = server.accept(aux); 00049 conn = &aux; 00050 printf("Aceitou conexao \n"); 00051 if (r != 0){ 00052 printf("Error in connection\n"); 00053 exit(1); 00054 } 00055 00056 TelnetSession(conn); 00057 printf("Fechou\n"); 00058 conn->close(); 00059 } 00060 } 00061 00062 void TelnetServer::TelnetSession(TCPSocketConnection *conn) 00063 { 00064 int r; 00065 int buffer_ptr=0; 00066 00067 printf("Connected to %s:%d\n",conn->get_address(),conn->get_port()); 00068 00069 conn->send(welcome_msg,strlen(welcome_msg)); 00070 conn->send(prompt_msg,strlen(prompt_msg)); 00071 00072 conn->set_blocking(true); 00073 00074 while(1) 00075 { 00076 char buf_rec[260]; 00077 unsigned char buf[260]; 00078 char buffer[260]; 00079 00080 //printf("Vai recevber\n"); 00081 r = conn->receive(buf_rec, strlen(buf_rec) ); 00082 00083 /* need memcpy becouse commands are ready to unsigned char and tcp receive char */ 00084 memcpy(buf, buf_rec, r); 00085 00086 //printf("Receive %d\n", r); 00087 00088 if(r == -1) 00089 { 00090 printf("Error %d %s\n", r, buf); 00091 break; 00092 } 00093 00094 if (r > 120){ 00095 printf("Max command length = 100 chars\n"); 00096 //send 00097 break; 00098 } 00099 00100 //printf("Got Here\n"); 00101 00102 for(int i=0;i<r;i++) 00103 { 00104 //check if it is a printable or any of the line control chars 00105 if((buf[i]>31 && buf[i] < 128) || buf[i]=='\r' || buf[i]=='\n' || buf[i]=='\t') 00106 { 00107 //append to the buffer 00108 buffer[buffer_ptr] = buf[i]; 00109 buffer_ptr++; 00110 } 00111 else if(buf[i] == '\b') //backspace 00112 { 00113 //erases the character from the command buffer 00114 if(buffer_ptr > 0) 00115 { 00116 buffer_ptr--; 00117 } 00118 //resets m variable state (will not cause error on the next block of code 00119 00120 } 00121 else if((int)buf[i] == 255) //IAC - Interpret As Command 00122 { 00123 if((int)buf[i+1] >= 251) 00124 { 00125 option_negotiator(conn,buf[i+1],buf[i+2]); 00126 i+=2; 00127 } 00128 else 00129 i+=1; 00130 } 00131 } 00132 00133 //detect a carriage return and line feed sequence. Trigger command processor 00134 if(buffer_ptr >= 2) 00135 { 00136 if(buffer[buffer_ptr-1] == '\n' && buffer[buffer_ptr-2] == '\r') 00137 { 00138 char **command; 00139 int command_count; 00140 00141 buffer[buffer_ptr-2] = '\0'; 00142 command_count = split((char*)buffer," ",&command); 00143 00144 printf("Command found: %s\n", command[0]); 00145 00146 //must find a function in the table and then execute it, passing the command array as an argument 00147 for(int i=0;cmds[i].pfn != 0;i++) 00148 { 00149 if(!strcasecmp(command[0],cmds[i].command_name)) 00150 { 00151 if((int)cmds[i].pfn == -1)//exit cmd 00152 { 00153 //delete buffer; 00154 return; 00155 } 00156 else 00157 { 00158 cmds[i].pfn(conn,command,command_count); 00159 break; 00160 } 00161 } 00162 } 00163 00164 //write the prompt 00165 conn->send(prompt_msg,strlen(prompt_msg)); 00166 buffer_ptr=0; 00167 } 00168 } 00169 } 00170 //delete buffer; 00171 } 00172 00173 void TelnetServer::option_negotiator(TCPSocketConnection *conn,unsigned char opt_cmd,unsigned char opt_param) 00174 { 00175 char opt[3]={0,0,0}; 00176 00177 if(opt_param == 1 && (opt_cmd == OPT_DO || opt_cmd == OPT_DONT)) //response for our will echo 00178 { 00179 printf("HERE"); 00180 return; 00181 } 00182 if(opt_cmd == OPT_DO) //every other option that it will ask to us to do, we won't 00183 { 00184 opt[0] = 255; 00185 opt[1] = OPT_WONT; 00186 opt[2] = opt_param; 00187 conn->send(opt,3); 00188 } 00189 else if(opt_cmd == OPT_WILL && opt_param==3) //OK to supperss go ahead 00190 { 00191 opt[0] = 255; 00192 opt[1] = OPT_DO; 00193 opt[2] = opt_param; 00194 conn->send(opt,3); 00195 } 00196 else if(opt_cmd == OPT_WILL) //every other option that it will ask do, we don't 00197 { 00198 opt[0] = 255; 00199 opt[1] = OPT_DONT; 00200 opt[2] = opt_param; 00201 conn->send(opt,3); 00202 00203 } 00204 00205 } 00206
Generated on Fri Jul 22 2022 19:25:55 by
