952
|
1 /*****************************************************************************/
|
|
2 /* socket-connect.c - contains functions for connecting to a server */
|
|
3 /* Copyright (C) 1998-2008 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp.h"
|
|
21 static const char cvsid[] = "$Id: protocols.c 952 2008-01-24 23:31:26Z masneyb $";
|
|
22
|
|
23 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR)
|
|
24
|
|
25 static struct hostent *
|
|
26 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop)
|
|
27 {
|
|
28 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT;
|
|
29 struct hostent *hent;
|
|
30
|
|
31 if (g_thread_supported ())
|
|
32 g_static_mutex_lock (&hostfunclock);
|
|
33
|
|
34 if ((hent = gethostbyname (name)) == NULL)
|
|
35 {
|
|
36 if (h_errnop)
|
|
37 *h_errnop = h_errno;
|
|
38 }
|
|
39 else
|
|
40 {
|
|
41 *result_buf = *hent;
|
|
42 hent = result_buf;
|
|
43 }
|
|
44
|
|
45 if (g_thread_supported ())
|
|
46 g_static_mutex_unlock (&hostfunclock);
|
|
47
|
|
48 return (hent);
|
|
49 }
|
|
50
|
|
51
|
|
52 int
|
|
53 lookup_host_with_gethostbyname (gftp_request *request, char *proxy_hostname,
|
|
54 struct hostent *hostp)
|
|
55 {
|
|
56 char *connect_host;
|
|
57
|
|
58 if (request->use_proxy)
|
|
59 connect_host = proxy_hostname;
|
|
60 else
|
|
61 connect_host = request->hostname;
|
|
62
|
|
63 request->logging_function (gftp_logging_misc, request,
|
|
64 _("Looking up %s\n"), connect_host);
|
|
65
|
|
66 if ((r_gethostbyname (connect_host, hostp, NULL)) == NULL)
|
|
67 {
|
|
68 request->logging_function (gftp_logging_error, request,
|
|
69 _("Cannot look up hostname %s: %s\n"),
|
|
70 connect_host, g_strerror (errno));
|
|
71 return (GFTP_ERETRYABLE);
|
|
72 }
|
|
73
|
|
74 return (0);
|
|
75 }
|
|
76
|
|
77
|
|
78 int
|
|
79 gftp_connect_server_legacy (gftp_request * request, char *service,
|
|
80 char *proxy_hostname, unsigned int proxy_port)
|
|
81 {
|
|
82 int sock, curhost, ret, connect_port;
|
|
83 struct sockaddr_in remote_address;
|
|
84 struct servent serv_struct;
|
|
85 struct hostent host;
|
|
86
|
|
87 ret = lookup_host_with_gethostbyname (request, proxy_hostname, &host);
|
|
88 if (ret != 0)
|
|
89 return (ret);
|
|
90
|
|
91 if (request->use_proxy)
|
|
92 connect_port = proxy_port;
|
|
93 else
|
|
94 connect_port = request->port;
|
|
95
|
|
96 if (connect_port == 0)
|
|
97 {
|
|
98 if (!r_getservbyname (service, "tcp", &serv_struct, NULL))
|
|
99 {
|
|
100 request->logging_function (gftp_logging_error, request,
|
|
101 _("Cannot look up service name %s/tcp. Please check your services file\n"),
|
|
102 service);
|
|
103 return (GFTP_ERETRYABLE);
|
|
104 }
|
|
105
|
|
106 connect_port = ntohs (serv_struct.s_port);
|
|
107
|
|
108 if (!request->use_proxy)
|
|
109 request->port = connect_port;
|
|
110 }
|
|
111
|
|
112 sock = GFTP_ERETRYABLE;
|
|
113 request->ai_family = AF_INET;
|
|
114
|
|
115 for (curhost = 0; host.h_addr_list[curhost] != NULL; curhost++)
|
|
116 {
|
|
117 if ((sock = socket (request->ai_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
|
118 {
|
|
119 request->logging_function (gftp_logging_error, request,
|
|
120 _("Failed to create a IPv4 socket: %s\n"),
|
|
121 g_strerror (errno));
|
|
122 return (GFTP_ERETRYABLE);
|
|
123 }
|
|
124
|
|
125 memset (&remote_address, 0, sizeof (remote_address));
|
|
126 remote_address.sin_family = AF_INET;
|
|
127 remote_address.sin_port = htons (connect_port);
|
|
128
|
|
129 memcpy (&remote_address.sin_addr,
|
|
130 host.h_addr_list[curhost],
|
|
131 host.h_length);
|
|
132
|
|
133 request->logging_function (gftp_logging_misc, request,
|
|
134 _("Trying %s:%d\n"),
|
|
135 host.h_name, ntohs (remote_address.sin_port));
|
|
136
|
|
137 if (connect (sock, (struct sockaddr *) &remote_address,
|
|
138 sizeof (remote_address)) == -1)
|
|
139 {
|
|
140 request->logging_function (gftp_logging_error, request,
|
|
141 _("Cannot connect to %s: %s\n"),
|
|
142 host.h_name, g_strerror (errno));
|
|
143 close (sock);
|
|
144 continue;
|
|
145 }
|
|
146
|
|
147 break;
|
|
148 }
|
|
149
|
|
150 if (host.h_addr_list[curhost] == NULL)
|
|
151 return (GFTP_ERETRYABLE);
|
|
152
|
|
153 request->remote_addr_len = host.h_length;
|
|
154 request->remote_addr = g_malloc0 (request->remote_addr_len);
|
|
155 memcpy (request->remote_addr, &host.h_addr_list[curhost],
|
|
156 request->remote_addr_len);
|
|
157
|
|
158 request->logging_function (gftp_logging_misc, request,
|
|
159 _("Connected to %s:%d\n"), host.h_name,
|
|
160 ntohs (remote_address.sin_port));
|
|
161
|
|
162 return (sock);
|
|
163 }
|
|
164
|
|
165 #endif
|