1 /* Establishing and handling network connections.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
31 # include <sys/socket.h>
33 # include <netinet/in.h>
34 # include <arpa/inet.h>
42 #endif /* HAVE_STRING_H */
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
45 #endif /* HAVE_SYS_SELECT_H */
55 /* Variables shared by bindport and acceptport: */
56 static int msock = -1;
57 static struct sockaddr *addr;
60 /* Create an internet connection to HOSTNAME on PORT. The created
61 socket will be stored to *SOCK. */
63 make_connection (int *sock, char *hostname, unsigned short port)
65 struct sockaddr_in sock_name;
66 /* struct hostent *hptr; */
68 /* Get internet address of the host. We can do it either by calling
69 ngethostbyname, or by calling store_hostaddress, from host.c.
70 storehostaddress is better since it caches calls to
73 if (!store_hostaddress ((unsigned char *)&sock_name.sin_addr, hostname))
76 if (!(hptr = ngethostbyname (hostname)))
78 /* Copy the address of the host to socket description. */
79 memcpy (&sock_name.sin_addr, hptr->h_addr, hptr->h_length);
82 /* Set port and protocol */
83 sock_name.sin_family = AF_INET;
84 sock_name.sin_port = htons (port);
86 /* Make an internet socket, stream type. */
87 if ((*sock = socket (AF_INET, SOCK_STREAM, 0)) == -1)
90 if (opt.bind_address != NULL)
92 /* Bind the client side to the requested address. */
93 if (bind (*sock, (struct sockaddr *) opt.bind_address,
94 sizeof (*opt.bind_address)))
98 /* Connect the socket to the remote host. */
99 if (connect (*sock, (struct sockaddr *) &sock_name, sizeof (sock_name)))
101 if (errno == ECONNREFUSED)
106 DEBUGP (("Created fd %d.\n", *sock));
111 test_socket_open (int sock)
117 /* Check if we still have a valid (non-EOF) connection. From Andrew
118 * Maholski's code in the Unix Socket FAQ. */
120 FD_ZERO (&check_set);
121 FD_SET (sock, &check_set);
123 /* Wait one microsecond */
127 /* If we get a timeout, then that means still connected */
128 if (select (sock + 1, &check_set, NULL, NULL, &to) == 0)
130 /* Connection is valid (not EOF), so continue */
136 /* Without select, it's hard to know for sure. */
141 /* Bind the local port PORT. This does all the necessary work, which
142 is creating a socket, setting SO_REUSEADDR option on it, then
143 calling bind() and listen(). If *PORT is 0, a random port is
144 chosen by the system, and its value is stored to *PORT. The
145 internal variable MPORT is set to the value of the ensuing master
146 socket. Call acceptport() to block for and accept a connection. */
148 bindport (unsigned short *port)
151 static struct sockaddr_in srv;
154 addr = (struct sockaddr *) &srv;
155 if ((msock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
157 if (setsockopt (msock, SOL_SOCKET, SO_REUSEADDR,
158 (char *)&optval, sizeof (optval)) < 0)
161 if (opt.bind_address == NULL)
163 srv.sin_family = AF_INET;
164 srv.sin_addr.s_addr = htonl (INADDR_ANY);
167 srv = *opt.bind_address;
169 srv.sin_port = htons (*port);
170 if (bind (msock, addr, sizeof (struct sockaddr_in)) < 0)
176 DEBUGP (("Master socket fd %d bound.\n", msock));
179 size_t addrlen = sizeof (struct sockaddr_in);
180 if (getsockname (msock, addr, (int *)&addrlen) < 0)
186 *port = ntohs (srv.sin_port);
188 if (listen (msock, 1) < 0)
198 /* Wait for file descriptor FD to be readable, MAXTIME being the
199 timeout in seconds. If WRITEP is non-zero, checks for FD being
202 Returns 1 if FD is accessible, 0 for timeout and -1 for error in
205 select_fd (int fd, int maxtime, int writep)
207 fd_set fds, exceptfds;
208 struct timeval timeout;
212 FD_ZERO (&exceptfds);
213 FD_SET (fd, &exceptfds);
214 timeout.tv_sec = maxtime;
216 /* HPUX reportedly warns here. What is the correct incantation? */
217 return select (fd + 1, writep ? NULL : &fds, writep ? &fds : NULL,
218 &exceptfds, &timeout);
220 #endif /* HAVE_SELECT */
222 /* Call accept() on MSOCK and store the result to *SOCK. This assumes
223 that bindport() has been used to initialize MSOCK to a correct
224 value. It blocks the caller until a connection is established. If
225 no connection is established for OPT.TIMEOUT seconds, the function
226 exits with an error status. */
228 acceptport (int *sock)
230 int addrlen = sizeof (struct sockaddr_in);
233 if (select_fd (msock, opt.timeout, 0) <= 0)
236 if ((*sock = accept (msock, addr, &addrlen)) < 0)
238 DEBUGP (("Created socket fd %d.\n", *sock));
242 /* Close SOCK, as well as the most recently remembered MSOCK, created
243 via bindport(). If SOCK is -1, close MSOCK only. */
247 /*shutdown (sock, 2);*/
255 /* Return the local IP address associated with the connection on FD.
256 It is returned in a static buffer. */
260 static unsigned char res[4];
261 struct sockaddr_in mysrv;
262 struct sockaddr *myaddr;
263 size_t addrlen = sizeof (mysrv);
265 myaddr = (struct sockaddr *) (&mysrv);
266 if (getsockname (fd, myaddr, (int *)&addrlen) < 0)
268 memcpy (res, &mysrv.sin_addr, 4);
272 /* Read at most LEN bytes from FD, storing them to BUF. This is
273 virtually the same as read(), but takes care of EINTR braindamage
274 and uses select() to timeout the stale connections (a connection is
275 stale if more than OPT.TIMEOUT time is spent in select() or
278 iread (int fd, char *buf, int len)
289 res = select_fd (fd, opt.timeout, 0);
291 while (res == -1 && errno == EINTR);
294 /* Set errno to ETIMEDOUT on timeout. */
296 /* #### Potentially evil! */
302 res = READ (fd, buf, len);
304 while (res == -1 && errno == EINTR);
309 /* Write LEN bytes from BUF to FD. This is similar to iread(), but
310 doesn't bother with select(). Unlike iread(), it makes sure that
311 all of BUF is actually written to FD, so callers needn't bother
312 with checking that the return value equals to LEN. Instead, you
313 should simply check for -1. */
315 iwrite (int fd, char *buf, int len)
319 /* `write' may write less than LEN bytes, thus the outward loop
320 keeps trying it until all was written, or an error occurred. The
321 inner loop is reserved for the usual EINTR f*kage, and the
322 innermost loop deals with the same during select(). */
332 res = select_fd (fd, opt.timeout, 1);
334 while (res == -1 && errno == EINTR);
337 /* Set errno to ETIMEDOUT on timeout. */
339 /* #### Potentially evil! */
345 res = WRITE (fd, buf, len);
347 while (res == -1 && errno == EINTR);