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

Subversion Repositories plasma

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

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
#undef INCLUDE_FILESYS
14
#define INCLUDE_FILESYS
15
#ifdef WIN32
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <ctype.h>
20
#define _LIBC
21
#endif
22
#include "rtos.h"
23
#include "tcpip.h"
24
 
25
#ifdef DLL_SETUP
26
static void ConsoleRun(IPSocket *socket, char *argv[]);
27
#endif
28
 
29
//******************* FTP Server ************************
30
typedef struct {
31
   IPSocket *socket;
32
   int ip, port, bytes, done, canReceive;
33
   FILE *file;
34
} FtpdInfo;
35
 
36
static void FtpdSender(IPSocket *socket)
37
{
38
   unsigned char buf[600];
39
   int i, bytes, bytes2;
40
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
41
 
42
   if(info == NULL || info->done)
43
      return;
44
   fseek(info->file, info->bytes, 0);
45
   for(i = 0; i < 100000; ++i)
46
   {
47
      bytes = fread(buf, 1, 512, info->file);
48
      bytes2 = IPWrite(socket, buf, bytes);
49
      info->bytes += bytes2;
50
      if(bytes != bytes2)
51
         return;
52
      if(bytes < 512)
53
      {
54
         fclose(info->file);
55
         IPClose(socket);
56
         info->done = 1;
57
         IPPrintf(info->socket, "226 Done\r\n");
58
         return;
59
      }
60
   }
61
}
62
 
63
 
64
static void FtpdReceiver(IPSocket *socket)
65
{
66
   unsigned char buf[600];
67
   int bytes, state = socket->state;
68
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
69
 
70
   if(info == NULL || info->done)
71
      return;
72
   do
73
   {
74
      bytes = IPRead(socket, buf, sizeof(buf));
75
      fwrite(buf, 1, bytes, info->file);
76
   } while(bytes);
77
 
78
   if(state > IP_TCP)
79
   {
80
      fclose(info->file);
81
      info->done = 1;
82
      IPPrintf(info->socket, "226 Done\r\n");
83
      IPClose(socket);
84
      return;
85
   }
86
}
87
 
88
 
89
static void FtpdServer(IPSocket *socket)
90
{
91
   uint8 buf[600];
92
   int bytes;
93
   int ip0, ip1, ip2, ip3, port0, port1;
94
   IPSocket *socketOut;
95
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
96
 
97
   if(socket == NULL)
98
      return;
99
   bytes = IPRead(socket, buf, sizeof(buf)-1);
100
   buf[bytes] = 0;
101
   //printf("(%s)\n", buf);
102
   if(socket->userPtr == NULL)
103
   {
104
      info = (FtpdInfo*)malloc(sizeof(FtpdInfo));
105
      if(info == NULL)
106
         return;
107
      memset(info, 0, sizeof(FtpdInfo));
108
      socket->userPtr = info;
109
      info->socket = socket;
110
      socket->timeoutReset = 60;
111
      IPPrintf(socket, "220 Connected to Plasma\r\n");
112
   }
113
   else if(socket->userPtr == (void*)-1)
114
   {
115
      return;
116
   }
117
   else if(strstr((char*)buf, "USER"))
118
   {
119
      if(strstr((char*)buf, "PlasmaSend"))
120
         info->canReceive = 1;
121
      IPPrintf(socket, "331 Password?\r\n");
122
   }
123
   else if(strstr((char*)buf, "PASS"))
124
   {
125
      IPPrintf(socket, "230 Logged in\r\n");
126
   }
127
   else if(strstr((char*)buf, "PORT"))
128
   {
129
      sscanf((char*)buf + 5, "%d,%d,%d,%d,%d,%d", &ip0, &ip1, &ip2, &ip3, &port0, &port1);
130
      info->ip = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
131
      info->port = (port0 << 8) | port1;
132
      //printf("ip=0x%x port=%d\n", info->ip, info->port);
133
      IPPrintf(socket, "200 OK\r\n");
134
   }
135
   else if(strstr((char*)buf, "RETR") || strstr((char*)buf, "STOR"))
136
   {
137
      char *ptr = strstr((char*)buf, "\r");
138
      if(ptr)
139
         *ptr = 0;
140
      info->file = NULL;
141
      info->bytes = 0;
142
      info->done = 0;
143
      if(strstr((char*)buf, "RETR"))
144
         info->file = fopen((char*)buf + 5, "rb");
145
      else if(info->canReceive)
146
         info->file = fopen((char*)buf + 5, "wb");
147
      if(info->file)
148
      {
149
         IPPrintf(socket, "150 File ready\r\n");
150
         if(strstr((char*)buf, "RETR"))
151
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdSender);
152
         else
153
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdReceiver);
154
         if(socketOut)
155
            socketOut->userPtr = info;
156
      }
