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

Subversion Repositories plasma

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

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 322 rhoads
   for(i = 0; i < 100000; ++i)
46 210 rhoads
   {
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 322 rhoads
   if(socket->state > IP_TCP)
435
      return;
436 210 rhoads
   bytes = IPRead(socket, buf, sizeof(buf)-1);
437 322 rhoads
   if(command == NULL)
438 210 rhoads
   {
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 321 rhoads
   socket->dontFlush = 0;
459 210 rhoads
   buf[bytes] = 0;
460 215 rhoads
   length = (int)strlen(command);
461
   for(j = 0; j < bytes; ++j)
462 210 rhoads
   {
463 215 rhoads
      if(buf[j] == 255)
464
         return;
465
      if(buf[j] == 8 || (buf[j] == 27 && buf[j+2] == 'D'))
466 210 rhoads
      {
467 215 rhoads
         if(buf[j] == 27)
468
            j += 2;
469
         if(length)
470
         {
471
            // Backspace
472
            command[--length] = 0;
473
            bufOut[0] = 8;
474
            bufOut[1] = ' ';
475
            bufOut[2] = 8;
476
            IPWrite(socket, (uint8*)bufOut, 3);
477
         }
478 210 rhoads
      }
479 215 rhoads
      else if(buf[j] == 27)
480 210 rhoads
      {
481 215 rhoads
         // Command History
482
         if(buf[j+2] == 'A')
483 210 rhoads
         {
484 215 rhoads
            if(++CommandIndex > COMMAND_BUFFER_COUNT)
485
               CommandIndex = COMMAND_BUFFER_COUNT;
486 210 rhoads
         }
487 215 rhoads
         else if(buf[j+2] == 'B')
488
         {
489
            if(--CommandIndex < 0)
490
               CommandIndex = 0;
491
         }
492
         else
493
            return;
494
         bufOut[0] = 8;
495
         bufOut[1] = ' ';
496
         bufOut[2] = 8;
497
         for(i = 0; i < length; ++i)
498
            IPWrite(socket, (uint8*)bufOut, 3);
499 315 rhoads
         command[0] = 0;
500 215 rhoads
         if(CommandIndex && CommandPtr[CommandIndex-1])
501 315 rhoads
            strncat(command, CommandPtr[CommandIndex-1], COMMAND_BUFFER_SIZE-1);
502 215 rhoads
         length = (int)strlen(command);
503
         IPWrite(socket, (uint8*)command, length);
504
         j += 2;
505 210 rhoads
      }
506 215 rhoads
      else
507 210 rhoads
      {
508 324 rhoads
         if(buf[j] == 0)
509
            buf[j] = '\n';  //Linux support
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 322 rhoads
         if(socket->state > IP_TCP)
585
            return;
586 215 rhoads
         command[0] = 0;
587
         length = 0;
588
         CommandIndex = 0;
589 321 rhoads
         if(socket->dontFlush == 0)
590
            IPPrintf(socket, "\r\n-> ");
591
      } //command entered
592
   } //bytes
593 215 rhoads
   IPWriteFlush(socket);
594 210 rhoads
}
595
 
596
 
597
void TelnetInit(TelnetFunc_t *funcList)
598
{
599
   IPSocket *socket;
600
   TelnetFuncList = funcList;
601
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
602
}
603
 
604
 
605
//******************* Console ************************
606
 
607 322 rhoads
#define STORAGE_SIZE 1024*64
608
static uint8 *myStorage;
609 215 rhoads
static IPSocket *socketTelnet;
610 322 rhoads
static char storageFilename[60];
611 210 rhoads
 
612
 
613 321 rhoads
static void ConsoleHelp(IPSocket *socket, char *argv[])
614 229 rhoads
{
615 321 rhoads
   char buf[200];
616
   int i;
617 322 rhoads
   (void)argv;
618 321 rhoads
   strcpy(buf, "Commands: ");
619
   for(i = 0; TelnetFuncList[i].name; ++i)
620 229 rhoads
   {
621 321 rhoads
      if(TelnetFuncList[i].func)
622 229 rhoads
      {
623 321 rhoads
         if(i)
624
            strcat(buf, ", ");
625
         strcat(buf, TelnetFuncList[i].name);
626 229 rhoads
      }
627
   }
628 321 rhoads
   IPPrintf(socket, buf);
629
}
630
 
631
 
632
static void ConsoleExit(IPSocket *socket, char *argv[])
633
{
634
   free(argv[0]);
635
   socket->userPtr = NULL;
636
   IPClose(socket);
637
}
638
 
639
 
640
static void ConsoleCat(IPSocket *socket, char *argv[])
641
{
642
   FILE *file;
643
   uint8 buf[200];
644
   int bytes;
645
 
646
   file = fopen(argv[1], "r");
647
   if(file == NULL)
648
      return;
649
   for(;;)
650 229 rhoads
   {
651 321 rhoads
      bytes = fread(buf, 1, sizeof(buf), file);
652
      if(bytes == 0)
653
         break;
654
      IPWrite(socket, buf, bytes);
655
   }
656
   fclose(file);
657
}
658
 
659
 
660
static void ConsoleCp(IPSocket *socket, char *argv[])
661
{
662
   FILE *fileIn, *fileOut;
663
   uint8 buf[200];
664
   int bytes;
665 322 rhoads
   (void)socket;
666 321 rhoads
 
667
   fileIn = fopen(argv[1], "r");
668
   if(fileIn == NULL)
669
      return;
670
   fileOut = fopen(argv[2], "w");
671
   if(fileOut)
672
   {
673
      for(;;)
674 229 rhoads
      {
675 321 rhoads
         bytes = fread(buf, 1, sizeof(buf), fileIn);
676
         if(bytes == 0)
677
            break;
678
         fwrite(buf, 1, bytes, fileOut);
679 229 rhoads
      }
680 321 rhoads
      fclose(fileOut);
681 229 rhoads
   }
682 321 rhoads
   fclose(fileIn);
683
}
684
 
685
 
686
static void ConsoleRm(IPSocket *socket, char *argv[])
687
{
688
   (void)socket;
689
   OS_fdelete(argv[1]);
690
}
691
 
692
 
693
static void ConsoleMkdir(IPSocket *socket, char *argv[])
694
{
695
   (void)socket;
696
   OS_fmkdir(argv[1]);
697
}
698
 
699
 
700
static void ConsoleLs(IPSocket *socket, char *argv[])
701
{
702
   FILE *file;
703 322 rhoads
   char buf[200];
704 321 rhoads
   int bytes;
705
 
706
   file = fopen(argv[1], "r");
707
   if(file == NULL)
708
      return;
709
   for(;;)
710 229 rhoads
   {
711 321 rhoads
      bytes = OS_fdir(file, buf);
712
      if(bytes)
713
         break;
714
      strcat(buf, "  ");
715 322 rhoads
      IPWrite(socket, (uint8*)buf, (int)strlen(buf));
716 229 rhoads
   }
717 321 rhoads
   fclose(file);
718
}
719
 
720
 
721 302 rhoads
#ifdef INCLUDE_FLASH
722 321 rhoads
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
723
{
724
   int bytes;
725
   (void)argv;
726
   IPPrintf(socket, "\r\nErasing");
727
   for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
728 302 rhoads
   {
729 321 rhoads
      IPPrintf(socket, ".");
730
      FlashErase(bytes);
731 302 rhoads
   }
732 321 rhoads
   IPPrintf(socket, "\r\nMust Reboot\r\n");
733
   OS_ThreadSleep(OS_WAIT_FOREVER);
734
}
735 302 rhoads
#endif
736 229 rhoads
 
737
 
738 321 rhoads
static void ConsoleMath(IPSocket *socket, char *argv[])
739 210 rhoads
{
740 321 rhoads
   int v1, v2, ch;
741
   if(argv[3][0] == 0)
742 210 rhoads
   {
743
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
744
      return;
745
   }
746 321 rhoads
   v1 = atoi(argv[1]);
747
   ch = argv[2][0];
748
   v2 = atoi(argv[3]);
749
   if(ch == '+')
750 210 rhoads
      v1 += v2;
751 321 rhoads
   else if(ch == '-')
752 210 rhoads
      v1 -= v2;
753 321 rhoads
   else if(ch == '*')
754 210 rhoads
      v1 *= v2;
755 321 rhoads
   else if(ch == '/')
756 210 rhoads
   {
757
      if(v2 != 0)
758
         v1 /= v2;
759
   }
760 321 rhoads
   IPPrintf(socket, "%d", v1);
761 210 rhoads
}
762
 
763
 
764 322 rhoads
static void PingCallback(IPSocket *socket)
765 210 rhoads
{
766
   IPSocket *socket2 = socket->userPtr;
767
   IPClose(socket);
768
   if(socket2)
769 229 rhoads
      IPPrintf(socket2, "Ping Reply");
770 212 rhoads
   else
771
      printf("Ping Reply\n");
772 210 rhoads
}
773
 
774
 
775
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
776
{
777 215 rhoads
   char buf[COMMAND_BUFFER_SIZE];
778 210 rhoads
   IPSocket *socketTelnet = arg;
779
   IPSocket *socketPing;
780 212 rhoads
   (void)socket;
781 322 rhoads
 
782 210 rhoads
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
783
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
784
   IPPrintf(socketTelnet, buf);
785
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
786
   socketPing->userPtr = socketTelnet;
787 322 rhoads
   buf[0] = 'A';
788
   IPWrite(socketPing, (uint8*)buf, 1);
789 210 rhoads
}
790
 
791
 
792 321 rhoads
static void ConsolePing(IPSocket *socket, char *argv[])
793 210 rhoads
{
794
   int ip0, ip1, ip2, ip3;
795 322 rhoads
 
796 321 rhoads
   if('0' <= argv[1][0] && argv[1][0] <= '9')
797 210 rhoads
   {
798 321 rhoads
      sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
799 210 rhoads
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
800 322 rhoads
      DnsResultCallback(socket, ip0, socket);
801 210 rhoads
   }
802
   else
803
   {
804 321 rhoads
      IPResolve(argv[1], DnsResultCallback, socket);
805 229 rhoads
      IPPrintf(socket, "Sent DNS request");
806 210 rhoads
   }
807
}
808
 
809
 
810 322 rhoads
static void ConsoleTransferDone(uint8 *data, int length)
811 210 rhoads
{
812 322 rhoads
   FILE *file;
813 229 rhoads
   IPPrintf(socketTelnet, "Transfer Done");
814 322 rhoads
   file = fopen(storageFilename, "w");
815
   if(file)
816
   {
817
      fwrite(data, 1, length, file);
818
      fclose(file);
819
   }
820
   if(myStorage)
821
      free(myStorage);
822
   myStorage = NULL;
823 210 rhoads
}
824
 
825
 
826 321 rhoads
static void ConsoleFtp(IPSocket *socket, char *argv[])
827 210 rhoads
{
828
   int ip0, ip1, ip2, ip3;
829 321 rhoads
   if(argv[1][0] == 0)
830 210 rhoads
   {
831 229 rhoads
      IPPrintf(socket, "ftp #.#.#.# User Password File");
832 210 rhoads
      return;
833
   }
834 321 rhoads
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
835 210 rhoads
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
836
   socketTelnet = socket;
837 322 rhoads
   if(myStorage == NULL)
838
      myStorage = (uint8*)malloc(STORAGE_SIZE);
839
   if(myStorage == NULL)
840
      return;
841
   strcpy(storageFilename, argv[4]);
842
   FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
843 215 rhoads
      0, ConsoleTransferDone);
844 210 rhoads
}
845
 
