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

Subversion Repositories plasma

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

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
#endif
20
#include "rtos.h"
21
#include "tcpip.h"
22
#ifdef WIN32
23
#define UartPrintf printf
24
#define OS_MQueueCreate(A,B,C) 0
25
#define OS_MQueueGet(A,B,C) 0
26
#define OS_ThreadCreate(A,B,C,D,E) 0
27
#endif
28
 
29
typedef struct {
30
   IPSocket *socket;
31
   int ip, port, bytes, done, canReceive;
32
   FILE *file;
33
} FtpdInfo;
34
 
35
static OS_MQueue_t *FtpMQueue;
36
 
37
 
38
static void FtpdSender(IPSocket *socket)
39
{
40
   unsigned char buf[600];
41
   int i, bytes, bytes2;
42
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
43
 
44
   if(info == NULL || info->done)
45
      return;
46
   fseek(info->file, info->bytes, 0);
47
   for(i = 0; i < 5; ++i)
48
   {
49
      bytes = fread(buf, 1, 512, info->file);
50
      bytes2 = IPWrite(socket, buf, bytes);
51
      info->bytes += bytes2;
52
      if(bytes != bytes2)
53
         return;
54
      if(bytes < 512)
55
      {
56
         fclose(info->file);
57
         IPClose(socket);
58
         info->done = 1;
59
         IPPrintf(info->socket, "226 Done\r\n");
60
         return;
61
      }
62
   }
63
}
64
 
65
 
66
static void FtpdReceiver(IPSocket *socket)
67
{
68
   unsigned char buf[600];
69
   int bytes;
70
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
71
 
72
   if(info == NULL || info->done)
73
      return;
74
   if(socket->state > IP_TCP)
75
   {
76
      fclose(info->file);
77
      IPClose(socket);
78
      info->done = 1;
79
      IPPrintf(info->socket, "226 Done\r\n");
80
      return;
81
   }
82
   bytes = IPRead(socket, buf, sizeof(buf));
83
   fwrite(buf, 1, bytes, info->file);
84
}
85
 
86
 
87
static void FtpdServerAction(IPSocket *socket)
88
{
89
   uint8 buf[600];
90
   int bytes;
91
   int ip0, ip1, ip2, ip3, port0, port1;
92
   IPSocket *socketOut;
93
   FtpdInfo *info = (FtpdInfo*)socket->userPtr;
94
 
95
   if(socket == NULL)
96
      return;
97
   bytes = IPRead(socket, buf, sizeof(buf)-1);
98
   buf[bytes] = 0;
99
   //printf("(%s)\n", buf);
100
   if(socket->userPtr == NULL)
101
   {
102
      info = (FtpdInfo*)malloc(sizeof(FtpdInfo));
103
      if(info == NULL)
104
         return;
105
      memset(info, 0, sizeof(FtpdInfo));
106
      socket->userPtr = info;
107
      info->socket = socket;
108
      socket->timeout = 0;
109
      IPPrintf(socket, "220 Connected to Plasma\r\n");
110
   }
111
   else if(strstr((char*)buf, "USER"))
112
   {
113
      if(strstr((char*)buf, "PlasmaSend"))
114
         info->canReceive = 1;
115
      socket->timeout = 0;
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
      socket->timeout = 0;
127
      sscanf((char*)buf + 5, "%d,%d,%d,%d,%d,%d", &ip0, &ip1, &ip2, &ip3, &port0, &port1);
128
      info->ip = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
129
      info->port = (port0 << 8) | port1;
130
      //printf("ip=0x%x port=%d\n", info->ip, info->port);
131
      IPPrintf(socket, "200 OK\r\n");
132
   }
133
   else if(strstr((char*)buf, "RETR") || strstr((char*)buf, "STOR"))
134
   {
135
      char *ptr = strstr((char*)buf, "\r");
136
      if(ptr)
137
         *ptr = 0;
138
      if(info == NULL)
139
         return;
140
      info->file = NULL;
141
      info->bytes = 0;
142
      info->done = 0;
143
      if(strstr((char*)buf, "RETR"))
144
         info->file = fopen((char*)buf + 5, "rb");
145
      else if(info->canReceive)
146
         info->file = fopen((char*)buf + 5, "wb");
147
      if(info->file)
148
      {
149
         IPPrintf(socket, "150 File ready\r\n");
150
         if(strstr((char*)buf, "RETR"))
151
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdSender);
152
         else
153
            socketOut = IPOpen(IP_MODE_TCP, info->ip, info->port, FtpdReceiver);
154
         if(socketOut)
155
            socketOut->userPtr = info;
156
      }
157
      else
158
      {
159
         IPPrintf(socket, "500 Error\r\n");
160
      }
161
   }
