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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [netutil.c] - Blame information for rev 321

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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