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

Subversion Repositories plasma

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

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 362 rhoads
   int bytes, i, j, k, length, found;
433 354 rhoads
   char *ptr, *command = socket->userPtr;
434
   char *argv[10];
435
 
436
   if(socket->state > IP_TCP)
437
      return;
438 362 rhoads
   for(;;)
439 354 rhoads
   {
440 362 rhoads
      bytes = IPRead(socket, buf, sizeof(buf)-1);
441 354 rhoads
      if(command == NULL)
442
      {
443 362 rhoads
         socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
444
         if(command == NULL)
445
         {
446
            IPClose(socket);
447
            return;
448
         }
449
         socket->timeoutReset = 300;
450
         buf[0] = 255; //IAC
451
         buf[1] = 251; //WILL
452
         buf[2] = 3;   //suppress go ahead
453
         buf[3] = 255; //IAC
454
         buf[4] = 251; //WILL
455
         buf[5] = 1;   //echo
456
         strcpy((char*)buf+6, "Welcome to Plasma.\r\n-> ");
457
         IPWrite(socket, buf, 6+23);
458
         IPWriteFlush(socket);
459
         command[0] = 0;
460 354 rhoads
         return;
461
      }
462 362 rhoads
      if(bytes == 0)
463 354 rhoads
         return;
464 362 rhoads
      socket->dontFlush = 0;
465
      buf[bytes] = 0;
466
      length = (int)strlen(command);
467
      for(j = 0; j < bytes; ++j)
468 354 rhoads
      {
469 362 rhoads
         if(buf[j] == 255)
470 354 rhoads
            return;
471 362 rhoads
         if(buf[j] == 8 || (buf[j] == 27 && buf[j+2] == 'D'))
472 354 rhoads
         {
473 362 rhoads
            if(buf[j] == 27)
474
               j += 2;
475
            if(length)
476
            {
477
               // Backspace
478
               command[--length] = 0;
479
               bufOut[0] = 8;
480
               bufOut[1] = ' ';
481
               bufOut[2] = 8;
482
               IPWrite(socket, (uint8*)bufOut, 3);
483
            }
484 354 rhoads
         }
485 362 rhoads
         else if(buf[j] == 27)
486 354 rhoads
         {
487 362 rhoads
            // Command History
488
            if(buf[j+2] == 'A')
489 354 rhoads
            {
490 362 rhoads
               if(++CommandIndex > COMMAND_BUFFER_COUNT)
491
                  CommandIndex = COMMAND_BUFFER_COUNT;
492 354 rhoads
            }
493 362 rhoads
            else if(buf[j+2] == 'B')
494
            {
495
               if(--CommandIndex < 0)
496
                  CommandIndex = 0;
497
            }
498
            else
499
               return;
500
            bufOut[0] = 8;
501
            bufOut[1] = ' ';
502
            bufOut[2] = 8;
503
            for(i = 0; i < length; ++i)
504
               IPWrite(socket, (uint8*)bufOut, 3);
505
            command[0] = 0;
506
            if(CommandIndex && CommandPtr[CommandIndex-1])
507
               strncat(command, CommandPtr[CommandIndex-1], COMMAND_BUFFER_SIZE-1);
508
            length = (int)strlen(command);
509
            IPWrite(socket, (uint8*)command, length);
510
            j += 2;
511 354 rhoads
         }
512 362 rhoads
         else
513 354 rhoads
         {
514 362 rhoads
            if(buf[j] == 0)
515
               buf[j] = '\n';  //Linux support
516
            if(length < COMMAND_BUFFER_SIZE-4 || (length <
517
               COMMAND_BUFFER_SIZE-2 && (buf[j] == '\r' || buf[j] == '\n')))
518 354 rhoads
            {
519 362 rhoads
               IPWrite(socket, buf+j, 1);
520
               command[length] = buf[j];
521
               command[++length] = 0;
522 354 rhoads
            }
523
         }
524 362 rhoads
         ptr = strstr(command, "\r\n");
525
         if(ptr)
526 354 rhoads
         {
527 362 rhoads
            // Save command in CommandHistory
528
            ptr[0] = 0;
529
            length = (int)strlen(command);
530
            if(length == 0)
531 354 rhoads
            {
532 362 rhoads
               IPPrintf(socket, "-> ");
533
               continue;
534 354 rhoads
            }
535 362 rhoads
            if(length < COMMAND_BUFFER_SIZE)
536
            {
537
               memmove(CommandHistory + length + 1, CommandHistory,
538
                  sizeof(CommandHistory) - length - 1);
539
               strcpy(CommandHistory, command);
540
               CommandHistory[sizeof(CommandHistory)-1] = 0;
541
               for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
542
               {
543
                  if(CommandPtr[i] == NULL || CommandPtr[i] + length + 1 >=
544
                     CommandHistory + sizeof(CommandHistory))
545
                     CommandPtr[i+1] = NULL;
546
                  else
547
                     CommandPtr[i+1] = CommandPtr[i] + length + 1;
548
               }
549
               CommandPtr[0] = CommandHistory;
550
            }
551
 
552
            //Start command
553
            for(i = 0; i < 10; ++i)
554
               argv[i] = "";
555
            i = 0;
556
            argv[i++] = command;
557
            for(ptr = command; *ptr && i < 10; ++ptr)
558
            {
559
               if(*ptr == ' ')
560
               {
561
                  *ptr = 0;
562
                  argv[i++] = ptr + 1;
563
               }
564
            }
565
            if(argv[0][0] == 0)
566
            {
567
               IPPrintf(socket, "-> ");
568
               continue;
569
            }
570
            found = 0;
571
            for(i = 0; TelnetFuncList[i].name; ++i)
572
            {
573
               if(strcmp(command, TelnetFuncList[i].name) == 0 &&
574
                  TelnetFuncList[i].func)
575
               {
576
                  found = 1;
577
                  for(k = 1; k < 10; ++k)
578
                  {
579
                     if(argv[k][0] == '>' && argv[k][1]) //stdout to file?
580
                     {
581
                        socket->fileOut = fopen(&argv[k][1], "a");
582
                        argv[k] = "";
583
                     }
584
                     if(argv[k][0] == '<' && argv[k][1]) //stdin from file?
585
                     {
586
                        socket->fileIn = fopen(&argv[k][1], "r");
587
                        argv[k] = "";
588
                     }
589
                  }
590
                  TelnetFuncList[i].func(socket, argv);
591
                  if(socket->fileOut)
592
                  {
593
                     fwrite("\r\n", 1, 2, socket->fileOut);
594
                     fclose(socket->fileOut);
595
                  }
596
                  socket->fileOut = NULL;
597
                  break;
598
               }
599
            }
600 354 rhoads
#ifdef DLL_SETUP
601 362 rhoads
            if(found == 0)
602
            {
603
               strcpy((char*)buf, "/flash/bin/");
604
               strcat((char*)buf, argv[0]);
605
               argv[0] = (char*)buf;
606
               ConsoleRun(socket, argv);
607
            }
608 354 rhoads
#endif
609 362 rhoads
            if(socket->state > IP_TCP)
610
               return;
611
            command[0] = 0;
612
            length = 0;
613
            CommandIndex = 0;
614
            if(socket->dontFlush == 0)
615
               IPPrintf(socket, "\r\n-> ");
616
         } //command entered
617
      } //bytes
618
      IPWriteFlush(socket);
619
   }
