OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [netutil.c] - Blame information for rev 416

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 354 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma TCP/IP Network Utilities
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 4/20/07
5
 * FILENAME: netutil.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Plasma FTP server and FTP client and TFTP server and client
11
 *    and Telnet server.
12
 *--------------------------------------------------------------------*/
13 416 rhoads
#define INSIDE_NETUTIL
14
#include "plasma.h"
15 354 rhoads
#include "rtos.h"
16
#include "tcpip.h"
17
 
18 416 rhoads
#ifndef EXCLUDE_DLL
19 354 rhoads
static void ConsoleRun(IPSocket *socket, char *argv[]);
20
#endif
21
 
22
//******************* FTP Server ************************
23
typedef struct {
24
   IPSocket *socket;
25
   int ip, port, bytes, done, canReceive;
26
   FILE *file;
27
} FtpdInfo;
28
 
29
static void FtpdSender(IPSocket *socket)
30
{
31
   unsigned char buf[600];
32
   int i, bytes, bytes2;
33
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
34
 
35
   if(info == NULL || info->done)
36
      return;
37
   fseek(info->file, info->bytes, 0);
38
   for(i = 0; i < 100000; ++i)
39
   {
40
      bytes = fread(buf, 1, 512, info->file);
41
      bytes2 = IPWrite(socket, buf, bytes);
42
      info->bytes += bytes2;
43
      if(bytes != bytes2)
44
         return;
45
      if(bytes < 512)
46
      {
47
         fclose(info->file);
48
         IPClose(socket);
49
         info->done = 1;
50
         IPPrintf(info->socket, "226 Done\r\n");
51
         return;
52
      }
53
   }
54
}
55
 
56
 
57
static void FtpdReceiver(IPSocket *socket)
58
{
59
   unsigned char buf[600];
60
   int bytes, state = socket->state;
61
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
62
 
63
   if(info == NULL || info->done)
64
      return;
65
   do
66
   {
67
      bytes = IPRead(socket, buf, sizeof(buf));
68
      fwrite(buf, 1, bytes, info->file);
69
   } while(bytes);
70
 
71
   if(state > IP_TCP)
72
   {
73
      fclose(info->file);
74
      info->done = 1;
75
      IPPrintf(info->socket, "226 Done\r\n");
76
      IPClose(socket);
77
      return;
78
   }
79
}
80
 
81
 
82
static void FtpdServer(IPSocket *socket)
83
{
84
   uint8 buf[600];
85
   int bytes;
86
   int ip0, ip1, ip2, ip3, port0, port1;
87
   IPSocket *socketOut;
88
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
89
 
90
   if(socket == NULL)
91
      return;
92
   bytes = IPRead(socket, buf, sizeof(buf)-1);
93
   buf[bytes] = 0;
94
   //printf("(%s)\n", buf);
95
   if(socket->userPtr == NULL)
96
   {
97
      info = (FtpdInfo*)malloc(sizeof(FtpdInfo));
98
      if(info == NULL)
99
         return;
100
      memset(info, 0, sizeof(FtpdInfo));
101
      socket->userPtr = info;
102
      info->socket = socket;
103
      socket->timeoutReset = 60;
104
      IPPrintf(socket, "220 Connected to Plasma\r\n");
105
   }
106
   else if(socket->userPtr == (void*)-1)
107
   {
108
      return;
109
   }
110
   else if(strstr((char*)buf, "USER"))
111
   {
112
      if(strstr((char*)buf, "PlasmaSend"))
113
         info->canReceive = 1;
114
      IPPrintf(socket, "331 Password?\r\n");
115
   }
116
   else if(strstr((char*)buf, "PASS"))
117
   {
118
      IPPrintf(socket, "230 Logged in\r\n");
119
   }
120
   else if(strstr((char*)buf, "PORT"))
121
   {
122
      sscanf((char*)buf + 5, "%d,%d,%d,%d,%d,%d", &ip0, &ip1, &ip2, &ip3, &port0, &port1);
123
      info->ip = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
124
      info->port = (port0 << 8) | port1;
125
      //printf("ip=0x%x port=%d\n", info->ip, info->port);
126
      IPPrintf(socket, "200 OK\r\n");
127
   }
128
   else if(strstr((char*)buf, "RETR") || strstr((char*)buf, "STOR"))
129
   {
130
      char *ptr = strstr((char*)buf, "\r");
131
      if(ptr)
132
         *ptr = 0;
133
      info->file = NULL;
134
      info->bytes = 0;
135
      info->done = 0;
136
      if(strstr((char*)buf, "RETR"))
137
         info->file = fopen((char*)buf + 5, "rb");
138 416 rhoads
      else if(info->canReceive && strcmp((char*)buf+5, "/flash/web"))
139 354 rhoads
         info->file = fopen((char*)buf + 5, "wb");
140
      if(info->file)
141
      {
142
         IPPrintf(socket, "150 File ready\r\n");
143
         if(strstr((char*)buf, "RETR"))
144
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdSender);
145
         else
146
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdReceiver);
147
         if(socketOut)
148
            socketOut->userPtr = info;
149
      }
150
      else
151
      {
152
         IPPrintf(socket, "500 Error\r\n");
153
      }
154
   }
155
   else if(strstr((char*)buf, "QUIT"))
156
   {
157
      if(socket->userPtr)
158
         free(socket->userPtr);
159
      socket->userPtr = (void*)-1;
160
      IPPrintf(socket, "221 Bye\r\n");
161
      IPClose(socket);
162
   }
163
   else if(bytes)
164
   {
165
      IPPrintf(socket, "500 Error\r\n");
166
   }
167
}
168
 
169
 
170
void FtpdInit(int UseFiles)
171
{
172
   (void)UseFiles;
173
   IPOpen(IP_MODE_TCP, 0, 21, FtpdServer);
174
}
175
 
176
 
177
//******************* FTP Client ************************
178
 
179
typedef struct {
180
   uint32 ip, port;
181
   char user[80], passwd[80], filename[80];
182
   uint8 *buf;
183
   int size, bytes, send, state;
184
} FtpInfo;
185
 
186
 