162
   else if(strstr((char*)buf, "QUIT"))
163
   {
164
      if(socket->userPtr)
165
         free(socket->userPtr);
166
      IPPrintf(socket, "221 Bye\r\n");
167
      IPClose(socket);
168
   }
169
   else if(bytes)
170
   {
171
      IPPrintf(socket, "500 Error\r\n");
172
   }
173
}
174
 
175
 
176
#ifndef WIN32
177
static void FtpdThread(void *Arg)
178
{
179
   IPSocket *socket=NULL;
180
   (void)Arg;
181
   for(;;)
182
   {
183
      OS_MQueueGet(HttpMQueue, &socket, OS_WAIT_FOREVER);
184
      FtpdServerAction(socket);
185
   }
186
}
187
#endif
188
 
189
 
190
static void FtpdServer(IPSocket *socket)
191
{
192
#ifdef WIN32
193
   FtpdServerAction(socket);
194
#else
195
   OS_MQueueSend(FtpMQueue, &socket);
196
#endif
197
}
198
 
199
 
200
void FtpdInit(int UseFiles)
201
{
202
   (void)UseFiles;
203
   FtpMQueue = OS_MQueueCreate("ftp", FRAME_COUNT, 4);
204
   OS_ThreadCreate("ftp", HttpThread, NULL, 50, 0);
205
   IPOpen(IP_MODE_TCP, 0, 21, FtpdServer);
206
}
207
 
208
 
209
//******************* FTP Client ************************
210
 
211
typedef struct {
212
   uint32 ip, port;
213
   char user[80], passwd[80], filename[80];
214
   uint8 *buf;
215
   int size, bytes, send, state;
216
} FtpInfo;
217
 
218
 
219
static void FtpCallbackTransfer(IPSocket *socket)
220
{
221
   int bytes;
222
   FtpInfo *info = (FtpInfo*)socket->userPtr;
223
 
224
   //printf("FtpCallbackTransfer\n");
225
   if(info == NULL)
226
      return;
227
   bytes = info->size - info->bytes;
228
   if(info->send == 0)
229
      bytes = IPRead(socket, info->buf + info->bytes, bytes);
230
   else
231
      bytes = IPWrite(socket, info->buf + info->bytes, bytes);
232
   info->bytes += bytes;
233
   if(info->bytes == info->size || socket->state > IP_TCP)
234
   {
235
      socket->userFunc(info->buf, info->bytes);
236
      free(info);
237
      socket->userPtr = NULL;
238
      IPClose(socket);
239
   }
240
}
241
 
242
 
243
static void FtpCallback(IPSocket *socket)
244
{
245
   char buf[600];
246
   FtpInfo *info = (FtpInfo*)socket->userPtr;
247
   int bytes, value;
248
 
249
   bytes = IPRead(socket, (uint8*)buf, sizeof(buf)-1);
250
   if(bytes == 0)
251
      return;
252
   buf[bytes] = 0;
253
   sscanf(buf, "%d", &value);
254
   if(bytes > 2)
255
      buf[bytes-2] = 0;
256
   //printf("FtpCallback(%d:%s)\n", socket->userData, buf);
257
   if(value / 100 != 2 && value / 100 != 3)
258
      return;
259
   buf[0] = 0;
260
   switch(socket->userData) {
261
   case 0:
262
      sprintf(buf, "USER %s\r\n", info->user);
263
      socket->userData = 1;
264
      break;
265
   case 1:
266
      sprintf(buf, "PASS %s\r\n", info->passwd);
267
      socket->userData = 2;
268
      if(value == 331)
269
         break;  //possible fall-through
270
   case 2:
271
      sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n",
272
         info->ip >> 24, (uint8)(info->ip >> 16),
273
         (uint8)(info->ip >> 8), (uint8)info->ip,
274
         (uint8)(info->port >> 8), (uint8)info->port);
275
      socket->userData = 3;
276
      break;
277
   case 3:
278
      if(info->send == 0)
279
         sprintf(buf, "RETR %s\r\n", info->filename);
280
      else
281
         sprintf(buf, "STOR %s\r\n", info->filename);
282
      socket->userData = 4;
283
      break;
284
   case 4:
285
      sprintf(buf, "QUIT\r\n");
286
      socket->userData = 9;
287
      break;
288
   }
