Back to index

glibc  2.9
accept.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,1993,1994,1997,1999,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 <fcntl.h>
00021 #include <string.h>
00022 #include <sys/socket.h>
00023 #include <hurd.h>
00024 #include <hurd/fd.h>
00025 #include <hurd/socket.h>
00026 
00027 /* Await a connection on socket FD.
00028    When a connection arrives, open a new socket to communicate with it,
00029    set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
00030    peer and *ADDR_LEN to the address's actual length, and return the
00031    new socket's descriptor, or -1 for errors.  */
00032 int
00033 accept (fd, addrarg, addr_len)
00034       int fd;
00035       __SOCKADDR_ARG addrarg;
00036       socklen_t *addr_len;
00037 {
00038   error_t err;
00039   socket_t new;
00040   addr_port_t aport;
00041   struct sockaddr *addr = addrarg.__sockaddr__;
00042   char *buf = (char *) addr;
00043   mach_msg_type_number_t buflen;
00044   int type;
00045 
00046   if (err = HURD_DPORT_USE (fd, __socket_accept (port, &new, &aport)))
00047     return __hurd_dfail (fd, err);
00048 
00049   if (addr != NULL)
00050     {
00051       buflen = *addr_len;
00052       err = __socket_whatis_address (aport, &type, &buf, &buflen);
00053       if (err == EOPNOTSUPP)
00054        /* If the protocol server can't tell us the address, just return a
00055           zero-length one.  */
00056        {
00057          buf = (char *)addr;
00058          buflen = 0;
00059          err = 0;
00060        }
00061     }
00062   __mach_port_deallocate (__mach_task_self (), aport);
00063 
00064   if (err)
00065     {
00066       __mach_port_deallocate (__mach_task_self (), new);
00067       return __hurd_dfail (fd, err);
00068     }
00069 
00070   if (addr != NULL)
00071     {
00072       if (*addr_len > buflen)
00073        *addr_len = buflen;
00074       
00075       if (buf != (char *) addr)
00076        {
00077          memcpy (addr, buf, *addr_len);
00078          __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
00079        }
00080 
00081       if (buflen > 0)
00082        addr->sa_family = type;
00083     }
00084 
00085   return _hurd_intern_fd (new, O_IGNORE_CTTY, 1);
00086 }
00087 libc_hidden_def (accept)