source: TI05-delivery/trunk/src/bbftp-client-3.2.0/bbftpc/sendproto.c @ 773

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-client-3.2.0/bbftpc/sendproto.c@773
Revision 773, 7.6 KB checked in by spascoe, 14 years ago (diff)

Initial import of bbftp source

Line 
1/*
2 * bbftpc/sendproto.c
3 * Copyright (C) 1999, 2000, 2001, 2002 IN2P3, CNRS
4 * bbftp@in2p3.fr
5 * http://doc.in2p3.fr/bbftp
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */ 
21
22/****************************************************************************
23
24 
25 RETURN :
26      0                  = everything OK
27     -1                 = Connection broken retry possible
28
29  This routine may exit in case of retry not possible
30   
31 sendproto.c v 2.0.0  2001/02/10    - Creation of the routine
32             v 2.0.1  2001/04/19    - Correct indentation
33                                    - Port to IRIX
34 
35*****************************************************************************/
36#include <bbftp.h>
37
38#include <errno.h>
39#include <fcntl.h>
40#include <netinet/in.h>
41#include <signal.h>
42#include <stdio.h>
43#include <sys/socket.h>
44#include <sys/stat.h>
45#if TIME_WITH_SYS_TIME
46# include <sys/time.h>
47# include <time.h>
48#else
49# if HAVE_SYS_TIME_H
50#  include <sys/time.h>
51# else
52#  include <time.h>
53# endif
54#endif
55#include <sys/types.h>
56#include <unistd.h>
57#if HAVE_STRING_H
58# include <string.h>
59#endif
60
61
62#include <client.h>
63#include <client_proto.h>
64#include <common.h>
65#include <structures.h>
66
67extern  int     timestamp;
68extern  int     protocolmin;
69extern  int     protocolmax;
70extern  int     incontrolsock ;
71extern  int     outcontrolsock ;
72extern  int         recvcontrolto ;
73extern  int         sendcontrolto ;
74extern  int     protocol ;
75extern  int     debug ;
76
77int sendproto() 
78{
79
80    char    buffer[8] ;
81    struct  message *msg ;
82    int     remoteprotomin ;
83    int     remoteprotomax ;
84    fd_set  selectmask ; /* Select mask */
85    int     nfds ; /* Max number of file descriptor */
86    int     code ;
87    int     retcode ;
88    int     msglen ;
89 
90
91    if ( debug ) printmessage(stdout,CASE_NORMAL,0,timestamp,"Sending protocol request\n") ;
92    /*
93    ** Now send the control message : First part
94    */
95    msg = (struct message *)buffer ;
96    msg->code = MSG_PROT ;
97    msg->msglen = 0 ;
98    if ( writemessage(outcontrolsock,buffer,MINMESSLEN,sendcontrolto,0) < 0 ) {
99        /*
100        ** We were not able to send the minimum message so
101        ** we are going to close the control socket and to
102        ** tell the calling program to restart a connection
103        */
104        printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT");
105        return -1 ; /* restart connection */
106    }
107    if ( debug ) printmessage(stdout,CASE_NORMAL,0,timestamp,"Sent message %x\n", msg->code) ;
108    /*
109    ** Now we are going to wait for the message on the control
110    ** connection
111    */
112waitcontrol:
113    nfds = sysconf(_SC_OPEN_MAX) ;
114    FD_ZERO(&selectmask) ;
115    FD_SET(incontrolsock,&selectmask) ;
116    retcode = select(FD_SETSIZE,&selectmask,0,0,0) ;
117    if ( retcode < 0 ) {
118        /*
119        ** Select error
120        */
121        if ( errno != EINTR ) {
122            /*
123            ** we have got an error so close the connection
124            ** and restart
125            */
126            printmessage(stderr,CASE_ERROR,66,timestamp,"Error select on control connection : %s\n",strerror(errno));
127            return -1 ;
128        } else {
129            /*
130            ** Interrupted by a signal
131            */
132            FD_ZERO(&selectmask) ;
133            FD_SET(incontrolsock,&selectmask) ;
134            goto waitcontrol ;
135        }
136    } else if ( retcode == 0 ) {
137        /*
138        ** Impossible we do not set any timer
139        */
140        FD_ZERO(&selectmask) ;
141        FD_SET(incontrolsock,&selectmask) ;
142        goto waitcontrol ;
143    } else {
144        /*
145        ** read the message
146        */
147        if ( readmessage(incontrolsock,buffer,MINMESSLEN,recvcontrolto,0) < 0 ) {
148            printmessage(stderr,CASE_ERROR,61,timestamp,"Error waiting %s message\n","MSG_PROT_ANS");
149            return -1 ;
150        }
151        msg = (struct message *)buffer ;
152        code = msg->code ;
153        if ( code == MSG_BAD || code == MSG_BAD_NO_RETRY) {
154            /*
155            ** The server does not understand protocol
156            */
157            printmessage(stderr,CASE_FATAL_ERROR,101,timestamp,"Incompatible deamon and client (remote protocol version (%d,%d), local (%d,%d))\n",1,1,protocolmin,protocolmax);
158           
159        } else if (msg->code == MSG_PROT_ANS ) {
160            /*
161            ** At this stage
162            */
163#ifndef WORDS_BIGENDIAN
164            msglen = ntohl(msg->msglen) ;
165#else
166            msglen = msg->msglen ;
167#endif
168            if ( msglen != 8 ) {
169                printmessage(stderr,CASE_ERROR,63,timestamp,"Unexpected message length while waiting for %s message\n","MSG_PROT_ANS");
170                 return -1 ;
171            }
172            if ( readmessage(incontrolsock,buffer,msglen,recvcontrolto,0) < 0) {
173                printmessage(stderr,CASE_ERROR,67,timestamp,"Error reading data for %s message\n","MSG_PROT_ANS");
174                return -1 ;
175            }
176#ifndef WORDS_BIGENDIAN
177            remoteprotomin = ntohl(msg->code) ;
178            remoteprotomax = ntohl(msg->msglen) ;
179#else
180            remoteprotomin = msg->code ;
181            remoteprotomax = msg->msglen ;
182#endif           
183            if ( (remoteprotomax < protocolmin) || (remoteprotomin > protocolmax) ) {
184                /*
185                ** Imcompatible version
186                */
187                msg->code = MSG_BAD_NO_RETRY ;
188                msg->msglen = 0 ;
189                writemessage(outcontrolsock,buffer,MINMESSLEN,recvcontrolto,0); 
190                printmessage(stderr,CASE_FATAL_ERROR,101,timestamp,"Incompatible deamon and client (remote protocol version (%d,%d), local (%d,%d))\n",remoteprotomin,remoteprotomax,protocolmin,protocolmax);
191               
192            } else if (remoteprotomax <= protocolmax ) {
193                protocol = remoteprotomax ;
194            } else {
195                protocol = protocolmax ;
196            }
197            msg->code = MSG_PROT_ANS ;
198#ifndef WORDS_BIGENDIAN
199            msg->msglen = ntohl(4) ;
200#else
201            msg->msglen = 4 ;
202#endif
203            if ( writemessage(outcontrolsock,buffer,MINMESSLEN,recvcontrolto,0) < 0 ){
204                printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT_ANS");
205                return -1 ; /* restart connection */
206            }
207#ifndef WORDS_BIGENDIAN
208            msg->code = ntohl(protocol) ;
209#else
210            msg->code = protocol ;
211#endif
212            if ( writemessage(outcontrolsock,buffer,4,recvcontrolto,0) < 0 ){
213                printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT_ANS");
214                return -1 ; /* restart connection */
215            }
216            return 0 ;
217        } else {
218            /*
219            ** Receive unkwown message so something is
220            ** going wrong. close the control socket
221            ** and restart
222            */
223            printmessage(stderr,CASE_ERROR,62,timestamp,"Unknown message while waiting for %s message\n","MSG_PROT_ANS");
224            return -1 ;
225        }
226    }
227    /*
228    ** Never reach this point but to in order to avoid stupid messages
229    ** from IRIX compiler set a return code to -1
230    */
231    return -1 ;
232
233}
Note: See TracBrowser for help on using the repository browser.