289
   IPWrite(socket, (uint8*)buf, strlen(buf));
290
   IPWriteFlush(socket);
291
   if(socket->userData == 9)
292
      IPClose(socket);
293
}
294
 
295
 
296
IPSocket *FtpTransfer(uint32 ip, char *user, char *passwd,
297
                      char *filename, uint8 *buf, int size,
298
                      int send, void (*callback)(uint8 *data, int size))
299
{
300
   IPSocket *socket, *socketTransfer;
301
   FtpInfo *info;
302
   uint8 *ptr;
303
   info = (FtpInfo*)malloc(sizeof(FtpInfo));
304
   if(info == NULL)
305
      return NULL;
306
   strncpy(info->user, user, 80);
307
   strncpy(info->passwd, passwd, 80);
308
   strncpy(info->filename, filename, 80);
309
   info->buf = buf;
310
   info->size = size;
311
   info->send = send;
312
   info->bytes = 0;
313
   info->state = 0;
314
   info->port = 2000;
315
   socketTransfer = IPOpen(IP_MODE_TCP, 0, info->port, FtpCallbackTransfer);
316
   socketTransfer->userPtr = info;
317
   socketTransfer->userFunc = callback;
318
   socket = IPOpen(IP_MODE_TCP, ip, 21, FtpCallback);
319
   socket->userPtr = info;
320
   socket->userFunc = callback;
321
   ptr = socket->headerSend;
322
   info->ip = IPAddressSelf();
323
   return socket;
324
}
325
 
326
 
327
//******************* TFTP Server ************************
328
 
329
 
330
static void TftpdCallback(IPSocket *socket)
331
{
332
   unsigned char buf[512+4];
333
   int bytes, blockNum;
334
   FILE *file = (FILE*)socket->userPtr;
335
   bytes = IPRead(socket, buf, sizeof(buf));
336
   //printf("TfptdCallback bytes=%d\n", bytes);
337
   if(bytes < 4 || buf[0])
338
      return;
339
   if(buf[1] == 1)  //RRQ = Read Request
340
   {
341
      if(file)
342
         fclose(file);
343
      file = fopen((char*)buf+2, "rb");
344
      socket->userPtr = file;
345
      if(file == NULL)
346
      {
347
         buf[0] = 0;
348
         buf[1] = 5;   //ERROR
349
         buf[2] = 0;
350
         buf[3] = 0;
351
         buf[4] = 'X'; //Error string
352
         buf[5] = 0;
353
         IPWrite(socket, buf, 6);
354
         return;
355
      }
356
   }
357
   if(buf[1] == 1 || buf[1] == 4) //ACK
358
   {
359
      if(file == NULL)
360
         return;
361
      if(buf[1] == 1)
362
         blockNum = 0;
363
      else
364
         blockNum = (buf[2] << 8) | buf[3];
365
      ++blockNum;
366
      buf[0] = 0;
367
      buf[1] = 3;  //DATA
368
      buf[2] = (uint8)(blockNum >> 8);
369
      buf[3] = (uint8)blockNum;
370
      fseek(file, (blockNum-1)*512, 0);
371
      bytes = fread(buf+4, 1, 512, file);
372
      IPWrite(socket, buf, bytes+4);
373
   }
374
}
375
 
376
 
377
void TftpdInit(void)
378
{
379
   IPSocket *socket;
380
   socket = IPOpen(IP_MODE_UDP, 0, 69, TftpdCallback);
381
}
382
 
383
 
384
//******************* TFTP Client ************************
385
 
386
 
387
static void TftpCallback(IPSocket *socket)
388
{
389
   unsigned char buf[512+4];
390
   int bytes, blockNum, length;
391
 
392
   bytes = IPRead(socket, buf, sizeof(buf));
393
   if(bytes < 4 || buf[0])
394
      return;
395
   blockNum = (buf[2] << 8) | buf[3];
396
   length = blockNum * 512 - 512 + bytes - 4;
397
   //printf("TftpCallback(%d,%d)\n", buf[1], blockNum);
398
   if(length > (int)socket->userData)
399
   {
400
      bytes -= length - (int)socket->userData;
401
      length = (int)socket->userData;
402
   }
403
   if(buf[1] == 3) //DATA
404
   {
405
      memcpy((uint8*)socket->userPtr + blockNum * 512 - 512, buf+4, bytes-4);
406
      buf[1] = 4; //ACK
407
      IPWrite(socket, buf, 4);
408
      if(bytes-4 < 512)
409
      {
410
         printf("Done %d bytes\n", length);
411
         socket->userFunc(socket->userPtr, length);
412
         IPClose(socket);
413
      }
414
   }
415
}
416
 