157
      else
158
      {
159
         IPPrintf(socket, "500 Error\r\n");
160
      }
161
   }
162
   else if(strstr((char*)buf, "QUIT"))
163
   {
164
      if(socket->userPtr)
165
         free(socket->userPtr);
166
      socket->userPtr = (void*)-1;
167
      IPPrintf(socket, "221 Bye\r\n");
168
      IPClose(socket);
169
   }
170
   else if(bytes)
171
   {
172
      IPPrintf(socket, "500 Error\r\n");
173
   }
174
}
175
 
176
 
177
void FtpdInit(int UseFiles)
178
{
179
   (void)UseFiles;
180
   IPOpen(IP_MODE_TCP, 0, 21, FtpdServer);
181
}
182
 
183
 
184
//******************* FTP Client ************************
185
 
186
typedef struct {
187
   uint32 ip, port;
188
   char user[80], passwd[80], filename[80];
189
   uint8 *buf;
190
   int size, bytes, send, state;
191
} FtpInfo;
192
 
193
 
194
static void FtpCallbackTransfer(IPSocket *socket)
195
{
196
   int bytes, state = socket->state;
197
   FtpInfo *info = (FtpInfo*)socket->userPtr;
198
 
199
   //printf("FtpCallbackTransfer\n");
200
   if(info == NULL)
201
      return;
202
   bytes = info->size - info->bytes;
203
   if(info->send == 0)
204
      bytes = IPRead(socket, info->buf + info->bytes, bytes);
205
   else
206
      bytes = IPWrite(socket, info->buf + info->bytes, bytes);
207
   info->bytes += bytes;
208
   if(info->bytes == info->size || (bytes == 0 && state > IP_TCP))
209
   {
210
      socket->userFunc(info->buf, info->bytes);
211
      free(info);
212
      socket->userPtr = NULL;
213
      IPClose(socket);
214
   }
215
}
216
 
217
 
218
static void FtpCallback(IPSocket *socket)
219
{
220
   char buf[600];
221
   FtpInfo *info = (FtpInfo*)socket->userPtr;
222
   int bytes, value;
223
 
224
   bytes = IPRead(socket, (uint8*)buf, sizeof(buf)-1);
225
   if(bytes == 0)
226
      return;
227
   buf[bytes] = 0;
228
   sscanf(buf, "%d", &value);
229
   if(bytes > 2)
230
      buf[bytes-2] = 0;
231
   //printf("FtpCallback(%d:%s)\n", socket->userData, buf);
232
   if(value / 100 != 2 && value / 100 != 3)
233
      return;
234
   buf[0] = 0;
235
   switch(socket->userData) {
236
   case 0:
237
      sprintf(buf, "USER %s\r\n", info->user);
238
      socket->userData = 1;
239
      break;
240
   case 1:
241
      sprintf(buf, "PASS %s\r\n", info->passwd);
242
      socket->userData = 2;
243
      if(value == 331)
244
         break;  //possible fall-through
245
   case 2:
246
      sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
247
         info->ip >> 24, (uint8)(info->ip >> 16),
248
         (uint8)(info->ip >> 8), (uint8)info->ip,
249
         (uint8)(info->port >> 8), (uint8)info->port);
250
      socket->userData = 3;
251
      break;
252
   case 3:
253
      if(info->send == 0)
254
         sprintf(buf, "RETR %s\r\n", info->filename);
255
      else
256
         sprintf(buf, "STOR %s\r\n", info->filename);
257
      socket->userData = 4;
258
      break;
259
   case 4:
260
      sprintf(buf, "QUIT\r\n");
261
      socket->userData = 9;
262
      break;
263
   }
264
   IPWrite(socket, (uint8*)buf, strlen(buf));
265
   IPWriteFlush(socket);
266
   if(socket->userData == 9)
267
      IPClose(socket);
268
}
269
 
270
 
