Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1 | /* netconn.c -- is a particular file descriptor a network connection?. */ |
| 2 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 3 | /* Copyright (C) 2002-2005 Free Software Foundation, Inc. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | |
| 23 | #include <bashtypes.h> |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 24 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 25 | # include <sys/file.h> |
| 26 | #endif |
| 27 | #include <posixstat.h> |
| 28 | #include <filecntl.h> |
| 29 | |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #if defined (HAVE_UNISTD_H) |
| 33 | # include <unistd.h> |
| 34 | #endif |
| 35 | |
| 36 | /* The second and subsequent conditions must match those used to decide |
| 37 | whether or not to call getpeername() in isnetconn(). */ |
| 38 | #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2) |
| 39 | # include <sys/socket.h> |
| 40 | #endif |
| 41 | |
| 42 | /* Is FD a socket or network connection? */ |
| 43 | int |
| 44 | isnetconn (fd) |
| 45 | int fd; |
| 46 | { |
| 47 | #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__) |
| 48 | int rv; |
| 49 | socklen_t l; |
| 50 | struct sockaddr sa; |
| 51 | |
| 52 | l = sizeof(sa); |
| 53 | rv = getpeername(fd, &sa, &l); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 54 | /* Posix.2 says getpeername can return these errors. */ |
| 55 | return ((rv < 0 && (errno == ENOTSOCK || errno == ENOTCONN || errno == EINVAL)) ? 0 : 1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 56 | #else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */ |
| 57 | # if defined (SVR4) || defined (SVR4_2) |
| 58 | /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */ |
| 59 | struct stat sb; |
| 60 | |
| 61 | if (isatty (fd)) |
| 62 | return (0); |
| 63 | if (fstat (fd, &sb) < 0) |
| 64 | return (0); |
| 65 | # if defined (S_ISFIFO) |
| 66 | if (S_ISFIFO (sb.st_mode)) |
| 67 | return (0); |
| 68 | # endif /* S_ISFIFO */ |
| 69 | return (S_ISCHR (sb.st_mode)); |
| 70 | # else /* !SVR4 && !SVR4_2 */ |
| 71 | # if defined (S_ISSOCK) && !defined (__BEOS__) |
| 72 | struct stat sb; |
| 73 | |
| 74 | if (fstat (fd, &sb) < 0) |
| 75 | return (0); |
| 76 | return (S_ISSOCK (sb.st_mode)); |
| 77 | # else /* !S_ISSOCK || __BEOS__ */ |
| 78 | return (0); |
| 79 | # endif /* !S_ISSOCK || __BEOS__ */ |
| 80 | # endif /* !SVR4 && !SVR4_2 */ |
| 81 | #endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */ |
| 82 | } |