Back to index

glibc  2.9
connect.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,94,95,96,97,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 <hurd.h>
00021 #include <hurd/fd.h>
00022 #include <sys/socket.h>
00023 #include <hurd/socket.h>
00024 #include <sys/un.h>
00025 #include <hurd/ifsock.h>
00026 
00027 #undef __connect
00028 
00029 /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
00030    For connectionless socket types, just set the default address to send to
00031    and the only address from which to accept transmissions.
00032    Return 0 on success, -1 for errors.  */
00033 int
00034 __connect (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
00035 {
00036   error_t err;
00037   addr_port_t aport;
00038   const struct sockaddr_un *addr = addrarg.__sockaddr_un__;
00039 
00040   if (addr->sun_family == AF_LOCAL)
00041     {
00042       /* For the local domain, we must look up the name as a file and talk
00043         to it with the ifsock protocol.  */
00044       file_t file = __file_name_lookup (addr->sun_path, 0, 0);
00045       if (file == MACH_PORT_NULL)
00046        return -1;
00047       err = __ifsock_getsockaddr (file, &aport);
00048       __mach_port_deallocate (__mach_task_self (), file);
00049       if (err == MIG_BAD_ID || err == EOPNOTSUPP)
00050        /* The file did not grok the ifsock protocol.  */
00051        err = ENOTSOCK;
00052       if (err)
00053        return __hurd_fail (err);
00054     }
00055   else
00056     err = EIEIO;
00057 
00058   err = HURD_DPORT_USE (fd,
00059                      ({
00060                        if (err)
00061                          err = __socket_create_address (port,
00062                                                     addr->sun_family,
00063                                                     (char *) addr, len,
00064                                                     &aport);
00065                        if (! err)
00066                          {
00067                            err = __socket_connect (port, aport);
00068                            __mach_port_deallocate (__mach_task_self (),
00069                                                 aport);
00070                          }
00071                        err;
00072                      }));
00073 
00074   return err ? __hurd_dfail (fd, err) : 0;
00075 }
00076 
00077 INTDEF(__connect)
00078 weak_alias (__connect, connect)