271
IPSocket *FtpTransfer(uint32 ip, char *user, char *passwd,
272
                      char *filename, uint8 *buf, int size,
273
                      int send, void (*callback)(uint8 *data, int size))
274
{
275
   IPSocket *socket, *socketTransfer;
276
   FtpInfo *info;
277
   uint8 *ptr;
278
   info = (FtpInfo*)malloc(sizeof(FtpInfo));
279
   if(info == NULL)
280
      return NULL;
281
   strncpy(info->user, user, 80);
282
   strncpy(info->passwd, passwd, 80);
283
   strncpy(info->filename, filename, 80);
284
   info->buf = buf;
285
   info->size = size;
286
   info->send = send;
287
   info->bytes = 0;
288
   info->state = 0;
289
   info->port = 2000;
290
   socketTransfer = IPOpen(IP_MODE_TCP, 0, info->port, FtpCallbackTransfer);
291
   socketTransfer->userPtr = info;
292
   socketTransfer->userFunc = callback;
293
   socket = IPOpen(IP_MODE_TCP, ip, 21, FtpCallback);
294
   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
         socket->userFunc(socket->userPtr, length);
386
         IPClose(socket);
387
      }
388
   }
389
}
390
 
391
 
392
IPSocket *TftpTransfer(uint32 ip, char *filename, uint8 *buffer, int size,
393
                       void (*callback)(uint8 *data, int bytes))
394
{
395
   IPSocket *socket;
396
   uint8 buf[512+4];
397
   int bytes;
398
   socket = IPOpen(IP_MODE_UDP, ip, 69, TftpCallback);
399
   socket->userPtr = buffer;
400
   socket->userData = size;
401
   socket->userFunc = callback;
402
   buf[0] = 0;
403
   buf[1] = 1; //read
404
   strcpy((char*)buf+2, filename);
405
   bytes = strlen(filename);
406
   strcpy((char*)buf+bytes+3, "octet");
407
   IPWrite(socket, buf, bytes+9);
408
   return socket;
409
}
410
 
411
 
412
//******************* Telnet Server ************************
413
 
414
#define COMMAND_BUFFER_SIZE 80
415
#define COMMAND_BUFFER_COUNT 10
416
static char CommandHistory[400];
417
static char *CommandPtr[COMMAND_BUFFER_COUNT];
418
static int CommandIndex;
419
 
420
typedef void (*ConsoleFunc)(IPSocket *socket, char *argv[]);
421
typedef struct {
422
   char *name;
423
   ConsoleFunc func;
424
} TelnetFunc_t;
425
static TelnetFunc_t *TelnetFuncList;
426
 
427
 
