1 /*--------------------------------------------------------------------
\r
2 * TITLE: Plasma TCP/IP Network Utilities
\r
3 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
\r
4 * DATE CREATED: 4/20/07
\r
5 * FILENAME: netutil.c
\r
6 * PROJECT: Plasma CPU core
\r
7 * COPYRIGHT: Software placed into the public domain by the author.
\r
8 * Software 'as is' without warranty. Author liable for nothing.
\r
10 * Plasma FTP server and FTP client and TFTP server and client
\r
11 * and Telnet server.
\r
12 *--------------------------------------------------------------------*/
\r
13 #undef INCLUDE_FILESYS
\r
14 #define INCLUDE_FILESYS
\r
26 static void ConsoleRun(IPSocket *socket, char *argv[]);
\r
29 //******************* FTP Server ************************
\r
32 int ip, port, bytes, done, canReceive;
\r
36 static void FtpdSender(IPSocket *socket)
\r
38 unsigned char buf[600];
\r
39 int i, bytes, bytes2;
\r
40 FtpdInfo *info = (FtpdInfo*)socket->userPtr;
\r
42 if(info == NULL || info->done)
\r
44 fseek(info->file, info->bytes, 0);
\r
45 for(i = 0; i < 100000; ++i)
\r
47 bytes = fread(buf, 1, 512, info->file);
\r
48 bytes2 = IPWrite(socket, buf, bytes);
\r
49 info->bytes += bytes2;
\r
57 IPPrintf(info->socket, "226 Done\r\n");
\r
64 static void FtpdReceiver(IPSocket *socket)
\r
66 unsigned char buf[600];
\r
67 int bytes, state = socket->state;
\r
68 FtpdInfo *info = (FtpdInfo*)socket->userPtr;
\r
70 if(info == NULL || info->done)
\r
74 bytes = IPRead(socket, buf, sizeof(buf));
\r
75 fwrite(buf, 1, bytes, info->file);
\r
82 IPPrintf(info->socket, "226 Done\r\n");
\r
89 static void FtpdServer(IPSocket *socket)
\r
93 int ip0, ip1, ip2, ip3, port0, port1;
\r
94 IPSocket *socketOut;
\r
95 FtpdInfo *info = (FtpdInfo*)socket->userPtr;
\r
99 bytes = IPRead(socket, buf, sizeof(buf)-1);
\r
101 //printf("(%s)\n", buf);
\r
102 if(socket->userPtr == NULL)
\r
104 info = (FtpdInfo*)malloc(sizeof(FtpdInfo));
\r
107 memset(info, 0, sizeof(FtpdInfo));
\r
108 socket->userPtr = info;
\r
109 info->socket = socket;
\r
110 socket->timeoutReset = 60;
\r
111 IPPrintf(socket, "220 Connected to Plasma\r\n");
\r
113 else if(socket->userPtr == (void*)-1)
\r
117 else if(strstr((char*)buf, "USER"))
\r
119 if(strstr((char*)buf, "PlasmaSend"))
\r
120 info->canReceive = 1;
\r
121 IPPrintf(socket, "331 Password?\r\n");
\r
123 else if(strstr((char*)buf, "PASS"))
\r
125 IPPrintf(socket, "230 Logged in\r\n");
\r
127 else if(strstr((char*)buf, "PORT"))
\r
129 sscanf((char*)buf + 5, "%d,%d,%d,%d,%d,%d", &ip0, &ip1, &ip2, &ip3, &port0, &port1);
\r
130 info->ip = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
\r
131 info->port = (port0 << 8) | port1;
\r
132 //printf("ip=0x%x port=%d\n", info->ip, info->port);
\r
133 IPPrintf(socket, "200 OK\r\n");
\r
135 else if(strstr((char*)buf, "RETR") || strstr((char*)buf, "STOR"))
\r
137 char *ptr = strstr((char*)buf, "\r");
\r
143 if(strstr((char*)buf, "RETR"))
\r
144 info->file = fopen((char*)buf + 5, "rb");
\r
145 else if(info->canReceive)
\r
146 info->file = fopen((char*)buf + 5, "wb");
\r
149 IPPrintf(socket, "150 File ready\r\n");
\r
150 if(strstr((char*)buf, "RETR"))
\r
151 socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdSender);
\r
153 socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdReceiver);
\r
155 socketOut->userPtr = info;
\r
159 IPPrintf(socket, "500 Error\r\n");
\r
162 else if(strstr((char*)buf, "QUIT"))
\r
164 if(socket->userPtr)
\r
165 free(socket->userPtr);
\r
166 socket->userPtr = (void*)-1;
\r
167 IPPrintf(socket, "221 Bye\r\n");
\r
172 IPPrintf(socket, "500 Error\r\n");
\r
177 void FtpdInit(int UseFiles)
\r
180 IPOpen(IP_MODE_TCP, 0, 21, FtpdServer);
\r
184 //******************* FTP Client ************************
\r
188 char user[80], passwd[80], filename[80];
\r
190 int size, bytes, send, state;
\r
194 static void FtpCallbackTransfer(IPSocket *socket)
\r
196 int bytes, state = socket->state;
\r
197 FtpInfo *info = (FtpInfo*)socket->userPtr;
\r
199 //printf("FtpCallbackTransfer\n");
\r
202 bytes = info->size - info->bytes;
\r
203 if(info->send == 0)
\r
204 bytes = IPRead(socket, info->buf + info->bytes, bytes);
\r
206 bytes = IPWrite(socket, info->buf + info->bytes, bytes);
\r
207 info->bytes += bytes;
\r
208 if(info->bytes == info->size || (bytes == 0 && state > IP_TCP))
\r
210 socket->userFunc(info->buf, info->bytes);
\r
212 socket->userPtr = NULL;
\r
218 static void FtpCallback(IPSocket *socket)
\r
221 FtpInfo *info = (FtpInfo*)socket->userPtr;
\r
224 bytes = IPRead(socket, (uint8*)buf, sizeof(buf)-1);
\r
228 sscanf(buf, "%d", &value);
\r
231 //printf("FtpCallback(%d:%s)\n", socket->userData, buf);
\r
232 if(value / 100 != 2 && value / 100 != 3)
\r
235 switch(socket->userData) {
\r
237 sprintf(buf, "USER %s\r\n", info->user);
\r
238 socket->userData = 1;
\r
241 sprintf(buf, "PASS %s\r\n", info->passwd);
\r
242 socket->userData = 2;
\r
244 break; //possible fall-through
\r
246 sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
\r
247 info->ip >> 24, (uint8)(info->ip >> 16),
\r
248 (uint8)(info->ip >> 8), (uint8)info->ip,
\r
249 (uint8)(info->port >> 8), (uint8)info->port);
\r
250 socket->userData = 3;
\r
253 if(info->send == 0)
\r
254 sprintf(buf, "RETR %s\r\n", info->filename);
\r
256 sprintf(buf, "STOR %s\r\n", info->filename);
\r
257 socket->userData = 4;
\r
260 sprintf(buf, "QUIT\r\n");
\r
261 socket->userData = 9;
\r
264 IPWrite(socket, (uint8*)buf, strlen(buf));
\r
265 IPWriteFlush(socket);
\r
266 if(socket->userData == 9)
\r
271 IPSocket *FtpTransfer(uint32 ip, char *user, char *passwd,
\r
272 char *filename, uint8 *buf, int size,
\r
273 int send, void (*callback)(uint8 *data, int size))
\r
275 IPSocket *socket, *socketTransfer;
\r
278 info = (FtpInfo*)malloc(sizeof(FtpInfo));
\r
281 strncpy(info->user, user, 80);
\r
282 strncpy(info->passwd, passwd, 80);
\r
283 strncpy(info->filename, filename, 80);
\r
290 socketTransfer = IPOpen(IP_MODE_TCP, 0, info->port, FtpCallbackTransfer);
\r
291 socketTransfer->userPtr = info;
\r
292 socketTransfer->userFunc = callback;
\r
293 socket = IPOpen(IP_MODE_TCP, ip, 21, FtpCallback);
\r
294 socket->userPtr = info;
\r
295 socket->userFunc = callback;
\r
296 ptr = socket->headerSend;
\r
297 info->ip = IPAddressSelf();
\r
302 //******************* TFTP Server ************************
\r
305 static void TftpdCallback(IPSocket *socket)
\r
307 unsigned char buf[512+4];
\r
308 int bytes, blockNum;
\r
309 FILE *file = (FILE*)socket->userPtr;
\r
310 bytes = IPRead(socket, buf, sizeof(buf));
\r
311 //printf("TfptdCallback bytes=%d\n", bytes);
\r
312 if(bytes < 4 || buf[0])
\r
314 if(buf[1] == 1) //RRQ = Read Request
\r
318 file = fopen((char*)buf+2, "rb");
\r
319 socket->userPtr = file;
\r
323 buf[1] = 5; //ERROR
\r
326 buf[4] = 'X'; //Error string
\r
328 IPWrite(socket, buf, 6);
\r
332 if(buf[1] == 1 || buf[1] == 4) //ACK
\r
339 blockNum = (buf[2] << 8) | buf[3];
\r
343 buf[2] = (uint8)(blockNum >> 8);
\r
344 buf[3] = (uint8)blockNum;
\r
345 fseek(file, (blockNum-1)*512, 0);
\r
346 bytes = fread(buf+4, 1, 512, file);
\r
347 IPWrite(socket, buf, bytes+4);
\r
352 void TftpdInit(void)
\r
355 socket = IPOpen(IP_MODE_UDP, 0, 69, TftpdCallback);
\r
359 //******************* TFTP Client ************************
\r
362 static void TftpCallback(IPSocket *socket)
\r
364 unsigned char buf[512+4];
\r
365 int bytes, blockNum, length;
\r
367 bytes = IPRead(socket, buf, sizeof(buf));
\r
368 if(bytes < 4 || buf[0])
\r
370 blockNum = (buf[2] << 8) | buf[3];
\r
371 length = blockNum * 512 - 512 + bytes - 4;
\r
372 //printf("TftpCallback(%d,%d)\n", buf[1], blockNum);
\r
373 if(length > (int)socket->userData)
\r
375 bytes -= length - (int)socket->userData;
\r
376 length = (int)socket->userData;
\r
378 if(buf[1] == 3) //DATA
\r
380 memcpy((uint8*)socket->userPtr + blockNum * 512 - 512, buf+4, bytes-4);
\r
382 IPWrite(socket, buf, 4);
\r
385 socket->userFunc(socket->userPtr, length);
\r
392 IPSocket *TftpTransfer(uint32 ip, char *filename, uint8 *buffer, int size,
\r
393 void (*callback)(uint8 *data, int bytes))
\r
398 socket = IPOpen(IP_MODE_UDP, ip, 69, TftpCallback);
\r
399 socket->userPtr = buffer;
\r
400 socket->userData = size;
\r
401 socket->userFunc = callback;
\r
404 strcpy((char*)buf+2, filename);
\r
405 bytes = strlen(filename);
\r
406 strcpy((char*)buf+bytes+3, "octet");
\r
407 IPWrite(socket, buf, bytes+9);
\r
412 //******************* Telnet Server ************************
\r
414 #define COMMAND_BUFFER_SIZE 80
\r
415 #define COMMAND_BUFFER_COUNT 10
\r
416 static char CommandHistory[400];
\r
417 static char *CommandPtr[COMMAND_BUFFER_COUNT];
\r
418 static int CommandIndex;
\r
420 typedef void (*ConsoleFunc)(IPSocket *socket, char *argv[]);
\r
425 static TelnetFunc_t *TelnetFuncList;
\r
428 static void TelnetServer(IPSocket *socket)
\r
430 uint8 buf[COMMAND_BUFFER_SIZE+4];
\r
432 int bytes, i, j, k, length, found;
\r
433 char *ptr, *command = socket->userPtr;
\r
436 if(socket->state > IP_TCP)
\r
440 bytes = IPRead(socket, buf, sizeof(buf)-1);
\r
441 if(command == NULL)
\r
443 socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
\r
444 if(command == NULL)
\r
449 socket->timeoutReset = 300;
\r
450 buf[0] = 255; //IAC
\r
451 buf[1] = 251; //WILL
\r
452 buf[2] = 3; //suppress go ahead
\r
453 buf[3] = 255; //IAC
\r
454 buf[4] = 251; //WILL
\r
456 strcpy((char*)buf+6, "Welcome to Plasma.\r\n-> ");
\r
457 IPWrite(socket, buf, 6+23);
\r
458 IPWriteFlush(socket);
\r
464 socket->dontFlush = 0;
\r
466 length = (int)strlen(command);
\r
467 for(j = 0; j < bytes; ++j)
\r
471 if(buf[j] == 8 || (buf[j] == 27 && buf[j+2] == 'D'))
\r
478 command[--length] = 0;
\r
482 IPWrite(socket, (uint8*)bufOut, 3);
\r
485 else if(buf[j] == 27)
\r
488 if(buf[j+2] == 'A')
\r
490 if(++CommandIndex > COMMAND_BUFFER_COUNT)
\r
491 CommandIndex = COMMAND_BUFFER_COUNT;
\r
493 else if(buf[j+2] == 'B')
\r
495 if(--CommandIndex < 0)
\r
503 for(i = 0; i < length; ++i)
\r
504 IPWrite(socket, (uint8*)bufOut, 3);
\r
506 if(CommandIndex && CommandPtr[CommandIndex-1])
\r
507 strncat(command, CommandPtr[CommandIndex-1], COMMAND_BUFFER_SIZE-1);
\r
508 length = (int)strlen(command);
\r
509 IPWrite(socket, (uint8*)command, length);
\r
515 buf[j] = '\n'; //Linux support
\r
516 if(length < COMMAND_BUFFER_SIZE-4 || (length <
\r
517 COMMAND_BUFFER_SIZE-2 && (buf[j] == '\r' || buf[j] == '\n')))
\r
519 IPWrite(socket, buf+j, 1);
\r
520 command[length] = buf[j];
\r
521 command[++length] = 0;
\r
524 ptr = strstr(command, "\r\n");
\r
527 // Save command in CommandHistory
\r
529 length = (int)strlen(command);
\r
532 IPPrintf(socket, "-> ");
\r
535 if(length < COMMAND_BUFFER_SIZE)
\r
537 memmove(CommandHistory + length + 1, CommandHistory,
\r
538 sizeof(CommandHistory) - length - 1);
\r
539 strcpy(CommandHistory, command);
\r
540 CommandHistory[sizeof(CommandHistory)-1] = 0;
\r
541 for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
\r
543 if(CommandPtr[i] == NULL || CommandPtr[i] + length + 1 >=
\r
544 CommandHistory + sizeof(CommandHistory))
\r
545 CommandPtr[i+1] = NULL;
\r
547 CommandPtr[i+1] = CommandPtr[i] + length + 1;
\r
549 CommandPtr[0] = CommandHistory;
\r
553 for(i = 0; i < 10; ++i)
\r
556 argv[i++] = command;
\r
557 for(ptr = command; *ptr && i < 10; ++ptr)
\r
562 argv[i++] = ptr + 1;
\r
565 if(argv[0][0] == 0)
\r
567 IPPrintf(socket, "-> ");
\r
571 for(i = 0; TelnetFuncList[i].name; ++i)
\r
573 if(strcmp(command, TelnetFuncList[i].name) == 0 &&
\r
574 TelnetFuncList[i].func)
\r
577 for(k = 1; k < 10; ++k)
\r
579 if(argv[k][0] == '>' && argv[k][1]) //stdout to file?
\r
581 socket->fileOut = fopen(&argv[k][1], "a");
\r
584 if(argv[k][0] == '<' && argv[k][1]) //stdin from file?
\r
586 socket->fileIn = fopen(&argv[k][1], "r");
\r
590 TelnetFuncList[i].func(socket, argv);
\r
591 if(socket->fileOut)
\r
593 fwrite("\r\n", 1, 2, socket->fileOut);
\r
594 fclose(socket->fileOut);
\r
596 socket->fileOut = NULL;
\r
603 strcpy((char*)buf, "/flash/bin/");
\r
604 strcat((char*)buf, argv[0]);
\r
605 argv[0] = (char*)buf;
\r
606 ConsoleRun(socket, argv);
\r
609 if(socket->state > IP_TCP)
\r
614 if(socket->dontFlush == 0)
\r
615 IPPrintf(socket, "\r\n-> ");
\r
616 } //command entered
\r
618 IPWriteFlush(socket);
\r
623 void TelnetInit(TelnetFunc_t *funcList)
\r
626 TelnetFuncList = funcList;
\r
627 socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
\r
631 //******************* Console ************************
\r
633 #define STORAGE_SIZE 1024*64
\r
634 static uint8 *myStorage;
\r
635 static IPSocket *socketTelnet;
\r
636 static char storageFilename[60];
\r
639 static void ConsoleHelp(IPSocket *socket, char *argv[])
\r
644 strcpy(buf, "Commands: ");
\r
645 for(i = 0; TelnetFuncList[i].name; ++i)
\r
647 if(TelnetFuncList[i].func)
\r
651 strcat(buf, TelnetFuncList[i].name);
\r
654 IPPrintf(socket, buf);
\r
658 static void ConsoleExit(IPSocket *socket, char *argv[])
\r
661 socket->userPtr = NULL;
\r
666 static void ConsoleCat(IPSocket *socket, char *argv[])
\r
672 file = fopen(argv[1], "r");
\r
677 bytes = fread(buf, 1, sizeof(buf), file);
\r
680 IPWrite(socket, buf, bytes);
\r
686 static void ConsoleCp(IPSocket *socket, char *argv[])
\r
688 FILE *fileIn, *fileOut;
\r
693 fileIn = fopen(argv[1], "r");
\r
696 fileOut = fopen(argv[2], "w");
\r
701 bytes = fread(buf, 1, sizeof(buf), fileIn);
\r
704 fwrite(buf, 1, bytes, fileOut);
\r
712 static void ConsoleRm(IPSocket *socket, char *argv[])
\r
715 OS_fdelete(argv[1]);
\r
719 static void ConsoleMkdir(IPSocket *socket, char *argv[])
\r
722 OS_fmkdir(argv[1]);
\r
726 static void ConsoleLs(IPSocket *socket, char *argv[])
\r
729 char buf[200], buf2[80];
\r
732 file = fopen(argv[1], "r");
\r
738 bytes = OS_fdir(file, buf);
\r
743 bytes = OS_flength(buf);
\r
744 sprintf(buf2, "%s:%d ", buf, bytes);
\r
745 bytes = strlen(buf2);
\r
746 bytes -= bytes % 20;
\r
753 IPPrintf(socket, "\n");
\r
756 IPPrintf(socket, "%s", buf2);
\r
762 #ifdef INCLUDE_FLASH
\r
763 static void ConsoleFlashErase(IPSocket *socket, char *argv[])
\r
767 IPPrintf(socket, "\r\nErasing");
\r
768 for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
\r
770 IPPrintf(socket, ".");
\r
773 IPPrintf(socket, "\r\nMust Reboot\r\n");
\r
774 OS_ThreadSleep(OS_WAIT_FOREVER);
\r
779 static void ConsoleMath(IPSocket *socket, char *argv[])
\r
782 if(argv[3][0] == 0)
\r
784 IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
\r
787 v1 = atoi(argv[1]);
\r
789 v2 = atoi(argv[3]);
\r
801 IPPrintf(socket, "%d", v1);
\r
805 static void PingCallback(IPSocket *socket)
\r
807 IPSocket *socket2 = socket->userPtr;
\r
810 IPPrintf(socket2, "Ping Reply");
\r
812 printf("Ping Reply\n");
\r
816 static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
\r
818 char buf[COMMAND_BUFFER_SIZE];
\r
819 IPSocket *socketTelnet = arg;
\r
820 IPSocket *socketPing;
\r
823 sprintf(buf, "ip=%d.%d.%d.%d\r\n",
\r
824 (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
\r
825 IPPrintf(socketTelnet, buf);
\r
826 socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
\r
827 socketPing->userPtr = socketTelnet;
\r
829 IPWrite(socketPing, (uint8*)buf, 1);
\r
833 static void ConsolePing(IPSocket *socket, char *argv[])
\r
835 int ip0, ip1, ip2, ip3;
\r
837 if('0' <= argv[1][0] && argv[1][0] <= '9')
\r
839 sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
\r
840 ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
\r
841 DnsResultCallback(socket, ip0, socket);
\r
845 IPResolve(argv[1], DnsResultCallback, socket);
\r
846 IPPrintf(socket, "Sent DNS request");
\r
851 static void ConsoleTransferDone(uint8 *data, int length)
\r
854 IPPrintf(socketTelnet, "Transfer Done");
\r
855 file = fopen(storageFilename, "w");
\r
858 fwrite(data, 1, length, file);
\r
867 static void ConsoleFtp(IPSocket *socket, char *argv[])
\r
869 int ip0, ip1, ip2, ip3;
\r
870 if(argv[1][0] == 0)
\r
872 IPPrintf(socket, "ftp #.#.#.# User Password File");
\r
875 sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
\r
876 ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
\r
877 socketTelnet = socket;
\r
878 if(myStorage == NULL)
\r
879 myStorage = (uint8*)malloc(STORAGE_SIZE);
\r
880 if(myStorage == NULL)
\r
882 strcpy(storageFilename, argv[4]);
\r
883 FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
\r
884 0, ConsoleTransferDone);
\r
888 static void ConsoleTftp(IPSocket *socket, char *argv[])
\r
890 int ip0, ip1, ip2, ip3;
\r
891 if(argv[1][0] == 0)
\r
893 IPPrintf(socket, "tftp #.#.#.# File");
\r
896 sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
\r
897 ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
\r
898 socketTelnet = socket;
\r
899 if(myStorage == NULL)
\r
900 myStorage = (uint8*)malloc(STORAGE_SIZE);
\r
901 if(myStorage == NULL)
\r
903 strcpy(storageFilename, argv[2]);
\r
904 TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
\r
908 static void ConsoleMkfile(IPSocket *socket, char *argv[])
\r
912 file = fopen("myfile.txt", "w");
\r
913 fwrite("Hello World!", 1, 12, file);
\r
915 IPPrintf(socket, "Created myfile.txt");
\r
919 static void ConsoleUptime(IPSocket *socket, char *argv[])
\r
921 int days, hours, minutes, seconds;
\r
923 //ticks per sec = 25E6/2^18 = 95.36743 -> 10.48576 ms/tick
\r
924 seconds = OS_ThreadTime() / 95;
\r
925 minutes = seconds / 60 % 60;
\r
926 hours = seconds / 3600 % 24;
\r
927 days = seconds / 3600 / 24;
\r
929 IPPrintf(socket, "%d days %2d:%2d:%2d\n", days, hours, minutes, seconds);
\r
933 static void ConsoleDump(IPSocket *socket, char *argv[])
\r
939 fileIn = fopen(argv[1], "r");
\r
942 for(j = 0; j < 1024*1024*16; j += 16)
\r
944 bytes = fread(buf, 1, 16, fileIn);
\r
947 IPPrintf(socket, "%8x ", j);
\r
948 for(i = 0; i < bytes; ++i)
\r
949 IPPrintf(socket, "%2x ", buf[i]);
\r
950 for( ; i < 16; ++i)
\r
951 IPPrintf(socket, " ");
\r
952 for(i = 0; i < bytes; ++i)
\r
954 if(isprint(buf[i]))
\r
955 IPPrintf(socket, "%c", buf[i]);
\r
957 IPPrintf(socket, ".");
\r
959 IPPrintf(socket, "\n");
\r
965 static void ConsoleGrep(IPSocket *socket, char *argv[])
\r
970 char *ptr, *ptrEnd;
\r
972 if(argv[1][0] == 0 || argv[2][0] == 0)
\r
974 IPPrintf(socket, "Usage: grep pattern file\n");
\r
977 fileIn = fopen(argv[2], "r");
\r
983 bytes += fread(buf + bytes, 1, sizeof(buf) - bytes - 1, fileIn);
\r
987 ptrEnd = strstr(buf, "\r");
\r
989 ptrEnd = strstr(buf, "\n");
\r
993 if(*++ptrEnd == '\n')
\r
996 ptr = strstr(buf, argv[1]);
\r
998 IPPrintf(socket, "%s\n", buf);
\r
1001 bytes = strlen(ptrEnd);
\r
1002 memcpy(buf, ptrEnd, bytes);
\r
1016 static void ConsoleRun(IPSocket *socket, char *argv[])
\r
1022 char *command, *ptr;
\r
1024 if(strcmp(argv[0], "run") == 0)
\r
1026 file = fopen(argv[0], "r");
\r
1029 IPPrintf(socket, "Can't find %s", argv[0]);
\r
1033 bytes = fread(code, 1, sizeof(code), file); //load first 128 bytes
\r
1034 if(code[0] >= ' ')
\r
1036 socket->fileIn = file; //script file
\r
1037 fseek(file, 0, 0);
\r
1041 funcPtr = (DllFunc)code;
\r
1042 ptr = funcPtr(NULL); //determine load address
\r
1044 memcpy(ptr, code, bytes); //copy to correct address
\r
1045 bytes += fread(ptr + bytes, 1, 1024*1024*8, file);
\r
1047 printf("address=0x%x bytes=%d\n", (int)ptr, bytes);
\r
1048 funcPtr = (DllFunc)ptr;
\r
1049 funcPtr = (DllFunc)funcPtr(DllFuncList); //initialize DLL, find Start()
\r
1051 //Register new command
\r
1052 command = argv[0];
\r
1055 ptr = strstr(command, "/");
\r
1058 command = ptr + 1;
\r
1060 for(i = 0; TelnetFuncList[i].name; ++i)
\r
1062 if(TelnetFuncList[i].name[0] == 0 ||
\r
1063 strcmp(TelnetFuncList[i].name, command) == 0)
\r
1065 TelnetFuncList[i].name = (char*)malloc(40);
\r
1066 strcpy(TelnetFuncList[i].name, command);
\r
1067 TelnetFuncList[i].func = (ConsoleFunc)funcPtr;
\r
1072 socket->userFunc = socket->funcPtr;
\r
1073 funcPtr(socket, argv);
\r
1077 typedef struct NameValue_t {
\r
1078 struct NameValue_t *next;
\r
1083 //Find the value associated with the name
\r
1084 void *IPNameValue(const char *name, void *value)
\r
1086 static NameValue_t *head;
\r
1087 NameValue_t *node;
\r
1088 for(node = head; node; node = node->next)
\r
1090 if(strcmp(node->name, name) == 0)
\r
1095 node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
\r
1098 strcpy(node->name, name);
\r
1099 node->value = value;
\r
1100 node->next = head;
\r
1104 node->value = value;
\r
1105 return node->value;
\r
1111 extern void EditFile(IPSocket *socket, char *argv[]);
\r
1114 static TelnetFunc_t MyFuncs[] = {
\r
1115 {"cat", ConsoleCat},
\r
1116 {"cp", ConsoleCp},
\r
1117 {"dump", ConsoleDump},
\r
1118 {"exit", ConsoleExit},
\r
1119 #ifdef INCLUDE_FLASH
\r
1120 {"flashErase", ConsoleFlashErase},
\r
1122 {"ftp", ConsoleFtp},
\r
1123 {"grep", ConsoleGrep},
\r
1124 {"help", ConsoleHelp},
\r
1125 {"ls", ConsoleLs},
\r
1126 {"math", ConsoleMath},
\r
1127 {"mkdir", ConsoleMkdir},
\r
1128 {"mkfile", ConsoleMkfile},
\r
1129 {"ping", ConsolePing},
\r
1130 {"rm", ConsoleRm},
\r
1131 {"tftp", ConsoleTftp},
\r
1132 {"uptime", ConsoleUptime},
\r
1134 {"run", ConsoleRun},
\r
1137 {"edit", EditFile},
\r
1154 void ConsoleInit(void)
\r
1158 TelnetInit(MyFuncs);
\r