(root)/
glibc-2.38/
sysdeps/
mach/
hurd/
connect.c
       1  /* Copyright (C) 1992-2023 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     <https://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 <sysdep-cancel.h>
      26  #include "hurd/hurdsocket.h"
      27  
      28  /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
      29     For connectionless socket types, just set the default address to send to
      30     and the only address from which to accept transmissions.
      31     Return 0 on success, -1 for errors.  */
      32  int
      33  __connect (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
      34  {
      35    error_t err;
      36    addr_port_t aport;
      37    const struct sockaddr_un *addr = addrarg.__sockaddr_un__;
      38    int cancel_oldtype;
      39  
      40    if (addr->sun_family == AF_LOCAL)
      41      {
      42        char *name = _hurd_sun_path_dupa (addr, len);
      43        /* For the local domain, we must look up the name as a file and talk
      44  	 to it with the ifsock protocol.  */
      45        file_t file;
      46        cancel_oldtype = LIBC_CANCEL_ASYNC();
      47        file = __file_name_lookup (name, 0, 0);
      48        LIBC_CANCEL_RESET (cancel_oldtype);
      49        if (file == MACH_PORT_NULL)
      50  	return -1;
      51        err = __ifsock_getsockaddr (file, &aport);
      52        __mach_port_deallocate (__mach_task_self (), file);
      53        if (err == MIG_BAD_ID || err == EOPNOTSUPP)
      54  	/* The file did not grok the ifsock protocol.  */
      55  	err = ENOTSOCK;
      56        if (err)
      57  	return __hurd_fail (err);
      58      }
      59    else
      60      err = EIEIO;
      61  
      62    err = HURD_DPORT_USE_CANCEL (fd,
      63  			({
      64  			  if (err)
      65  			    err = __socket_create_address (port,
      66  							   addr->sun_family,
      67  							   (char *) addr, len,
      68  							   &aport);
      69  			  if (! err)
      70  			    {
      71  			      cancel_oldtype = LIBC_CANCEL_ASYNC();
      72  			      err = __socket_connect (port, aport);
      73  			      LIBC_CANCEL_RESET (cancel_oldtype);
      74  			      __mach_port_deallocate (__mach_task_self (),
      75  						      aport);
      76  			    }
      77  			  err;
      78  			}));
      79  
      80    return err ? __hurd_dfail (fd, err) : 0;
      81  }
      82  
      83  libc_hidden_def (__connect)
      84  weak_alias (__connect, connect)