2 * netopen.c -- functions to make tcp/udp connections
8 /* Copyright (C) 1987,1991 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)
31 #include <sys/types.h>
33 #if defined (HAVE_SYS_SOCKET_H)
34 # include <sys/socket.h>
37 #if defined (HAVE_NETINET_IN_H)
38 # include <netinet/in.h>
41 #if defined (HAVE_NETDB_H)
45 #if defined (HAVE_ARPA_INET_H)
46 # include <arpa/inet.h>
57 #if !defined (HAVE_INET_ATON)
58 extern int inet_aton ();
61 extern char *xmalloc ();
63 /* Stuff the internet address corresponding to HOST into AP, in network
64 byte order. Return 1 on success, 0 on failure. */
75 if (isdigit (host[0]))
77 /* If the first character is a digit, guess that it's an
78 Internet address and return immediately if inet_aton succeeds. */
79 r = inet_aton (host, ap);
83 #if !defined (HAVE_GETHOSTBYNAME)
86 h = gethostbyname (host);
89 bcopy(h->h_addr, (char *)ap, h->h_length);
97 /* Return 1 if SERV is a valid port number and stuff the converted value into
98 PP in network byte order. */
100 _getserv (serv, proto, pp)
108 if (legal_number (serv, &l))
112 s = (unsigned short)(l & 0xFFFF);
119 #if defined (HAVE_GETSERVBYNAME)
123 se = getservbyname (serv, (proto == 't') ? "tcp" : "udp");
127 *pp = se->s_port; /* ports returned in network byte order */
130 #else /* !HAVE_GETSERVBYNAME */
132 #endif /* !HAVE_GETSERVBYNAME */
136 _netopen(host, serv, typ)
141 struct sockaddr_in sin;
146 if (_getaddr(host, &ina) == 0)
148 internal_error ("%s: host unknown", host);
153 if (_getserv(serv, typ, &p) == 0)
155 internal_error("%s: invalid service", serv);
160 bzero ((char *)&sin, sizeof(sin));
161 sin.sin_family = AF_INET;
165 s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
168 sys_error ("socket");
172 if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
175 sys_error("connect");
185 * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
186 * host `host' on port `port' and return the connected socket.
195 np = xmalloc (strlen (path) + 1);
202 internal_error ("%s: bad network path specification", path);
206 fd = _netopen (s, t, path[5]);
214 * Open a TCP connection to host `host' on the port defined for service
215 * `serv' and return the connected socket.
221 return (_netopen (host, serv, 't'));
225 * Open a UDP connection to host `host' on the port defined for service
226 * `serv' and return the connected socket.
232 return _netopen (host, serv, 'u');
236 #else /* !HAVE_NETWORK */
242 internal_error ("network operations not supported");
246 #endif /* !HAVE_NETWORK */