Blame sysdeps/mach/hurd/recvmsg.c

Packit 6c4009
/* Copyright (C) 2001-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 License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   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; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <hurd/socket.h>
Packit 6c4009
Packit 6c4009
/* Receive a message as described by MESSAGE from socket FD.
Packit 6c4009
   Returns the number of bytes read or -1 for errors.  */
Packit 6c4009
ssize_t
Packit 6c4009
__libc_recvmsg (int fd, struct msghdr *message, int flags)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  addr_port_t aport;
Packit 6c4009
  char *data = NULL;
Packit 6c4009
  mach_msg_type_number_t len = 0;
Packit 6c4009
  mach_port_t *ports;
Packit 6c4009
  mach_msg_type_number_t nports = 0;
Packit 6c4009
  char *cdata = NULL;
Packit 6c4009
  mach_msg_type_number_t clen = 0;
Packit 6c4009
  size_t amount;
Packit 6c4009
  char *buf;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  /* Find the total number of bytes to be read.  */
Packit 6c4009
  amount = 0;
Packit 6c4009
  for (i = 0; i < message->msg_iovlen; i++)
Packit 6c4009
    {
Packit 6c4009
      amount += message->msg_iov[i].iov_len;
Packit 6c4009
Packit 6c4009
      /* As an optimization, we set the initial values of DATA and LEN
Packit 6c4009
         from the first non-empty iovec.  This kicks-in in the case
Packit 6c4009
         where the whole packet fits into that iovec buffer.  */
Packit 6c4009
      if (data == NULL && message->msg_iov[i].iov_len > 0)
Packit 6c4009
	{
Packit 6c4009
	  data = message->msg_iov[i].iov_base;
Packit 6c4009
	  len = message->msg_iov[i].iov_len;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  buf = data;
Packit 6c4009
  if (err = HURD_DPORT_USE (fd, __socket_recv (port, &aport,
Packit 6c4009
					       flags, &data, &len,
Packit 6c4009
					       &ports, &nports,
Packit 6c4009
					       &cdata, &clen,
Packit 6c4009
					       &message->msg_flags, amount)))
Packit 6c4009
    return __hurd_sockfail (fd, flags, err);
Packit 6c4009
Packit 6c4009
  if (message->msg_name != NULL && aport != MACH_PORT_NULL)
Packit 6c4009
    {
Packit 6c4009
      char *buf = message->msg_name;
Packit 6c4009
      mach_msg_type_number_t buflen = message->msg_namelen;
Packit 6c4009
      int type;
Packit 6c4009
Packit 6c4009
      err = __socket_whatis_address (aport, &type, &buf, &buflen);
Packit 6c4009
      if (err == EOPNOTSUPP)
Packit 6c4009
	/* If the protocol server can't tell us the address, just return a
Packit 6c4009
	   zero-length one.  */
Packit 6c4009
	{
Packit 6c4009
	  buf = message->msg_name;
Packit 6c4009
	  buflen = 0;
Packit 6c4009
	  err = 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (err)
Packit 6c4009
	{
Packit 6c4009
	  __mach_port_deallocate (__mach_task_self (), aport);
Packit 6c4009
	  return __hurd_sockfail (fd, flags, err);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (message->msg_namelen > buflen)
Packit 6c4009
	message->msg_namelen = buflen;
Packit 6c4009
Packit 6c4009
      if (buf != message->msg_name)
Packit 6c4009
	{
Packit 6c4009
	  memcpy (message->msg_name, buf, message->msg_namelen);
Packit 6c4009
	  __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (buflen > 0)
Packit 6c4009
	((struct sockaddr *) message->msg_name)->sa_family = type;
Packit 6c4009
    }
Packit 6c4009
  else if (message->msg_name != NULL)
Packit 6c4009
    message->msg_namelen = 0;
Packit 6c4009
Packit 6c4009
  __mach_port_deallocate (__mach_task_self (), aport);
Packit 6c4009
Packit 6c4009
  if (buf == data)
Packit 6c4009
    buf += len;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Copy the data into MSG.  */
Packit 6c4009
      if (len > amount)
Packit 6c4009
	message->msg_flags |= MSG_TRUNC;
Packit 6c4009
      else
Packit 6c4009
	amount = len;
Packit 6c4009
Packit 6c4009
      buf = data;
Packit 6c4009
      for (i = 0; i < message->msg_iovlen; i++)
Packit 6c4009
	{
Packit 6c4009
#define min(a, b)	((a) > (b) ? (b) : (a))
Packit 6c4009
	  size_t copy = min (message->msg_iov[i].iov_len, amount);
Packit 6c4009
Packit 6c4009
	  memcpy (message->msg_iov[i].iov_base, buf, copy);
Packit 6c4009
Packit 6c4009
	  buf += copy;
Packit 6c4009
	  amount -= copy;
Packit 6c4009
	  if (len == 0)
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      __vm_deallocate (__mach_task_self (), (vm_address_t) data, len);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Copy the control message into MSG.  */
Packit 6c4009
  if (clen > message->msg_controllen)
Packit 6c4009
    message->msg_flags |= MSG_CTRUNC;
Packit 6c4009
  else
Packit 6c4009
    message->msg_controllen = clen;
Packit 6c4009
  memcpy (message->msg_control, cdata, message->msg_controllen);
Packit 6c4009
Packit 6c4009
  __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
Packit 6c4009
Packit 6c4009
  return (buf - data);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__libc_recvmsg, recvmsg)
Packit 6c4009
weak_alias (__libc_recvmsg, __recvmsg)