428
static void TelnetServer(IPSocket *socket)
429
{
430
   uint8 buf[COMMAND_BUFFER_SIZE+4];
431
   char bufOut[32];
432
   int bytes, i, j, length, found;
433
   char *ptr, *command = socket->userPtr;
434
   char *argv[10];
435
 
436
   if(socket->state > IP_TCP)
437
      return;
438
   bytes = IPRead(socket, buf, sizeof(buf)-1);
439
   if(command == NULL)
440
   {
441
      socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
442
      if(command == NULL)
443
      {
444
         IPClose(socket);
445
         return;
446
      }
447
      socket->timeoutReset = 300;
448
      buf[0] = 255; //IAC
449
      buf[1] = 251; //WILL
450
      buf[2] = 3;   //suppress go ahead
451
      buf[3] = 255; //IAC
452
      buf[4] = 251; //WILL
453
      buf[5] = 1;   //echo
454
      strcpy((char*)buf+6, "Welcome to Plasma.\r\n-> ");
455
      IPWrite(socket, buf, 6+23);
456
      IPWriteFlush(socket);
457
      command[0] = 0;
458
      return;
459
   }
460
   socket->dontFlush = 0;
461
   buf[bytes] = 0;
462
   length = (int)strlen(command);
463
   for(j = 0; j < bytes; ++j)
464
   {
465
      if(buf[j] == 255)
466
         return;
467
      if(buf[j] == 8 || (buf[j] == 27 && buf[j+2] == 'D'))
468
      {
469
         if(buf[j] == 27)
470
            j += 2;
471
         if(length)
472
         {
473
            // Backspace
474
            command[--length] = 0;
475
            bufOut[0] = 8;
476
            bufOut[1] = ' ';
477
            bufOut[2] = 8;
478
            IPWrite(socket, (uint8*)bufOut, 3);
479
         }
480
      }
481
      else if(buf[j] == 27)
482
      {
483
         // Command History
484
         if(buf[j+2] == 'A')
485
         {
486
            if(++CommandIndex > COMMAND_BUFFER_COUNT)
487
               CommandIndex = COMMAND_BUFFER_COUNT;
488
         }
489
         else if(buf[j+2] == 'B')
490
         {
491
            if(--CommandIndex < 0)
492
               CommandIndex = 0;
493
         }
494
         else
495
            return;
496
         bufOut[0] = 8;
497
         bufOut[1] = ' ';
498
         bufOut[2] = 8;
499
         for(i = 0; i < length; ++i)
500
            IPWrite(socket, (uint8*)bufOut, 3);
501
         command[0] = 0;
502
         if(CommandIndex && CommandPtr[CommandIndex-1])
503
            strncat(command, CommandPtr[CommandIndex-1], COMMAND_BUFFER_SIZE-1);
504
         length = (int)strlen(command);
505
         IPWrite(socket, (uint8*)command, length);
506
         j += 2;
507
      }
508
      else
509
      {
510
         if(buf[j] == 0)
511
            buf[j] = '\n';  //Linux support
512
         if(length < COMMAND_BUFFER_SIZE-4 || (length <
513
            COMMAND_BUFFER_SIZE-2 && (buf[j] == '\r' || buf[j] == '\n')))
514
         {
515
            IPWrite(socket, buf+j, 1);
516
            command[length] = buf[j];
517
            command[++length] = 0;
518
         }
519
      }
520
      ptr = strstr(command, "\r\n");
521
      if(ptr)
522
      {
523
         // Save command in CommandHistory
524
         ptr[0] = 0;
525
         length = (int)strlen(command);
526
         if(length == 0)
527
         {
528
            IPPrintf(socket, "-> ");
529
            continue;
530
         }
531
         if(length < COMMAND_BUFFER_SIZE)
532
         {
533
            memmove(CommandHistory + length + 1, CommandHistory,
534
               sizeof(CommandHistory) - length - 1);
535
            strcpy(CommandHistory, command);
536
            CommandHistory[sizeof(CommandHistory)-1] = 0;
537
            for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
538
            {
539
               if(CommandPtr[i] == NULL || CommandPtr[i] + length + 1 >=
540
                  CommandHistory + sizeof(CommandHistory))
541
                  CommandPtr[i+1] = NULL;
542
               else
543
                  CommandPtr[i+1] = CommandPtr[i] + length + 1;
544
            }
545
            CommandPtr[0] = CommandHistory;
546
         }
547
 
548
         //Start command
549
         for(i = 0; i < 10; ++i)
550
            argv[i] = "";
551
         i = 0;
552
         argv[i++] = command;
553
         for(ptr = command; *ptr && i < 10; ++ptr)
554
         {
555
            if(*ptr == ' ')
556
            {
557
               *ptr = 0;
558
               argv[i++] = ptr + 1;
559
            }
560
         }
561
         if(argv[0][0] == 0)
562
         {
563
            IPPrintf(socket, "-> ");
564
            continue;
565
         }
566
         found = 0;
567
         for(i = 0; TelnetFuncList[i].name; ++i)
568
         {
569
            if(strcmp(command, TelnetFuncList[i].name) == 0 &&
570
               TelnetFuncList[i].func)
571
            {
572
               found = 1;
573
               TelnetFuncList[i].func(socket, argv);
574
               break;
575
            }
576
         }
577
#ifdef DLL_SETUP
578
         if(found == 0)
579
         {
580
            strcpy((char*)buf, "/flash/bin/");
581
            strcat((char*)buf, argv[0]);
582
            argv[0] = (char*)buf;
583
            ConsoleRun(socket, argv);
584
         }
585
#endif
586
         if(socket->state > IP_TCP)
587
            return;
588
         command[0] = 0;
589
         length = 0;
590
         CommandIndex = 0;
591
         if(socket->dontFlush == 0)
592
            IPPrintf(socket, "\r\n-> ");
593
      } //command entered
594
   } //bytes
595
   IPWriteFlush(socket);
596
}
597
 
598
 
599
void TelnetInit(TelnetFunc_t *funcList)
600
{
601
   IPSocket *socket;
602
   TelnetFuncList = funcList;
603
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
604
}
605
 
