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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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