417
 
418
IPSocket *TftpTransfer(uint32 ip, char *filename, uint8 *buffer, int size,
419
                       void (*callback)(uint8 *data, int bytes))
420
{
421
   IPSocket *socket;
422
   uint8 buf[512+4];
423
   int bytes;
424
   socket = IPOpen(IP_MODE_UDP, ip, 69, TftpCallback);
425
   socket->userPtr = buffer;
426
   socket->userData = size;
427
   socket->userFunc = callback;
428
   buf[0] = 0;
429
   buf[1] = 1; //read
430
   strcpy((char*)buf+2, filename);
431
   bytes = strlen(filename);
432
   strcpy((char*)buf+bytes+3, "octet");
433
   IPWrite(socket, buf, bytes+9);
434
   return socket;
435
}
436
 
437
 
438
//******************* Telnet Server ************************
439
 
440
static TelnetFunc_t *TelnetFuncList;
441
 
442
 
443
static void TelnetServer(IPSocket *socket)
444
{
445
   uint8 buf[512+4];
446
   int bytes, i;
447
   char *ptr;
448
   static char command[80];
449
   char command2[80];
450
   bytes = IPRead(socket, buf, sizeof(buf)-1);
451
   if(bytes == 0)
452
   {
453
      if(socket->userData)
454
         return;
455
      socket->userData = 1;
456
      socket->timeout = 0;
457
      socket->headerSend[47] |= 0x08; //PSH
458
      buf[0] = 255; //IAC
459
      buf[1] = 251; //WILL
460
      buf[2] = 3;   //suppress go ahead
461
      buf[3] = 255; //IAC
462
      buf[4] = 251; //WILL
463
      buf[5] = 1;   //echo
464
      strcpy((char*)buf+6, "Welcome to Plasma.\r\n-> ");
465
      IPWrite(socket, buf, 6+23);
466
      IPWriteFlush(socket);
467
      command[0] = 0;
468
      return;
469
   }
470
   buf[bytes] = 0;
471
   if(buf[0] == 255 || buf[0] == 27)
472
      return;
473
   if(buf[0] != 8)
474
   {
475
      IPWrite(socket, buf, bytes);
476
      IPWriteFlush(socket);
477
      strncat(command, (char*)buf, sizeof(command));
478
   }
479
   else
480
   {
481
      //backspace
482
      bytes = strlen(command);
483
      if(bytes)
484
      {
485
         command[bytes-1] = 0;
486
         buf[0] = 8;
487
         buf[1] = ' ';
488
         buf[2] = 8;
489
         IPWrite(socket, buf, 3);
490
         IPWriteFlush(socket);
491
      }
492
   }
493
   ptr = strstr(command, "\r");
494
   if(ptr)
495
   {
496
      ptr[0] = 0;
497
      buf[0] = 0;
498
      if(strncmp(command, "help", 4) == 0)
499
      {
500
         sprintf((char*)buf, "Commands: help, exit");
501
         for(i = 0; TelnetFuncList[i].name; ++i)
502
         {
503
            strcat((char*)buf, ", ");
504
            strcat((char*)buf, TelnetFuncList[i].name);
505
         }
506
         strcat((char*)buf, "\r\n");
507
      }
508
      else if(strncmp(command, "exit", 4) == 0)
509
         IPClose(socket);
510
      else if(command[0])
511
      {
512
         strcpy(command2, command);
513
         ptr = strstr(command2, " ");
514
         if(ptr)
515
            ptr[0] = 0;
516
         for(i = 0; TelnetFuncList[i].name; ++i)
517
         {
518
            if(strcmp(command2, TelnetFuncList[i].name) == 0)
519
            {
520
               TelnetFuncList[i].func(socket, command);
521
               break;
522
            }
523
         }
524
         if(TelnetFuncList[i].name == NULL)
525
            sprintf((char*)buf, "Unknown command (%s)\r\n", command);
526
      }
527
      strcat((char*)buf, "-> ");
528
      IPPrintf(socket, (char*)buf);
529
      command[0] = 0;
530
   }
531
}
532
 