846
 
847 321 rhoads
static void ConsoleTftp(IPSocket *socket, char *argv[])
848 210 rhoads
{
849
   int ip0, ip1, ip2, ip3;
850 321 rhoads
   if(argv[1][0] == 0)
851 210 rhoads
   {
852 229 rhoads
      IPPrintf(socket, "tftp #.#.#.# File");
853 210 rhoads
      return;
854
   }
855 321 rhoads
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
856 210 rhoads
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
857
   socketTelnet = socket;
858 322 rhoads
   if(myStorage == NULL)
859
      myStorage = (uint8*)malloc(STORAGE_SIZE);
860
   if(myStorage == NULL)
861
      return;
862
   strcpy(storageFilename, argv[2]);
863
   TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
864 210 rhoads
}
865
 
866
 
867 321 rhoads
static void ConsoleMkfile(IPSocket *socket, char *argv[])
868 212 rhoads
{
869 215 rhoads
   OS_FILE *file;
870 321 rhoads
   (void)argv;
871 215 rhoads
   file = fopen("myfile.txt", "w");
872
   fwrite("Hello World!", 1, 12, file);
873
   fclose(file);
874 229 rhoads
   IPPrintf(socket, "Created myfile.txt");
875 210 rhoads
}
876
 
877
 
878 315 rhoads
#ifdef DLL_SETUP
879
#include "dll.h"
880 321 rhoads
 
