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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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