187
static void FtpCallbackTransfer(IPSocket *socket)
188
{
189
   int bytes, state = socket->state;
190
   FtpInfo *info = (FtpInfo*)socket->userPtr;
191
 
192
   //printf("FtpCallbackTransfer\n");
193
   if(info == NULL)
194
      return;
195
   bytes = info->size - info->bytes;
196
   if(info->send == 0)
197
      bytes = IPRead(socket, info->buf + info->bytes, bytes);
198
   else
199
      bytes = IPWrite(socket, info->buf + info->bytes, bytes);
200
   info->bytes += bytes;
201
   if(info->bytes == info->size || (bytes == 0 && state > IP_TCP))
202
   {
203 400 rhoads
      socket->userFunc(socket, info->buf, info->bytes);
204 354 rhoads
      free(info);
205
      socket->userPtr = NULL;
206
      IPClose(socket);
207
   }
208
}
209
 
210
 
211
static void FtpCallback(IPSocket *socket)
212
{
213
   char buf[600];
214
   FtpInfo *info = (FtpInfo*)socket->userPtr;
215
   int bytes, value;
216
 
217
   bytes = IPRead(socket, (uint8*)buf, sizeof(buf)-1);
218
   if(bytes == 0)
219
      return;
220
   buf[bytes] = 0;
221
   sscanf(buf, "%d", &value);
222
   if(bytes > 2)
223
      buf[bytes-2] = 0;
224
   //printf("FtpCallback(%d:%s)\n", socket->userData, buf);
225
   if(value / 100 != 2 && value / 100 != 3)
226
      return;
227
   buf[0] = 0;
228
   switch(socket->userData) {
229
   case 0:
230
      sprintf(buf, "USER %s\r\n", info->user);
231
      socket->userData = 1;
232
      break;
233
   case 1:
234
      sprintf(buf, "PASS %s\r\n", info->passwd);
235
      socket->userData = 2;
236
      if(value == 331)
237
         break;  //possible fall-through
238
   case 2:
239
      sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
240
         info->ip >> 24, (uint8)(info->ip >> 16),
241
         (uint8)(info->ip >> 8), (uint8)info->ip,
242
         (uint8)(info->port >> 8), (uint8)info->port);
243
      socket->userData = 3;
244
      break;
245
   case 3:
246
      if(info->send == 0)
247
         sprintf(buf, "RETR %s\r\n", info->filename);
248
      else
249
         sprintf(buf, "STOR %s\r\n", info->filename);
250
      socket->userData = 4;
251
      break;
252
   case 4:
253
      sprintf(buf, "QUIT\r\n");
254
      socket->userData = 9;
255
      break;
256
   }
257
   IPWrite(socket, (uint8*)buf, strlen(buf));
258
   IPWriteFlush(socket);
259
   if(socket->userData == 9)
260
      IPClose(socket);
261
}
262
 
263
 
264
IPSocket *FtpTransfer(uint32 ip, char *user, char *passwd,
265
                      char *filename, uint8 *buf, int size,
266 400 rhoads
                      int send, IPCallbackPtr callback)
267 354 rhoads
{
268
   IPSocket *socket, *socketTransfer;
269
   FtpInfo *info;
270
   uint8 *ptr;
271
   info = (FtpInfo*)malloc(sizeof(FtpInfo));
272
   if(info == NULL)
273
      return NULL;
274
   strncpy(info->user, user, 80);
275
   strncpy(info->passwd, passwd, 80);
276
   strncpy(info->filename, filename, 80);
277
   info->buf = buf;
278
   info->size = size;
279
   info->send = send;
280
   info->bytes = 0;
281
   info->state = 0;
282
   info->port = 2000;
283
   socketTransfer = IPOpen(IP_MODE_TCP, 0, info->port, FtpCallbackTransfer);
284
   socketTransfer->userPtr = info;
285
   socketTransfer->userFunc = callback;
286
   socket = IPOpen(IP_MODE_TCP, ip, 21, FtpCallback);
287
   socket->userPtr = info;
288
   socket->userFunc = callback;
289
   ptr = socket->headerSend;
290
   info->ip = IPAddressSelf();
291
   return socket;
292
}
293
 
294
 
295
//******************* TFTP Server ************************
296
 
297
 
298
static void TftpdCallback(IPSocket *socket)
299
{
300
   unsigned char buf[512+4];
301
   int bytes, blockNum;
302
   FILE *file = (FILE*)socket->userPtr;
303
   bytes = IPRead(socket, buf, sizeof(buf));
304
   //printf("TfptdCallback bytes=%d\n", bytes);
305
   if(bytes < 4 || buf[0])
306
      return;
307
   if(buf[1] == 1)  //RRQ = Read Request
308
   {
309
      if(file)
310
         fclose(file);
311
      file = fopen((char*)buf+2, "rb");
312
      socket->userPtr = file;
313
      if(file == NULL)
314
      {
315
         buf[0] = 0;
316
         buf[1] = 5;   //ERROR
317
         buf[2] = 0;
318
         buf[3] = 0;
319
         buf[4] = 'X'; //Error string
320
         buf[5] = 0;
321
         IPWrite(socket, buf, 6);
322
         return;
323
      }
324
   }
325
   if(buf[1] == 1 || buf[1] == 4) //ACK
326
   {
327
      if(file == NULL)
328
         return;
329
      if(buf[1] == 1)
330
         blockNum = 0;
331
      else
332
         blockNum = (buf[2] << 8) | buf[3];
333
      ++blockNum;
334
      buf[0] = 0;
335
      buf[1] = 3;  //DATA
336
      buf[2] = (uint8)(blockNum >> 8);
337
      buf[3] = (uint8)blockNum;
338
      fseek(file, (blockNum-1)*512, 0);
339
      bytes = fread(buf+4, 1, 512, file);
340
      IPWrite(socket, buf, bytes+4);
341
   }
342
}
343
 
344
 
345
void TftpdInit(void)
346
{
347
   IPSocket *socket;
348
   socket = IPOpen(IP_MODE_UDP, 0, 69, TftpdCallback);
349
}
350
 
351
 
352
//******************* TFTP Client ************************
353
 
354
 
355
static void TftpCallback(IPSocket *socket)
356
{
357
   unsigned char buf[512+4];
358
   int bytes, blockNum, length;
359
 
360
   bytes = IPRead(socket, buf, sizeof(buf));
361
   if(bytes < 4 || buf[0])
362
      return;
363
   blockNum = (buf[2] << 8) | buf[3];
364
   length = blockNum * 512 - 512 + bytes - 4;
365
   //printf("TftpCallback(%d,%d)\n", buf[1], blockNum);
366
   if(length > (int)socket->userData)
367
   {
368
      bytes -= length - (int)socket->userData;
369
      length = (int)socket->userData;
370
   }
371
   if(buf[1] == 3) //DATA
372
   {
373
      memcpy((uint8*)socket->userPtr + blockNum * 512 - 512, buf+4, bytes-4);
374
      buf[1] = 4; //ACK
375
      IPWrite(socket, buf, 4);
376
      if(bytes-4 < 512)
377
      {
378 416 rhoads
         socket->userFunc(socket, (uint8*)socket->userPtr, length);
379 354 rhoads
         IPClose(socket);
380
      }
381
   }
382
}
383
 