881
static void ConsoleRun(IPSocket *socket, char *argv[])
882 315 rhoads
{
883 321 rhoads
   FILE *file;
884
   int bytes, i;
885
   uint8 code[128];
886 315 rhoads
   DllFunc funcPtr;
887 321 rhoads
   DllInfo info;
888
   int *bss;
889
   char *command, *ptr;
890 315 rhoads
 
891 321 rhoads
   if(strcmp(argv[0], "run") == 0)
892
      ++argv;
893
   info.socket = socket;
894
   info.dllFuncList = DllFuncList;
895
   file = fopen(argv[0], "r");
896 315 rhoads
   if(file == NULL)
897
   {
898 321 rhoads
      IPPrintf(socket, "Can't find %s", argv[0]);
899 315 rhoads
      return;
900
   }
901 321 rhoads
 
902
   bytes = fread(code, 1, sizeof(code), file);
903
   funcPtr = (DllFunc)code;
904
   funcPtr(&info);     //call entry() to fill in info
905
 
906
   memcpy(info.entry, code, bytes);
907
   bytes = fread(info.entry + bytes, 1, 1024*1024*8, file) + bytes;
908 315 rhoads
   fclose(file);
909 321 rhoads
   printf("address=0x%x bytes=%d\n", (int)info.entry, bytes);
910
   for(bss = info.bssStart; bss < info.bssEnd; ++bss)
911
      *bss = 0;
912
   *info.pDllF = DllFuncList;
913
 
914
   //Register new command
915
   command = argv[0];
916
   for(;;)
917
   {
918
      ptr = strstr(command, "/");
919
      if(ptr == NULL)
920
         break;
921
      command = ptr + 1;
922
   }
923
   for(i = 0; TelnetFuncList[i].name; ++i)
924
   {
925
      if(TelnetFuncList[i].name[0] == 0 ||
926
         strcmp(TelnetFuncList[i].name, command) == 0)
927
      {
928
         TelnetFuncList[i].name = (char*)malloc(40);
929
         strcpy(TelnetFuncList[i].name, command);
930
         TelnetFuncList[i].func = (ConsoleFunc)info.startPtr;
931
         break;
932
      }
933
   }
934
 
935
   socket->userFunc = socket->funcPtr;
936
   info.startPtr(socket, argv);
937 315 rhoads
}
938 324 rhoads
 