620 354 rhoads
}
621
 
622
 
623
void TelnetInit(TelnetFunc_t *funcList)
624
{
625
   IPSocket *socket;
626
   TelnetFuncList = funcList;
627
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
628
}
629
 
630
 
631
//******************* Console ************************
632
 
633
#define STORAGE_SIZE 1024*64
634
static uint8 *myStorage;
635
static IPSocket *socketTelnet;
636
static char storageFilename[60];
637
 
638
 
639
static void ConsoleHelp(IPSocket *socket, char *argv[])
640
{
641
   char buf[200];
642
   int i;
643
   (void)argv;
644
   strcpy(buf, "Commands: ");
645
   for(i = 0; TelnetFuncList[i].name; ++i)
646
   {
647
      if(TelnetFuncList[i].func)
648
      {
649
         if(i)
650
            strcat(buf, ", ");
651
         strcat(buf, TelnetFuncList[i].name);
652
      }
653
   }
654
   IPPrintf(socket, buf);
655
}
656
 
657
 
658
static void ConsoleExit(IPSocket *socket, char *argv[])
659
{
660
   free(argv[0]);
661
   socket->userPtr = NULL;
662
   IPClose(socket);
663
}
664
 
665
 
666
static void ConsoleCat(IPSocket *socket, char *argv[])
667
{
668
   FILE *file;
669
   uint8 buf[200];
670
   int bytes;
671
 
672
   file = fopen(argv[1], "r");
673
   if(file == NULL)
674
      return;
675
   for(;;)
676
   {
677
      bytes = fread(buf, 1, sizeof(buf), file);
678
      if(bytes == 0)
679
         break;
680
      IPWrite(socket, buf, bytes);
681
   }
682
   fclose(file);
683
}
684
 
