ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/router/ftp.cpp
Revision: 1.2
Committed: 2008-01-01T09:40:34Z (16 years, 6 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * ftp.cpp - ip router
3     *
4 gbeauche 1.2 * Basilisk II (C) 1997-2008 Christian Bauer
5 gbeauche 1.1 *
6     * Windows platform specific code copyright (C) Lauri Pesonen
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21     */
22    
23     #include "sysdeps.h"
24     #include <ctype.h>
25     #include "dump.h"
26     #include "prefs.h"
27     #include "ftp.h"
28    
29     #if DEBUG
30     #pragma optimize("",off)
31     #endif
32    
33     #include "debug.h"
34    
35     static int m_ftp_port_count = 0;
36     #define MAX_FTP_PORTS 100
37     static uint16 m_ftp_ports[MAX_FTP_PORTS];
38    
39     bool ftp_is_ftp_port( uint16 port )
40     {
41     for( int i=0; i<m_ftp_port_count; i++ ) {
42     if( m_ftp_ports[i] == port ) return true;
43     }
44     return false;
45     }
46    
47     void init_ftp()
48     {
49     const char *str = PrefsFindString("ftp_port_list");
50    
51     if(str) {
52     char *ftp = new char [ strlen(str) + 1 ];
53     if(ftp) {
54     strcpy( ftp, str );
55     char *p = ftp;
56     while( p && *p ) {
57     char *pp = strchr( p, ',' );
58     if(pp) *pp++ = 0;
59     if( m_ftp_port_count < MAX_FTP_PORTS ) {
60     m_ftp_ports[m_ftp_port_count++] = (uint16)strtoul(p,0,0);
61     }
62     p = pp;
63     }
64     delete [] ftp;
65     }
66     }
67     }
68    
69     void ftp_modify_port_command(
70     char *buf,
71     int &count,
72     const uint32 max_size,
73     const uint32 ip,
74     const uint16 port,
75     const bool is_pasv
76     )
77     {
78     if( max_size < 100 ) {
79     // impossible
80     return;
81     }
82    
83     sprintf(
84     buf,
85     (is_pasv ? "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d).%c%c" : "PORT %d,%d,%d,%d,%d,%d%c%c"),
86     ip >> 24,
87     (ip >> 16) & 0xFF,
88     (ip >> 8) & 0xFF,
89     ip & 0xFF,
90     (port >> 8) & 0xFF,
91     port & 0xFF,
92     0x0d, 0x0a
93     );
94    
95     count = strlen(buf);
96    
97     D(bug("ftp_modify_port_command: \"%s\"\r\n", buf ));
98     }
99    
100     // this should be robust. rather skip it than do anything dangerous.
101     void ftp_parse_port_command(
102     char *buf,
103     uint32 count,
104     uint16 &ftp_data_port,
105     bool is_pasv
106     )
107     {
108     ftp_data_port = 0;
109    
110     if( !count ) return;
111    
112     uint8 b[100];
113     uint32 ftp_ip = 0;
114    
115     // make it a c-string
116     if( count >= sizeof(b) ) count = sizeof(b)-1;
117     memcpy( b, buf, count );
118     b[ count ] = 0;
119    
120     for( uint32 i=0; i<count; i++ ) {
121     if( b[i] < ' ' || b[i] > 'z' ) {
122     b[i] = ' ';
123     } else {
124     b[i] = tolower(b[i]);
125     }
126     }
127    
128     // D(bug("FTP: \"%s\"\r\n", b ));
129    
130     char *s = (char *)b;
131    
132     while( *s == ' ' ) s++;
133    
134     if(is_pasv) {
135     /*
136     LOCAL SERVER: ..227 Entering Passive Mode (192,168,0,2,6,236). 0d 0a
137     */
138     if( atoi(s) == 227 && strstr(s,"passive") ) {
139     while( *s && *s != '(' ) s++;
140     if( *s++ == 0 ) s = 0;
141     } else {
142     s = 0;
143     }
144     } else {
145     /*
146     LOCAL CLIENT: PORT 192,168,0,1,14,147 0d 0a
147     */
148     if( strncmp(s,"port ",5) == 0 ) {
149     s += 5;
150     } else {
151     s = 0;
152     }
153     }
154    
155     if(s && *s) {
156     // get remote ip (used only for verification)
157     for( uint32 i=0; i<4; i++ ) {
158     while( *s == ' ' ) s++;
159     if(!isdigit(*s)) {
160     ftp_ip = 0;
161     break;
162     }
163     ftp_ip = (ftp_ip << 8) + atoi(s);
164     while( *s && *s != ',' ) s++;
165     if(!*s) {
166     ftp_ip = 0;
167     break;
168     }
169     s++;
170     }
171    
172     if(ftp_ip) {
173     // get local port
174     for( uint32 i=0; i<2; i++ ) {
175     while( *s == ' ' ) s++;
176     if(!isdigit(*s)) {
177     ftp_data_port = 0;
178     break;
179     }
180     ftp_data_port = (ftp_data_port << 8) + atoi(s);
181     while( *s && *s != ',' && *s != ')' ) s++;
182     if(!*s)
183     break;
184     else
185     s++;
186     }
187     }
188     }
189     if(ftp_data_port) {
190     D(bug("ftp_parse_port_command: \"%s\"; port is %d\r\n", b, ftp_data_port ));
191     }
192     }