384
 
385
IPSocket *TftpTransfer(uint32 ip, char *filename, uint8 *buffer, int size,
386 400 rhoads
                       IPCallbackPtr callback)
387 354 rhoads
{
388
   IPSocket *socket;
389
   uint8 buf[512+4];
390
   int bytes;
391
   socket = IPOpen(IP_MODE_UDP, ip, 69, TftpCallback);
392
   socket->userPtr = buffer;
393
   socket->userData = size;
394
   socket->userFunc = callback;
395
   buf[0] = 0;
396
   buf[1] = 1; //read
397
   strcpy((char*)buf+2, filename);
398
   bytes = strlen(filename);
399
   strcpy((char*)buf+bytes+3, "octet");
400
   IPWrite(socket, buf, bytes+9);
401
   return socket;
402
}
403
 
404
 
405
//******************* Telnet Server ************************
406
 
407
#define COMMAND_BUFFER_SIZE 80
408
#define COMMAND_BUFFER_COUNT 10
409
static char CommandHistory[400];
410
static char *CommandPtr[COMMAND_BUFFER_COUNT];
411
static int CommandIndex;
412 416 rhoads
static OS_Semaphore_t *semProtect;
413 354 rhoads
 
414
typedef void (*ConsoleFunc)(IPSocket *socket, char *argv[]);
415
typedef struct {
416
   char *name;
417
   ConsoleFunc func;
418
} TelnetFunc_t;
419
static TelnetFunc_t *TelnetFuncList;
420
 
421 416 rhoads
static int TelnetGetLine(IPSocket *socket, uint8 *bufIn, int bytes)
422 354 rhoads
{
423 416 rhoads
   int i, j, length;
424
   char *ptr, *command = (char*)socket->userPtr;
425 354 rhoads
   char bufOut[32];
426
 
427 416 rhoads
   length = (int)strlen(command);
428
   for(j = 0; j < bytes; ++j)
429
   {
430
      if(bufIn[j] == 255)
431
         break;
432
      if(bufIn[j] == 8 || (bufIn[j] == 27 && bufIn[j+2] == 'D'))
433
      {
434
         if(bufIn[j] == 27)
435
            j += 2;
436
         if(length)
437
         {
438
            // Backspace
439
            command[--length] = 0;
440
            bufOut[0] = 8;
441
            bufOut[1] = ' ';
442
            bufOut[2] = 8;
443
            IPWrite(socket, (uint8*)bufOut, 3);
444
            IPWriteFlush(socket);
445
         }
446
      }
447
      else if(bufIn[j] == 27)
448
      {
449
         // Command History
450
         if(bufIn[j+2] == 'A')
451
         {
452
            if(++CommandIndex > COMMAND_BUFFER_COUNT)
453
               CommandIndex = COMMAND_BUFFER_COUNT;
454
         }
455
         else if(bufIn[j+2] == 'B')
456
         {
457
            if(--CommandIndex < 0)
458
               CommandIndex = 0;
459
         }
460
         else
461
            return -1;
462
         bufOut[0] = 8;
463
         bufOut[1] = ' ';
464
         bufOut[2] = 8;
465
         for(i = 0; i < length; ++i)
466
            IPWrite(socket, (uint8*)bufOut, 3);
467
         command[0] = 0;
468
         if(CommandIndex && CommandPtr[CommandIndex-1])
469
            strncat(command, CommandPtr[CommandIndex-1], COMMAND_BUFFER_SIZE-1);
470
         length = (int)strlen(command);
471
         IPWrite(socket, (uint8*)command, length);
472
         IPWriteFlush(socket);
473
         j += 2;
474
      }
475
      else
476
      {
477
         if(bufIn[j] == 0)
478
            bufIn[j] = '\n';  //Linux support
479
         if(length < COMMAND_BUFFER_SIZE-4 || (length <
480
            COMMAND_BUFFER_SIZE-2 && (bufIn[j] == '\r' || bufIn[j] == '\n')))
481
         {
482
            IPWrite(socket, (uint8*)bufIn+j, 1);
483
            IPWriteFlush(socket);
484
            command[length] = bufIn[j];
485
            command[++length] = 0;
486
         }
487
      }
488
      ptr = strstr(command, "\r\n");
489
      if(ptr == NULL)
490
         ptr = strstr(command, "\n");
491
      if(ptr)
492
      {
493
         // Save command in CommandHistory
494
         ptr[0] = 0;
495
         length = (int)strlen(command);
496
         if(length == 0)
497
         {
498
            IPPrintf(socket, "\n-> ");
499
            continue;
500
         }
501
         if(length < COMMAND_BUFFER_SIZE)
502
         {
503
            OS_SemaphorePend(semProtect, OS_WAIT_FOREVER);
504
            memmove(CommandHistory + length + 1, CommandHistory,
505
               sizeof(CommandHistory) - length - 1);
506
            strcpy(CommandHistory, command);
507
            CommandHistory[sizeof(CommandHistory)-1] = 0;
508
            for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
509
            {
510
               if(CommandPtr[i] == NULL || CommandPtr[i] + length + 1 >=
511
                  CommandHistory + sizeof(CommandHistory))
512
                  CommandPtr[i+1] = NULL;
513
               else
514
                  CommandPtr[i+1] = CommandPtr[i] + length + 1;
515
            }
516
            CommandPtr[0] = CommandHistory;
517
            OS_SemaphorePost(semProtect);
518
         }
519
         return 0;
520
      }
521
   }
522
   return -1;
523
}
524
 
525
 