685
 
686
static void ConsoleCp(IPSocket *socket, char *argv[])
687
{
688
   FILE *fileIn, *fileOut;
689
   uint8 buf[200];
690
   int bytes;
691
   (void)socket;
692
 
693
   fileIn = fopen(argv[1], "r");
694
   if(fileIn == NULL)
695
      return;
696
   fileOut = fopen(argv[2], "w");
697
   if(fileOut)
698
   {
699
      for(;;)
700
      {
701
         bytes = fread(buf, 1, sizeof(buf), fileIn);
702
         if(bytes == 0)
703
            break;
704
         fwrite(buf, 1, bytes, fileOut);
705
      }
706
      fclose(fileOut);
707
   }
708
   fclose(fileIn);
709
}
710
 
711
 
712
static void ConsoleRm(IPSocket *socket, char *argv[])
713
{
714
   (void)socket;
715
   OS_fdelete(argv[1]);
716
}
717
 
718
 
719
static void ConsoleMkdir(IPSocket *socket, char *argv[])
720
{
721
   (void)socket;
722
   OS_fmkdir(argv[1]);
723
}
724
 
725
 
726
static void ConsoleLs(IPSocket *socket, char *argv[])
727
{
728
   FILE *file;
729
   char buf[200], buf2[80];
730
   int bytes, width;
731
 
732
   file = fopen(argv[1], "r");
733
   if(file == NULL)
734
      return;
735
   width = 0;
736
   for(;;)
737
   {
738
      bytes = OS_fdir(file, buf);
739
      if(bytes)
740
         break;
741
      if(buf[0] == 255)
742
         continue;
743
      bytes = OS_flength(buf);
744
      sprintf(buf2, "%s:%d                    ", buf, bytes);
745
      bytes = strlen(buf2);
746
      bytes -= bytes % 20;
747
      buf2[bytes] = 0;
748
      width += bytes;
749
      if(width == 80)
750
         --bytes;
751
      if(width > 80)
752
      {
753
         IPPrintf(socket, "\n");
754
         width = bytes;
755
      }
756 362 rhoads
      IPPrintf(socket, "%s", buf2);
757 354 rhoads
   }
758
   fclose(file);
759
}
760
 
761
 
762
#ifdef INCLUDE_FLASH
763
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
764
{
765
   int bytes;
766
   (void)argv;
767
   IPPrintf(socket, "\r\nErasing");
768
   for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
769
   {
770
      IPPrintf(socket, ".");
771
      FlashErase(bytes);
772
   }
773
   IPPrintf(socket, "\r\nMust Reboot\r\n");
774
   OS_ThreadSleep(OS_WAIT_FOREVER);
775
}
776
#endif
777
 
778
 
779
static void ConsoleMath(IPSocket *socket, char *argv[])
780
{
781
   int v1, v2, ch;
782
   if(argv[3][0] == 0)
783
   {
784
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
785
      return;
786
   }
787
   v1 = atoi(argv[1]);
788
   ch = argv[2][0];
789
   v2 = atoi(argv[3]);
790
   if(ch == '+')
791
      v1 += v2;
792
   else if(ch == '-')
793
      v1 -= v2;
794
   else if(ch == '*')
795
      v1 *= v2;
796
   else if(ch == '/')
797
   {
798
      if(v2 != 0)
799
         v1 /= v2;
800
   }
801
   IPPrintf(socket, "%d", v1);
802
}
803
 
