Make changes to the bsd library to get it to built with netperf.
[akaros.git] / user / bsd / listen.c
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 "priv.h"
27
28 /*
29  * replace the fd with a pipe and start a process to
30  * accept calls in.  this is all to make select work.
31  * NO LONGER A PROC ON AKAROS.
32  */
33 static int
34 listenproc(Rock *r, int fd)
35 {
36         Rock *nr;
37         char *net;
38         int cfd, nfd, dfd;
39         struct stat d;
40         char *p;
41         char listen[Ctlsize];
42         char name[Ctlsize];
43
44         switch(r->stype){
45         case SOCK_DGRAM:
46                 net = "udp";
47                 break;
48         case SOCK_STREAM:
49                 net = "tcp";
50                 break;
51         }
52
53         strcpy(listen, r->ctl);
54         p = strrchr(listen, '/');
55         if(p == 0)
56                 return -1;
57         strcpy(p+1, "listen");
58
59         /* start listening process */
60                 cfd = open(listen, O_RDWR);
61                 if(cfd < 0)
62                         return -1;
63
64                 dfd = _sock_data(cfd, net, r->domain, r->stype, r->protocol, &nr);
65                 if(dfd < 0)
66                         return -1;
67
68                 return fd;
69
70 }
71
72 int
73 listen(fd, backlog)
74         int fd;
75         int backlog;
76 {
77         Rock *r;
78         int n, cfd;
79         char msg[128];
80         struct sockaddr_in *lip;
81         struct sockaddr_un *lunix;
82
83         r = _sock_findrock(fd, 0);
84         if(r == 0){
85                 errno = ENOTSOCK;
86                 return -1;
87         }
88
89         switch(r->domain){
90         case PF_INET:
91                 cfd = open(r->ctl, O_RDWR);
92                 if(cfd < 0){
93                         errno = EBADF;
94                         return -1;
95                 }
96                 lip = (struct sockaddr_in*)&r->addr;
97                 if(lip->sin_port >= 0) {
98                         if(write(cfd, "bind 0", 6) < 0) {
99                                 errno = EINVAL; //EGREG;
100                                 close(cfd);
101                                 return -1;
102                         }
103                         snprintf(msg, sizeof msg, "announce %d",
104                                 ntohs(lip->sin_port));
105                 }
106                 else
107                         strcpy(msg, "announce *");
108                 n = write(cfd, msg, strlen(msg));
109                 if(n < 0){
110                         errno = EOPNOTSUPP;     /* Improve error reporting!!! */
111                         close(cfd);
112                         return -1;
113                 }
114                 close(cfd);
115
116                 return fd;
117         case PF_UNIX:
118                 if(r->other < 0){
119                         errno = EINVAL;//EGREG;
120                         return -1;
121                 }
122                 lunix = (struct sockaddr_un*)&r->addr;
123                 if(_sock_srv(lunix->sun_path, r->other) < 0){
124                         r->other = -1;
125                         return -1;
126                 }
127                 r->other = -1;
128                 return 0;
129         default:
130                 errno = EAFNOSUPPORT;
131                 return -1;
132         }
133 }