606
 
607
//******************* Console ************************
608
 
609
#define STORAGE_SIZE 1024*64
610
static uint8 *myStorage;
611
static IPSocket *socketTelnet;
612
static char storageFilename[60];
613
 
614
 
615
static void ConsoleHelp(IPSocket *socket, char *argv[])
616
{
617
   char buf[200];
618
   int i;
619
   (void)argv;
620
   strcpy(buf, "Commands: ");
621
   for(i = 0; TelnetFuncList[i].name; ++i)
622
   {
623
      if(TelnetFuncList[i].func)
624
      {
625
         if(i)
626
            strcat(buf, ", ");
627
         strcat(buf, TelnetFuncList[i].name);
628
      }
629
   }
630
   IPPrintf(socket, buf);
631
}
632
 
633
 
634
static void ConsoleExit(IPSocket *socket, char *argv[])
635
{
636
   free(argv[0]);
637
   socket->userPtr = NULL;
638
   IPClose(socket);
639
}
640
 
641
 
642
static void ConsoleCat(IPSocket *socket, char *argv[])
643
{
644
   FILE *file;
645
   uint8 buf[200];
646
   int bytes;
647
 
648
   file = fopen(argv[1], "r");
649
   if(file == NULL)
650
      return;
651
   for(;;)
652
   {
653
      bytes = fread(buf, 1, sizeof(buf), file);
654
      if(bytes == 0)
655
         break;
656
      IPWrite(socket, buf, bytes);
657
   }
658
   fclose(file);
659
}
660
 
661
 
662
static void ConsoleCp(IPSocket *socket, char *argv[])
663
{
664
   FILE *fileIn, *fileOut;
665
   uint8 buf[200];
666
   int bytes;
667
   (void)socket;
668
 
669
   fileIn = fopen(argv[1], "r");
670
   if(fileIn == NULL)
671
      return;
672
   fileOut = fopen(argv[2], "w");
673
   if(fileOut)
674
   {
675
      for(;;)
676
      {
677
         bytes = fread(buf, 1, sizeof(buf), fileIn);
678
         if(bytes == 0)
679
            break;
680
         fwrite(buf, 1, bytes, fileOut);
681
      }
682
      fclose(fileOut);
683
   }
684
   fclose(fileIn);
685
}
686
 
687
 
688
static void ConsoleRm(IPSocket *socket, char *argv[])
689
{
690
   (void)socket;
691
   OS_fdelete(argv[1]);
692
}
693
 
694
 
695
static void ConsoleMkdir(IPSocket *socket, char *argv[])
696
{
697
   (void)socket;
698
   OS_fmkdir(argv[1]);
699
}
700
 
701
 
702
static void ConsoleLs(IPSocket *socket, char *argv[])
703
{
704
   FILE *file;
705
   char buf[200], buf2[80];
706
   int bytes, width;
707
 
708
   file = fopen(argv[1], "r");
709
   if(file == NULL)
710
      return;
711
   width = 0;
712
   for(;;)
713
   {
714
      bytes = OS_fdir(file, buf);
715
      if(bytes)
716
         break;
717
      if(buf[0] == 255)
718
         continue;
719
      bytes = OS_flength(buf);
720
      sprintf(buf2, "%s:%d                    ", buf, bytes);
721
      bytes = strlen(buf2);
722
      bytes -= bytes % 20;
723
      buf2[bytes] = 0;
724
      width += bytes;
725
      if(width == 80)
726
         --bytes;
727
      if(width > 80)
728
      {
729
         IPPrintf(socket, "\n");
730
         width = bytes;
731
      }
732
      IPWrite(socket, (uint8*)buf2, bytes);
733
   }
734
   fclose(file);
735
}
736
 
737
 
738
#ifdef INCLUDE_FLASH
739
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
740
{
741
   int bytes;
742
   (void)argv;
743
   IPPrintf(socket, "\r\nErasing");
744
   for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
745
   {
746
      IPPrintf(socket, ".");
747
      FlashErase(bytes);
748
   }
749
   IPPrintf(socket, "\r\nMust Reboot\r\n");
750
   OS_ThreadSleep(OS_WAIT_FOREVER);
751
}
752
#endif
753
 
754
 