804
 
805
static void PingCallback(IPSocket *socket)
806
{
807
   IPSocket *socket2 = socket->userPtr;
808
   IPClose(socket);
809
   if(socket2)
810
      IPPrintf(socket2, "Ping Reply");
811
   else
812
      printf("Ping Reply\n");
813
}
814
 
815
 
816
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
817
{
818
   char buf[COMMAND_BUFFER_SIZE];
819
   IPSocket *socketTelnet = arg;
820
   IPSocket *socketPing;
821
   (void)socket;
822
 
823
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
824
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
825
   IPPrintf(socketTelnet, buf);
826
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
827
   socketPing->userPtr = socketTelnet;
828
   buf[0] = 'A';
829
   IPWrite(socketPing, (uint8*)buf, 1);
830
}
831
 
832
 
833
static void ConsolePing(IPSocket *socket, char *argv[])
834
{
835
   int ip0, ip1, ip2, ip3;
836
 
837
   if('0' <= argv[1][0] && argv[1][0] <= '9')
838
   {
839
      sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
840
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
841
      DnsResultCallback(socket, ip0, socket);
842
   }
843
   else
844
   {
845
      IPResolve(argv[1], DnsResultCallback, socket);
846
      IPPrintf(socket, "Sent DNS request");
847
   }
848
}
849
 
850
 
851
static void ConsoleTransferDone(uint8 *data, int length)
852
{
853
   FILE *file;
854
   IPPrintf(socketTelnet, "Transfer Done");
855
   file = fopen(storageFilename, "w");
856
   if(file)
857
   {
858
      fwrite(data, 1, length, file);
859
      fclose(file);
860
   }
861
   if(myStorage)
862
      free(myStorage);
863
   myStorage = NULL;
864
}
865
 
866
 
867
static void ConsoleFtp(IPSocket *socket, char *argv[])
868
{
869
   int ip0, ip1, ip2, ip3;
870
   if(argv[1][0] == 0)
871
   {
872
      IPPrintf(socket, "ftp #.#.#.# User Password File");
873
      return;
874
   }
875
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
876
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
877
   socketTelnet = socket;
878
   if(myStorage == NULL)
879
      myStorage = (uint8*)malloc(STORAGE_SIZE);
880
   if(myStorage == NULL)
881
      return;
882
   strcpy(storageFilename, argv[4]);
883
   FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
884
      0, ConsoleTransferDone);
885
}
886
 
887
 
888
static void ConsoleTftp(IPSocket *socket, char *argv[])
889
{
890
   int ip0, ip1, ip2, ip3;
891
   if(argv[1][0] == 0)
892
   {
893
      IPPrintf(socket, "tftp #.#.#.# File");
894
      return;
895
   }
896
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
897
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
898
   socketTelnet = socket;
899
   if(myStorage == NULL)
900
      myStorage = (uint8*)malloc(STORAGE_SIZE);
901
   if(myStorage == NULL)
902
      return;
903
   strcpy(storageFilename, argv[2]);
904
   TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
905
}
906
 
907
 
908
static void ConsoleMkfile(IPSocket *socket, char *argv[])
909
{
910
   OS_FILE *file;
911
   (void)argv;
912
   file = fopen("myfile.txt", "w");
913
   fwrite("Hello World!", 1, 12, file);
914
   fclose(file);
915
   IPPrintf(socket, "Created myfile.txt");
916
}
917
 
918
 
919 362 rhoads
static void ConsoleUptime(IPSocket *socket, char *argv[])
920
{
921
   int days, hours, minutes, seconds;
922
   (void)argv;
923
   seconds = OS_ThreadTime() / 100;
924
   minutes = seconds / 60 % 60;
925
   hours = seconds / 3600 % 24;
926
   days = seconds / 3600 / 24;
927
   seconds %= 60;
928
   IPPrintf(socket, "%d days %2d:%2d:%2d\n", days, hours, minutes, seconds);
929
}
930
 
