]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/netopen.c
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. */
107 if (legal_number (serv
, &l
))
111 s
= (unsigned short)(l
& 0xFFFF);
122 _netopen(host
, serv
, typ
)
127 struct sockaddr_in sin
;
132 if (_getaddr(host
, &ina
) == 0)
134 internal_error ("%s: host unknown", host
);
138 if (_getserv(serv
, &p
) == 0)
140 internal_error("%s: invalid service", serv
);
144 bzero ((char *)&sin
, sizeof(sin
));
145 sin
.sin_family
= AF_INET
;
149 s
= socket(AF_INET
, (typ
== 't') ? SOCK_STREAM
: SOCK_DGRAM
, 0);
152 sys_error ("socket");
156 if (connect (s
, (struct sockaddr
*)&sin
, sizeof (sin
)) < 0)
158 sys_error("connect");
167 * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
168 * host `host' on port `port' and return the connected socket.
177 np
= xmalloc (strlen (path
) + 1);
184 internal_error ("%s: bad network path specification", path
);
188 fd
= _netopen (s
, t
, path
[5]);
196 * Open a TCP connection to host `host' on the port defined for service
197 * `serv' and return the connected socket.
203 return (_netopen (host
, serv
, 't'));
207 * Open a UDP connection to host `host' on the port defined for service
208 * `serv' and return the connected socket.
214 return _netopen (host
, serv
, 'u');
218 #else /* !HAVE_NETWORK */
224 internal_error ("network operations not supported");
228 #endif /* !HAVE_NETWORK */