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 | |
---|
27 | RETURN 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 <stdio.h> |
---|
54 | |
---|
55 | #include <errno.h> |
---|
56 | #include <fcntl.h> |
---|
57 | #include <netinet/in.h> |
---|
58 | #include <netinet/tcp.h> |
---|
59 | #include <bbftpd_private_log.h> |
---|
60 | #include <sys/socket.h> |
---|
61 | #include <sys/types.h> |
---|
62 | #include <unistd.h> |
---|
63 | #if HAVE_STRING_H |
---|
64 | # include <string.h> |
---|
65 | #endif |
---|
66 | |
---|
67 | #include <common.h> |
---|
68 | #include <daemon.h> |
---|
69 | |
---|
70 | extern struct sockaddr_in his_addr; /* Remote adresse */ |
---|
71 | extern struct sockaddr_in ctrl_addr; /* Local adresse */ |
---|
72 | extern int fixeddataport ; |
---|
73 | |
---|
74 | int createreceivesock(port,socknum,logmessage) |
---|
75 | int port ; |
---|
76 | int socknum ; |
---|
77 | char *logmessage ; |
---|
78 | { |
---|
79 | int sock ; |
---|
80 | struct sockaddr_in data_source ; |
---|
81 | int on = 1 ; |
---|
82 | int tcpwinsize = TCPWINSIZE ; |
---|
83 | int addrlen ; |
---|
84 | int retcode ; |
---|
85 | |
---|
86 | sock = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ; |
---|
87 | if ( sock < 0 ) { |
---|
88 | bbftpd_log(BBFTPD_ERR, "Cannot create receive socket %d : %s",socknum,strerror(errno)); |
---|
89 | sprintf(logmessage,"Cannot create receive socket %d : %s",socknum,strerror(errno)) ; |
---|
90 | return (-1) ; |
---|
91 | } |
---|
92 | if ( setsockopt(sock,SOL_SOCKET, SO_REUSEADDR,(char *)&on,sizeof(on)) < 0 ) { |
---|
93 | close(sock) ; |
---|
94 | bbftpd_log(BBFTPD_ERR,"Cannot set SO_REUSEADDR on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
95 | sprintf(logmessage,"Cannot set SO_REUSEADDR on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
96 | return (-1) ; |
---|
97 | } |
---|
98 | if ( setsockopt(sock,SOL_SOCKET, SO_RCVBUF,(char *)&tcpwinsize,sizeof(tcpwinsize)) < 0 ) { |
---|
99 | close(sock) ; |
---|
100 | bbftpd_log(BBFTPD_ERR,"Cannot set SO_RCVBUF on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
101 | sprintf(logmessage,"Cannot set SO_RCVBUF on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
102 | return (-1) ; |
---|
103 | } |
---|
104 | if ( setsockopt(sock,SOL_SOCKET, SO_SNDBUF,(char *)&tcpwinsize,sizeof(tcpwinsize)) < 0 ) { |
---|
105 | close(sock) ; |
---|
106 | bbftpd_log(BBFTPD_ERR,"Cannot set SO_SNDBUF on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
107 | sprintf(logmessage,"Cannot set SO_SNDBUF on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
108 | return (-1) ; |
---|
109 | } |
---|
110 | if ( setsockopt(sock,IPPROTO_TCP, TCP_NODELAY,(char *)&on,sizeof(on)) < 0 ) { |
---|
111 | close(sock) ; |
---|
112 | bbftpd_log(BBFTPD_ERR,"Cannot set TCP_NODELAY on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
113 | sprintf(logmessage,"Cannot set TCP_NODELAY on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
114 | return (-1) ; |
---|
115 | } |
---|
116 | data_source.sin_family = AF_INET; |
---|
117 | #ifdef SUNOS |
---|
118 | data_source.sin_addr = ctrl_addr.sin_addr; |
---|
119 | #else |
---|
120 | data_source.sin_addr.s_addr = INADDR_ANY; |
---|
121 | #endif |
---|
122 | if ( fixeddataport == 1 ) { |
---|
123 | data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); |
---|
124 | } else { |
---|
125 | data_source.sin_port = 0; |
---|
126 | } |
---|
127 | if ( bind(sock, (struct sockaddr *) &data_source,sizeof(data_source)) < 0) { |
---|
128 | close(sock) ; |
---|
129 | bbftpd_log(BBFTPD_ERR,"Cannot bind on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
130 | sprintf(logmessage,"Cannot bind on receive socket %d : %s",socknum,strerror(errno)) ; |
---|
131 | return (-1) ; |
---|
132 | } |
---|
133 | data_source.sin_addr = his_addr.sin_addr; |
---|
134 | data_source.sin_port = htons(port); |
---|
135 | addrlen = sizeof(data_source) ; |
---|
136 | retcode = connect(sock,(struct sockaddr *) &data_source,addrlen) ; |
---|
137 | if ( retcode < 0 ) { |
---|
138 | if ( errno == EINTR ) { |
---|
139 | /* |
---|
140 | ** In this case we are going to tell the calling program |
---|
141 | ** to retry so we close the socket and set the return code |
---|
142 | ** to zero |
---|
143 | */ |
---|
144 | close(sock) ; |
---|
145 | bbftpd_log(BBFTPD_ERR,"Cannot connect receive socket %d : %s, telling calling to retry ",socknum,strerror(errno)) ; |
---|
146 | return 0 ; |
---|
147 | } else { |
---|
148 | close(sock) ; |
---|
149 | bbftpd_log(BBFTPD_ERR,"Cannot connect receive socket %d : %s",socknum,strerror(errno)) ; |
---|
150 | sprintf(logmessage,"Cannot connect receive socket %d : %s",socknum,strerror(errno)) ; |
---|
151 | return (-1) ; |
---|
152 | } |
---|
153 | } |
---|
154 | return sock ; |
---|
155 | } |
---|