931
 
932
static void ConsoleDump(IPSocket *socket, char *argv[])
933
{
934
   FILE *fileIn;
935
   uint8 buf[16];
936
   int bytes, i, j;
937
 
938
   fileIn = fopen(argv[1], "r");
939
   if(fileIn == NULL)
940
      return;
941
   for(j = 0; j < 1024*1024*16; j += 16)
942
   {
943
      bytes = fread(buf, 1, 16, fileIn);
944
      if(bytes == 0)
945
         break;
946
      IPPrintf(socket, "%8x ", j);
947
      for(i = 0; i < bytes; ++i)
948
         IPPrintf(socket, "%2x ", buf[i]);
949
      for( ; i < 16; ++i)
950
         IPPrintf(socket, "   ");
951
      for(i = 0; i < bytes; ++i)
952
      {
953
         if(isprint(buf[i]))
954
            IPPrintf(socket, "%c", buf[i]);
955
         else
956
            IPPrintf(socket, ".");
957
      }
958
      IPPrintf(socket, "\n");
959
   }
960
   fclose(fileIn);
961
}
962
 
963
 
964
static void ConsoleGrep(IPSocket *socket, char *argv[])
965
{
966
   FILE *fileIn;
967
   char buf[200];
968
   int bytes;
969
   char *ptr, *ptrEnd;
970
 
971
   if(argv[1][0] == 0 || argv[2][0] == 0)
972
   {
973
      IPPrintf(socket, "Usage: grep pattern file\n");
974
      return;
975
   }
976
   fileIn = fopen(argv[2], "r");
977
   if(fileIn == NULL)
978
      return;
979
   bytes = 0;
980
   for(;;)
981
   {
982
      bytes += fread(buf + bytes, 1, sizeof(buf) - bytes - 1, fileIn);
983
      if(bytes == 0)
984
         break;
985
      buf[bytes] = 0;
986
      ptrEnd = strstr(buf, "\r");
987
      if(ptrEnd == NULL)
988
         ptrEnd = strstr(buf, "\n");
989
      if(ptrEnd)
990
      {
991
         *ptrEnd = 0;
992
         if(*++ptrEnd == '\n')
993
            ++ptrEnd;
994
      }
995
      ptr = strstr(buf, argv[1]);
996
      if(ptr)
997
         IPPrintf(socket, "%s\n", buf);
998
      if(ptrEnd)
999
      {
1000
         bytes = strlen(ptrEnd);
1001
         memcpy(buf, ptrEnd, bytes);
1002
      }
1003
      else
1004
      {
1005
         bytes = 0;
1006
      }
1007
   }
1008
   fclose(fileIn);
1009
}
1010
 
1011
 
1012 354 rhoads
#ifdef DLL_SETUP
1013
#include "dll.h"
1014
 
1015
static void ConsoleRun(IPSocket *socket, char *argv[])
1016
{
1017
   FILE *file;
1018
   int bytes, i;
1019
   uint8 code[128];
1020
   DllFunc funcPtr;
1021
   char *command, *ptr;
1022
 
1023
   if(strcmp(argv[0], "run") == 0)
1024
      ++argv;
1025
   file = fopen(argv[0], "r");
1026
   if(file == NULL)
1027
   {
1028
      IPPrintf(socket, "Can't find %s", argv[0]);
1029
      return;
1030
   }
1031
 
1032 362 rhoads
   bytes = fread(code, 1, sizeof(code), file);  //load first 128 bytes
1033
   if(code[0] >= ' ')
1034
   {
1035
      socket->fileIn = file;       //script file
1036
      fseek(file, 0, 0);
1037
      return;
1038
   }
1039
 
1040 354 rhoads
   funcPtr = (DllFunc)code;
1041 362 rhoads
   ptr = funcPtr(NULL);            //determine load address
1042 354 rhoads
 
1043 362 rhoads
   memcpy(ptr, code, bytes);       //copy to correct address
1044
   bytes += fread(ptr + bytes, 1, 1024*1024*8, file);
1045 354 rhoads
   fclose(file);
1046 362 rhoads
   printf("address=0x%x bytes=%d\n", (int)ptr, bytes);
1047
   funcPtr = (DllFunc)ptr;
1048
   funcPtr = (DllFunc)funcPtr(DllFuncList);  //initialize DLL, find Start()
1049 354 rhoads
 
1050
   //Register new command
1051
   command = argv[0];
1052
   for(;;)
1053
   {
1054
      ptr = strstr(command, "/");
1055
      if(ptr == NULL)
1056
         break;
1057
      command = ptr + 1;
1058
   }
1059
   for(i = 0; TelnetFuncList[i].name; ++i)
1060
   {
1061
      if(TelnetFuncList[i].name[0] == 0 ||
1062
         strcmp(TelnetFuncList[i].name, command) == 0)
1063
      {
1064
         TelnetFuncList[i].name = (char*)malloc(40);
1065
         strcpy(TelnetFuncList[i].name, command);
1066 362 rhoads
         TelnetFuncList[i].func = (ConsoleFunc)funcPtr;
1067 354 rhoads
         break;
1068
      }
1069
   }
1070
 
1071
   socket->userFunc = socket->funcPtr;
1072 362 rhoads
   funcPtr(socket, argv);
1073 354 rhoads
}
1074
 
