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

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [telnet.c] - Blame information for rev 78

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  boot-loader-ethmac.c                                        //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  The main functions for the boot loader application. This    //
10 78 csantifort
//  application is embedded in the FPGA's SRAM and is used      //
11 61 csantifort
//  to load larger applications into the DDR3 memory on         //
12
//  the development board.                                      //
13
//                                                              //
14
//  Author(s):                                                  //
15
//      - Conor Santifort, csantifort.amber@gmail.com           //
16
//                                                              //
17
//////////////////////////////////////////////////////////////////
18
//                                                              //
19
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
20
//                                                              //
21
// This source file may be used and distributed without         //
22
// restriction provided that this copyright statement is not    //
23
// removed from the file and that any derivative work contains  //
24
// the original copyright notice and the associated disclaimer. //
25
//                                                              //
26
// This source file is free software; you can redistribute it   //
27
// and/or modify it under the terms of the GNU Lesser General   //
28
// Public License as published by the Free Software Foundation; //
29
// either version 2.1 of the License, or (at your option) any   //
30
// later version.                                               //
31
//                                                              //
32
// This source is distributed in the hope that it will be       //
33
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
34
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
35
// PURPOSE.  See the GNU Lesser General Public License for more //
36
// details.                                                     //
37
//                                                              //
38
// You should have received a copy of the GNU Lesser General    //
39
// Public License along with this source; if not, download it   //
40
// from http://www.opencores.org/lgpl.shtml                     //
41
//                                                              //
42
----------------------------------------------------------------*/
43
 
44
 
45
#include "timer.h"
46
#include "line-buffer.h"
47
#include "packet.h"
48
#include "tcp.h"
49
#include "telnet.h"
50
 
51
void parse_telnet_options(char* buf, socket_t* socket)
52
{
53
    int     i;
54
    int     stage = 0;
55
    char    stage1;
56 78 csantifort
 
57 61 csantifort
    for (i=0;i<socket->rx_packet->tcp_payload_len;i++) {
58 78 csantifort
 
59 61 csantifort
        if (stage == 0) {
60
            switch (buf[i]) {
61
                case 241: stage = 0; break;  // NOP
62 78 csantifort
                case 255: stage = 1;
63 61 csantifort
                                 if (socket->telnet_connection_state == TELNET_CLOSED) {
64
                                     socket->telnet_connection_state = TELNET_OPEN;
65
                                    }
66
                         break;  // IAC
67 78 csantifort
 
68
                default:  if (buf[i] < 128)
69 61 csantifort
                    goto telnet_payload;
70
            }
71 78 csantifort
 
72 61 csantifort
        } else if (stage == 1) {
73
            stage1 = buf[i];
74
            switch (buf[i]) {
75
                case 241        : stage = 0; break;  // NOP
76
                case 250        : stage = 2; break;  // SB
77
                case TELNET_WILL: stage = 2; break;  // 0xfb WILL
78
                case TELNET_WONT: stage = 2; break;  // 0xfc WONT
79
                case TELNET_DO  : stage = 2; break;  // 0xfd DO
80
                case TELNET_DONT: stage = 2; break;  // 0xfe DONT
81
                default         : stage = 2; break;
82
            }
83 78 csantifort
 
84 61 csantifort
        } else {  // stage = 2
85 78 csantifort
            stage = 0;
86 61 csantifort
            switch (buf[i]) {
87
                case 1:   // echo
88
                    /* Client request that server echos stuff back to client */
89
                    if (stage1 == TELNET_DO)
90
                        socket->telnet_echo_mode = 1;
91
                    /* Client request that server does not echo stuff back to client */
92
                    else if (stage1 == TELNET_DONT)
93
                        socket->telnet_echo_mode = 0;
94
                    break;
95 78 csantifort
 
96 61 csantifort
                case 3:   break;  // suppress go ahead
97
                case 5:   break;  // status
98
                case 6:   break;  // time mark
99
                case 24:  break;  // terminal type
100
                case 31:  break;  // window size
101
                case 32:  break;  // terminal speed
102
                case 33:  break;  // remote flow control
103
                case 34:  break;  // linemode
104
                case 35:  break;  // X display location
105
                case 39:  break;  // New environmental variable option
106
                default:  break;
107
                }
108
            }
109
        }
110
 
111 78 csantifort
    return;
112
 
113 61 csantifort
    telnet_payload:
114
        socket->rx_packet->telnet_payload_len = socket->rx_packet->tcp_payload_len - i;
115
        parse_telnet_payload(&buf[i], socket);
116
}
117
 
118
 
119
void parse_telnet_payload(char * buf, socket_t* socket)
120
{
121
    int i;
122
    int cr = 0;
123
    int windows = 0;
124
    for (i=0;i<socket->rx_packet->telnet_payload_len;i++) {
125 78 csantifort
        if (buf[i] == '\n')
126 61 csantifort
            windows = 1;
127
        else if (buf[i] < 128 && buf[i] != 0) {
128
            /* end of a line */
129
            /* receive \r\n from Windows, \r from Linux */
130
            if (buf[i] == '\r') {
131
                cr=1;
132
                put_byte(socket->telnet_rxbuf, buf[i], 1); /* last byte of line */
133 78 csantifort
                }
134 61 csantifort
            else {
135
                put_byte(socket->telnet_rxbuf, buf[i], 0); /* not last byte of line */
136 78 csantifort
                }
137 61 csantifort
            }
138
        }
139 78 csantifort
 
140 61 csantifort
    if (socket->telnet_echo_mode) {
141
        if (cr && !windows) {
142
            buf[socket->rx_packet->telnet_payload_len] = '\n';
143
            socket->rx_packet->telnet_payload_len++;
144
            }
145
        tcp_reply(socket, buf, socket->rx_packet->telnet_payload_len);
146
        }
147
}
148
 
149
 
150
void telnet_options(socket_t* socket)
151
{
152
    char buf[3];
153
 
154
    // telnet options
155
    // Will echo - advertise that I have the ability to echo back commands to the client
156 78 csantifort
    buf[0] = 0xff; buf[1] = TELNET_WILL; buf[2] = 0x01;
157 61 csantifort
    tcp_reply(socket, buf, 3);
158
 
159
}
160
 
161
 
162
void telnet_tx(socket_t* socket, line_buf_t* txbuf)
163
{
164
    int line_len;
165
    int total_line_len;
166
    char* line;
167
    char* first_line;
168
 
169 78 csantifort
    /* Parse telnet tx buffer
170
       Grab as many lines as possible to stuff into a packet to transmit */
171
    line_len = get_line(txbuf, &first_line);
172
    if (line_len) {
173
        total_line_len = line_len;
174
        while (total_line_len < MAX_TELNET_TX && line_len) {
175
            line_len = get_line(txbuf, &line);
176
            total_line_len += line_len;
177
            }
178
        tcp_tx(socket, first_line, total_line_len);
179
        }
180 61 csantifort
}
181 78 csantifort
 

powered by: WebSVN 2.1.0

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