526
static void TelnetServerCallback(IPSocket *socket)
527
{
528
   uint8 bufIn[COMMAND_BUFFER_SIZE+4];
529
   int bytes, i, k, found, rc;
530
   char *ptr, *command = (char*)socket->userPtr;
531
   char command2[COMMAND_BUFFER_SIZE];
532
   char *argvStorage[12], **argv = argvStorage+2;
533
 
534 354 rhoads
   if(socket->state > IP_TCP)
535
      return;
536 362 rhoads
   for(;;)
537 354 rhoads
   {
538 416 rhoads
      bytes = IPRead(socket, bufIn, sizeof(bufIn)-1);
539 354 rhoads
      if(command == NULL)
540
      {
541 362 rhoads
         socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
542
         if(command == NULL)
543
         {
544
            IPClose(socket);
545
            return;
546
         }
547 416 rhoads
         socket->timeoutReset = 60*15;
548
         bufIn[0] = 255; //IAC
549
         bufIn[1] = 251; //WILL
550
         bufIn[2] = 3;   //suppress go ahead
551
         bufIn[3] = 255; //IAC
552
         bufIn[4] = 251; //WILL
553
         bufIn[5] = 1;   //echo
554
         strcpy((char*)bufIn+6, "Welcome to Plasma.\r\n-> ");
555
         IPWrite(socket, bufIn, 6+23);
556 362 rhoads
         IPWriteFlush(socket);
557
         command[0] = 0;
558 416 rhoads
         OS_ThreadInfoSet(OS_ThreadSelf(), 0, (void*)socket);  //for stdin and stdout
559 354 rhoads
         return;
560
      }
561 362 rhoads
      if(bytes == 0)
562 354 rhoads
         return;
563 362 rhoads
      socket->dontFlush = 0;
564 416 rhoads
      bufIn[bytes] = 0;
565
 
566
      //Get a complete command line
567
      rc = TelnetGetLine(socket, bufIn, bytes);
568
      if(rc)
569
         continue;
570
      strcpy(command2, command);
571
      command[0] = 0;
572
 
573
      //Process arguments
574
      for(i = 0; i < 10; ++i)
575
         argv[i] = "";
576
      i = 0;
577
      argv[i++] = command2;
578
      for(ptr = command2; *ptr && i < 10; ++ptr)
579 354 rhoads
      {
580 416 rhoads
         if(*ptr == ' ')
581 354 rhoads
         {
582 416 rhoads
            *ptr = 0;
583
            argv[i++] = ptr + 1;
584 354 rhoads
         }
585 416 rhoads
      }
586
      if(argv[0][0] == 0)
587
      {
588
         IPPrintf(socket, "-> ");
589
         continue;
590
      }
591
 
592
      //Check for file in or out
593
      for(k = 1; k < 10; ++k)
594
      {
595
         if(argv[k][0] == '>') //stdout to file?
596 354 rhoads
         {
597 416 rhoads
            if(argv[k][1])
598
               socket->fileOut = fopen(&argv[k][1], "a");
599
            else
600
               socket->fileOut = fopen(argv[k+1], "a");
601
            argv[k] = "";
602 354 rhoads
         }
603 416 rhoads
         if(argv[k][0] == '<') //stdin from file?
604
         {
605
            if(argv[k][1])
606
               socket->fileIn = fopen(&argv[k][1], "r");
607
            else
608
               socket->fileIn = fopen(argv[k+1], "r");
609
            argv[k] = "";
610
         }
611
      }
612
 
613
      //Find command
614
      found = 0;
615
      for(i = 0; TelnetFuncList[i].name; ++i)
616
      {
617
         if(strcmp(argv[0], TelnetFuncList[i].name) == 0 &&
618
            TelnetFuncList[i].func)
619
         {
620
            found = 1;
621
            TelnetFuncList[i].func(socket, argv);
622
            break;
623
         }
624
      }
625
#ifndef EXCLUDE_DLL
626
      if(found == 0)
627
      {
628
         FILE *file = fopen(argv[0], "r");
629
         if(file)
630
         {
631
            fclose(file);
632
            ConsoleRun(socket, argv);
633
         }
634 362 rhoads
         else
635 354 rhoads
         {
636 416 rhoads
            strcpy((char*)bufIn, "/flash/bin/");
637
            strcat((char*)bufIn, argv[0]);
638
            argv[0] = (char*)bufIn;
639
            ConsoleRun(socket, argv);
640 354 rhoads
         }
641 416 rhoads
      }
642 354 rhoads
#endif
643 416 rhoads
      if(socket->fileOut)
644
      {
645
         fwrite("\r\n", 1, 2, (FILE*)socket->fileOut);
646
         fclose((FILE*)socket->fileOut);
647
         socket->fileOut = NULL;
648
      }
649
 
650
      if(socket->state > IP_TCP)
651
         return;
652
      CommandIndex = 0;
653
      if(socket->dontFlush == 0)
654
         IPPrintf(socket, "\r\n-> ");
655 362 rhoads
   }
656 354 rhoads
}
657
 
658
 
659 416 rhoads
static void TelnetThread(void *socketIn)
660
{
661
   IPSocket *socket = (IPSocket*)socketIn;
662
 
663
   while(socket->state <= IP_TCP)
664
   {
665
      OS_SemaphorePend(socket->userSemaphore, OS_WAIT_FOREVER);
666
      TelnetServerCallback(socket);
667
   }
668
   if(socket->userPtr)
669
      free(socket->userPtr);
670
   socket->userPtr = NULL;
671
   OS_SemaphoreDelete(socket->userSemaphore);
672
   socket->userSemaphore = NULL;
673
   OS_ThreadExit();
674
}
675
 
676
 
677
static void TelnetServer(IPSocket *socket)
678
{
679
   if(socket->state <= IP_TCP && socket->userSemaphore == NULL)
680
   {
681
      socket->userSemaphore = OS_SemaphoreCreate("telnet", 0);
682
      OS_ThreadCreate("telnet", TelnetThread, socket, 100, 1024*8);
683
   }
684
   if(socket->userSemaphore)
685
      OS_SemaphorePost(socket->userSemaphore);
686
}
687
 
688
 
689 354 rhoads
void TelnetInit(TelnetFunc_t *funcList)
690
{
691
   IPSocket *socket;
692
   TelnetFuncList = funcList;
693 416 rhoads
   semProtect = OS_SemaphoreCreate("telprot", 1);
694
#ifndef WIN32
695
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);  //create thread
696
#else
697
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServerCallback);
698
#endif
699 354 rhoads
}
700
 
701
 
702
//******************* Console ************************
703
 
704
#define STORAGE_SIZE 1024*64
705
static uint8 *myStorage;
706
static IPSocket *socketTelnet;
707
static char storageFilename[60];
708
 
709
 
710 416 rhoads
int ConsoleScanf(char *format,
711
   int arg0, int arg1, int arg2, int arg3,
712
   int arg4, int arg5, int arg6, int arg7)
