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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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