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

Subversion Repositories mlite

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

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

powered by: WebSVN 2.1.0

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