2 * netopen.c -- functions to make tcp/udp connections
8 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
10 This file is part of GNU Bash, the Bourne Again SHell.
12 Bash is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 Bash is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Bash; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
28 #if defined (HAVE_NETWORK)
30 #if defined (HAVE_UNISTD_H)
35 #include <sys/types.h>
37 #if defined (HAVE_SYS_SOCKET_H)
38 # include <sys/socket.h>
41 #if defined (HAVE_NETINET_IN_H)
42 # include <netinet/in.h>
45 #if defined (HAVE_NETDB_H)
49 #if defined (HAVE_ARPA_INET_H)
50 # include <arpa/inet.h>
65 #if !defined (HAVE_INET_ATON)
66 extern int inet_aton __P((const char *, struct in_addr *));
69 #ifndef HAVE_GETADDRINFO
70 /* Stuff the internet address corresponding to HOST into AP, in network
71 byte order. Return 1 on success, 0 on failure. */
82 if (host[0] >= '0' && host[0] <= '9')
84 /* If the first character is a digit, guess that it's an
85 Internet address and return immediately if inet_aton succeeds. */
86 r = inet_aton (host, ap);
90 #if !defined (HAVE_GETHOSTBYNAME)
93 h = gethostbyname (host);
96 bcopy(h->h_addr, (char *)ap, h->h_length);
104 /* Return 1 if SERV is a valid port number and stuff the converted value into
105 PP in network byte order. */
107 _getserv (serv, proto, pp)
115 if (legal_number (serv, &l))
117 s = (unsigned short)(l & 0xFFFF);
126 #if defined (HAVE_GETSERVBYNAME)
130 se = getservbyname (serv, (proto == 't') ? "tcp" : "udp");
134 *pp = se->s_port; /* ports returned in network byte order */
137 #else /* !HAVE_GETSERVBYNAME */
139 #endif /* !HAVE_GETSERVBYNAME */
143 * Open a TCP or UDP connection to HOST on port SERV. Uses the
144 * traditional BSD mechanisms. Returns the connected socket or -1 on error.
147 _netopen4(host, serv, typ)
152 struct sockaddr_in sin;
156 if (_getaddr(host, &ina) == 0)
158 internal_error (_("%s: host unknown"), host);
163 if (_getserv(serv, typ, &p) == 0)
165 internal_error(_("%s: invalid service"), serv);
170 memset ((char *)&sin, 0, sizeof(sin));
171 sin.sin_family = AF_INET;
175 s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
178 sys_error ("socket");
182 if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
185 sys_error("connect");
193 #endif /* ! HAVE_GETADDRINFO */
195 #ifdef HAVE_GETADDRINFO
197 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
198 * which provides support for IPv6. Returns the connected socket or -1
202 _netopen6 (host, serv, typ)
207 struct addrinfo hints, *res, *res0;
210 memset ((char *)&hints, 0, sizeof (hints));
211 /* XXX -- if problems with IPv6, set to PF_INET for IPv4 only */
212 #ifdef DEBUG /* PF_INET is the one that works for me */
213 hints.ai_family = PF_INET;
215 hints.ai_family = PF_UNSPEC;
217 hints.ai_socktype = (typ == 't') ? SOCK_STREAM : SOCK_DGRAM;
219 gerr = getaddrinfo (host, serv, &hints, &res0);
222 if (gerr == EAI_SERVICE)
223 internal_error ("%s: %s", serv, gai_strerror (gerr));
225 internal_error ("%s: %s", host, gai_strerror (gerr));
230 for (res = res0; res; res = res->ai_next)
232 if ((s = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
236 sys_error ("socket");
240 if (connect (s, res->ai_addr, res->ai_addrlen) < 0)
248 sys_error ("connect");
259 #endif /* HAVE_GETADDRINFO */
262 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
263 * if available, falling back to the traditional BSD mechanisms otherwise.
264 * Returns the connected socket or -1 on error.
267 _netopen(host, serv, typ)
271 #ifdef HAVE_GETADDRINFO
272 return (_netopen6 (host, serv, typ));
274 return (_netopen4 (host, serv, typ));
279 * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
280 * host `host' on port `port' and return the connected socket.
289 np = (char *)xmalloc (strlen (path) + 1);
296 internal_error (_("%s: bad network path specification"), path);
300 fd = _netopen (s, t, path[5]);
308 * Open a TCP connection to host `host' on the port defined for service
309 * `serv' and return the connected socket.
315 return (_netopen (host, serv, 't'));
319 * Open a UDP connection to host `host' on the port defined for service
320 * `serv' and return the connected socket.
326 return _netopen (host, serv, 'u');
330 #else /* !HAVE_NETWORK */
336 internal_error (_("network operations not supported"));
340 #endif /* !HAVE_NETWORK */