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

Subversion Repositories plasma

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

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 336 rhoads
            strcpy((char*)buf, "/flash/bin/");
579
            strcat((char*)buf, argv[0]);
580
            argv[0] = (char*)buf;
581 321 rhoads
            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 339 rhoads
   char buf[200], buf2[80];
704
   int bytes, width;
705 321 rhoads
 
706
   file = fopen(argv[1], "r");
707
   if(file == NULL)
708
      return;
709 339 rhoads
   width = 0;
710 321 rhoads
   for(;;)
711 229 rhoads
   {
712 321 rhoads
      bytes = OS_fdir(file, buf);
713
      if(bytes)
714
         break;
715 339 rhoads
      if(buf[0] == 255)
716
         continue;
717
      bytes = OS_flength(buf);
718
      sprintf(buf2, "%s:%d                    ", buf, bytes);
719
      bytes = strlen(buf2);
720
      bytes -= bytes % 20;
721
      buf2[bytes] = 0;
722
      width += bytes;
723
      if(width == 80)
724
         --bytes;
725
      if(width > 80)
726
      {
727
         IPPrintf(socket, "\n");
728
         width = bytes;
729
      }
730
      IPWrite(socket, (uint8*)buf2, bytes);
731 229 rhoads
   }
732 321 rhoads
   fclose(file);
733
}
734
 
735
 
736 302 rhoads
#ifdef INCLUDE_FLASH
737 321 rhoads
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
738
{
739
   int bytes;
740
   (void)argv;
741
   IPPrintf(socket, "\r\nErasing");
742
   for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
743 302 rhoads
   {
744 321 rhoads
      IPPrintf(socket, ".");
745
      FlashErase(bytes);
746 302 rhoads
   }
747 321 rhoads
   IPPrintf(socket, "\r\nMust Reboot\r\n");
748
   OS_ThreadSleep(OS_WAIT_FOREVER);
749
}
750 302 rhoads
#endif
751 229 rhoads
 
752
 
753 321 rhoads
static void ConsoleMath(IPSocket *socket, char *argv[])
754 210 rhoads
{
755 321 rhoads
   int v1, v2, ch;
756
   if(argv[3][0] == 0)
757 210 rhoads
   {
758
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
759
      return;
760
   }
761 321 rhoads
   v1 = atoi(argv[1]);
762
   ch = argv[2][0];
763
   v2 = atoi(argv[3]);
764
   if(ch == '+')
765 210 rhoads
      v1 += v2;
766 321 rhoads
   else if(ch == '-')
767 210 rhoads
      v1 -= v2;
768 321 rhoads
   else if(ch == '*')
769 210 rhoads
      v1 *= v2;
770 321 rhoads
   else if(ch == '/')
771 210 rhoads
   {
772
      if(v2 != 0)
773
         v1 /= v2;
774
   }
775 321 rhoads
   IPPrintf(socket, "%d", v1);
776 210 rhoads
}
777
 
778
 
779 322 rhoads
static void PingCallback(IPSocket *socket)
780 210 rhoads
{
781
   IPSocket *socket2 = socket->userPtr;
782
   IPClose(socket);
783
   if(socket2)
784 229 rhoads
      IPPrintf(socket2, "Ping Reply");
785 212 rhoads
   else
786
      printf("Ping Reply\n");
787 210 rhoads
}
788
 
789
 
790
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
791
{
792 215 rhoads
   char buf[COMMAND_BUFFER_SIZE];
793 210 rhoads
   IPSocket *socketTelnet = arg;
794
   IPSocket *socketPing;
795 212 rhoads
   (void)socket;
796 322 rhoads
 
797 210 rhoads
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
798
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
799
   IPPrintf(socketTelnet, buf);
800
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
801
   socketPing->userPtr = socketTelnet;
802 322 rhoads
   buf[0] = 'A';
803
   IPWrite(socketPing, (uint8*)buf, 1);
804 210 rhoads
}
805
 
806
 
