Imported from ../bash-3.0.tar.gz.
[platform/upstream/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-2002 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 #if defined (HAVE_UNISTD_H)
31 #  include <unistd.h>
32 #endif
33
34 #include <stdio.h> 
35 #include <sys/types.h>
36
37 #if defined (HAVE_SYS_SOCKET_H)
38 #  include <sys/socket.h>
39 #endif
40
41 #if defined (HAVE_NETINET_IN_H)
42 #  include <netinet/in.h>
43 #endif
44
45 #if defined (HAVE_NETDB_H)
46 #  include <netdb.h>
47 #endif
48
49 #if defined (HAVE_ARPA_INET_H)
50 #  include <arpa/inet.h>
51 #endif
52
53 #include <bashansi.h>
54 #include <bashintl.h>
55
56 #include <errno.h>
57
58 #include <shell.h>
59 #include <xmalloc.h>
60
61 #ifndef errno
62 extern int errno;
63 #endif
64
65 #if !defined (HAVE_INET_ATON)
66 extern int inet_aton __P((const char *, struct in_addr *));
67 #endif
68
69 #ifndef HAVE_GETADDRINFO
70 /* Stuff the internet address corresponding to HOST into AP, in network
71    byte order.  Return 1 on success, 0 on failure. */
72
73 static int
74 _getaddr (host, ap)
75      char *host;
76      struct in_addr *ap;
77 {
78   struct hostent *h;
79   int r;
80
81   r = 0;
82   if (host[0] >= '0' && host[0] <= '9')
83     {
84       /* If the first character is a digit, guess that it's an
85          Internet address and return immediately if inet_aton succeeds. */
86       r = inet_aton (host, ap);
87       if (r)
88         return r;
89     }
90 #if !defined (HAVE_GETHOSTBYNAME)
91   return 0;
92 #else
93   h = gethostbyname (host);
94   if (h && h->h_addr)
95     {
96       bcopy(h->h_addr, (char *)ap, h->h_length);
97       return 1;
98     }
99 #endif
100   return 0;
101   
102 }
103
104 /* Return 1 if SERV is a valid port number and stuff the converted value into
105    PP in network byte order. */   
106 static int
107 _getserv (serv, proto, pp)
108      char *serv;
109      int proto;
110      unsigned short *pp;
111 {
112   intmax_t l;
113   unsigned short s;
114
115   if (legal_number (serv, &l))
116     {
117       s = (unsigned short)(l & 0xFFFF);
118       if (s != l)
119         return (0);
120       s = htons (s);
121       if (pp)
122         *pp = s;
123       return 1;
124     }
125   else
126 #if defined (HAVE_GETSERVBYNAME)
127     {
128       struct servent *se;
129
130       se = getservbyname (serv, (proto == 't') ? "tcp" : "udp");
131       if (se == 0)
132         return 0;
133       if (pp)
134         *pp = se->s_port;       /* ports returned in network byte order */
135       return 1;
136     }
137 #else /* !HAVE_GETSERVBYNAME */
138     return 0;
139 #endif /* !HAVE_GETSERVBYNAME */
140 }
141
142 /*
143  * Open a TCP or UDP connection to HOST on port SERV.  Uses the
144  * traditional BSD mechanisms.  Returns the connected socket or -1 on error.
145  */
146 static int 
147 _netopen4(host, serv, typ)
148      char *host, *serv;
149      int typ;
150 {
151   struct in_addr ina;
152   struct sockaddr_in sin;
153   unsigned short p;
154   int s, e;
155
156   if (_getaddr(host, &ina) == 0)
157     {
158       internal_error (_("%s: host unknown"), host);
159       errno = EINVAL;
160       return -1;
161     }
162
163   if (_getserv(serv, typ, &p) == 0)
164     {
165       internal_error(_("%s: invalid service"), serv);
166       errno = EINVAL;
167       return -1;
168     }
169         
170   memset ((char *)&sin, 0, sizeof(sin));
171   sin.sin_family = AF_INET;
172   sin.sin_port = p;
173   sin.sin_addr = ina;
174
175   s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
176   if (s < 0)
177     {
178       sys_error ("socket");
179       return (-1);
180     }
181
182   if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
183     {
184       e = errno;
185       sys_error("connect");
186       close(s);
187       errno = e;
188       return (-1);
189     }
190
191   return(s);
192 }
193 #endif /* ! HAVE_GETADDRINFO */
194
195 #ifdef HAVE_GETADDRINFO
196 /*
197  * Open a TCP or UDP connection to HOST on port SERV.  Uses getaddrinfo(3)
198  * which provides support for IPv6.  Returns the connected socket or -1
199  * on error.
200  */
201 static int
202 _netopen6 (host, serv, typ)
203      char *host, *serv;
204      int typ;
205 {
206   int s, e;
207   struct addrinfo hints, *res, *res0;
208   int gerr;
209
210   memset ((char *)&hints, 0, sizeof (hints));
211   /* XXX -- if problems with IPv6, set to PF_INET for IPv4 only */
212 #ifdef DEBUG    /* PF_INET is the one that works for me */
213   hints.ai_family = PF_INET;
214 #else
215   hints.ai_family = PF_UNSPEC;
216 #endif
217   hints.ai_socktype = (typ == 't') ? SOCK_STREAM : SOCK_DGRAM;
218
219   gerr = getaddrinfo (host, serv, &hints, &res0);
220   if (gerr)
221     {
222       if (gerr == EAI_SERVICE)
223         internal_error ("%s: %s", serv, gai_strerror (gerr));
224       else
225         internal_error ("%s: %s", host, gai_strerror (gerr));
226       errno = EINVAL;
227       return -1;
228     }
229
230   for (res = res0; res; res = res->ai_next)
231     {
232       if ((s = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
233         {
234           if (res->ai_next)
235             continue;
236           sys_error ("socket");
237           freeaddrinfo (res0);
238           return -1;
239         }
240       if (connect (s, res->ai_addr, res->ai_addrlen) < 0)
241         {
242           if (res->ai_next)
243             {
244               close (s);
245               continue;
246             }
247           e = errno;
248           sys_error ("connect");
249           close (s);
250           freeaddrinfo (res0);
251           errno = e;
252           return -1;
253         }
254       freeaddrinfo (res0);
255       break;
256     }
257   return s;
258 }
259 #endif /* HAVE_GETADDRINFO */
260
261 /*
262  * Open a TCP or UDP connection to HOST on port SERV.  Uses getaddrinfo(3)
263  * if available, falling back to the traditional BSD mechanisms otherwise.
264  * Returns the connected socket or -1 on error.
265  */
266 static int 
267 _netopen(host, serv, typ)
268      char *host, *serv;
269      int typ;
270 {
271 #ifdef HAVE_GETADDRINFO
272   return (_netopen6 (host, serv, typ));
273 #else
274   return (_netopen4 (host, serv, typ));
275 #endif
276 }
277
278 /*
279  * Open a TCP or UDP connection given a path like `/dev/tcp/host/port' to
280  * host `host' on port `port' and return the connected socket.
281  */
282 int
283 netopen (path)
284      char *path;
285 {
286   char *np, *s, *t;
287   int fd;
288
289   np = (char *)xmalloc (strlen (path) + 1);
290   strcpy (np, path);
291
292   s = np + 9;
293   t = strchr (s, '/');
294   if (t == 0)
295     {
296       internal_error (_("%s: bad network path specification"), path);
297       return -1;
298     }
299   *t++ = '\0';
300   fd = _netopen (s, t, path[5]);
301   free (np);
302
303   return fd;
304 }
305
306 #if 0
307 /*
308  * Open a TCP connection to host `host' on the port defined for service
309  * `serv' and return the connected socket.
310  */
311 int
312 tcpopen (host, serv)
313      char *host, *serv;
314 {
315   return (_netopen (host, serv, 't'));
316 }
317
318 /*
319  * Open a UDP connection to host `host' on the port defined for service
320  * `serv' and return the connected socket.
321  */
322 int
323 udpopen (host, serv)
324      char *host, *serv;
325 {
326   return _netopen (host, serv, 'u');
327 }
328 #endif
329
330 #else /* !HAVE_NETWORK */
331
332 int
333 netopen (path)
334      char *path;
335 {
336   internal_error (_("network operations not supported"));
337   return -1;
338 }
339
340 #endif /* !HAVE_NETWORK */