demux: mp4: set bitmap mask when possible
[vlc.git] / src / network / rootbind.c
blobf10c21955972ec846689fd172644ce07101d95cf
1 /*****************************************************************************
2 * rootbind.c: bind to reserved ports through the root wrapper
3 *****************************************************************************
4 * Copyright © 2005-2008 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #define _XPG4_2 /* ancillary data on Solaris */
27 #if !defined (_WIN32) && !defined (__OS2__)
28 # define ENABLE_ROOTWRAP 1
29 #endif
31 #include <stddef.h>
32 struct sockaddr;
33 int rootwrap_bind (int, int, int, const struct sockaddr *, size_t);
35 #include <errno.h>
37 #ifdef ENABLE_ROOTWRAP
39 #include <string.h>
40 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
46 #endif
47 #include <sys/uio.h>
48 #include <sys/un.h>
49 #include <netinet/in.h>
50 #include <pthread.h>
52 /* Required yet non-standard cmsg functions */
53 #ifndef CMSG_ALIGN
54 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
55 #endif
56 #ifndef CMSG_SPACE
57 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
58 #endif
59 #ifndef CMSG_LEN
60 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
61 #endif
62 #if !defined(MSG_NOSIGNAL)
63 /* If the other end of the pipe hangs up and MSG_NOSIGNAL is missing, the
64 * process will get a (likely fatal) SIGPIPE signal. Then again, the other end
65 * can screw us up in various ways already (e.g. not answer to deadlock). */
66 # define MSG_NOSIGNAL 0
67 #endif
69 #if defined(__OS2__) && !defined(ALIGN)
70 /* CMSG_NXTHDR requires this */
71 # define ALIGN(p) _ALIGN(p)
72 #endif
74 /**
75 * Receive a file descriptor from another process
77 static int recv_fd (int p)
79 struct msghdr hdr;
80 struct iovec iov;
81 struct cmsghdr *cmsg;
82 int val, fd;
83 char buf[CMSG_SPACE (sizeof (fd))];
85 hdr.msg_name = NULL;
86 hdr.msg_namelen = 0;
87 hdr.msg_iov = &iov;
88 hdr.msg_iovlen = 1;
89 hdr.msg_control = buf;
90 hdr.msg_controllen = sizeof (buf);
92 iov.iov_base = &val;
93 iov.iov_len = sizeof (val);
95 if (recvmsg (p, &hdr, 0) != sizeof (val))
96 return -1;
98 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
99 cmsg = CMSG_NXTHDR (&hdr, cmsg))
101 if ((cmsg->cmsg_level == SOL_SOCKET)
102 && (cmsg->cmsg_type == SCM_RIGHTS)
103 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
105 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
106 return fd;
110 errno = val;
111 return -1;
115 * Tries to obtain a bound TCP socket from the root process
117 int rootwrap_bind (int family, int socktype, int protocol,
118 const struct sockaddr *addr, size_t alen)
120 /* can't use libvlc */
121 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
122 struct sockaddr_storage ss;
123 int fd, sock = -1;
125 const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK");
126 if (sockenv != NULL)
127 sock = atoi (sockenv);
128 if (sock == -1)
130 errno = EACCES;
131 return -1;
134 switch (family)
136 case AF_INET:
137 if (alen < sizeof (struct sockaddr_in))
139 errno = EINVAL;
140 return -1;
142 break;
144 #ifdef AF_INET6
145 case AF_INET6:
146 if (alen < sizeof (struct sockaddr_in6))
148 errno = EINVAL;
149 return -1;
151 break;
152 #endif
154 default:
155 errno = EAFNOSUPPORT;
156 return -1;
159 if (family != addr->sa_family)
161 errno = EAFNOSUPPORT;
162 return -1;
165 /* Only TCP is implemented at the moment */
166 if ((socktype != SOCK_STREAM)
167 || (protocol && (protocol != IPPROTO_TCP)))
169 errno = EACCES;
170 return -1;
173 memset (&ss, 0, sizeof (ss));
174 memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen);
176 pthread_mutex_lock (&mutex);
177 if (send (sock, &ss, sizeof (ss), MSG_NOSIGNAL) != sizeof (ss))
179 pthread_mutex_unlock (&mutex);
180 return -1;
183 fd = recv_fd (sock);
184 pthread_mutex_unlock (&mutex);
185 return fd;
188 #else
189 int rootwrap_bind (int family, int socktype, int protocol,
190 const struct sockaddr *addr, size_t alen)
192 (void)family;
193 (void)socktype;
194 (void)protocol;
195 (void)addr;
196 (void)alen;
197 errno = EACCES;
198 return -1;
201 #endif /* ENABLE_ROOTWRAP */