713
{
714
   IPSocket *socket = (IPSocket*)OS_ThreadInfoGet(OS_ThreadSelf(), 0);
715
   char *command = (char*)socket->userPtr;
716
   uint8 bufIn[256];
717
   int bytes;
718
   int rc;
719
 
720
   while(socket->state <= IP_TCP)
721
   {
722
      bytes = IPRead(socket, (unsigned char*)bufIn, sizeof(bufIn));
723
      rc = TelnetGetLine(socket, bufIn, bytes);
724
      if(rc == 0)
725
         break;
726
      OS_ThreadSleep(1);
727
   }
728
 
729
   rc = sscanf(command, format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
730
   command[0] = 0;
731
   return rc;
732
}
733
 
734
 
735
int ConsoleKbhit(void)
736
{
737
   IPSocket *socket = (IPSocket*)OS_ThreadInfoGet(OS_ThreadSelf(), 0);
738
 
739
   if(socket->state > IP_TCP || socket->frameReadTail || socket->fileIn)
740
      return 1;
741
   return 0;
742
}
743
 
744
 
745
int ConsoleGetch(void)
746
{
747
   unsigned char buffer[8];
748
   int rc, i;
749
 
750
   for(i = 0; i < 100*60; ++i)
751
   {
752
      if(ConsoleKbhit())
753
         break;
754
      OS_ThreadSleep(5);
755
   }
756
 
757
   rc = IPRead(NULL, buffer, 1);
758
   if(rc <= 0)
759
      return -1;
760
   return buffer[0];
761
}
762
 
763
 
764
int ConsolePrintf(char *format,
765
   int arg0, int arg1, int arg2, int arg3,
766
   int arg4, int arg5, int arg6, int arg7)
767
{
768
   return IPPrintf(NULL, format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
769
}
770
 
771
 
772 354 rhoads
static void ConsoleHelp(IPSocket *socket, char *argv[])
773
{
774
   char buf[200];
775
   int i;
776
   (void)argv;
777 416 rhoads
   strcpy(buf, "Version " __DATE__ " " __TIME__ "\nCommands: ");
778 354 rhoads
   for(i = 0; TelnetFuncList[i].name; ++i)
779
   {
780
      if(TelnetFuncList[i].func)
781
      {
782
         if(i)
783
            strcat(buf, ", ");
784
         strcat(buf, TelnetFuncList[i].name);
785
      }
786
   }
787
   IPPrintf(socket, buf);
788
}
789
 
790
 
791
static void ConsoleExit(IPSocket *socket, char *argv[])
792
{
793
   IPClose(socket);
794
}
795
 
796
 
797 416 rhoads
static void ConsoleMemcpyReboot(int *dst, int *src, int length)
798
{
799
   OS_FuncPtr_t funcPtr = (OS_FuncPtr_t)dst;
800
   while(length-- > 0)
801
      *dst++ = *src++;
802
   funcPtr(NULL);
803
}
804
 
805
 
806
static void ConsoleReboot(IPSocket *socket, char *argv[])
807
{
808
   FILE *file = NULL;
809
   OS_FuncPtr_t funcPtr;
810
   unsigned char *ptr;
811
   int length;
812
   (void)socket;
813
 
814
   if(argv[1][0])
815
      file = fopen(argv[1], "r");
816
#ifndef EXCLUDE_FLASH
817
   if(file && strcmp(argv[2], "flash") == 0)
818
   {
819
      unsigned char buffer[64];
820
      int offset;
821
 
822
      FlashErase(0);  //erase 128KB sector
823
      offset = 0;
824
      for(;;)
825
      {
826
         length = fread(buffer, 1, sizeof(buffer), file);
827
         if(length == 0)
828
            break;
829
         FlashWrite((uint16*)buffer, offset, length);
830
         offset += length;
831
      }
832
      fclose(file);
833
      file = NULL;
834
   }
835
#endif
836
   if(file)
837
   {
838
      //Reboot running new image
839
      typedef void (*RebootFunc)(int *, int *, int);
840
      typedef void (*MemcpyFunc)(int *dst, int *src, int length);
841
      union {  //convert from function pointer to data pointer
842
         RebootFunc rebootFunc;
843
         MemcpyFunc memcpyFunc;
844
         unsigned char *ptr;
845
      } rfUnion;
846
 
847
      ptr=(unsigned char*)malloc(256+1024*128);
848
      if(ptr == NULL)
849
         return;
850
      rfUnion.memcpyFunc = ConsoleMemcpyReboot;
851
      memcpy(ptr, rfUnion.ptr, 256);   //rfUnion.ptr==ConsoleMemcpyReboot
852
      rfUnion.ptr = ptr;
853
      ptr += 256;
854
      length = fread(ptr, 1, 128*1024, file) + 3;
855
      fclose(file);
856
      printf("rebooting 0x%x 0x%x %d\n", (int)rfUnion.rebootFunc, (int)ptr, length);
857
      OS_ThreadSleep(100);
858
      OS_CriticalBegin();
859
      rfUnion.rebootFunc((int*)RAM_EXTERNAL_BASE, (int*)ptr, length>>2);
860
   }
861
   funcPtr = NULL;
862
   OS_CriticalBegin();
863
   funcPtr(NULL);
864
}
865
 
866
 
867 354 rhoads
static void ConsoleCat(IPSocket *socket, char *argv[])
868
{
869
   FILE *file;
870
   uint8 buf[200];
871
   int bytes;
872
 
873
   file = fopen(argv[1], "r");
874
   if(file == NULL)
875
      return;
876
   for(;;)
877
   {
878
      bytes = fread(buf, 1, sizeof(buf), file);
879
      if(bytes == 0)
880
         break;
881
      IPWrite(socket, buf, bytes);
882
   }
883
   fclose(file);
884
}
885
 
886
 
887
static void ConsoleCp(IPSocket *socket, char *argv[])
888
{
889
   FILE *fileIn, *fileOut;
890
   uint8 buf[200];
891
   int bytes;
892
   (void)socket;
893
 
894 416 rhoads
   fileOut = fopen(argv[2], "r");
895
   if(fileOut)
896
   {
897
      IPPrintf(socket, "File already exists!\n");
898
      fclose(fileOut);
899
      return;
900
   }
901 354 rhoads
   fileIn = fopen(argv[1], "r");
902
   if(fileIn == NULL)
903 416 rhoads
   {
904
      IPPrintf(socket, "Error!\n");
905 354 rhoads
      return;
906 416 rhoads
   }
907 354 rhoads
   fileOut = fopen(argv[2], "w");
908
   if(fileOut)
909
   {
910
      for(;;)
911
      {
912
         bytes = fread(buf, 1, sizeof(buf), fileIn);
913
         if(bytes == 0)
914
            break;
915
         fwrite(buf, 1, bytes, fileOut);
916
      }
917
      fclose(fileOut);
918
   }
919
   fclose(fileIn);
920
}
921
 
922
 
923
static void ConsoleRm(IPSocket *socket, char *argv[])
924
{
925
   (void)socket;
926
   OS_fdelete(argv[1]);
927
}
928
 
929
 
930
static void ConsoleMkdir(IPSocket *socket, char *argv[])
931
{
932
   (void)socket;
933
   OS_fmkdir(argv[1]);
934
}
935
 
936
 
937
static void ConsoleLs(IPSocket *socket, char *argv[])
938
{
939
   FILE *file;
940
   char buf[200], buf2[80];
941
   int bytes, width;
942
 
943
   file = fopen(argv[1], "r");
944
   if(file == NULL)
945
      return;
946
   width = 0;
947
   for(;;)
948
   {
949
      bytes = OS_fdir(file, buf);
950
      if(bytes)
951
         break;
952 411 rhoads
      if((unsigned char)buf[0] == 255)
953 354 rhoads
         continue;
954
      bytes = OS_flength(buf);
955
      sprintf(buf2, "%s:%d                    ", buf, bytes);
956
      bytes = strlen(buf2);
957
      bytes -= bytes % 20;
958
      buf2[bytes] = 0;
959
      width += bytes;
960
      if(width == 80)
961
         --bytes;
962
      if(width > 80)
963
      {
964
         IPPrintf(socket, "\n");
965
         width = bytes;
966
      }
967 362 rhoads
      IPPrintf(socket, "%s", buf2);
968 354 rhoads
   }
969
   fclose(file);
970
}
971
 
972
 
973 416 rhoads
#ifndef EXCLUDE_FLASH
974 354 rhoads
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
975
{
976
   int bytes;
977 416 rhoads
   OS_FuncPtr_t funcPtr = NULL;
978 354 rhoads
   (void)argv;
979 416 rhoads
   IPPrintf(socket, "\r\nErasing\n");
980
   OS_ThreadSleep(10);
981
   OS_CriticalBegin();
982 354 rhoads
   for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
983
   {
984 416 rhoads
      UartPrintfCritical(".");
985 354 rhoads
      FlashErase(bytes);
986
   }
987 416 rhoads
   funcPtr(NULL);
988 354 rhoads
}
989
#endif
990
 
991
 
992
static void ConsoleMath(IPSocket *socket, char *argv[])
993
{
994
   int v1, v2, ch;
995
   if(argv[3][0] == 0)
996
   {
997
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
998
      return;
999
   }
1000
   v1 = atoi(argv[1]);
1001
   ch = argv[2][0];
1002
   v2 = atoi(argv[3]);
1003
   if(ch == '+')
1004
      v1 += v2;
1005
   else if(ch == '-')
1006
      v1 -= v2;
1007
   else if(ch == '*')
1008
      v1 *= v2;
1009
   else if(ch == '/')
1010
   {
1011
      if(v2 != 0)
1012
         v1 /= v2;
1013
   }
1014
   IPPrintf(socket, "%d", v1);
1015
}
1016
 
1017
 
1018
static void PingCallback(IPSocket *socket)
1019
{
1020 416 rhoads
   IPSocket *socket2 = (IPSocket*)socket->userPtr;
1021 354 rhoads
   IPClose(socket);
1022
   if(socket2)
1023
      IPPrintf(socket2, "Ping Reply");
1024
   else
1025
      printf("Ping Reply\n");
1026
}
1027
 
1028
 
1029 400 rhoads
static void DnsResultCallback(IPSocket *socket, uint8 *arg, int ipIn)
1030 354 rhoads
{
1031
   char buf[COMMAND_BUFFER_SIZE];
1032 400 rhoads
   IPSocket *socketTelnet = (IPSocket*)arg;
1033 354 rhoads
   IPSocket *socketPing;
1034 400 rhoads
   uint32 ip=ipIn;
1035 354 rhoads
   (void)socket;
1036
 
1037
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
1038
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
1039
   IPPrintf(socketTelnet, buf);
1040
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
1041
   socketPing->userPtr = socketTelnet;
1042
   buf[0] = 'A';
1043
   IPWrite(socketPing, (uint8*)buf, 1);
1044
}
1045
 
1046
 
1047
static void ConsolePing(IPSocket *socket, char *argv[])
1048
{
1049
   int ip0, ip1, ip2, ip3;
1050
 
1051
   if('0' <= argv[1][0] && argv[1][0] <= '9')
1052
   {
1053
      sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
1054
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
1055 400 rhoads
      DnsResultCallback(socket, (uint8*)socket, ip0);
1056 354 rhoads
   }
1057
   else
1058
   {
1059 400 rhoads
      IPResolve(argv[1], DnsResultCallback, (void*)socket);
1060 354 rhoads
      IPPrintf(socket, "Sent DNS request");
1061
   }
1062
}
1063
 
1064
 
1065 400 rhoads
static void ConsoleTransferDone(IPSocket *socket, uint8 *data, int length)
1066 354 rhoads
{
1067
   FILE *file;
1068 400 rhoads
   (void)socket;
1069
 
1070 354 rhoads
   IPPrintf(socketTelnet, "Transfer Done");
1071
   file = fopen(storageFilename, "w");
1072
   if(file)
1073
   {
1074
      fwrite(data, 1, length, file);
1075
      fclose(file);
1076
   }
1077
   if(myStorage)
1078
      free(myStorage);
1079
   myStorage = NULL;
1080
}
1081
 
1082
 
1083
static void ConsoleFtp(IPSocket *socket, char *argv[])
1084
{
1085
   int ip0, ip1, ip2, ip3;
1086
   if(argv[1][0] == 0)
1087
   {
1088
      IPPrintf(socket, "ftp #.#.#.# User Password File");
1089
      return;
1090
   }
1091
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
1092
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
1093
   socketTelnet = socket;
1094
   if(myStorage == NULL)
1095
      myStorage = (uint8*)malloc(STORAGE_SIZE);
1096
   if(myStorage == NULL)
1097
      return;
1098
   strcpy(storageFilename, argv[4]);
1099
   FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
1100
      0, ConsoleTransferDone);
1101
}
1102
 
1103
 
1104
static void ConsoleTftp(IPSocket *socket, char *argv[])
1105
{
1106
   int ip0, ip1, ip2, ip3;
1107
   if(argv[1][0] == 0)
1108
   {
1109
      IPPrintf(socket, "tftp #.#.#.# File");
1110
      return;
1111
   }
1112
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
1113
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
1114
   socketTelnet = socket;
1115
   if(myStorage == NULL)
1116
      myStorage = (uint8*)malloc(STORAGE_SIZE);
1117
   if(myStorage == NULL)
1118
      return;
1119
   strcpy(storageFilename, argv[2]);
1120
   TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
1121
}
1122
 
1123
 
1124
static void ConsoleMkfile(IPSocket *socket, char *argv[])
1125
{
1126
   OS_FILE *file;
1127
   (void)argv;
1128
   file = fopen("myfile.txt", "w");
1129
   fwrite("Hello World!", 1, 12, file);
1130
   fclose(file);
1131
   IPPrintf(socket, "Created myfile.txt");
1132
}
1133
 
1134
 
1135 362 rhoads
static void ConsoleUptime(IPSocket *socket, char *argv[])
1136
{
1137 416 rhoads
   unsigned int ticks;
1138 362 rhoads
   int days, hours, minutes, seconds;
1139
   (void)argv;
1140 366 rhoads
   //ticks per sec = 25E6/2^18 = 95.36743 -> 10.48576 ms/tick
1141 416 rhoads
   ticks = OS_ThreadTime();      // 1/(1-95.3674/95) = -259
1142
   seconds = (ticks - ticks / 259) / 95;
1143 362 rhoads
   minutes = seconds / 60 % 60;
1144
   hours = seconds / 3600 % 24;
1145
   days = seconds / 3600 / 24;
1146
   seconds %= 60;
1147
   IPPrintf(socket, "%d days %2d:%2d:%2d\n", days, hours, minutes, seconds);
1148
}
1149
 
1150
 
1151
static void ConsoleDump(IPSocket *socket, char *argv[])
1152
{
1153
   FILE *fileIn;
1154
   uint8 buf[16];
1155
   int bytes, i, j;
1156
 
1157
   fileIn = fopen(argv[1], "r");
1158
   if(fileIn == NULL)
1159
      return;
1160
   for(j = 0; j < 1024*1024*16; j += 16)
1161
   {
1162
      bytes = fread(buf, 1, 16, fileIn);
1163
      if(bytes == 0)
1164
         break;
1165
      IPPrintf(socket, "%8x ", j);
1166
      for(i = 0; i < bytes; ++i)
1167
         IPPrintf(socket, "%2x ", buf[i]);
1168
      for( ; i < 16; ++i)
1169
         IPPrintf(socket, "   ");
1170
      for(i = 0; i < bytes; ++i)
1171
      {
1172
         if(isprint(buf[i]))
1173
            IPPrintf(socket, "%c", buf[i]);
1174
         else
1175
            IPPrintf(socket, ".");
1176
      }
1177
      IPPrintf(socket, "\n");
1178
   }
1179
   fclose(fileIn);
1180
}
1181
 
1182
 
1183
static void ConsoleGrep(IPSocket *socket, char *argv[])
1184
{
1185
   FILE *fileIn;
1186
   char buf[200];
1187
   int bytes;
1188
   char *ptr, *ptrEnd;
1189
 
1190
   if(argv[1][0] == 0 || argv[2][0] == 0)
1191
   {
1192
      IPPrintf(socket, "Usage: grep pattern file\n");
1193
      return;
1194
   }
1195
   fileIn = fopen(argv[2], "r");
1196
   if(fileIn == NULL)
1197
      return;
1198
   bytes = 0;
1199
   for(;;)
1200
   {
1201
      bytes += fread(buf + bytes, 1, sizeof(buf) - bytes - 1, fileIn);
1202
      if(bytes == 0)
1203
         break;
1204
      buf[bytes] = 0;
1205
      ptrEnd = strstr(buf, "\r");
1206
      if(ptrEnd == NULL)
1207
         ptrEnd = strstr(buf, "\n");
1208
      if(ptrEnd)
1209
      {
1210
         *ptrEnd = 0;
1211
         if(*++ptrEnd == '\n')
1212
            ++ptrEnd;
1213
      }
1214
      ptr = strstr(buf, argv[1]);
1215
      if(ptr)
1216
         IPPrintf(socket, "%s\n", buf);
1217
      if(ptrEnd)
1218
      {
1219
         bytes = strlen(ptrEnd);
1220
         memcpy(buf, ptrEnd, bytes);
1221
      }
1222
      else
1223
      {
1224
         bytes = 0;
1225
      }
1226
   }
1227
   fclose(fileIn);
1228
}
1229
 
1230
 
1231 416 rhoads
#ifndef EXCLUDE_DLL
1232
#ifndef WIN32
1233
#define DLL_SETUP
1234 354 rhoads
#include "dll.h"
1235 416 rhoads
#else
1236
typedef void *(*DllFunc)();
1237
DllFunc DllFuncList[1];
1238
#define ntohl(A) (((A)>>24)|(((A)&0x00ff0000)>>8)|(((A)&0xff00)<<8)|((A)<<24))
1239
#define ntohs(A) (uint16)((((A)&0xff00)>>8)|((A)<<8))
1240
#endif
1241 354 rhoads
 
1242 416 rhoads
typedef struct
1243
{
1244
   uint8 e_ident[16];
1245
   uint16 e_e_type;
1246
   uint16 e_machine;
1247
   uint32 e_version;
1248
   uint32 e_entry;
1249
   uint32 e_phoff;
1250
   uint32 e_shoff;
1251
   uint32 e_flags;
1252
   uint16 e_ehsize;
1253
   uint16 e_phentsize;
1254
   uint16 e_phnum;
1255
   uint16 e_shentsize;
1256
   uint16 e_shnum;
1257
   uint16 e_shstrndx;
1258
} ElfHeader;
1259
 
1260
typedef struct
1261
{
1262
   uint32 p_type;
1263
   uint32 p_offset;
1264
   uint32 p_vaddr;
1265
   uint32 p_paddr;
1266
   uint32 p_filesz;
1267
   uint32 p_memsz;
1268
   uint32 p_flags;
1269
   uint32 p_align;
1270
} Elf32_Phdr;
1271
 
1272
 
1273
static unsigned int ConsoleLoadElf(FILE *file, uint8 *ptr)
1274
{
1275
   int i;
1276
   ElfHeader *elfHeader = (ElfHeader*)ptr;
1277
   Elf32_Phdr *elfProgram;
1278
#ifdef WIN32
1279
   elfHeader->e_entry = ntohl(elfHeader->e_entry);
1280
   elfHeader->e_phoff = ntohl(elfHeader->e_phoff);
1281
   elfHeader->e_phentsize = ntohs(elfHeader->e_phentsize);
1282
   elfHeader->e_phnum = ntohs(elfHeader->e_phnum);
1283
#endif
1284
   //printf("Entry=0x%x ", elfHeader->e_entry);
1285
   OS_ThreadSleep(10);
1286
 
1287
   for(i = 0; i < elfHeader->e_phnum; ++i)
1288
   {
1289
      elfProgram = (Elf32_Phdr*)(ptr + elfHeader->e_phoff +
1290
                         elfHeader->e_phentsize * i);
1291
#ifdef WIN32
1292
      elfProgram->p_offset = ntohl(elfProgram->p_offset);
1293
      elfProgram->p_vaddr = ntohl(elfProgram->p_vaddr);
1294
      elfProgram->p_filesz = ntohl(elfProgram->p_filesz);
1295
#endif
1296
      //printf("0x%x 0x%x 0x%x\n", elfProgram->p_vaddr, elfProgram->p_offset, elfProgram->p_filesz);
1297
      fseek(file, elfProgram->p_offset, 0);
1298
#ifndef WIN32
1299
      fread((char*)elfProgram->p_vaddr, 1, elfProgram->p_filesz, file);
1300
#endif
1301
   }
1302
   return elfHeader->e_entry;
1303
}
1304
 
1305
 
1306 354 rhoads
static void ConsoleRun(IPSocket *socket, char *argv[])
1307
{
1308
   FILE *file;
1309 416 rhoads
   int bytes, i, run=0;
1310
   uint8 code[256];
1311 354 rhoads
   DllFunc funcPtr;
1312
 
1313
   if(strcmp(argv[0], "run") == 0)
1314 416 rhoads
   {
1315
      run = 1;
1316 354 rhoads
      ++argv;
1317 416 rhoads
   }
1318 354 rhoads
   file = fopen(argv[0], "r");
1319
   if(file == NULL)
1320
   {
1321
      IPPrintf(socket, "Can't find %s", argv[0]);
1322
      return;
1323
   }
1324
 
1325 416 rhoads
   memset(code, 0, sizeof(code));
1326
   bytes = fread(code, 1, sizeof(code), file);  //load first bytes
1327
   if(strncmp((char*)code + 1, "ELF", 3) == 0)
1328 362 rhoads
   {
1329 416 rhoads
      funcPtr = (DllFunc)ConsoleLoadElf(file, code);
1330
      fclose(file);
1331
      argv[-1] = (char*)DllFuncList;  //DllF = argv[-1]
1332
      argv[-2] = (char*)socket;
1333
      for(i = 0; i < 10; ++i)
1334
      {
1335
         if(argv[i] == 0)
1336
            break;
1337
      }
1338
#ifndef WIN32
1339
      funcPtr(i, argv);
1340
#endif
1341 362 rhoads
      return;
1342
   }
1343 416 rhoads
   if(run == 0)
1344 354 rhoads
   {
1345 416 rhoads
      fclose(file);
1346
      return;
1347 354 rhoads
   }
1348
 
1349 416 rhoads
   socket->fileIn = file;       //script file
1350
   fseek(file, 0, 0);
1351 354 rhoads
}
1352
 
1353
 
1354
typedef struct NameValue_t {
1355
   struct NameValue_t *next;
1356
   void *value;
1357 416 rhoads
   char name[4];
1358 354 rhoads
} NameValue_t;
1359
 
1360
//Find the value associated with the name
1361
void *IPNameValue(const char *name, void *value)
1362
{
1363
   static NameValue_t *head;
1364
   NameValue_t *node;
1365 416 rhoads
   OS_SemaphorePend(semProtect, OS_WAIT_FOREVER);
1366 354 rhoads
   for(node = head; node; node = node->next)
1367
   {
1368
      if(strcmp(node->name, name) == 0)
1369
         break;
1370
   }
1371
   if(node == NULL)
1372
   {
1373
      node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
1374
      if(node == NULL)
1375
         return NULL;
1376
      strcpy(node->name, name);
1377
      node->value = value;
1378
      node->next = head;
1379
      head = node;
1380
   }
1381
   if(value)
1382
      node->value = value;
1383 416 rhoads
   OS_SemaphorePost(semProtect);
1384 354 rhoads
   return node->value;
1385
}
1386
#endif
1387
 
1388
 
1389
#ifdef EDIT_FILE
1390
extern void EditFile(IPSocket *socket, char *argv[]);
1391
#endif
1392
 
1393
static TelnetFunc_t MyFuncs[] = {
1394
   {"cat", ConsoleCat},
1395
   {"cp", ConsoleCp},
1396 362 rhoads
   {"dump", ConsoleDump},
1397 354 rhoads
   {"exit", ConsoleExit},
1398 416 rhoads
#ifndef EXCLUDE_FLASH
1399 354 rhoads
   {"flashErase", ConsoleFlashErase},
1400
#endif
1401
   {"ftp", ConsoleFtp},
1402 362 rhoads
   {"grep", ConsoleGrep},
1403 354 rhoads
   {"help", ConsoleHelp},
1404
   {"ls", ConsoleLs},
1405
   {"math", ConsoleMath},
1406
   {"mkdir", ConsoleMkdir},
1407
   {"mkfile", ConsoleMkfile},
1408
   {"ping", ConsolePing},
1409 416 rhoads
   {"reboot", ConsoleReboot},
1410 354 rhoads
   {"rm", ConsoleRm},
1411
   {"tftp", ConsoleTftp},
1412 362 rhoads
   {"uptime", ConsoleUptime},
1413 416 rhoads
#ifndef EXCLUDE_DLL
1414 354 rhoads
   {"run", ConsoleRun},
1415
#endif
1416
#ifdef EDIT_FILE
1417
   {"edit", EditFile},
1418
#endif
1419
   {NULL, NULL}
1420
};
1421
 
1422
 
1423
void ConsoleInit(void)
1424
{
1425
   FtpdInit(1);
1426
   TftpdInit();
1427
   TelnetInit(MyFuncs);
1428
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.