807 321 rhoads
static void ConsolePing(IPSocket *socket, char *argv[])
808 210 rhoads
{
809
   int ip0, ip1, ip2, ip3;
810 322 rhoads
 
811 321 rhoads
   if('0' <= argv[1][0] && argv[1][0] <= '9')
812 210 rhoads
   {
813 321 rhoads
      sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
814 210 rhoads
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
815 322 rhoads
      DnsResultCallback(socket, ip0, socket);
816 210 rhoads
   }
817
   else
818
   {
819 321 rhoads
      IPResolve(argv[1], DnsResultCallback, socket);
820 229 rhoads
      IPPrintf(socket, "Sent DNS request");
821 210 rhoads
   }
822
}
823
 
824
 
825 322 rhoads
static void ConsoleTransferDone(uint8 *data, int length)
826 210 rhoads
{
827 322 rhoads
   FILE *file;
828 229 rhoads
   IPPrintf(socketTelnet, "Transfer Done");
829 322 rhoads
   file = fopen(storageFilename, "w");
830
   if(file)
831
   {
832
      fwrite(data, 1, length, file);
833
      fclose(file);
834
   }
835
   if(myStorage)
836
      free(myStorage);
837
   myStorage = NULL;
838 210 rhoads
}
839
 
840
 
841 321 rhoads
static void ConsoleFtp(IPSocket *socket, char *argv[])
842 210 rhoads
{
843
   int ip0, ip1, ip2, ip3;
844 321 rhoads
   if(argv[1][0] == 0)
845 210 rhoads
   {
846 229 rhoads
      IPPrintf(socket, "ftp #.#.#.# User Password File");
847 210 rhoads
      return;
848
   }
849 321 rhoads
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
850 210 rhoads
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
851
   socketTelnet = socket;
852 322 rhoads
   if(myStorage == NULL)
853
      myStorage = (uint8*)malloc(STORAGE_SIZE);
854
   if(myStorage == NULL)
855
      return;
856
   strcpy(storageFilename, argv[4]);
857
   FtpTransfer(ip0, argv[2], argv[3], argv[4], myStorage, STORAGE_SIZE-1,
858 215 rhoads
      0, ConsoleTransferDone);
859 210 rhoads
}
860
 
861
 
862 321 rhoads
static void ConsoleTftp(IPSocket *socket, char *argv[])
863 210 rhoads
{
864
   int ip0, ip1, ip2, ip3;
865 321 rhoads
   if(argv[1][0] == 0)
866 210 rhoads
   {
867 229 rhoads
      IPPrintf(socket, "tftp #.#.#.# File");
868 210 rhoads
      return;
869
   }
870 321 rhoads
   sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
871 210 rhoads
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
872
   socketTelnet = socket;
873 322 rhoads
   if(myStorage == NULL)
874
      myStorage = (uint8*)malloc(STORAGE_SIZE);
875
   if(myStorage == NULL)
876
      return;
877
   strcpy(storageFilename, argv[2]);
878
   TftpTransfer(ip0, argv[2], myStorage, STORAGE_SIZE-1, ConsoleTransferDone);
879 210 rhoads
}
880
 
881
 
882 321 rhoads
static void ConsoleMkfile(IPSocket *socket, char *argv[])
883 212 rhoads
{
884 215 rhoads
   OS_FILE *file;
885 321 rhoads
   (void)argv;
886 215 rhoads
   file = fopen("myfile.txt", "w");
887
   fwrite("Hello World!", 1, 12, file);
888
   fclose(file);
889 229 rhoads
   IPPrintf(socket, "Created myfile.txt");
890 210 rhoads
}
891
 
892
 
893 315 rhoads
#ifdef DLL_SETUP
894
#include "dll.h"
895 321 rhoads
 
