Imported from ../bash-2.05b.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 <errno.h>
55
56 #include <shell.h>
57 #include <xmalloc.h>
58
59 #ifndef errno
60 extern int errno;
61 #endif
62
63 #if !defined (HAVE_INET_ATON)
64 extern int inet_aton __P((const char *, struct in_addr *));
65 #endif
66
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. */
70
71 static int
72 _getaddr (host, ap)
73      char *host;
74      struct in_addr *ap;
75 {
76   struct hostent *h;
77   int r;
78
79   r = 0;
80   if (host[0] >= '0' && host[0] <= '9')
81     {
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);
85       if (r)
86         return r;
87     }
88 #if !defined (HAVE_GETHOSTBYNAME)
89   return 0;
90 #else
91   h = gethostbyname (host);
92   if (h && h->h_addr)
93     {
94       bcopy(h->h_addr, (char *)ap, h->h_length);
95       return 1;
96     }
97 #endif
98   return 0;
99   
100 }
101
102 /* Return 1 if SERV is a valid port number and stuff the converted value into
103    PP in network byte order. */   
104 static int
105 _getserv (serv, proto, pp)
106      char *serv;
107      int proto;
108      unsigned short *pp;
109 {
110   intmax_t l;
111   unsigned short s;
112
113   if (legal_number (serv, &l))
114     {
115       s = (unsigned short)(l & 0xFFFF);
116       if (s != l)
117         return (0);
118       s = htons (s);
119       if (pp)
120         *pp = s;
121       return 1;
122     }
123   else
124 #if defined (HAVE_GETSERVBYNAME)
125     {
126       struct servent *se;
127
128       se = getservbyname (serv, (proto == 't') ? "tcp" : "udp");
129       if (se == 0)
130         return 0;
131       if (pp)
132         *pp = se->s_port;       /* ports returned in network byte order */
133       return 1;
134     }
135 #else /* !HAVE_GETSERVBYNAME */
136     return 0;
137 #endif /* !HAVE_GETSERVBYNAME */
138 }
139
140 /*
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.
143  */
144 static int 
145 _netopen4(host, serv, typ)
146      char *host, *serv;
147      int typ;
148 {
149   struct in_addr ina;
150   struct sockaddr_in sin;
151   unsigned short p;
152   int s, e;
153
154   if (_getaddr(host, &ina) == 0)
155     {
156       internal_error ("%s: host unknown", host);
157       errno = EINVAL;
158       return -1;
159     }
160
161   if (_getserv(serv, typ, &p) == 0)
162     {
163       internal_error("%s: invalid service", serv);
164       errno = EINVAL;
165       return -1;
166     }
167         
168   memset ((char *)&sin, 0, sizeof(sin));
169   sin.sin_family = AF_INET;
170   sin.sin_port = p;
171   sin.sin_addr = ina;
172
173   s = socket(AF_INET, (typ == 't') ? SOCK_STREAM : SOCK_DGRAM, 0);
174   if (s < 0)
175     {
176       sys_error ("socket");
177       return (-1);
178     }
179
180   if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0)
181     {
182       e = errno;
183       sys_error("connect");
184       close(s);
185       errno = e;
186       return (-1);
187     }
188
189   return(s);
190 }
191 #endif /* ! HAVE_GETADDRINFO */
192
193 #ifdef HAVE_GETADDRINFO
194 /*
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
197  * on error.
198  */
199 static int
200 _netopen6 (host, serv, typ)
201      char *host, *serv;
202      int typ;
203 {
204   int s, e;
205   struct addrinfo hints, *res, *res0;
206   int gerr;
207
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;
212 #else
213   hints.ai_family = PF_UNSPEC;
214 #endif
215   hints.ai_socktype = (typ == 't') ? SOCK_STREAM : SOCK_DGRAM;
216
217   gerr = getaddrinfo (host, serv, &hints, &res0);
218   if (gerr)
219     {
220       if (gerr == EAI_SERVICE)
221         internal_error ("%s: %s", serv, gai_strerror (gerr));
222       else
223         internal_error ("%s: %s", host, gai_strerror (gerr));
224       errno = EINVAL;
225       return -1;
226     }
227
228   for (res = res0; res; res = res->ai_next)
229     {
230       if ((s = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
231         {
232           if (res->ai_next)
233             continue;
234           sys_error ("socket");
235           freeaddrinfo (res0);
236           return -1;
237         }
238       if (connect (s, res->ai_addr, res->ai_addrlen) < 0)
239         {
240           if (res->ai_next)
241             {
242               close (s);
243               continue;
244             }
245           e = errno;
246           sys_error ("connect");
247           close (s);
248           freeaddrinfo (res0);
249           errno = e;
250           return -1;
251         }
252       freeaddrinfo (res0);
253       break;
254     }
255   return s;
256 }
257 #endif /* HAVE_GETADDRINFO */
258
259 /*
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.
263  */
264 static int 
265 _netopen(host, serv, typ)
266      char *host, *serv;
267      int typ;
268 {
269 #ifdef HAVE_GETADDRINFO
270   return (_netopen6 (host, serv, typ));
271 #else
272   return (_netopen4 (host, serv, typ));
273 #endif
274 }
275
276 /*
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.
279  */
280 int
281 netopen (path)
282      char *path;
283 {
284   char *np, *s, *t;
285   int fd;
286
287   np = (char *)xmalloc (strlen (path) + 1);
288   strcpy (np, path);
289
290   s = np + 9;
291   t = strchr (s, '/');
292   if (t == 0)
293     {
294       internal_error ("%s: bad network path specification", path);
295       return -1;
296     }
297   *t++ = '\0';
298   fd = _netopen (s, t, path[5]);
299   free (np);
300
301   return fd;
302 }
303
304 #if 0
305 /*
306  * Open a TCP connection to host `host' on the port defined for service
307  * `serv' and return the connected socket.
308  */
309 int
310 tcpopen (host, serv)
311      char *host, *serv;
312 {
313   return (_netopen (host, serv, 't'));
314 }
315
316 /*
317  * Open a UDP connection to host `host' on the port defined for service
318  * `serv' and return the connected socket.
319  */
320 int
321 udpopen (host, serv)
322      char *host, *serv;
323 {
324   return _netopen (host, serv, 'u');
325 }
326 #endif
327
328 #else /* !HAVE_NETWORK */
329
330 int
331 netopen (path)
332      char *path;
333 {
334   internal_error ("network operations not supported");
335   return -1;
336 }
337
338 #endif /* !HAVE_NETWORK */