533
 
534
void TelnetInit(TelnetFunc_t *funcList)
535
{
536
   IPSocket *socket;
537
   TelnetFuncList = funcList;
538
   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);
539
}
540
 
541
 
542
//******************* Console ************************
543
 
544 212 rhoads
static uint8 myBuffer[1024*3], myString[80];
545 210 rhoads
IPSocket *socketTelnet;
546
 
547
 
548
void TransferDone(uint8 *data, int bytes)
549
{
550
   printf("TransferDone(0x%x, %d)\n", data, bytes);
551
   data[bytes] = 0;
552
   if(bytes > 500)
553
      data[500] = 0;
554
   printf("%s\n", data);
555
}
556
 
557
 
558
static void TelnetInfo(IPSocket *socket, char *command)
559
{
560
   (void)command;
561
   IPPrintf(socket, "Steve was here!\r\n");
562
}
563
 
564
 
565
static void TelnetMath(IPSocket *socket, char *command)
566
{
567
   int v1, v2;
568
   char buf[20], buf2[20];
569
   (void)socket;
570
   if(strlen(command) < 6)
571
   {
572
      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
573
      return;
574
   }
575
   sscanf(command, "%s%d%s%d", buf, &v1, buf2, &v2);
576
   if(buf2[0] == '+')
577
      v1 += v2;
578
   else if(buf2[0] == '-')
579
      v1 -= v2;
580
   else if(buf2[0] == '*')
581
      v1 *= v2;
582
   else if(buf2[0] == '/')
583
   {
584
      if(v2 != 0)
585
         v1 /= v2;
586
   }
587
   sprintf((char*)myBuffer, "%d\r\n", v1);
588 212 rhoads
   IPPrintf(socket, (char*)myBuffer);
589 210 rhoads
}
590
 
591
 
592
void PingCallback(IPSocket *socket)
593
{
594
   IPSocket *socket2 = socket->userPtr;
595
   //printf("Ping Reply\n");
596
   IPClose(socket);
597
   if(socket2)
598
      IPPrintf(socket2, "Ping Reply\r\n");
599 212 rhoads
   else
600
      printf("Ping Reply\n");
601 210 rhoads
}
602
 
603
 
604
static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg)
605
{
606
   char buf[80];
607
   IPSocket *socketTelnet = arg;
608
   IPSocket *socketPing;
609 212 rhoads
   (void)socket;
610 210 rhoads
   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",
611
      (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);
612
   IPPrintf(socketTelnet, buf);
613
   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);
614
   socketPing->userPtr = socketTelnet;
615
   myBuffer[0] = 'A';
616
   IPWrite(socketPing, myBuffer, 1);
617
}
618
 
619
 
620
static void TelnetPing(IPSocket *socket, char *command)
621
{
622
   int ip0, ip1, ip2, ip3;
623
   char buf[20];
624
   IPSocket *socketPing;
625
   if('0' <= command[5] && command[5] <= '9')
626
   {
627
      sscanf(command, "%s %d.%d.%d.%d", buf, &ip0, &ip1, &ip2, &ip3);
628
      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
629
      socketPing = IPOpen(IP_MODE_PING, ip0, 0, PingCallback);
630
      socketPing->userPtr = socket;
631
      myBuffer[0] = 'A';
632
      IPWrite(socketPing, myBuffer, 1);
633
      IPPrintf(socket, "Sent ping\r\n");
634
   }
635
   else
636
   {
637
      IPResolve(command+5, DnsResultCallback, socket);
638
      IPPrintf(socket, "Sent DNS request\r\n");
639
   }
640
}
641
 
642
 
643
void TelnetTransferDone(uint8 *data, int length)
644
{
645
   data[length] = 0;
646
   IPPrintf(socketTelnet, "Transfer Done\r\n");
647
}
648
 
649
 