939
 
940
typedef struct NameValue_t {
941
   struct NameValue_t *next;
942
   void *value;
943
   char name[1];
944
} NameValue_t;
945
 
946
//Find the value associated with the name
947
void *IPNameValue(const char *name, void *value)
948
{
949
   static NameValue_t *head;
950
   NameValue_t *node;
951
   for(node = head; node; node = node->next)
952
   {
953
      if(strcmp(node->name, name) == 0)
954
         break;
955
   }
956
   if(node == NULL)
957
   {
958
      node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
959
      if(node == NULL)
960
         return NULL;
961
      strcpy(node->name, name);
962
      node->value = value;
963
      node->next = head;
964
      head = node;
965
   }
966
   if(value)
967
      node->value = value;
968
   return node->value;
969
}
970 315 rhoads
#endif
971
 
972
 
973 321 rhoads
#ifdef EDIT_FILE
974
extern void EditFile(IPSocket *socket, char *argv[]);
975
#endif
976
 
977 210 rhoads
static TelnetFunc_t MyFuncs[] = {
978 321 rhoads
   {"cat", ConsoleCat},
979
   {"cp", ConsoleCp},
980
   {"exit", ConsoleExit},
981
#ifdef INCLUDE_FLASH
982
   {"flashErase", ConsoleFlashErase},
983
#endif
984
   {"ftp", ConsoleFtp},
985
   {"help", ConsoleHelp},
986
   {"ls", ConsoleLs},
987
   {"math", ConsoleMath},
988
   {"mkdir", ConsoleMkdir},
989
   {"mkfile", ConsoleMkfile},
990
   {"ping", ConsolePing},
991
   {"rm", ConsoleRm},
992
   {"tftp", ConsoleTftp},
993 315 rhoads
#ifdef DLL_SETUP
994 321 rhoads
   {"run", ConsoleRun},
995 315 rhoads
#endif
996 321 rhoads
#ifdef EDIT_FILE
997
   {"edit", EditFile},
998
#endif
999
   {"", NULL},
1000
   {"", NULL},
1001
   {"", NULL},
1002
   {"", NULL},
1003
   {"", NULL},
1004
   {"", NULL},
1005
   {"", NULL},
1006
   {"", NULL},
1007
   {"", NULL},
1008
   {"", NULL},
1009
   {"", NULL},
1010
   {NULL, NULL}
1011 210 rhoads
};
1012
 
1013
 
1014
void ConsoleInit(void)
1015
{
1016
   FtpdInit(1);
1017
   TftpdInit();
1018
   TelnetInit(MyFuncs);
1019
}

powered by: WebSVN 2.1.0

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