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

Subversion Repositories plasma

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

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
         if(CommandIndex && CommandPtr[CommandIndex-1])
491
            strcpy(command, CommandPtr[CommandIndex-1]);
492
         else
493
            command[0] = 0;
494
         length = (int)strlen(command);
495
         IPWrite(socket, (uint8*)command, length);
496
         j += 2;
497 210 rhoads
      }
498 215 rhoads
      else
499 210 rhoads
      {
500 229 rhoads
         if(length < COMMAND_BUFFER_SIZE-4 || (length <
501
            COMMAND_BUFFER_SIZE-2 && (buf[j] == '\r' || buf[j] == '\n')))
502 210 rhoads
         {
503 215 rhoads
            IPWrite(socket, buf+j, 1);
504
            command[length] = buf[j];
505
            command[++length] = 0;
506
         }
507
      }
508
      ptr = strstr(command, "\r\n");
509
      if(ptr)
510
      {
511
         ptr[0] = 0;
512
         bufOut[0] = 0;
513
         if(command[0])
514
         {
515
            // Save command in CommandHistory
516
            memcpy(CommandHistory + length + 1, CommandHistory,
517
               sizeof(CommandHistory) - length - 1);
518
            for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
519 210 rhoads
            {
520 215 rhoads
               if(CommandPtr[i+1] && CommandPtr[i+1] + length + 1 >=
521
                  CommandHistory + sizeof(CommandHistory))
522
                  CommandPtr[i+1] = NULL;
523
               else if(CommandPtr[i])
524
                  CommandPtr[i+1] = CommandPtr[i] + length + 1;
525 210 rhoads
            }
526 215 rhoads
            CommandPtr[CommandIndex] = CommandHistory;
527
            strcpy(CommandHistory, command);
528 210 rhoads
         }
529 215 rhoads
         if(strncmp(command, "help", 4) == 0)
530
         {
531 302 rhoads
            sprintf((char*)bufOut, "Commands: help, exit, cat, cp, ls, mkdir, rm, flasherase");
532 215 rhoads
            for(i = 0; TelnetFuncList[i].name; ++i)
533
            {
534
               strcat((char*)bufOut, ", ");
535
               strcat((char*)bufOut, TelnetFuncList[i].name);
536
            }
537
         }
538
         else if(strncmp(command, "exit", 4) == 0)
539
         {
540
            free(command);
541
            socket->userPtr = NULL;
542
            IPClose(socket);
543
            return;
544
         }
545
         else if(command[0])
546
         {
547
            strcpy(bufOut, command);
548
            ptr = strstr(bufOut, " ");
549
            if(ptr)
550
               ptr[0] = 0;
551
            for(i = 0; TelnetFuncList[i].name; ++i)
552
            {
553
               if(strcmp(bufOut, TelnetFuncList[i].name) == 0)
554
               {
555
                  TelnetFuncList[i].func(socket, command);
556
                  break;
557
               }
558
            }
559
            bufOut[0] = 0;
560
            if(TelnetFuncList[i].name == NULL)
561 229 rhoads
            {
562
               rc = BusyBox(socket, command);
563
               if(rc)
564
                  sprintf((char*)bufOut, "Unknown command (%s)", command);
565
            }
566 215 rhoads
         }
567 229 rhoads
         strcat((char*)bufOut, "\r\n-> ");
568 215 rhoads
         IPPrintf(socket, (char*)bufOut);
569
         command[0] = 0;
570
         length = 0;
571
         CommandIndex = 0;
572 210 rhoads
      }
573
   }
574 215 rhoads
   IPWriteFlush(socket);
575 210 rhoads
}
576
 
577
 
578
void TelnetInit(TelnetFunc_t *funcList)
579
{
580
   IPSocket *socket;
581
   TelnetFuncList = funcList;
582
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
583
}
584
 
585
 
586
//******************* Console ************************
587
 
588 215 rhoads
static uint8 myBuffer[1024*3];
589
static IPSocket *socketTelnet;
590 210 rhoads
 
591
 