755
static void ConsoleMath(IPSocket *socket, char *argv[])
756
{
757
   int v1, v2, ch;
758
   if(argv[3][0] == 0)
759
   {
760
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
761
      return;
762
   }
763
   v1 = atoi(argv[1]);
764
   ch = argv[2][0];
765
   v2 = atoi(argv[3]);
766
   if(ch == '+')
767
      v1 += v2;
768
   else if(ch == '-')
769
      v1 -= v2;
770
   else if(ch == '*')
771
      v1 *= v2;
772
   else if(ch == '/')
773
   {
774
      if(v2 != 0)
775
         v1 /= v2;
776
   }
777
   IPPrintf(socket, "%d", v1);
778
}
779
 
780
 
781
static void PingCallback(IPSocket *socket)
782
{
783
   IPSocket *socket2 = socket->userPtr;
784
   IPClose(socket);
785
   if(socket2)
786
      IPPrintf(socket2, "Ping Reply");
787
   else
788
      printf("Ping Reply\n");
789
}
790
 
791
 
792
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
793
{
794
   char buf[COMMAND_BUFFER_SIZE];
795
   IPSocket *socketTelnet = arg;
796
   IPSocket *socketPing;
797
   (void)socket;
798
 
799
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
800
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
801
   IPPrintf(socketTelnet, buf);
802
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
803
   socketPing->userPtr = socketTelnet;
804
   buf[0] = 'A';
805
   IPWrite(socketPing, (uint8*)buf, 1);
806
}
807
 
808
 
809
static void ConsolePing(IPSocket *socket, char *argv[])
810
{
811
   int ip0, ip1, ip2, ip3;
812
 
813
   if('0' <= argv[1][0] && argv[1][0] <= '9')
814
   {
815
      sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
816
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
817
      DnsResultCallback(socket, ip0, socket);
818
   }
819
   else
820
   {
821
      IPResolve(argv[1], DnsResultCallback, socket);
822
      IPPrintf(socket, "Sent DNS request");
823
   }
824
}
825
 
826
 
827
static void ConsoleTransferDone(uint8 *data, int length)
828
{
829
   FILE *file;
830
   IPPrintf(socketTelnet, "Transfer Done");
831
   file = fopen(storageFilename, "w");
832
   if(file)
833
   {
834
      fwrite(data, 1, length, file);
835
      fclose(file);
836
   }
837
   if(myStorage)
838
      free(myStorage);
839
   myStorage = NULL;
840
}
841
 
842
 
843
static void ConsoleFtp(IPSocket *socket, char *argv[])
844
{
845
   int ip0, ip1, ip2, ip3;
846
   if(argv[1][0] == 0)
847
   {
848
      IPPrintf(socket, "ftp #.#.#.# User Password File");
849
      return;
850
   }
851
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
852
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
853
   socketTelnet = socket;
854
   if(myStorage == NULL)
855
      myStorage = (uint8*)malloc(STORAGE_SIZE);
856
   if(myStorage == NULL)
857
      return;
858
   strcpy(storageFilename, argv[4]);
859
   FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
860
      0, ConsoleTransferDone);
861
}
862
 
863
 
864
static void ConsoleTftp(IPSocket *socket, char *argv[])
865
{
866
   int ip0, ip1, ip2, ip3;
867
   if(argv[1][0] == 0)
868
   {
869
      IPPrintf(socket, "tftp #.#.#.# File");
870
      return;
871
   }
872
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
873
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
874
   socketTelnet = socket;
875
   if(myStorage == NULL)
876
      myStorage = (uint8*)malloc(STORAGE_SIZE);
877
   if(myStorage == NULL)
878
      return;
879
   strcpy(storageFilename, argv[2]);
880
   TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
881
}
882
 
883
 
884
static void ConsoleMkfile(IPSocket *socket, char *argv[])
885
{
886
   OS_FILE *file;
887
   (void)argv;
888
   file = fopen("myfile.txt", "w");
889
   fwrite("Hello World!", 1, 12, file);
890
   fclose(file);
891
   IPPrintf(socket, "Created myfile.txt");
892
}
893
 
894
 
895
#ifdef DLL_SETUP
896
#include "dll.h"
897
 
