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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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