Pushes the Plan 9 BSD shims into glibc (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.19-akaros / sysdeps / akaros / socket.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 <errno.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18
19 /* bsd extensions */
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23
24 #include "plan9_sockets.h"
25
26 /* Create a new socket of type TYPE in domain DOMAIN, using
27    protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
28    Returns a file descriptor for the new socket, or -1 for errors.  */
29 int __socket(int domain, int stype, int protocol)
30 {
31         Rock *r;
32         int cfd, fd, n;
33         int pfd[2];
34         char *net;
35         char msg[128];
36
37         switch (domain) {
38                 case PF_INET:
39                         /* get a free network directory */
40                         switch (stype) {
41                                 case SOCK_DGRAM:
42                                         net = "udp";
43                                         cfd = open("/net/udp/clone", O_RDWR);
44                                         /* All BSD UDP sockets are in 'headers' mode, where each packet has
45                                          * the remote addr:port, local addr:port and other info. */
46                                         if (!(cfd < 0)) {
47                                                 n = snprintf(msg, sizeof(msg), "headers");
48                                                 n = write(cfd, msg, n);
49                                                 if (n < 0) {
50                                                         perror("UDP socket headers failed");
51                                                         return -1;
52                                                 }
53                                         }
54                                         break;
55                                 case SOCK_STREAM:
56                                         net = "tcp";
57                                         cfd = open("/net/tcp/clone", O_RDWR);
58                                         break;
59                                 default:
60                                         errno = EPROTONOSUPPORT;
61                                         return -1;
62                         }
63                         if (cfd < 0) {
64                                 return -1;
65                         }
66                         return _sock_data(cfd, net, domain, stype, protocol, 0);
67                 case PF_UNIX:
68                         if (pipe(pfd) < 0) {
69                                 return -1;
70                         }
71                         r = _sock_newrock(pfd[0]);
72                         r->domain = domain;
73                         r->stype = stype;
74                         r->protocol = protocol;
75                         r->other = pfd[1];
76                         return pfd[0];
77                 default:
78                         errno = EPROTONOSUPPORT;
79                         return -1;
80         }
81 }
82
83 weak_alias(__socket, socket)