1075
 
1076
typedef struct NameValue_t {
1077
   struct NameValue_t *next;
1078
   void *value;
1079
   char name[1];
1080
} NameValue_t;
1081
 
1082
//Find the value associated with the name
1083
void *IPNameValue(const char *name, void *value)
1084
{
1085
   static NameValue_t *head;
1086
   NameValue_t *node;
1087
   for(node = head; node; node = node->next)
1088
   {
1089
      if(strcmp(node->name, name) == 0)
1090
         break;
1091
   }
1092
   if(node == NULL)
1093
   {
1094
      node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
1095
      if(node == NULL)
1096
         return NULL;
1097
      strcpy(node->name, name);
1098
      node->value = value;
1099
      node->next = head;
1100
      head = node;
1101
   }
1102
   if(value)
1103
      node->value = value;
1104
   return node->value;
1105
}
1106
#endif
1107
 
1108
 
1109
#ifdef EDIT_FILE
1110
extern void EditFile(IPSocket *socket, char *argv[]);
1111
#endif
1112
 
1113
static TelnetFunc_t MyFuncs[] = {
1114
   {"cat", ConsoleCat},
1115
   {"cp", ConsoleCp},
1116 362 rhoads
   {"dump", ConsoleDump},
1117 354 rhoads
   {"exit", ConsoleExit},
1118
#ifdef INCLUDE_FLASH
1119
   {"flashErase", ConsoleFlashErase},
1120
#endif
1121
   {"ftp", ConsoleFtp},
1122 362 rhoads
   {"grep", ConsoleGrep},
1123 354 rhoads
   {"help", ConsoleHelp},
1124
   {"ls", ConsoleLs},
1125
   {"math", ConsoleMath},
1126
   {"mkdir", ConsoleMkdir},
1127
   {"mkfile", ConsoleMkfile},
1128
   {"ping", ConsolePing},
1129
   {"rm", ConsoleRm},
1130
   {"tftp", ConsoleTftp},
1131 362 rhoads
   {"uptime", ConsoleUptime},
1132 354 rhoads
#ifdef DLL_SETUP
1133
   {"run", ConsoleRun},
1134
#endif
1135
#ifdef EDIT_FILE
1136
   {"edit", EditFile},
1137
#endif
1138
   {"", NULL},
1139
   {"", NULL},
1140
   {"", NULL},
1141
   {"", NULL},
1142
   {"", NULL},
1143
   {"", NULL},
1144
   {"", NULL},
1145
   {"", NULL},
1146
   {"", NULL},
1147
   {"", NULL},
1148
   {"", NULL},
1149
   {NULL, NULL}
1150
};
1151
 
1152
 
1153
void ConsoleInit(void)
1154
{
1155
   FtpdInit(1);
1156
   TftpdInit();
1157
   TelnetInit(MyFuncs);
1158
}

powered by: WebSVN 2.1.0

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