]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/netopen.c
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / lib / sh / netopen.c
1 /*
2 * netopen.c -- functions to make tcp/udp connections
3 *
4 * Chet Ramey
5 * chet@ins.CWRU.Edu
6 */
7
8 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
9
10 This file is part of GNU Bash, the Bourne Again SHell.
11
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)
15 any later version.
16
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.
21
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. */
25
26 #include <config.h>
27
28 #if defined (HAVE_NETWORK)
29
30 #include <stdio.h>
31 #include <sys/types.h>
32
33 #if defined (HAVE_SYS_SOCKET_H)
34 # include <sys/socket.h>
35 #endif
36
37 #if defined (HAVE_NETINET_IN_H)
38 # include <netinet/in.h>
39 #endif
40
41 #if defined (HAVE_NETDB_H)
42 # include <netdb.h>
43 #endif
44
45 #if defined (HAVE_ARPA_INET_H)
46 # include <arpa/inet.h>
47 #endif
48
49 #include <bashansi.h>
50 #include <ctype.h>
51 #include <errno.h>
52
53 #ifndef errno
54 extern int errno;
55 #endif
56
57 #if !defined (HAVE_INET_ATON)
58 extern int inet_aton ();
59 #endif
60
61 extern char *xmalloc ();
62
63 /* Stuff the internet address corresponding to HOST into AP, in network
64 byte order. Return 1 on success, 0 on failure. */
65
66 static int
67 _getaddr (host, ap)
68 char *host;
69 struct in_addr *ap;
70 {
71 struct hostent *h;
72 int r;
73
74 r = 0;
75 if (isdigit (host[0]))
76 {
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);
80 if (r)
81 return r;
82 }
83 #if !defined (HAVE_GETHOSTBYNAME)
84 return 0;
85 #else
86 h = gethostbyname (host);
87 if (h && h->h_addr)
88 {
89 bcopy(h->h_addr, (char *)ap, h->h_length);
90 return 1;
91 }
92 #endif
93 return 0;
94
95 }
96
97 /* Return 1 if SERV is a valid port number and stuff the converted value into
98 PP in network byte order. */
99 static int
100 _getserv (serv, pp)
101 char *serv;
102 unsigned short *pp;
103 {
104 long l;
105 unsigned short s;
106
107 if (legal_number (serv, &l))
108 {
109 if (l > 65535)
110 return 0;
111 s = (unsigned short)(l & 0xFFFF);
112 s = htons (s);
113 if (pp)
114 *pp = s;
115 return 1;
116 }
117 else
118 return 0;
119 }
120
121 static int
122 _netopen(host, serv, typ)
123 char *host, *serv;
124 int typ;
125 {
126 struct in_addr ina;
127 struct sockaddr_in sin;
128 unsigned short p;
129 int s;
130 char **cp;
131
132 if (_getaddr(host, &ina) == 0)
133 {
134 internal_error ("%s: host unknown", host);
135 return -1;
136 }
137
138 if (_getserv(serv, &p) == 0)
139 {
140 internal_error("%s: invalid service", serv);
141 return -1;
142 }
143
144 bzero ((char *)&sin, sizeof(sin));
145 sin.sin_family = AF_INET;
146 sin.sin_port = p;
147 sin.sin_addr = ina;
148
149 s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
150 if (s < 0)
151 {
152 sys_error ("socket");
153 return (-1);
154 }
155
156 if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
157 {
158 sys_error("connect");
159 close(s);
160 return (-1);
161 }
162
163 return(s);
164 }
165
166 /*
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.
169 */
170 int
171 netopen (path)
172 char *path;
173 {
174 char *np, *s, *t;
175 int fd;
176
177 np = xmalloc (strlen (path) + 1);
178 strcpy (np, path);
179
180 s = np + 9;
181 t = strchr (s, '/');
182 if (t == 0)
183 {
184 internal_error ("%s: bad network path specification", path);
185 return -1;
186 }
187 *t++ = '\0';
188 fd = _netopen (s, t, path[5]);
189 free (np);
190
191 return fd;
192 }
193
194 #if 0
195 /*
196 * Open a TCP connection to host `host' on the port defined for service
197 * `serv' and return the connected socket.
198 */
199 int
200 tcpopen (host, serv)
201 char *host, *serv;
202 {
203 return (_netopen (host, serv, 't'));
204 }
205
206 /*
207 * Open a UDP connection to host `host' on the port defined for service
208 * `serv' and return the connected socket.
209 */
210 int
211 udpopen (host, serv)
212 char *host, *serv;
213 {
214 return _netopen (host, serv, 'u');
215 }
216 #endif
217
218 #else /* !HAVE_NETWORK */
219
220 int
221 netopen (path)
222 char *path;
223 {
224 internal_error ("network operations not supported");
225 return -1;
226 }
227
228 #endif /* !HAVE_NETWORK */