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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [ppp/] [current/] [tests/] [windows_telnet.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/windows_telnet.c
4
//
5
//      Test program showing how to create a client that dials into
6
//      a Windows PPP server and waits for a telnet session.
7
//
8
//==========================================================================
9
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
10
// -------------------------------------------                              
11
// This file is part of eCos, the Embedded Configurable Operating System.   
12
// Copyright (C) 2003 Free Software Foundation, Inc.                        
13
//
14
// eCos is free software; you can redistribute it and/or modify it under    
15
// the terms of the GNU General Public License as published by the Free     
16
// Software Foundation; either version 2 or (at your option) any later      
17
// version.                                                                 
18
//
19
// eCos is distributed in the hope that it will be useful, but WITHOUT      
20
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
21
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
22
// for more details.                                                        
23
//
24
// You should have received a copy of the GNU General Public License        
25
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
26
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
27
//
28
// As a special exception, if other files instantiate templates or use      
29
// macros or inline functions from this file, or you compile this file      
30
// and link it with other works to produce a work based on this file,       
31
// this file does not by itself cause the resulting work to be covered by   
32
// the GNU General Public License. However the source code for this file    
33
// must still be made available in accordance with section (3) of the GNU   
34
// General Public License v2.                                               
35
//
36
// This exception does not invalidate any other reasons why a work based    
37
// on this file might be covered by the GNU General Public License.         
38
// -------------------------------------------                              
39
// ####ECOSGPLCOPYRIGHTEND####                                              
40
// ####BSDALTCOPYRIGHTBEGIN####                                             
41
// -------------------------------------------                              
42
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
43
// or other sources, and if so are covered by the appropriate copyright     
44
// and license included herein.                                             
45
// -------------------------------------------                              
46
// ####BSDALTCOPYRIGHTEND####                                               
47
//==========================================================================
48
//#####DESCRIPTIONBEGIN####
49
//
50
// Author(s):    oyvind
51
// Contributors: oyvind harboe
52
// Date:         2004-06-24
53
// Purpose:      
54
// Description:  Example/test on how to connect to a Windows PPP server
55
//              
56
//
57
//####DESCRIPTIONEND####
58
//
59
//==========================================================================
60
 
61
 
62
#include <pkgconf/system.h>
63
#include <pkgconf/net.h>
64
#include <network.h>
65
#include <cyg/ppp/ppp.h>
66
 
67
static char *windows_script[] =
68
{
69
     "TIMEOUT",         "2",
70
     "",                "CLIENTCLIENT\\c",
71
     "CLIENTSERVER",    "\\c",
72
 
73
};
74
 
75
void telnet(void)
76
{
77
     struct   sockaddr_in sin;
78
     struct   sockaddr_in pin;
79
 
80
     /* get an internet domain socket */
81
     int sd;
82
     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
83
     {
84
          return;
85
     }
86
 
87
     /* complete the socket structure */
88
     memset(&sin, 0, sizeof(sin));
89
     sin.sin_len = sizeof(sin);
90
     sin.sin_family = AF_INET;
91
     sin.sin_addr.s_addr = INADDR_ANY;
92
     sin.sin_port = htons(23);
93
 
94
     unsigned int opt = 1;
95
     if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
96
     {
97
          goto out;
98
     }
99
 
100
     /* bind the socket to the port number */
101
     if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
102
     {
103
          goto out;
104
     }
105
 
106
     /* show that we are willing to listen */
107
     if (listen(sd, SOMAXCONN) == -1)
108
     {
109
          goto out;
110
     }
111
 
112
     int sd_current;
113
     /* wait for a client to talk to us */
114
     socklen_t addrlen = sizeof(pin);
115
     if ((sd_current = accept(sd, (struct sockaddr *)  &pin, &addrlen)) == -1)
116
     {
117
          goto out;
118
     }
119
 
120
     for (;;)
121
     {
122
          char *prompt="eCos>";
123
          int promptlen = strlen(prompt);
124
 
125
          if (write(sd_current, prompt, promptlen) != promptlen)
126
          {
127
               goto AbortSession;
128
          }
129
          /* get a message from the client */
130
          char dir[256];
131
          int len;
132
          size_t i;
133
          for (i=0; i<sizeof(dir)-1; i++)
134
          {
135
               // returns when a full line has been collected
136
               len=read(sd_current, dir+i, 1);
137
               if (len != 1)
138
               {
139
                    goto AbortSession;
140
               }
141
               if (write(sd_current, dir+i, 1)!=1)
142
               {
143
                    goto AbortSession;
144
               }
145
 
146
               // ignore CR
147
               if (dir[i]=='\r')
148
               {
149
                    i--;
150
               }
151
 
152
               // Break out on a new line
153
               if (dir[i]=='\n')
154
               {
155
                    break;
156
               }
157
               dir[i]=0;
158
          }
159
     }
160
 AbortSession:
161
     /* close up both sockets */
162
     close(sd_current);
163
 out:
164
     close(sd);
165
}
166
 
167
 
168
 
169
int main(int argc, char **argv)
170
{
171
     // Bring up the TCP/IP network
172
     init_all_network_interfaces();
173
 
174
     for (;;)
175
     {
176
          cyg_ppp_options_t options;
177
          cyg_ppp_handle_t ppp_handle;
178
 
179
          // Initialize the options
180
          cyg_ppp_options_init( &options );
181
 
182
          options.script=windows_script;
183
          options.baud = CYGNUM_SERIAL_BAUD_38400;
184
          options.flowctl = CYG_PPP_FLOWCTL_NONE;
185
          options.idle_time_limit = 0; // never shut down.      
186
 
187
          // Start up PPP
188
          ppp_handle = cyg_ppp_up( "/dev/ser0", &options );
189
 
190
          // Wait for it to get running
191
          if( cyg_ppp_wait_up( ppp_handle ) == 0 )
192
          {
193
               // Make use of PPP
194
               for (;;)
195
               {
196
                    telnet();
197
               }
198
 
199
               // never reached, but  for illustration:
200
 
201
               // Bring PPP link down
202
               cyg_ppp_down( ppp_handle );
203
 
204
               // Wait for connection to go down.
205
               cyg_ppp_wait_down( ppp_handle );
206
          }
207
     }
208
}

powered by: WebSVN 2.1.0

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