898
static void ConsoleRun(IPSocket *socket, char *argv[])
899
{
900
   FILE *file;
901
   int bytes, i;
902
   uint8 code[128];
903
   DllFunc funcPtr;
904
   DllInfo info;
905
   int *bss;
906
   char *command, *ptr;
907
 
908
   if(strcmp(argv[0], "run") == 0)
909
      ++argv;
910
   info.socket = socket;
911
   info.dllFuncList = DllFuncList;
912
   file = fopen(argv[0], "r");
913
   if(file == NULL)
914
   {
915
      IPPrintf(socket, "Can't find %s", argv[0]);
916
      return;
917
   }
918
 
919
   bytes = fread(code, 1, sizeof(code), file);
920
   funcPtr = (DllFunc)code;
921
   funcPtr(&info);     //call entry() to fill in info
922
 
923
   memcpy(info.entry, code, bytes);
924
   bytes = fread(info.entry + bytes, 1, 1024*1024*8, file) + bytes;
925
   fclose(file);
926
   printf("address=0x%x bytes=%d\n", (int)info.entry, bytes);
927
   for(bss = info.bssStart; bss < info.bssEnd; ++bss)
928
      *bss = 0;
929
   *info.pDllF = DllFuncList;
930
 
931
   //Register new command
932
   command = argv[0];
933
   for(;;)
934
   {
935
      ptr = strstr(command, "/");
936
      if(ptr == NULL)
937
         break;
938
      command = ptr + 1;
939
   }
940
   for(i = 0; TelnetFuncList[i].name; ++i)
941
   {
942
      if(TelnetFuncList[i].name[0] == 0 ||
943
         strcmp(TelnetFuncList[i].name, command) == 0)
944
      {
945
         TelnetFuncList[i].name = (char*)malloc(40);
946
         strcpy(TelnetFuncList[i].name, command);
947
         TelnetFuncList[i].func = (ConsoleFunc)info.startPtr;
948
         break;
949
      }
950
   }
951
 
952
   socket->userFunc = socket->funcPtr;
953
   info.startPtr(socket, argv);
954
}
955
 
956
 
957
typedef struct NameValue_t {
958
   struct NameValue_t *next;
959
   void *value;
960
   char name[1];
961
} NameValue_t;
962
 
963
//Find the value associated with the name
964
void *IPNameValue(const char *name, void *value)
965
{
966
   static NameValue_t *head;
967
   NameValue_t *node;
968
   for(node = head; node; node = node->next)
969
   {
970
      if(strcmp(node->name, name) == 0)
971
         break;
972
   }
973
   if(node == NULL)
974
   {
975
      node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
976
      if(node == NULL)
977
         return NULL;
978
      strcpy(node->name, name);
979
      node->value = value;
980
      node->next = head;
981
      head = node;
982
   }
983
   if(value)
984
      node->value = value;
985
   return node->value;
986
}
987
#endif
988
 
989
 
990
#ifdef EDIT_FILE
991
extern void EditFile(IPSocket *socket, char *argv[]);
992
#endif
993
 
994
static TelnetFunc_t MyFuncs[] = {
995
   {"cat", ConsoleCat},
996
   {"cp", ConsoleCp},
997
   {"exit", ConsoleExit},
998
#ifdef INCLUDE_FLASH
999
   {"flashErase", ConsoleFlashErase},
1000
#endif
1001
   {"ftp", ConsoleFtp},
1002
   {"help", ConsoleHelp},
1003
   {"ls", ConsoleLs},
1004
   {"math", ConsoleMath},
1005
   {"mkdir", ConsoleMkdir},
1006
   {"mkfile", ConsoleMkfile},
1007
   {"ping", ConsolePing},
1008
   {"rm", ConsoleRm},
1009
   {"tftp", ConsoleTftp},
1010
#ifdef DLL_SETUP
1011
   {"run", ConsoleRun},
1012
#endif
1013
#ifdef EDIT_FILE
1014
   {"edit", EditFile},
1015
#endif
1016
   {"", NULL},
1017
   {"", NULL},
1018
   {"", NULL},
1019
   {"", NULL},
1020
   {"", NULL},
1021
   {"", NULL},
1022
   {"", NULL},
1023
   {"", NULL},
1024
   {"", NULL},
1025
   {"", NULL},
1026
   {"", NULL},
1027
   {NULL, NULL}
1028
};
1029
 
1030
 
1031
void ConsoleInit(void)
1032
{
1033
   FtpdInit(1);
1034
   TftpdInit();
1035
   TelnetInit(MyFuncs);
1036
}

powered by: WebSVN 2.1.0

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