]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/netopen.c
4197db1b9e842957a53ffa78cb207ba4de18c965
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>
63 #if !defined (HAVE_INET_ATON)
64 extern int inet_aton
__P((const char *, struct in_addr
*));
67 #ifndef HAVE_GETADDRINFO
68 /* Stuff the internet address corresponding to HOST into AP, in network
69 byte order. Return 1 on success, 0 on failure. */
80 if (host
[0] >= '0' && host
[0] <= '9')
82 /* If the first character is a digit, guess that it's an
83 Internet address and return immediately if inet_aton succeeds. */
84 r
= inet_aton (host
, ap
);
88 #if !defined (HAVE_GETHOSTBYNAME)
91 h
= gethostbyname (host
);
94 bcopy(h
->h_addr
, (char *)ap
, h
->h_length
);
102 /* Return 1 if SERV is a valid port number and stuff the converted value into
103 PP in network byte order. */
105 _getserv (serv
, proto
, pp
)
113 if (legal_number (serv
, &l
))
115 s
= (unsigned short)(l
& 0xFFFF);
124 #if defined (HAVE_GETSERVBYNAME)
128 se
= getservbyname (serv
, (proto
== 't') ? "tcp" : "udp");
132 *pp
= se
->s_port
; /* ports returned in network byte order */
135 #else /* !HAVE_GETSERVBYNAME */
137 #endif /* !HAVE_GETSERVBYNAME */
141 * Open a TCP or UDP connection to HOST on port SERV. Uses the
142 * traditional BSD mechanisms. Returns the connected socket or -1 on error.
145 _netopen4(host
, serv
, typ
)
150 struct sockaddr_in sin
;
154 if (_getaddr(host
, &ina
) == 0)
156 internal_error ("%s: host unknown", host
);
161 if (_getserv(serv
, typ
, &p
) == 0)
163 internal_error("%s: invalid service", serv
);
168 memset ((char *)&sin
, 0, sizeof(sin
));
169 sin
.sin_family
= AF_INET
;
173 s
= socket(AF_INET
, (typ
== 't') ? SOCK_STREAM
: SOCK_DGRAM
, 0);
176 sys_error ("socket");
180 if (connect (s
, (struct sockaddr
*)&sin
, sizeof (sin
)) < 0)
183 sys_error("connect");
191 #endif /* ! HAVE_GETADDRINFO */
193 #ifdef HAVE_GETADDRINFO
195 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
196 * which provides support for IPv6. Returns the connected socket or -1
200 _netopen6 (host
, serv
, typ
)
205 struct addrinfo hints
, *res
, *res0
;
208 memset ((char *)&hints
, 0, sizeof (hints
));
209 /* XXX -- if problems with IPv6, set to PF_INET for IPv4 only */
210 #ifdef DEBUG /* PF_INET is the one that works for me */
211 hints
.ai_family
= PF_INET
;
213 hints
.ai_family
= PF_UNSPEC
;
215 hints
.ai_socktype
= (typ
== 't') ? SOCK_STREAM
: SOCK_DGRAM
;
217 gerr
= getaddrinfo (host
, serv
, &hints
, &res0
);
220 if (gerr
== EAI_SERVICE
)
221 internal_error ("%s: %s", serv
, gai_strerror (gerr
));
223 internal_error ("%s: %s", host
, gai_strerror (gerr
));
228 for (res
= res0
; res
; res
= res
->ai_next
)
230 if ((s
= socket (res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
)) < 0)
234 sys_error ("socket");
238 if (connect (s
, res
->ai_addr
, res
->ai_addrlen
) < 0)
246 sys_error ("connect");
257 #endif /* HAVE_GETADDRINFO */
260 * Open a TCP or UDP connection to HOST on port SERV. Uses getaddrinfo(3)
261 * if available, falling back to the traditional BSD mechanisms otherwise.
262 * Returns the connected socket or -1 on error.
265 _netopen(host
, serv
, typ
)
269 #ifdef HAVE_GETADDRINFO
270 return (_netopen6 (host
, serv
, typ
));
272 return (_netopen4 (host
, serv
, typ
));
277 * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
278 * host `host' on port `port' and return the connected socket.
287 np
= (char *)xmalloc (strlen (path
) + 1);
294 internal_error ("%s: bad network path specification", path
);
298 fd
= _netopen (s
, t
, path
[5]);
306 * Open a TCP connection to host `host' on the port defined for service
307 * `serv' and return the connected socket.
313 return (_netopen (host
, serv
, 't'));
317 * Open a UDP connection to host `host' on the port defined for service
318 * `serv' and return the connected socket.
324 return _netopen (host
, serv
, 'u');
328 #else /* !HAVE_NETWORK */
334 internal_error ("network operations not supported");
338 #endif /* !HAVE_NETWORK */