xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 1992-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <hurd.h> |
| 20 | #include <hurd/fd.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <hurd/socket.h> |
| 23 | #include <sys/un.h> |
| 24 | #include <hurd/ifsock.h> |
| 25 | #include "hurd/hurdsocket.h" |
| 26 | |
| 27 | /* Open a connection on socket FD to peer at ADDR (which LEN bytes long). |
| 28 | For connectionless socket types, just set the default address to send to |
| 29 | and the only address from which to accept transmissions. |
| 30 | Return 0 on success, -1 for errors. */ |
| 31 | int |
| 32 | __connect (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len) |
| 33 | { |
| 34 | error_t err; |
| 35 | addr_port_t aport; |
| 36 | const struct sockaddr_un *addr = addrarg.__sockaddr_un__; |
| 37 | |
| 38 | if (addr->sun_family == AF_LOCAL) |
| 39 | { |
| 40 | char *name = _hurd_sun_path_dupa (addr, len); |
| 41 | /* For the local domain, we must look up the name as a file and talk |
| 42 | to it with the ifsock protocol. */ |
| 43 | file_t file = __file_name_lookup (name, 0, 0); |
| 44 | if (file == MACH_PORT_NULL) |
| 45 | return -1; |
| 46 | err = __ifsock_getsockaddr (file, &aport); |
| 47 | __mach_port_deallocate (__mach_task_self (), file); |
| 48 | if (err == MIG_BAD_ID || err == EOPNOTSUPP) |
| 49 | /* The file did not grok the ifsock protocol. */ |
| 50 | err = ENOTSOCK; |
| 51 | if (err) |
| 52 | return __hurd_fail (err); |
| 53 | } |
| 54 | else |
| 55 | err = EIEIO; |
| 56 | |
| 57 | err = HURD_DPORT_USE (fd, |
| 58 | ({ |
| 59 | if (err) |
| 60 | err = __socket_create_address (port, |
| 61 | addr->sun_family, |
| 62 | (char *) addr, len, |
| 63 | &aport); |
| 64 | if (! err) |
| 65 | { |
| 66 | err = __socket_connect (port, aport); |
| 67 | __mach_port_deallocate (__mach_task_self (), |
| 68 | aport); |
| 69 | } |
| 70 | err; |
| 71 | })); |
| 72 | |
| 73 | return err ? __hurd_dfail (fd, err) : 0; |
| 74 | } |
| 75 | |
| 76 | libc_hidden_def (__connect) |
| 77 | weak_alias (__connect, connect) |