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