telnet.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * Test the telnet functions.
  3. *
  4. * Perform a test using the telnet functions.
  5. * This code exports two new global functions:
  6. *
  7. * void telnet_listenForClients(void (*callback)(uint8_t *buffer, size_t size))
  8. * void telnet_sendData(uint8_t *buffer, size_t size)
  9. *
  10. * For additional details and documentation see:
  11. * * Free book on ESP32 - https://leanpub.com/kolban-ESP32
  12. *
  13. *
  14. * Neil Kolban <kolban1@kolban.com>
  15. *
  16. * ****************************
  17. * Additional portions were taken from
  18. * https://github.com/PocketSprite/8bkc-sdk/blob/master/8bkc-components/8bkc-hal/vfs-stdout.c
  19. *
  20. */
  21. #include <stdlib.h> // Required for libtelnet.h
  22. #include <esp_log.h>
  23. #include "libtelnet.h"
  24. #include "stdbool.h"
  25. #include <lwip/def.h>
  26. #include <lwip/sockets.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include "sdkconfig.h"
  30. #include "freertos/ringbuf.h"
  31. #include "esp_app_trace.h"
  32. #include "telnet.h"
  33. #include "esp_vfs.h"
  34. #include "esp_vfs_dev.h"
  35. #include "esp_attr.h"
  36. #include "soc/uart_struct.h"
  37. #include "driver/uart.h"
  38. #include "config.h"
  39. #include "nvs_utilities.h"
  40. #include "platform_esp32.h"
  41. #include "trace.h"
  42. /************************************
  43. * Globals
  44. */
  45. #define TELNET_STACK_SIZE 8048
  46. #define TELNET_RX_BUF 1024
  47. const static char tag[] = "telnet";
  48. static int uart_fd=0;
  49. RingbufHandle_t buf_handle;
  50. SemaphoreHandle_t xSemaphore = NULL;
  51. static size_t send_chunk=300;
  52. static size_t log_buf_size=2000; //32-bit aligned size
  53. static bool bIsEnabled=false;
  54. static int partnerSocket=0;
  55. static telnet_t *tnHandle;
  56. /************************************
  57. * Forward declarations
  58. */
  59. static void telnet_task(void *data);
  60. static ssize_t stdout_read(int fd, void* data, size_t size);
  61. static int stdout_open(const char * path, int flags, int mode);
  62. static int stdout_close(int fd);
  63. static int stdout_fstat(int fd, struct stat * st);
  64. static ssize_t stdout_write(int fd, const void * data, size_t size);
  65. static char *eventToString(telnet_event_type_t type);
  66. static void handle_telnet_conn();
  67. static void process_logs( UBaseType_t bytes);
  68. static bool bMirrorToUART=false;
  69. struct telnetUserData {
  70. int sockfd;
  71. telnet_t *tnHandle;
  72. char * rxbuf;
  73. };
  74. void init_telnet(){
  75. char *val= get_nvs_value_alloc(NVS_TYPE_STR, "telnet_enable");
  76. if (!val || strlen(val) == 0 || !strcasestr("YXD",val) ) {
  77. ESP_LOGI(tag,"Telnet support disabled");
  78. if(val) free(val);
  79. return;
  80. }
  81. bMirrorToUART = strcasestr("D",val)!=NULL;
  82. FREE_AND_NULL(val);
  83. val=get_nvs_value_alloc(NVS_TYPE_STR, "telnet_block");
  84. if(val){
  85. send_chunk=atol(val);
  86. free(val);
  87. send_chunk=send_chunk>0?send_chunk:500;
  88. }
  89. val=get_nvs_value_alloc(NVS_TYPE_STR, "telnet_buffer");
  90. if(val){
  91. log_buf_size=atol(val);
  92. free(val);
  93. log_buf_size=log_buf_size>0?log_buf_size:4000;
  94. }
  95. // Create the semaphore to guard a shared resource.
  96. vSemaphoreCreateBinary( xSemaphore );
  97. // Redirect the output to our telnet handler as soon as possible
  98. StaticRingbuffer_t *buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  99. uint8_t *buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t)*log_buf_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  100. buf_handle = xRingbufferCreateStatic(log_buf_size, RINGBUF_TYPE_BYTEBUF, buffer_storage, buffer_struct);
  101. if (buf_handle == NULL) {
  102. ESP_LOGE(tag,"Failed to create ring buffer for telnet!");
  103. return;
  104. }
  105. ESP_LOGI(tag, "***Redirecting log output to telnet");
  106. const esp_vfs_t vfs = {
  107. .flags = ESP_VFS_FLAG_DEFAULT,
  108. .write = &stdout_write,
  109. .open = &stdout_open,
  110. .fstat = &stdout_fstat,
  111. .close = &stdout_close,
  112. .read = &stdout_read,
  113. };
  114. if(bMirrorToUART){
  115. uart_fd=open("/dev/uart/0", O_RDWR);
  116. }
  117. ESP_ERROR_CHECK(esp_vfs_register("/dev/pkspstdout", &vfs, NULL));
  118. freopen("/dev/pkspstdout", "w", stdout);
  119. freopen("/dev/pkspstdout", "w", stderr);
  120. bIsEnabled=true;
  121. }
  122. void start_telnet(void * pvParameter){
  123. static bool isStarted=false;
  124. StaticTask_t *xTaskBuffer = (StaticTask_t*) heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  125. StackType_t *xStack = malloc(TELNET_STACK_SIZE);
  126. if(!isStarted && bIsEnabled) {
  127. xTaskCreateStatic( (TaskFunction_t) &telnet_task, "telnet", TELNET_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, xTaskBuffer);
  128. isStarted=true;
  129. }
  130. }
  131. static void telnet_task(void *data) {
  132. int serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  133. struct sockaddr_in serverAddr;
  134. serverAddr.sin_family = AF_INET;
  135. serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  136. serverAddr.sin_port = htons(23);
  137. int rc = bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
  138. if (rc < 0) {
  139. ESP_LOGE(tag, "bind: %d (%s)", errno, strerror(errno));
  140. close(serverSocket);
  141. return;
  142. }
  143. rc = listen(serverSocket, 5);
  144. if (rc < 0) {
  145. ESP_LOGE(tag, "listen: %d (%s)", errno, strerror(errno));
  146. close(serverSocket);
  147. return;
  148. }
  149. while(1) {
  150. socklen_t len = sizeof(serverAddr);
  151. rc = accept(serverSocket, (struct sockaddr *)&serverAddr, &len);
  152. if (rc < 0 ){
  153. ESP_LOGE(tag, "accept: %d (%s)", errno, strerror(errno));
  154. return;
  155. }
  156. else {
  157. partnerSocket = rc;
  158. ESP_LOGD(tag, "We have a new client connection!");
  159. handle_telnet_conn();
  160. ESP_LOGD(tag, "Telnet connection terminated");
  161. }
  162. }
  163. close(serverSocket);
  164. vTaskDelete(NULL);
  165. }
  166. /**
  167. * Convert a telnet event type to its string representation.
  168. */
  169. static char *eventToString(telnet_event_type_t type) {
  170. switch(type) {
  171. case TELNET_EV_COMPRESS:
  172. return "TELNET_EV_COMPRESS";
  173. case TELNET_EV_DATA:
  174. return "TELNET_EV_DATA";
  175. case TELNET_EV_DO:
  176. return "TELNET_EV_DO";
  177. case TELNET_EV_DONT:
  178. return "TELNET_EV_DONT";
  179. case TELNET_EV_ENVIRON:
  180. return "TELNET_EV_ENVIRON";
  181. case TELNET_EV_ERROR:
  182. return "TELNET_EV_ERROR";
  183. case TELNET_EV_IAC:
  184. return "TELNET_EV_IAC";
  185. case TELNET_EV_MSSP:
  186. return "TELNET_EV_MSSP";
  187. case TELNET_EV_SEND:
  188. return "TELNET_EV_SEND";
  189. case TELNET_EV_SUBNEGOTIATION:
  190. return "TELNET_EV_SUBNEGOTIATION";
  191. case TELNET_EV_TTYPE:
  192. return "TELNET_EV_TTYPE";
  193. case TELNET_EV_WARNING:
  194. return "TELNET_EV_WARNING";
  195. case TELNET_EV_WILL:
  196. return "TELNET_EV_WILL";
  197. case TELNET_EV_WONT:
  198. return "TELNET_EV_WONT";
  199. case TELNET_EV_ZMP:
  200. return "TELNET_EV_ZMP";
  201. }
  202. return "Unknown type";
  203. } // eventToString
  204. /**
  205. * Telnet handler.
  206. */
  207. void process_received_data(const char * buffer, size_t size){
  208. //ESP_LOGD(tag, "received data, len=%d", event->data.size);
  209. char * command = malloc(size+1);
  210. const char * c=buffer;
  211. // scrub from any escape command
  212. if(*c == '\e'){
  213. while(*(c++) !='n'){
  214. --size;
  215. };
  216. --size;
  217. }
  218. memcpy(command,c,size);
  219. command[size]='\0';
  220. if(command[0]!='\r' && command[0]!='\n'){
  221. // echo the command buffer out to uart and run
  222. if(bMirrorToUART){
  223. write(uart_fd, command, size);
  224. }
  225. run_command((char *)command);
  226. }
  227. free(command);
  228. }
  229. static void handle_telnet_events(
  230. telnet_t *thisTelnet,
  231. telnet_event_t *event,
  232. void *userData) {
  233. int rc;
  234. struct telnetUserData *telnetUserData = (struct telnetUserData *)userData;
  235. switch(event->type) {
  236. case TELNET_EV_SEND:
  237. rc = send(telnetUserData->sockfd, event->data.buffer, event->data.size, 0);
  238. if (rc < 0) {
  239. //printf("ERROR: (telnet) send: %d (%s)", errno, strerror(errno));
  240. }
  241. break;
  242. case TELNET_EV_DATA:
  243. process_received_data(event->data.buffer, event->data.size);
  244. break;
  245. case TELNET_EV_TTYPE:
  246. printf("telnet event: %s\n", eventToString(event->type));
  247. telnet_ttype_send(telnetUserData->tnHandle);
  248. break;
  249. default:
  250. printf("telnet event: %s\n", eventToString(event->type));
  251. break;
  252. } // End of switch event type
  253. } // myhandle_telnet_events
  254. static void process_logs(UBaseType_t count){
  255. //Receive an item from no-split ring buffer
  256. size_t item_size;
  257. UBaseType_t uxItemsWaiting;
  258. UBaseType_t uxBytesToSend=count;
  259. vRingbufferGetInfo(buf_handle, NULL, NULL, NULL, NULL, &uxItemsWaiting);
  260. if(count == 0){
  261. // this sends the entire buffer to the remote client
  262. uxBytesToSend = uxItemsWaiting;
  263. }
  264. if( partnerSocket ==0 && (uxItemsWaiting*100 / log_buf_size) <75){
  265. // We still have some room in the ringbuffer and there's no telnet
  266. // connection yet, so bail out for now.
  267. //printf("%s() Log buffer used %u of %u bytes used\n", __FUNCTION__, uxItemsWaiting, log_buf_size);
  268. return;
  269. }
  270. while(uxBytesToSend>0){
  271. char *item = (char *)xRingbufferReceiveUpTo(buf_handle, &item_size, pdMS_TO_TICKS(50), uxBytesToSend);
  272. //Check received data
  273. if (item != NULL) {
  274. uxBytesToSend-=item_size;
  275. if(partnerSocket!=0){
  276. telnet_send_text(tnHandle, item, item_size);
  277. }
  278. //Return Item
  279. vRingbufferReturnItem(buf_handle, (void *)item);
  280. }
  281. else{
  282. break;
  283. }
  284. }
  285. }
  286. static void handle_telnet_conn() {
  287. static const telnet_telopt_t my_telopts[] = {
  288. { TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO },
  289. { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
  290. { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
  291. { TELNET_TELOPT_ZMP, TELNET_WONT, TELNET_DO },
  292. { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
  293. { TELNET_TELOPT_BINARY, TELNET_WILL, TELNET_DO },
  294. { TELNET_TELOPT_NAWS, TELNET_WILL, TELNET_DONT },
  295. {TELNET_TELOPT_LINEMODE, TELNET_WONT, TELNET_DO },
  296. { -1, 0, 0 }
  297. };
  298. struct telnetUserData *pTelnetUserData = (struct telnetUserData *)malloc(sizeof(struct telnetUserData));
  299. tnHandle = telnet_init(my_telopts, handle_telnet_events, 0, pTelnetUserData);
  300. pTelnetUserData->rxbuf = (char *) heap_caps_malloc(TELNET_RX_BUF, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  301. pTelnetUserData->tnHandle = tnHandle;
  302. pTelnetUserData->sockfd = partnerSocket;
  303. // flush all the log buffer on connect
  304. process_logs(0);
  305. while(1) {
  306. //ESP_LOGD(tag, "waiting for data");
  307. ssize_t len = recv(partnerSocket, pTelnetUserData->rxbuf, TELNET_RX_BUF, MSG_DONTWAIT);
  308. if (len >0 ) {
  309. telnet_recv(tnHandle, pTelnetUserData->rxbuf, len);
  310. }
  311. else if (errno != EAGAIN && errno !=EWOULDBLOCK ){
  312. telnet_free(tnHandle);
  313. tnHandle = NULL;
  314. free(pTelnetUserData->rxbuf);
  315. pTelnetUserData->rxbuf=NULL;
  316. free(pTelnetUserData);
  317. partnerSocket = 0;
  318. return;
  319. }
  320. process_logs(send_chunk);
  321. taskYIELD();
  322. }
  323. } // handle_telnet_conn
  324. // ******************* stdout/stderr Redirection to ringbuffer
  325. static ssize_t stdout_write(int fd, const void * data, size_t size) {
  326. if (xSemaphoreTake(xSemaphore, (TickType_t) 10) == pdTRUE) {
  327. // #1 Write to ringbuffer
  328. if (buf_handle == NULL) {
  329. printf("%s() ABORT. file handle _log_remote_fp is NULL\n",
  330. __FUNCTION__);
  331. } else {
  332. //Send an item
  333. UBaseType_t res = xRingbufferSend(buf_handle, data, size,
  334. pdMS_TO_TICKS(100));
  335. if (res != pdTRUE) {
  336. // flush some entries
  337. process_logs(size);
  338. res = xRingbufferSend(buf_handle, data, size,
  339. pdMS_TO_TICKS(100));
  340. if (res != pdTRUE) {
  341. printf("%s() ABORT. Unable to store log entry in buffer\n",
  342. __FUNCTION__);
  343. }
  344. }
  345. }
  346. xSemaphoreGive(xSemaphore);
  347. } else {
  348. // We could not obtain the semaphore and can therefore not access
  349. // the shared resource safely.
  350. }
  351. return bMirrorToUART?write(uart_fd, data, size):true;
  352. }
  353. static ssize_t stdout_read(int fd, void* data, size_t size) {
  354. return read(fd, data, size);
  355. }
  356. static int stdout_open(const char * path, int flags, int mode) {
  357. return 0;
  358. }
  359. static int stdout_close(int fd) {
  360. return 0;
  361. }
  362. static int stdout_fstat(int fd, struct stat * st) {
  363. st->st_mode = S_IFCHR;
  364. return 0;
  365. }