source: TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/createreceivesock.c @ 773

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

Initial import of bbftp source

Line 
1/*
2 * bbftpd/createreceivesock.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 Create the receive socket and connect to it.
26
27RETURN VALUE :
28 In case of failure no socket is opened and -1 is returned.
29 If connect fail due to interrupted system call 0 is returned.
30 In case of success the socket is returned.
31 
32 createreceivesock.c v 1.3.0 2000/03/20
33                     v 1.4.0 2000/03/20 - Correct bug on sprintf ( forgot the
34                                           socknum)
35                                          Bug shown by gfarrache
36                     v 1.6.0 2000/03/24 - Set Send buffer to TCPWINSIZE for
37                                          get option
38                     v 1.6.1 2000/03/24 - Portage to OSF1
39                                        - Set the socket in non block mode
40                     v 1.8.7 2000/05/24 - Change headers
41                     v 1.9.3 2000/10:12 - Use FIXEDDATAPORT in order to
42                                          manage the local port
43                     v 1.9.4 2000/10/16 - Make all sockets blocking in order
44                                          to prevent the error in case of lack
45                                          of memory
46                                        - Supress %m
47                     v 2.0.1 2001/04/23 - Correct indentation
48                     v 2.0.2 2001/05/04 - Correct include for RFIO
49
50*****************************************************************************/
51#include <bbftpd.h>
52
53#include <errno.h>
54#include <fcntl.h>
55#include <netinet/in.h>
56#include <netinet/tcp.h>
57#include <syslog.h>
58#include <sys/socket.h>
59#include <sys/types.h>
60#include <unistd.h>
61#if HAVE_STRING_H
62# include <string.h>
63#endif
64
65#include <common.h>
66#include <daemon.h>
67
68extern struct sockaddr_in his_addr;        /* Remote adresse */
69extern struct sockaddr_in ctrl_addr;    /* Local adresse */
70extern int  fixeddataport ;
71
72int createreceivesock(port,socknum,logmessage)
73    int        port ;
74    int        socknum ;
75    char    *logmessage ;
76{
77    int    sock ;
78    struct sockaddr_in data_source ;
79    int    on = 1 ;
80    int tcpwinsize = TCPWINSIZE ;
81    int    addrlen ;
82    int    retcode ;
83
84    sock = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ;
85    if ( sock < 0 ) {
86        syslog(BBFTPD_ERR, "Cannot create receive socket %d : %s",socknum,strerror(errno));
87        sprintf(logmessage,"Cannot create receive socket %d : %s",socknum,strerror(errno)) ;
88        return (-1) ;
89    }
90    if ( setsockopt(sock,SOL_SOCKET, SO_REUSEADDR,(char *)&on,sizeof(on)) < 0 ) {
91        close(sock) ;
92        syslog(BBFTPD_ERR,"Cannot set SO_REUSEADDR on receive socket %d : %s",socknum,strerror(errno)) ;
93        sprintf(logmessage,"Cannot set SO_REUSEADDR on receive socket %d : %s",socknum,strerror(errno)) ;
94        return (-1) ;
95    }
96    if ( setsockopt(sock,SOL_SOCKET, SO_RCVBUF,(char *)&tcpwinsize,sizeof(tcpwinsize)) < 0 ) {
97        close(sock) ;
98        syslog(BBFTPD_ERR,"Cannot set SO_RCVBUF on receive socket %d : %s",socknum,strerror(errno)) ;
99        sprintf(logmessage,"Cannot set SO_RCVBUF on receive socket %d : %s",socknum,strerror(errno)) ;
100        return (-1) ;
101    }
102    if ( setsockopt(sock,SOL_SOCKET, SO_SNDBUF,(char *)&tcpwinsize,sizeof(tcpwinsize)) < 0 ) {
103        close(sock) ;
104        syslog(BBFTPD_ERR,"Cannot set SO_SNDBUF on receive socket %d : %s",socknum,strerror(errno)) ;
105        sprintf(logmessage,"Cannot set SO_SNDBUF on receive socket %d : %s",socknum,strerror(errno)) ;
106        return (-1) ;
107    }
108    if ( setsockopt(sock,IPPROTO_TCP, TCP_NODELAY,(char *)&on,sizeof(on)) < 0 ) {
109        close(sock) ;
110        syslog(BBFTPD_ERR,"Cannot set TCP_NODELAY on receive socket %d : %s",socknum,strerror(errno)) ;
111        sprintf(logmessage,"Cannot set TCP_NODELAY on receive socket %d : %s",socknum,strerror(errno)) ;
112        return (-1) ;
113    }
114    data_source.sin_family = AF_INET;
115#ifdef SUNOS
116    data_source.sin_addr = ctrl_addr.sin_addr;
117#else
118    data_source.sin_addr.s_addr = INADDR_ANY;
119#endif
120    if ( fixeddataport == 1 ) {
121        data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1);
122    } else {
123        data_source.sin_port = 0;
124    }
125    if ( bind(sock, (struct sockaddr *) &data_source,sizeof(data_source)) < 0) {
126        close(sock) ;
127        syslog(BBFTPD_ERR,"Cannot bind on receive socket %d : %s",socknum,strerror(errno)) ;
128        sprintf(logmessage,"Cannot bind on receive socket %d : %s",socknum,strerror(errno)) ;
129        return (-1) ;
130    }
131    data_source.sin_addr = his_addr.sin_addr;
132    data_source.sin_port = htons(port);
133    addrlen = sizeof(data_source) ;
134    retcode = connect(sock,(struct sockaddr *) &data_source,addrlen) ;
135    if ( retcode < 0 ) {
136        if ( errno == EINTR ) {
137            /*
138            ** In this case we are going to tell the calling program
139            ** to retry so we close the socket and set the return code
140            ** to zero
141            */
142            close(sock) ;
143            syslog(BBFTPD_ERR,"Cannot connect receive socket %d : %s, telling calling to retry ",socknum,strerror(errno)) ;
144            return 0 ;
145        } else {
146            close(sock) ;
147            syslog(BBFTPD_ERR,"Cannot connect receive socket %d : %s",socknum,strerror(errno)) ;
148            sprintf(logmessage,"Cannot connect receive socket %d : %s",socknum,strerror(errno)) ;
149            return (-1) ;
150        }
151    }
152    return sock ;
153}
Note: See TracBrowser for help on using the repository browser.