592 229 rhoads
//Implements cat, cp, rm, mkdir, ls
593
int BusyBox(IPSocket *socket, char *command)
594
{
595
   FILE *fileIn, *fileOut;
596
   int bytes, bytes2;
597
   char bufA[COMMAND_BUFFER_SIZE];
598
   char bufB[COMMAND_BUFFER_SIZE];
599
   char bufC[COMMAND_BUFFER_SIZE];
600
   char bufD[COMMAND_BUFFER_SIZE];
601
   memset(bufA, 0, sizeof(bufA));
602
   memset(bufB, 0, sizeof(bufB));
603
   memset(bufC, 0, sizeof(bufC));
604
   memset(bufD, 0, sizeof(bufD));
605
   sscanf(command, "%s %s %s %s", bufA, bufB, bufC, bufD);
606
   if(strcmp(bufA, "cat") == 0)
607
   {
608
      fileIn = fopen(bufB, "r");
609
      if(fileIn)
610
      {
611
         for(;;)
612
         {
613
            bytes = fread(bufD, 1, sizeof(bufD), fileIn);
614
            if(bytes == 0)
615
               break;
616
            bytes2 = IPWrite(socket, (uint8*)bufD, bytes);
617
            if(bytes != bytes2)
618
               printf("(%d %d) ", bytes, bytes2);
619
         }
620
         fclose(fileIn);
621
      }
622
      return 0;
623
   }
624
   else if(strcmp(bufA, "cp") == 0)
625
   {
626
      fileIn = fopen(bufB, "r");
627
      if(fileIn)
628
      {
629
         fileOut = fopen(bufC, "w");
630
         if(fileOut)
631
         {
632
            for(;;)
633
            {
634
               bytes = fread(bufD, 1, sizeof(bufD), fileIn);
635
               if(bytes == 0)
636
                  break;
637
               bytes2 = fwrite(bufD, 1, bytes, fileOut);
638
            }
639
            fclose(fileOut);
640
         }
641
         fclose(fileIn);
642
      }
643
      return 0;
644
   }
645
   else if(strcmp(bufA, "rm") == 0)
646
   {
647
      OS_fdelete(bufB);
648
      return 0;
649
   }
650
   else if(strcmp(bufA, "mkdir") == 0)
651
   {
652
      OS_fmkdir(bufB);
653
      return 0;
654
   }
655
   else if(strcmp(bufA, "ls") == 0)
656
   {
657
      fileIn = fopen(bufB, "r");
658
      if(fileIn)
659
      {
660
         for(;;)
661
         {
662
            bytes = OS_fdir(fileIn, bufD);
663
            if(bytes)
664
               break;
665
            strcat(bufD, "  ");
666
            IPWrite(socket, (uint8*)bufD, strlen(bufD));
667
         }
668
         fclose(fileIn);
669
      }
670
      return 0;
671
   }
672 302 rhoads
#ifdef INCLUDE_FLASH
673
   else if(strcmp(bufA, "flasherase") == 0)
674
   {
675
      strcpy(bufD, "\r\nErasing");
676
      IPWrite(socket, (uint8*)bufD, strlen(bufD));
677
      IPWriteFlush(socket);
678
      strcpy(bufD, ".");
679
      for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
680
      {
681
         IPWrite(socket, (uint8*)bufD, strlen(bufD));
682
         IPWriteFlush(socket);
683
         FlashErase(bytes);
684
      }
685
      strcpy(bufD, "\r\nMust Reboot\r\n");
686
      IPWrite(socket, (uint8*)bufD, strlen(bufD));
687
      IPWriteFlush(socket);
688
      OS_ThreadSleep(OS_WAIT_FOREVER);
689
      return 0;
690
   }
691
#endif
692 229 rhoads
   return -1;
693
}
694
 
695
 
696 210 rhoads
void TransferDone(uint8 *data, int bytes)
697
{
698
   printf("TransferDone(0x%x, %d)\n", data, bytes);
699
   data[bytes] = 0;
700
   if(bytes > 500)
701
      data[500] = 0;
702
   printf("%s\n", data);
703
}
704
 
705
 
706 215 rhoads
static void ConsoleInfo(IPSocket *socket, char *command)
707 210 rhoads
{
708
   (void)command;
709 229 rhoads
   IPPrintf(socket, "Steve was here!");
710 210 rhoads
}
711
 
712
 
713 215 rhoads
static void ConsoleMath(IPSocket *socket, char *command)
714 210 rhoads
{
715
   int v1, v2;
716
   char buf[20], buf2[20];
717
   (void)socket;
718
   if(strlen(command) < 6)
719
   {
720
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
721
      return;
722
   }
723
   sscanf(command, "%s%d%s%d", buf, &v1, buf2, &v2);
724
   if(buf2[0] == '+')
725
      v1 += v2;
726
   else if(buf2[0] == '-')
727
      v1 -= v2;
728
   else if(buf2[0] == '*')
729
      v1 *= v2;
730
   else if(buf2[0] == '/')
731
   {
732
      if(v2 != 0)
733
         v1 /= v2;
734
   }
735 229 rhoads
   sprintf((char*)myBuffer, "%d", v1);
736 212 rhoads
   IPPrintf(socket, (char*)myBuffer);
737 210 rhoads
}
738
 
739
 
740
void PingCallback(IPSocket *socket)
741
{
742
   IPSocket *socket2 = socket->userPtr;
743
   //printf("Ping Reply\n");
744
   IPClose(socket);
745
   if(socket2)
746 229 rhoads
      IPPrintf(socket2, "Ping Reply");
747 212 rhoads
   else
748
      printf("Ping Reply\n");
749 210 rhoads
}
750
 
751
 
752
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
753
{
754 215 rhoads
   char buf[COMMAND_BUFFER_SIZE];
755 210 rhoads
   IPSocket *socketTelnet = arg;
756
   IPSocket *socketPing;
757 212 rhoads
   (void)socket;
758 210 rhoads
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
759
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
760
   IPPrintf(socketTelnet, buf);
761
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
762
   socketPing->userPtr = socketTelnet;
763
   myBuffer[0] = 'A';
764
   IPWrite(socketPing, myBuffer, 1);
765
}
766
 
