Back to index

glibc  2.9
recvmsg.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public License as
00006    published by the Free Software Foundation; either version 2.1 of the
00007    License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.  */
00018 
00019 #include <errno.h>
00020 #include <string.h>
00021 #include <sys/socket.h>
00022 
00023 #include <hurd.h>
00024 #include <hurd/fd.h>
00025 #include <hurd/socket.h>
00026 
00027 /* Receive a message as described by MESSAGE from socket FD.
00028    Returns the number of bytes read or -1 for errors.  */
00029 ssize_t
00030 __libc_recvmsg (int fd, struct msghdr *message, int flags)
00031 {
00032   error_t err;
00033   addr_port_t aport;
00034   char *data = NULL;
00035   mach_msg_type_number_t len = 0;
00036   mach_port_t *ports;
00037   mach_msg_type_number_t nports = 0;
00038   char *cdata = NULL;
00039   mach_msg_type_number_t clen = 0;
00040   size_t amount;
00041   char *buf;
00042   int i;
00043 
00044   /* Find the total number of bytes to be read.  */
00045   amount = 0;
00046   for (i = 0; i < message->msg_iovlen; i++)
00047     {
00048       amount += message->msg_iov[i].iov_len;
00049 
00050       /* As an optimization, we set the initial values of DATA and LEN
00051          from the first non-empty iovec.  This kicks-in in the case
00052          where the whole packet fits into that iovec buffer.  */
00053       if (data == NULL && message->msg_iov[i].iov_len > 0)
00054        {
00055          data = message->msg_iov[i].iov_base;
00056          len = message->msg_iov[i].iov_len;
00057        }
00058     }
00059 
00060   buf = data;
00061   if (err = HURD_DPORT_USE (fd, __socket_recv (port, &aport,
00062                                           flags, &data, &len,
00063                                           &ports, &nports,
00064                                           &cdata, &clen,
00065                                           &message->msg_flags, amount)))
00066     return __hurd_sockfail (fd, flags, err);
00067 
00068   if (message->msg_name != NULL)
00069     {
00070       char *buf = message->msg_name;
00071       mach_msg_type_number_t buflen = message->msg_namelen;
00072       int type;
00073 
00074       err = __socket_whatis_address (aport, &type, &buf, &buflen);
00075       if (err == EOPNOTSUPP)
00076        /* If the protocol server can't tell us the address, just return a
00077           zero-length one.  */
00078        {
00079          buf = message->msg_name;
00080          buflen = 0;
00081          err = 0;
00082        }
00083 
00084       if (err)
00085        {
00086          __mach_port_deallocate (__mach_task_self (), aport);
00087          return __hurd_sockfail (fd, flags, err);
00088        }
00089 
00090       if (message->msg_namelen > buflen)
00091        message->msg_namelen = buflen;
00092 
00093       if (buf != message->msg_name)
00094        {
00095          memcpy (message->msg_name, buf, message->msg_namelen);
00096          __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
00097        }
00098 
00099       if (buflen > 0)
00100        ((struct sockaddr *) message->msg_name)->sa_family = type;
00101     }
00102 
00103   __mach_port_deallocate (__mach_task_self (), aport);
00104 
00105   if (buf == data)
00106     buf += len;
00107   else
00108     {
00109       /* Copy the data into MSG.  */
00110       if (len > amount)
00111        message->msg_flags |= MSG_TRUNC;
00112       else
00113        amount = len;
00114 
00115       buf = data;
00116       for (i = 0; i < message->msg_iovlen; i++)
00117        {
00118 #define min(a, b)    ((a) > (b) ? (b) : (a))
00119          size_t copy = min (message->msg_iov[i].iov_len, amount);
00120 
00121          memcpy (message->msg_iov[i].iov_base, buf, copy);
00122 
00123          buf += copy;
00124          amount -= copy;
00125          if (len == 0)
00126            break;
00127        }
00128 
00129       __vm_deallocate (__mach_task_self (), (vm_address_t) data, len);
00130     }
00131 
00132   /* Copy the control message into MSG.  */
00133   if (clen > message->msg_controllen)
00134     message->msg_flags |= MSG_CTRUNC;
00135   else
00136     message->msg_controllen = clen;
00137   memcpy (message->msg_control, cdata, message->msg_controllen);
00138 
00139   __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
00140 
00141   return (buf - data);
00142 }
00143 
00144 weak_alias (__libc_recvmsg, recvmsg)
00145 weak_alias (__libc_recvmsg, __recvmsg)