demux: avi: PTSToByte remove useless casts and change type
[vlc.git] / src / posix / netconf.c
blobe2db67b386372dbaf742cf8c9298a27605b8779e
1 /*****************************************************************************
2 * netconf.c : Network configuration
3 *****************************************************************************
4 * Copyright (C) 2013 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 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <spawn.h>
33 #include <unistd.h>
35 extern char **environ;
37 #include <vlc_common.h>
38 #include <vlc_fs.h>
39 #include <vlc_network.h>
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
46 char *vlc_getProxyUrl(const char *url)
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];
55 if (vlc_pipe(fd))
56 return NULL;
58 if (posix_spawn_file_actions_init(&actions))
59 return NULL;
60 if (posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null",
61 O_RDONLY, 0644) ||
62 posix_spawn_file_actions_adddup2(&actions, fd[1], STDOUT_FILENO))
64 posix_spawn_file_actions_destroy(&actions);
65 return NULL;
68 posix_spawnattr_init(&attr);
70 sigset_t set;
72 sigemptyset(&set);
73 posix_spawnattr_setsigmask(&attr, &set);
74 sigaddset (&set, SIGPIPE);
75 posix_spawnattr_setsigdefault(&attr, &set);
76 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF
77 | POSIX_SPAWN_SETSIGMASK);
80 if (posix_spawnp(&pid, "proxy", &actions, &attr, argv, environ))
81 pid = -1;
83 posix_spawnattr_destroy(&attr);
84 posix_spawn_file_actions_destroy(&actions);
85 vlc_close(fd[1]);
87 if (pid != -1)
89 char buf[1024];
90 size_t len = 0;
94 ssize_t val = read(fd[0], buf + len, sizeof (buf) - len);
95 if (val <= 0)
96 break;
97 len += val;
99 while (len < sizeof (buf));
101 vlc_close(fd[0]);
102 while (waitpid(pid, &(int){ 0 }, 0) == -1);
104 if (len >= 9 && !strncasecmp(buf, "direct://", 9))
105 return NULL;
107 char *end = memchr(buf, '\n', len);
108 if (end != NULL)
110 *end = '\0';
111 return strdup(buf);
113 /* Parse error: fallback (may be due to missing executable) */
115 else
116 vlc_close(fd[0]);
118 /* Fallback to environment variable */
119 char *var = getenv("http_proxy");
120 if (var != NULL)
121 var = strdup(var);
122 return var;