]> git.sesse.net Git - vlc/blob - src/posix/netconf.c
wasapi: audio capture client module (fixes #7205)
[vlc] / src / posix / netconf.c
1 /*****************************************************************************
2  * netconf.c : Network configuration
3  *****************************************************************************
4  * Copyright (C) 2013 RĂ©mi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <spawn.h>
33 #include <unistd.h>
34
35 extern char **environ;
36
37 #include <vlc_common.h>
38 #include <vlc_fs.h>
39 #include <vlc_network.h>
40
41 /**
42  * Determines the network proxy server to use (if any).
43  * @param url absolute URL for which to get the proxy server
44  * @return proxy URL, NULL if no proxy or error
45  */
46 char *vlc_getProxyUrl(const char *url)
47 {
48     /* libproxy helper */
49     pid_t pid;
50     posix_spawn_file_actions_t actions;
51     posix_spawnattr_t attr;
52     char *argv[3] = { (char *)"proxy", (char *)url, NULL };
53     int fd[2];
54
55     if (vlc_pipe(fd))
56         return NULL;
57
58     posix_spawn_file_actions_init(&actions);
59     posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null",
60                                      O_RDONLY, 0644);
61     posix_spawn_file_actions_adddup2(&actions, fd[1], STDOUT_FILENO);
62
63     posix_spawnattr_init(&attr);
64     {
65         sigset_t set;
66
67         sigemptyset(&set);
68         posix_spawnattr_setsigmask(&attr, &set);
69         sigaddset (&set, SIGPIPE);
70         posix_spawnattr_setsigdefault(&attr, &set);
71         posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF
72                                       | POSIX_SPAWN_SETSIGMASK);
73     }
74
75     if (posix_spawnp(&pid, "proxy", &actions, &attr, argv, environ))
76         pid = -1;
77
78     posix_spawnattr_destroy(&attr);
79     posix_spawn_file_actions_destroy(&actions);
80     close(fd[1]);
81
82     if (pid != -1)
83     {
84         char buf[1024];
85         size_t len = 0;
86
87         do
88         {
89              ssize_t val = read(fd[0], buf + len, sizeof (buf) - len);
90              if (val <= 0)
91                  break;
92              len += val;
93         }
94         while (len < sizeof (buf));
95
96         close(fd[0]);
97         while (waitpid(pid, &(int){ 0 }, 0) == -1);
98
99         if (len >= 9 && !strncasecmp(buf, "direct://", 9))
100             return NULL;
101
102         char *end = memchr(buf, '\n', len);
103         if (end != NULL)
104         {
105             *end = '\0';
106             return strdup(buf);
107         }
108         /* Parse error: fallback (may be due to missing executable) */
109     }
110     else
111         close(fd[0]);
112
113     /* Fallback to environment variable */
114     char *var = getenv("http_proxy");
115     if (var != NULL)
116         var = strdup(var);
117     return var;
118 }