Stop using snprintf in write_hex_to_fd (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 #include <sys/close_cb.h>
19 #include <ros/common.h>
20
21 /* bsd extensions */
22 #include <sys/uio.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25
26 #include <sys/plan9_helpers.h>
27
28 /* Create a new socket of type TYPE in domain DOMAIN, using
29    protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
30    Returns a file descriptor for the new socket, or -1 for errors.  */
31 int __socket(int domain, int type, int protocol)
32 {
33         Rock *r;
34         int cfd, n;
35         int pfd[2];
36         const char *net;
37         char msg[128];
38         static struct close_cb _sock_close_cb = {.func = _sock_fd_closed};
39
40         run_once(register_close_cb(&_sock_close_cb));
41
42         switch (domain) {
43                 case PF_INET:
44                         /* get a free network directory */
45                         switch (_sock_strip_opts(type)) {
46                                 case SOCK_DGRAM:
47                                         net = "udp";
48                                         cfd = open("/net/udp/clone", O_RDWR);
49                                         /* All BSD UDP sockets are in 'headers' mode, where each
50                                          * packet has the remote addr:port, local addr:port and
51                                          * other info. */
52                                         if (!(cfd < 0)) {
53                                                 n = snprintf(msg, sizeof(msg), "headers");
54                                                 n = write(cfd, msg, n);
55                                                 if (n < 0) {
56                                                         perror("UDP socket headers failed");
57                                                         return -1;
58                                                 }
59                                                 if (lseek(cfd, 0, SEEK_SET) != 0) {
60                                                         perror("UDP socket seek failed");
61                                                         return -1;
62                                                 }
63                                         }
64                                         break;
65                                 case SOCK_STREAM:
66                                         net = "tcp";
67                                         cfd = open("/net/tcp/clone", O_RDWR);
68                                         break;
69                                 default:
70                                         errno = EPROTONOSUPPORT;
71                                         return -1;
72                         }
73                         if (cfd < 0) {
74                                 return -1;
75                         }
76                         return _sock_data(cfd, net, domain, type, protocol, 0);
77                 case PF_UNIX:
78                         if (pipe(pfd) < 0) {
79                                 return -1;
80                         }
81                         r = _sock_newrock(pfd[0]);
82                         r->domain = domain;
83                         r->stype = _sock_strip_opts(type);
84                         r->sopts = _sock_get_opts(type);
85                         r->protocol = protocol;
86                         r->other = pfd[1];
87                         return pfd[0];
88                 default:
89                         errno = EPROTONOSUPPORT;
90                         return -1;
91         }
92 }
93
94 weak_alias(__socket, socket)