650
static void TelnetFtp(IPSocket *socket, char *command)
651
{
652
   char buf[40], user[40], passwd[40], name[80];
653
   int ip0, ip1, ip2, ip3;
654
   if(strlen(command) < 10)
655
   {
656
      IPPrintf(socket, "ftp #.#.#.# User Password File\r\n");
657
      return;
658
   }
659
   sscanf(command, "%s %d.%d.%d.%d %s %s %s",
660
      buf, &ip0, &ip1, &ip2, &ip3, user, passwd, name);
661
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
662
   socketTelnet = socket;
663
   FtpTransfer(ip0, user, passwd, name, myBuffer, sizeof(myBuffer)-1,
664
      0, TelnetTransferDone);
665
}
666
 
667
 
668
static void TelnetTftp(IPSocket *socket, char *command)
669
{
670
   char buf[80], name[80];
671
   int ip0, ip1, ip2, ip3;
672
   if(strlen(command) < 10)
673
   {
674
      IPPrintf(socket, "tftp #.#.#.# File\r\n");
675
      return;
676
   }
677
   sscanf(command, "%s %d.%d.%d.%d %s %s %s",
678
      buf, &ip0, &ip1, &ip2, &ip3, name);
679
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
680
   socketTelnet = socket;
681
   TftpTransfer(ip0, name, myBuffer, sizeof(myBuffer)-1, TelnetTransferDone);
682
}
683
 
684
 
685 212 rhoads
static void TelnetHttpCallback(IPSocket *socket)
686
{
687
   int length;
688
   if(myString[0])
689
      IPPrintf(socket, myString);
690
   myString[0] = 0;
691
   length = strlen(myBuffer);
692
   length += IPRead(socket, myBuffer+length, sizeof(myBuffer)-length-1);
693
   myBuffer[length] = 0;
694
   if(length >= sizeof(myBuffer)-1 || socket->state > IP_TCP)
695
   {
696
      IPClose(socket);
697
      IPPrintf(socketTelnet, "Done\r\n");
698
   }
699
}
700
 
701
static void TelnetHttp(IPSocket *socket, char *command)
702
{
703
   char buf[80], name[80];
704
   int ip0, ip1, ip2, ip3;
705
   IPSocket *socketHttp;
706
   if(strlen(command) < 5)
707
   {
708
      IPPrintf(socket, "http #.#.#.# File\r\n");
709
      return;
710
   }
711
   sscanf(command, "%s %d.%d.%d.%d %s %s %s",
712
      buf, &ip0, &ip1, &ip2, &ip3, name);
713
   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
714
   socketTelnet = socket;
715
   sprintf(myString, "GET %s HTTP/1.0\r\n\r\n", name);
716
   myBuffer[0] = 0;
717
   socketHttp = IPOpen(IP_MODE_TCP, ip0, 80, TelnetHttpCallback);
718
}
719
 
720
 
721 210 rhoads
static void TelnetShow(IPSocket *socket, char *command)
722
{
723 212 rhoads
   int i;
724
   (void)command;
725
   // Insert '\r' before '\n'
726
   for(i = 0; myBuffer[i] && i < sizeof(myBuffer); ++i)
727
   {
728
      if(myBuffer[i] == '\n' && myBuffer[i-1] != '\r')
729
      {
730
         memcpy(myBuffer+i+1, myBuffer+i, sizeof(myBuffer)-2-i);
731
         myBuffer[i] = '\r';
732
      }
733
   }
734
   myBuffer[sizeof(myBuffer)-1] = 0;
735
   IPPrintf(socket, (char*)myBuffer);
736 210 rhoads
   IPPrintf(socket, "\r\n");
737
}
738
 
739
 
740
static void TelnetClear(IPSocket *socket, char *command)
741
{
742 212 rhoads
   (void)socket;
743
   (void)command;
744 210 rhoads
   memset(myBuffer, 0, sizeof(myBuffer));
745
}
746
 
747
 
748
static TelnetFunc_t MyFuncs[] = {
749
   "info", 0, TelnetInfo,
750
   "math", 0, TelnetMath,
751
   "ping", 0, TelnetPing,
752
   "ftp", 0, TelnetFtp,
753
   "tftp", 0, TelnetTftp,
754 212 rhoads
   "http", 0, TelnetHttp,
755 210 rhoads
   "show", 0, TelnetShow,
756
   "clear", 0, TelnetClear,
757
   NULL, 0, NULL
758
};
759
 
760
 
761
void ConsoleInit(void)
762
{
763
   FtpdInit(1);
764
   TftpdInit();
765
   TelnetInit(MyFuncs);
766
}

powered by: WebSVN 2.1.0

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