896
static void ConsoleRun(IPSocket *socket, char *argv[])
897 315 rhoads
{
898 321 rhoads
   FILE *file;
899
   int bytes, i;
900
   uint8 code[128];
901 315 rhoads
   DllFunc funcPtr;
902 321 rhoads
   DllInfo info;
903
   int *bss;
904
   char *command, *ptr;
905 315 rhoads
 
906 321 rhoads
   if(strcmp(argv[0], "run") == 0)
907
      ++argv;
908
   info.socket = socket;
909
   info.dllFuncList = DllFuncList;
910
   file = fopen(argv[0], "r");
911 315 rhoads
   if(file == NULL)
912
   {
913 321 rhoads
      IPPrintf(socket, "Can't find %s", argv[0]);
914 315 rhoads
      return;
915
   }
916 321 rhoads
 
917
   bytes = fread(code, 1, sizeof(code), file);
918
   funcPtr = (DllFunc)code;
919
   funcPtr(&info);     //call entry() to fill in info
920
 
921
   memcpy(info.entry, code, bytes);
922
   bytes = fread(info.entry + bytes, 1, 1024*1024*8, file) + bytes;
923 315 rhoads
   fclose(file);
924 321 rhoads
   printf("address=0x%x bytes=%d\n", (int)info.entry, bytes);
925
   for(bss = info.bssStart; bss < info.bssEnd; ++bss)
926
      *bss = 0;
927
   *info.pDllF = DllFuncList;
928
 
929
   //Register new command
930
   command = argv[0];
931
   for(;;)
932
   {
933
      ptr = strstr(command, "/");
934
      if(ptr == NULL)
935
         break;
936
      command = ptr + 1;
937
   }
938
   for(i = 0; TelnetFuncList[i].name; ++i)
939
   {
940
      if(TelnetFuncList[i].name[0] == 0 ||
941
         strcmp(TelnetFuncList[i].name, command) == 0)
942
      {
943
         TelnetFuncList[i].name = (char*)malloc(40);
944
         strcpy(TelnetFuncList[i].name, command);
945
         TelnetFuncList[i].func = (ConsoleFunc)info.startPtr;
946
         break;
947
      }
948
   }
949
 
950
   socket->userFunc = socket->funcPtr;
951
   info.startPtr(socket, argv);
952 315 rhoads
}
953 324 rhoads
 
954
 
955
typedef struct NameValue_t {
956
   struct NameValue_t *next;
957
   void *value;
958
   char name[1];
959
} NameValue_t;
960
 
961
//Find the value associated with the name
962
void *IPNameValue(const char *name, void *value)
963
{
964
   static NameValue_t *head;
965
   NameValue_t *node;
966
   for(node = head; node; node = node->next)
967
   {
968
      if(strcmp(node->name, name) == 0)
969
         break;
970
   }
971
   if(node == NULL)
972
   {
973
      node = (NameValue_t*)malloc(sizeof(NameValue_t) + (int)strlen(name));
974
      if(node == NULL)
975
         return NULL;
976
      strcpy(node->name, name);
977
      node->value = value;
978
      node->next = head;
979
      head = node;
980
   }
981
   if(value)
982
      node->value = value;
983
   return node->value;
984
}
985 315 rhoads
#endif
986
 
987
 
988 321 rhoads
#ifdef EDIT_FILE
989
extern void EditFile(IPSocket *socket, char *argv[]);
990
#endif
991
 
992 210 rhoads
static TelnetFunc_t MyFuncs[] = {
993 321 rhoads
   {"cat", ConsoleCat},
994
   {"cp", ConsoleCp},
995
   {"exit", ConsoleExit},
996
#ifdef INCLUDE_FLASH
997
   {"flashErase", ConsoleFlashErase},
998
#endif
999
   {"ftp", ConsoleFtp},
1000
   {"help", ConsoleHelp},
1001
   {"ls", ConsoleLs},
1002
   {"math", ConsoleMath},
1003
   {"mkdir", ConsoleMkdir},
1004
   {"mkfile", ConsoleMkfile},
1005
   {"ping", ConsolePing},
1006
   {"rm", ConsoleRm},
1007
   {"tftp", ConsoleTftp},
1008 315 rhoads
#ifdef DLL_SETUP
1009 321 rhoads
   {"run", ConsoleRun},
1010 315 rhoads
#endif
1011 321 rhoads
#ifdef EDIT_FILE
1012
   {"edit", EditFile},
1013
#endif
1014
   {"", NULL},
1015
   {"", NULL},
1016
   {"", NULL},
1017
   {"", NULL},
1018
   {"", NULL},
1019
   {"", NULL},
1020
   {"", NULL},
1021
   {"", NULL},
1022
   {"", NULL},
1023
   {"", NULL},
1024
   {"", NULL},
1025
   {NULL, NULL}
1026 210 rhoads
};
1027
 
1028
 
1029
void ConsoleInit(void)
1030
{
1031
   FtpdInit(1);
1032
   TftpdInit();
1033
   TelnetInit(MyFuncs);
1034
}

powered by: WebSVN 2.1.0

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