Back to index

glibc  2.9
recvfrom.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1997, 1999, 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
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the 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; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <string.h>
00021 #include <sys/socket.h>
00022 #include <hurd.h>
00023 #include <hurd/fd.h>
00024 #include <hurd/socket.h>
00025 
00026 /* Read N bytes into BUF through socket FD.
00027    If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
00028    the sender, and store the actual size of the address in *ADDR_LEN.
00029    Returns the number of bytes read or -1 for errors.  */
00030 ssize_t
00031 __recvfrom (fd, buf, n, flags, addrarg, addr_len)
00032      int fd;
00033      void *buf;
00034      size_t n;
00035      int flags;
00036      __SOCKADDR_ARG addrarg;
00037      socklen_t *addr_len;
00038 {
00039   error_t err;
00040   mach_port_t addrport;
00041   char *bufp = buf;
00042   mach_msg_type_number_t nread = n;
00043   mach_port_t *ports;
00044   mach_msg_type_number_t nports = 0;
00045   char *cdata = NULL;
00046   mach_msg_type_number_t clen = 0;
00047   struct sockaddr *addr = addrarg.__sockaddr__;
00048 
00049   if (err = HURD_DPORT_USE (fd, __socket_recv (port, &addrport,
00050                                           flags, &bufp, &nread,
00051                                           &ports, &nports,
00052                                           &cdata, &clen,
00053                                           &flags,
00054                                           n)))
00055     return __hurd_sockfail (fd, flags, err);
00056 
00057   /* Get address data for the returned address port if requested.  */
00058   if (addr != NULL)
00059     {
00060       char *buf = (char *) addr;
00061       mach_msg_type_number_t buflen = *addr_len;
00062       int type;
00063 
00064       err = __socket_whatis_address (addrport, &type, &buf, &buflen);
00065       if (err == EOPNOTSUPP)
00066        /* If the protocol server can't tell us the address, just return a
00067           zero-length one.  */
00068        {
00069          buf = (char *)addr;
00070          buflen = 0;
00071          err = 0;
00072        }
00073 
00074       if (err)
00075        {
00076          __mach_port_deallocate (__mach_task_self (), addrport);
00077          return __hurd_sockfail (fd, flags, err);
00078        }
00079       
00080       if (*addr_len > buflen)
00081        *addr_len = buflen;
00082 
00083       if (buf != (char *) addr)
00084        {
00085          memcpy (addr, buf, *addr_len);
00086          __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
00087        }
00088 
00089       if (buflen > 0)
00090        addr->sa_family = type;
00091     }
00092 
00093   __mach_port_deallocate (__mach_task_self (), addrport);
00094 
00095   /* Toss control data; we don't care.  */
00096   __vm_deallocate (__mach_task_self (), (vm_address_t) cdata, clen);
00097 
00098   if (bufp != buf)
00099     {
00100       memcpy (buf, bufp, nread);
00101       __vm_deallocate (__mach_task_self (), (vm_address_t) bufp, nread);
00102     }
00103 
00104   return nread;
00105 }
00106 
00107 weak_alias (__recvfrom, recvfrom)