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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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