akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/getsockname.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9/* posix */
  10#include <sys/types.h>
  11#include <unistd.h>
  12#include <stdlib.h>
  13#include <stdio.h>
  14#include <fcntl.h>
  15#include <string.h>
  16#include <errno.h>
  17
  18/* bsd extensions */
  19#include <sys/uio.h>
  20#include <sys/socket.h>
  21#include <netinet/in.h>
  22#include <sys/un.h>
  23
  24#include <sys/plan9_helpers.h>
  25
  26/* Put the local address of FD into *ADDR and its length in *LEN.  */
  27int __getsockname(int fd, __SOCKADDR_ARG addr, socklen_t * __restrict alen)
  28{
  29        Rock *r;
  30        int i;
  31        struct sockaddr_in *lip;
  32        struct sockaddr_un *lunix;
  33
  34        r = _sock_findrock(fd, 0);
  35        if (r == 0) {
  36                errno = ENOTSOCK;
  37                return -1;
  38        }
  39
  40        switch (r->domain) {
  41        case PF_INET:
  42                lip = addr.__sockaddr_in__;
  43                _sock_ingetaddr(r, lip, alen, "local");
  44                break;
  45        case PF_UNIX:
  46                lunix = (struct sockaddr_un *)&r->addr;
  47                i = &lunix->sun_path[strlen(lunix->sun_path)] - (char *)lunix;
  48                memmove(addr.__sockaddr_un__, lunix, i);
  49                *alen = i;
  50                break;
  51        default:
  52                errno = EAFNOSUPPORT;
  53                return -1;
  54        }
  55        return 0;
  56}
  57
  58weak_alias(__getsockname, getsockname)
  59