akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/listen.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#include <sys/stat.h>
  18#include <signal.h>
  19
  20/* socket extensions */
  21#include <sys/uio.h>
  22#include <sys/socket.h>
  23#include <netinet/in.h>
  24#include <sys/un.h>
  25
  26#include <sys/plan9_helpers.h>
  27
  28/* Prepare to accept connections on socket FD.
  29   N connection requests will be queued before further requests are refused.
  30   Returns 0 on success, -1 for errors.  */
  31int __listen(int fd, int backlog)
  32{
  33        Rock *r;
  34        int n;
  35        char msg[128];
  36        struct sockaddr_in *lip;
  37        struct sockaddr_un *lunix;
  38
  39        r = _sock_findrock(fd, 0);
  40        if (r == 0) {
  41                errno = ENOTSOCK;
  42                return -1;
  43        }
  44
  45        switch (r->domain) {
  46        case PF_INET:
  47                lip = (struct sockaddr_in *)&r->addr;
  48                if (lip->sin_port >= 0) {
  49                        if (write(r->ctl_fd, "bind 0", 6) < 0) {
  50                                errno = EINVAL; //EGREG;
  51                                return -1;
  52                        }
  53                        snprintf(msg, sizeof msg, "announce %d",
  54                                 ntohs(lip->sin_port));
  55                } else
  56                        strcpy(msg, "announce *");
  57                n = write(r->ctl_fd, msg, strlen(msg));
  58                if (n < 0) {
  59                        errno = EOPNOTSUPP;     /* Improve error reporting!!! */
  60                        return -1;
  61                }
  62                return 0;
  63        case PF_UNIX:
  64                if (r->other < 0) {
  65                        errno = EINVAL; //EGREG;
  66                        return -1;
  67                }
  68                lunix = (struct sockaddr_un *)&r->addr;
  69                if (_sock_srv(lunix->sun_path, r->other) < 0) {
  70                        r->other = -1;
  71                        return -1;
  72                }
  73                r->other = -1;
  74                return 0;
  75        default:
  76                errno = EAFNOSUPPORT;
  77                return -1;
  78        }
  79}
  80
  81weak_alias(__listen, listen)
  82