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
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Bash is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Bash. If not, see <http://www.gnu.org/licenses/>.
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 static int _getaddr __P((char *, struct in_addr *));
71 static int _getserv __P((char *, int, unsigned short *));
72 static int _netopen4 __P((char *, char *, int));
73 #else /* HAVE_GETADDRINFO */
74 static int _netopen6 __P((char *, char *, int));
77 static int _netopen __P((char *, char *, int));
79 #ifndef HAVE_GETADDRINFO
80 /* Stuff the internet address corresponding to HOST into AP, in network
81 byte order. Return 1 on success, 0 on failure. */
92 if (host[0] >= '0' && host[0] <= '9')
94 /* If the first character is a digit, guess that it's an
95 Internet address and return immediately if inet_aton succeeds. */
96 r = inet_aton (host, ap);
100 #if !defined (HAVE_GETHOSTBYNAME)
103 h = gethostbyname (host);
106 bcopy(h->h_addr, (char *)ap, h->h_length);
114 /* Return 1 if SERV is a valid port number and stuff the converted value into
115 PP in network byte order. */
117 _getserv (serv, proto, pp)
125 if (legal_number (serv, &l))
127 s = (unsigned short)(l & 0xFFFF);
136 #if defined (HAVE_GETSERVBYNAME)
140 se = getservbyname (serv, (proto == 't') ? "tcp" : "udp");
144 *pp = se->s_port; /* ports returned in network byte order */
147 #else /* !HAVE_GETSERVBYNAME */
149 #endif /* !HAVE_GETSERVBYNAME */
153 * Open a TCP or UDP connection to HOST on port SERV. Uses the
154 * traditional BSD mechanisms. Returns the connected socket or -1 on error.
157 _netopen4(host, serv, typ)
162 struct sockaddr_in sin;
166 if (_getaddr(host, &ina) == 0)
168 internal_error (_("%s: host unknown"), host);
173 if (_getserv(serv, typ, &p) == 0)
175 internal_error(_("%s: invalid service"), serv);
180 memset ((char *)&sin, 0, sizeof(sin));
181 sin.sin_family = AF_INET;
185 s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
188 sys_error ("socket");
192 if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
195 sys_error("connect");
203 #endif /* ! HAVE_GETADDRINFO */
205 #ifdef HAVE_GETADDRINFO
207 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
208 * which provides support for IPv6. Returns the connected socket or -1
212 _netopen6 (host, serv, typ)
217 struct addrinfo hints, *res, *res0;
220 memset ((char *)&hints, 0, sizeof (hints));
221 /* XXX -- if problems with IPv6, set to PF_INET for IPv4 only */
222 #ifdef DEBUG /* PF_INET is the one that works for me */
223 hints.ai_family = PF_INET;
225 hints.ai_family = PF_UNSPEC;
227 hints.ai_socktype = (typ == 't') ? SOCK_STREAM : SOCK_DGRAM;
229 gerr = getaddrinfo (host, serv, &hints, &res0);
232 if (gerr == EAI_SERVICE)
233 internal_error ("%s: %s", serv, gai_strerror (gerr));
235 internal_error ("%s: %s", host, gai_strerror (gerr));
240 for (res = res0; res; res = res->ai_next)
242 if ((s = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
246 sys_error ("socket");
250 if (connect (s, res->ai_addr, res->ai_addrlen) < 0)
258 sys_error ("connect");
269 #endif /* HAVE_GETADDRINFO */
272 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
273 * if available, falling back to the traditional BSD mechanisms otherwise.
274 * Returns the connected socket or -1 on error.
277 _netopen(host, serv, typ)
281 #ifdef HAVE_GETADDRINFO
282 return (_netopen6 (host, serv, typ));
284 return (_netopen4 (host, serv, typ));
289 * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
290 * host `host' on port `port' and return the connected socket.
299 np = (char *)xmalloc (strlen (path) + 1);
306 internal_error (_("%s: bad network path specification"), path);
310 fd = _netopen (s, t, path[5]);
318 * Open a TCP connection to host `host' on the port defined for service
319 * `serv' and return the connected socket.
325 return (_netopen (host, serv, 't'));
329 * Open a UDP connection to host `host' on the port defined for service
330 * `serv' and return the connected socket.
336 return _netopen (host, serv, 'u');
340 #else /* !HAVE_NETWORK */
346 internal_error (_("network operations not supported"));
350 #endif /* !HAVE_NETWORK */