Blame sysdeps/mach/hurd/sendto.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/un.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/ifsock.h>
Packit 6c4009
#include <hurd/socket.h>
Packit 6c4009
#include "hurd/hurdsocket.h"
Packit 6c4009
Packit 6c4009
/* Send N bytes of BUF on socket FD to peer at address ADDR (which is
Packit 6c4009
   ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.  */
Packit 6c4009
ssize_t
Packit 6c4009
__sendto (int fd,
Packit 6c4009
	  const void *buf,
Packit 6c4009
	  size_t n,
Packit 6c4009
	  int flags,
Packit 6c4009
	  const struct sockaddr_un *addr,
Packit 6c4009
	  socklen_t addr_len)
Packit 6c4009
{
Packit 6c4009
  addr_port_t aport = MACH_PORT_NULL;
Packit 6c4009
  error_t err;
Packit 6c4009
  size_t wrote;
Packit 6c4009
Packit 6c4009
  /* Get an address port for the desired destination address.  */
Packit 6c4009
  error_t create_address_port (io_t port,
Packit 6c4009
			       const struct sockaddr_un *addr,
Packit 6c4009
			       socklen_t addr_len,
Packit 6c4009
			       addr_port_t *aport)
Packit 6c4009
    {
Packit 6c4009
      error_t err_port;
Packit 6c4009
Packit 6c4009
      if (addr->sun_family == AF_LOCAL)
Packit 6c4009
	{
Packit 6c4009
	  char *name = _hurd_sun_path_dupa (addr, addr_len);
Packit 6c4009
	  /* For the local domain, we must look up the name as a file and talk
Packit 6c4009
	     to it with the ifsock protocol.  */
Packit 6c4009
	  file_t file = __file_name_lookup (name, 0, 0);
Packit 6c4009
	  if (file == MACH_PORT_NULL)
Packit 6c4009
	    return errno;
Packit 6c4009
	  err_port = __ifsock_getsockaddr (file, aport);
Packit 6c4009
	  __mach_port_deallocate (__mach_task_self (), file);
Packit 6c4009
	  if (err_port == MIG_BAD_ID || err_port == EOPNOTSUPP)
Packit 6c4009
	    /* The file did not grok the ifsock protocol.  */
Packit 6c4009
	    err_port = ENOTSOCK;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  err_port = __socket_create_address (port,
Packit 6c4009
					      addr->sun_family,
Packit 6c4009
					      (char *) addr,
Packit 6c4009
					      addr_len,
Packit 6c4009
					      aport);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      return err_port;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = HURD_DPORT_USE (fd,
Packit 6c4009
			({
Packit 6c4009
			  if (addr != NULL)
Packit 6c4009
			    err = create_address_port (port, addr, addr_len,
Packit 6c4009
						       &aport);
Packit 6c4009
			  else
Packit 6c4009
			    err = 0;
Packit 6c4009
			  if (! err)
Packit 6c4009
			    {
Packit 6c4009
			      /* Send the data.  */
Packit 6c4009
			      err = __socket_send (port, aport,
Packit 6c4009
						   flags, buf, n,
Packit 6c4009
						   NULL,
Packit 6c4009
						   MACH_MSG_TYPE_COPY_SEND, 0,
Packit 6c4009
						   NULL, 0, &wrote);
Packit 6c4009
			    }
Packit 6c4009
			  err;
Packit 6c4009
			}));
Packit 6c4009
Packit 6c4009
  if (aport != MACH_PORT_NULL)
Packit 6c4009
    __mach_port_deallocate (__mach_task_self (), aport);
Packit 6c4009
Packit 6c4009
  return err ? __hurd_sockfail (fd, flags, err) : wrote;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__sendto, sendto)