767
 
768 215 rhoads
static void ConsolePing(IPSocket *socket, char *command)
769 210 rhoads
{
770
   int ip0, ip1, ip2, ip3;
771
   char buf[20];
772
   IPSocket *socketPing;
773
   if('0' <= command[5] && command[5] <= '9')
774
   {
775
      sscanf(command, "%s %d.%d.%d.%d", buf, &ip0, &ip1, &ip2, &ip3);
776
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
777
      socketPing = IPOpen(IP_MODE_PING, ip0, 0, PingCallback);
778
      socketPing->userPtr = socket;
779
      myBuffer[0] = 'A';
780
      IPWrite(socketPing, myBuffer, 1);
781 229 rhoads
      IPPrintf(socket, "Sent ping");
782 210 rhoads
   }
783
   else
784
   {
785
      IPResolve(command+5, DnsResultCallback, socket);
786 229 rhoads
      IPPrintf(socket, "Sent DNS request");
787 210 rhoads
   }
788
}
789
 
790
 
791 215 rhoads
void ConsoleTransferDone(uint8 *data, int length)
792 210 rhoads
{
793
   data[length] = 0;
794 229 rhoads
   IPPrintf(socketTelnet, "Transfer Done");
795 210 rhoads
}
796
 
797
 
798 215 rhoads
static void ConsoleFtp(IPSocket *socket, char *command)
799 210 rhoads
{
800 215 rhoads
   char buf[40], user[40], passwd[40], name[COMMAND_BUFFER_SIZE];
801 210 rhoads
   int ip0, ip1, ip2, ip3;
802
   if(strlen(command) < 10)
803
   {
804 229 rhoads
      IPPrintf(socket, "ftp #.#.#.# User Password File");
805 210 rhoads
      return;
806
   }
807
   sscanf(command, "%s %d.%d.%d.%d %s %s %s",
808
      buf, &ip0, &ip1, &ip2, &ip3, user, passwd, name);
809
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
810
   socketTelnet = socket;
811
   FtpTransfer(ip0, user, passwd, name, myBuffer, sizeof(myBuffer)-1,
812 215 rhoads
      0, ConsoleTransferDone);
813 210 rhoads
}
814
 
815
 
816 215 rhoads
static void ConsoleTftp(IPSocket *socket, char *command)
817 210 rhoads
{
818 215 rhoads
   char buf[COMMAND_BUFFER_SIZE], name[80];
819 210 rhoads
   int ip0, ip1, ip2, ip3;
820
   if(strlen(command) < 10)
821
   {
822 229 rhoads
      IPPrintf(socket, "tftp #.#.#.# File");
823 210 rhoads
      return;
824
   }
825 215 rhoads
   sscanf(command, "%s %d.%d.%d.%d %s",
826 210 rhoads
      buf, &ip0, &ip1, &ip2, &ip3, name);
827
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
828
   socketTelnet = socket;
829 215 rhoads
   TftpTransfer(ip0, name, myBuffer, sizeof(myBuffer)-1, ConsoleTransferDone);
830 210 rhoads
}
831
 
832
 
833 215 rhoads
static void ConsoleShow(IPSocket *socket, char *command)
834 212 rhoads
{
835 215 rhoads
   (void)command;
836
   IPPrintf(socket, (char*)myBuffer);
837 212 rhoads
}
838
 
839 215 rhoads
 
840
static void ConsoleClear(IPSocket *socket, char *command)
841 212 rhoads
{
842 215 rhoads
   (void)socket;
843
   (void)command;
844
   memset(myBuffer, 0, sizeof(myBuffer));
845 212 rhoads
}
846
 
847
 
848 215 rhoads
static void ConsoleMkfile(IPSocket *socket, char *command)
849 210 rhoads
{
850 215 rhoads
   OS_FILE *file;
851 212 rhoads
   (void)command;
852 215 rhoads
   file = fopen("myfile.txt", "w");
853
   fwrite("Hello World!", 1, 12, file);
854
   fclose(file);
855 229 rhoads
   IPPrintf(socket, "Created myfile.txt");
856 210 rhoads
}
857
 
858
 
859
static TelnetFunc_t MyFuncs[] = {
860 215 rhoads
   {"info", 0, ConsoleInfo},
861
   {"math", 0, ConsoleMath},
862
   {"ping", 0, ConsolePing},
863
   {"ftp", 0, ConsoleFtp},
864
   {"tftp", 0, ConsoleTftp},
865
   {"show", 0, ConsoleShow},
866
   {"clear", 0, ConsoleClear},
867
   {"mkfile", 0, ConsoleMkfile},
868
   {NULL, 0, NULL}
869 210 rhoads
};
870
 
871
 
872
void ConsoleInit(void)
873
{
874
   FtpdInit(1);
875
   TftpdInit();
876
   TelnetInit(MyFuncs);
877
}

powered by: WebSVN 2.1.0

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