2004-01-05 15:10:58 +01:00
|
|
|
/*****************************************************************************
|
2005-12-05 10:36:39 +01:00
|
|
|
* io.c: network I/O functions
|
2004-01-05 15:10:58 +01:00
|
|
|
*****************************************************************************
|
LGPL
Re-license almost all of libVLC and libVLCcore to LGPLv2.1+
This move was authorized by the developers, either:
- by e-mail,
- by vote at the VideoLAN Dev Days 2011,
- on the license website,
- in a contract, oral or written.
No objection was raised, so far.
The developers agreeing are:
Justus Piater
Alexis Ballier
Alexander Bethke
Mohammed Adnène Trojette
Alex Converse
Alexey Sokolov
Alexis de Lattre
Andre Pang
Anthony Loiseau
Cyril Deguet
André Weber
Boris Dorès
Brieuc Jeunhomme
Benjamin Drung
Hugo Beauzée-Luyssen
Benoit Steiner
Benjamin Pracht
Bernie Purcell
Przemyslaw Fiala
Arnaud de Bossoreille de Ribou
Brad Smith
Nick Briggs
Christopher Rath
Christophe Courtaut
Christopher Mueller
Clement Chesnin
Andres Krapf
Damien Fouilleul
David Flynn
Sebastien Zwickert
Antoine Cellerier
Jérôme Decoodt
Jérome Decoodt
Dylan Yudaken
Eduard Babayan
Eugenio Jarosiewicz
Elliot Murphy
Eric Petit
Erwan Tulou
Etienne Membrives
Ludovic Fauvet
Fabio Ritrovato
Tobias Güntner
Jakub Wieczorek
Frédéric Crozat
Francois Cartegnie
Laurent Aimar
Florian G. Pflug
Felix Paul Kühne
Frank Enderle
Rafaël Carré
Simon Latapie
Gildas Bazin
Geoffroy Couprie
Julien / Gellule
Gildas Bazin
Arnaud Schauly
Toralf Niebuhr
Vicente Jimenez Aguilar
Derk-Jan Hartman
Henri Fallon
Ilkka Ollakka
Olivier Teulière
Rémi Duraffort
Jakob Leben
Jean-Baptiste Kempf
Jean-Paul Saman
Jean-Philippe Grimaldi
Jean-François Massol
Gaël Hendryckx
Jakob Leben
Jean-Marc Dressler
Jai Menon
Johan Bilien
Johann Ransay
Joris van Rooij
JP Dinger
Jean-Philippe André
Adrien Grand
Juha Jeronen
Juho Vähä-Herttua
Kaarlo Raiha
Kaarlo Raiha
Kamil Baldyga
Keary Griffin
Ken Self
KO Myung-Hun
Pierre Ynard
Filippo Carone
Loïc Minier
Luca Barbato
Lucas C. Villa Real
Lukas Durfina
Adrien Maglo
Marc Ariberti
Mark Lee
Mark Moriarty
Martin Storsjö
Christophe Massiot
Michel Kaempf
Marian Ďurkovič
Mirsal Ennaime
Carlo Calabrò
Damien Lucas
Naohiro Koriyama
Basos G
Pierre Baillet
Vincent Penquerc'h
Olivier Aubert
Pankaj Yadav
Paul Corke
Pierre d'Herbemont
Philippe Morin
Antoine Lejeune
Michael Ploujnikov
Jean-Marc Dressler
Michael Hanselmann
Rafaël Carré
Ramiro Polla
Rémi Denis-Courmont
Renaud Dartus
Richard Shepherd
Faustino Osuna
Arnaud Vallat
Rob Jonson
Robert Jedrzejczyk
Steve Lhomme
Rocky Bernstein
Romain Goyet
Rov Juvano
Sam Hocevar
Martin T. H. Sandsmark
Sebastian Birk
Sébastien Escudier
Vincent Seguin
Fabio Ritrovato
Sigmund Augdal Helberg
Casian Andrei
Srikanth Raju
Hannes Domani
Stéphane Borel
Stephan Krempel
Stephan Assmus
Tony Castley
Pavlov Konstantin
Eric Petit
Tanguy Krotoff
Dennis van Amerongen
Michel Lespinasse
Can Wu
Xavier Marchesini
Sébastien Toque
Christophe Mutricy
Yoann Peronneau
Yohann Martineau
Yuval Tze
Scott Caudle
Clément Stenac
It is possible, that some minor piece of code was badly tracked, for
some reasons (SVN, mainly) or that some small developers did not answer.
However, as an "œuvre collective", defined as in "CPI 113-2 alinéa 3",
and seeing "Cour. Cass. 17 Mai 1978", and seeing that the editor and
the very vast majority of developers have agreed (> 99.99% of the code,
> 99% of developers), we are fine here.
2011-11-27 21:44:15 +01:00
|
|
|
* Copyright (C) 2004-2005, 2007 VLC authors and VideoLAN
|
2006-09-08 19:51:58 +02:00
|
|
|
* Copyright © 2005-2006 Rémi Denis-Courmont
|
2004-01-05 15:10:58 +01:00
|
|
|
*
|
|
|
|
* Authors: Laurent Aimar <fenrir@videolan.org>
|
2019-09-04 21:10:51 +02:00
|
|
|
* Rémi Denis-Courmont
|
2007-02-15 16:34:50 +01:00
|
|
|
* Christophe Mutricy <xtophe at videolan dot org>
|
2004-01-05 15:10:58 +01:00
|
|
|
*
|
LGPL
Re-license almost all of libVLC and libVLCcore to LGPLv2.1+
This move was authorized by the developers, either:
- by e-mail,
- by vote at the VideoLAN Dev Days 2011,
- on the license website,
- in a contract, oral or written.
No objection was raised, so far.
The developers agreeing are:
Justus Piater
Alexis Ballier
Alexander Bethke
Mohammed Adnène Trojette
Alex Converse
Alexey Sokolov
Alexis de Lattre
Andre Pang
Anthony Loiseau
Cyril Deguet
André Weber
Boris Dorès
Brieuc Jeunhomme
Benjamin Drung
Hugo Beauzée-Luyssen
Benoit Steiner
Benjamin Pracht
Bernie Purcell
Przemyslaw Fiala
Arnaud de Bossoreille de Ribou
Brad Smith
Nick Briggs
Christopher Rath
Christophe Courtaut
Christopher Mueller
Clement Chesnin
Andres Krapf
Damien Fouilleul
David Flynn
Sebastien Zwickert
Antoine Cellerier
Jérôme Decoodt
Jérome Decoodt
Dylan Yudaken
Eduard Babayan
Eugenio Jarosiewicz
Elliot Murphy
Eric Petit
Erwan Tulou
Etienne Membrives
Ludovic Fauvet
Fabio Ritrovato
Tobias Güntner
Jakub Wieczorek
Frédéric Crozat
Francois Cartegnie
Laurent Aimar
Florian G. Pflug
Felix Paul Kühne
Frank Enderle
Rafaël Carré
Simon Latapie
Gildas Bazin
Geoffroy Couprie
Julien / Gellule
Gildas Bazin
Arnaud Schauly
Toralf Niebuhr
Vicente Jimenez Aguilar
Derk-Jan Hartman
Henri Fallon
Ilkka Ollakka
Olivier Teulière
Rémi Duraffort
Jakob Leben
Jean-Baptiste Kempf
Jean-Paul Saman
Jean-Philippe Grimaldi
Jean-François Massol
Gaël Hendryckx
Jakob Leben
Jean-Marc Dressler
Jai Menon
Johan Bilien
Johann Ransay
Joris van Rooij
JP Dinger
Jean-Philippe André
Adrien Grand
Juha Jeronen
Juho Vähä-Herttua
Kaarlo Raiha
Kaarlo Raiha
Kamil Baldyga
Keary Griffin
Ken Self
KO Myung-Hun
Pierre Ynard
Filippo Carone
Loïc Minier
Luca Barbato
Lucas C. Villa Real
Lukas Durfina
Adrien Maglo
Marc Ariberti
Mark Lee
Mark Moriarty
Martin Storsjö
Christophe Massiot
Michel Kaempf
Marian Ďurkovič
Mirsal Ennaime
Carlo Calabrò
Damien Lucas
Naohiro Koriyama
Basos G
Pierre Baillet
Vincent Penquerc'h
Olivier Aubert
Pankaj Yadav
Paul Corke
Pierre d'Herbemont
Philippe Morin
Antoine Lejeune
Michael Ploujnikov
Jean-Marc Dressler
Michael Hanselmann
Rafaël Carré
Ramiro Polla
Rémi Denis-Courmont
Renaud Dartus
Richard Shepherd
Faustino Osuna
Arnaud Vallat
Rob Jonson
Robert Jedrzejczyk
Steve Lhomme
Rocky Bernstein
Romain Goyet
Rov Juvano
Sam Hocevar
Martin T. H. Sandsmark
Sebastian Birk
Sébastien Escudier
Vincent Seguin
Fabio Ritrovato
Sigmund Augdal Helberg
Casian Andrei
Srikanth Raju
Hannes Domani
Stéphane Borel
Stephan Krempel
Stephan Assmus
Tony Castley
Pavlov Konstantin
Eric Petit
Tanguy Krotoff
Dennis van Amerongen
Michel Lespinasse
Can Wu
Xavier Marchesini
Sébastien Toque
Christophe Mutricy
Yoann Peronneau
Yohann Martineau
Yuval Tze
Scott Caudle
Clément Stenac
It is possible, that some minor piece of code was badly tracked, for
some reasons (SVN, mainly) or that some small developers did not answer.
However, as an "œuvre collective", defined as in "CPI 113-2 alinéa 3",
and seeing "Cour. Cass. 17 Mai 1978", and seeing that the editor and
the very vast majority of developers have agreed (> 99.99% of the code,
> 99% of developers), we are fine here.
2011-11-27 21:44:15 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or
|
2004-01-05 15:10:58 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
LGPL
Re-license almost all of libVLC and libVLCcore to LGPLv2.1+
This move was authorized by the developers, either:
- by e-mail,
- by vote at the VideoLAN Dev Days 2011,
- on the license website,
- in a contract, oral or written.
No objection was raised, so far.
The developers agreeing are:
Justus Piater
Alexis Ballier
Alexander Bethke
Mohammed Adnène Trojette
Alex Converse
Alexey Sokolov
Alexis de Lattre
Andre Pang
Anthony Loiseau
Cyril Deguet
André Weber
Boris Dorès
Brieuc Jeunhomme
Benjamin Drung
Hugo Beauzée-Luyssen
Benoit Steiner
Benjamin Pracht
Bernie Purcell
Przemyslaw Fiala
Arnaud de Bossoreille de Ribou
Brad Smith
Nick Briggs
Christopher Rath
Christophe Courtaut
Christopher Mueller
Clement Chesnin
Andres Krapf
Damien Fouilleul
David Flynn
Sebastien Zwickert
Antoine Cellerier
Jérôme Decoodt
Jérome Decoodt
Dylan Yudaken
Eduard Babayan
Eugenio Jarosiewicz
Elliot Murphy
Eric Petit
Erwan Tulou
Etienne Membrives
Ludovic Fauvet
Fabio Ritrovato
Tobias Güntner
Jakub Wieczorek
Frédéric Crozat
Francois Cartegnie
Laurent Aimar
Florian G. Pflug
Felix Paul Kühne
Frank Enderle
Rafaël Carré
Simon Latapie
Gildas Bazin
Geoffroy Couprie
Julien / Gellule
Gildas Bazin
Arnaud Schauly
Toralf Niebuhr
Vicente Jimenez Aguilar
Derk-Jan Hartman
Henri Fallon
Ilkka Ollakka
Olivier Teulière
Rémi Duraffort
Jakob Leben
Jean-Baptiste Kempf
Jean-Paul Saman
Jean-Philippe Grimaldi
Jean-François Massol
Gaël Hendryckx
Jakob Leben
Jean-Marc Dressler
Jai Menon
Johan Bilien
Johann Ransay
Joris van Rooij
JP Dinger
Jean-Philippe André
Adrien Grand
Juha Jeronen
Juho Vähä-Herttua
Kaarlo Raiha
Kaarlo Raiha
Kamil Baldyga
Keary Griffin
Ken Self
KO Myung-Hun
Pierre Ynard
Filippo Carone
Loïc Minier
Luca Barbato
Lucas C. Villa Real
Lukas Durfina
Adrien Maglo
Marc Ariberti
Mark Lee
Mark Moriarty
Martin Storsjö
Christophe Massiot
Michel Kaempf
Marian Ďurkovič
Mirsal Ennaime
Carlo Calabrò
Damien Lucas
Naohiro Koriyama
Basos G
Pierre Baillet
Vincent Penquerc'h
Olivier Aubert
Pankaj Yadav
Paul Corke
Pierre d'Herbemont
Philippe Morin
Antoine Lejeune
Michael Ploujnikov
Jean-Marc Dressler
Michael Hanselmann
Rafaël Carré
Ramiro Polla
Rémi Denis-Courmont
Renaud Dartus
Richard Shepherd
Faustino Osuna
Arnaud Vallat
Rob Jonson
Robert Jedrzejczyk
Steve Lhomme
Rocky Bernstein
Romain Goyet
Rov Juvano
Sam Hocevar
Martin T. H. Sandsmark
Sebastian Birk
Sébastien Escudier
Vincent Seguin
Fabio Ritrovato
Sigmund Augdal Helberg
Casian Andrei
Srikanth Raju
Hannes Domani
Stéphane Borel
Stephan Krempel
Stephan Assmus
Tony Castley
Pavlov Konstantin
Eric Petit
Tanguy Krotoff
Dennis van Amerongen
Michel Lespinasse
Can Wu
Xavier Marchesini
Sébastien Toque
Christophe Mutricy
Yoann Peronneau
Yohann Martineau
Yuval Tze
Scott Caudle
Clément Stenac
It is possible, that some minor piece of code was badly tracked, for
some reasons (SVN, mainly) or that some small developers did not answer.
However, as an "œuvre collective", defined as in "CPI 113-2 alinéa 3",
and seeing "Cour. Cass. 17 Mai 1978", and seeing that the editor and
the very vast majority of developers have agreed (> 99.99% of the code,
> 99% of developers), we are fine here.
2011-11-27 21:44:15 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
2004-01-05 15:10:58 +01:00
|
|
|
*
|
LGPL
Re-license almost all of libVLC and libVLCcore to LGPLv2.1+
This move was authorized by the developers, either:
- by e-mail,
- by vote at the VideoLAN Dev Days 2011,
- on the license website,
- in a contract, oral or written.
No objection was raised, so far.
The developers agreeing are:
Justus Piater
Alexis Ballier
Alexander Bethke
Mohammed Adnène Trojette
Alex Converse
Alexey Sokolov
Alexis de Lattre
Andre Pang
Anthony Loiseau
Cyril Deguet
André Weber
Boris Dorès
Brieuc Jeunhomme
Benjamin Drung
Hugo Beauzée-Luyssen
Benoit Steiner
Benjamin Pracht
Bernie Purcell
Przemyslaw Fiala
Arnaud de Bossoreille de Ribou
Brad Smith
Nick Briggs
Christopher Rath
Christophe Courtaut
Christopher Mueller
Clement Chesnin
Andres Krapf
Damien Fouilleul
David Flynn
Sebastien Zwickert
Antoine Cellerier
Jérôme Decoodt
Jérome Decoodt
Dylan Yudaken
Eduard Babayan
Eugenio Jarosiewicz
Elliot Murphy
Eric Petit
Erwan Tulou
Etienne Membrives
Ludovic Fauvet
Fabio Ritrovato
Tobias Güntner
Jakub Wieczorek
Frédéric Crozat
Francois Cartegnie
Laurent Aimar
Florian G. Pflug
Felix Paul Kühne
Frank Enderle
Rafaël Carré
Simon Latapie
Gildas Bazin
Geoffroy Couprie
Julien / Gellule
Gildas Bazin
Arnaud Schauly
Toralf Niebuhr
Vicente Jimenez Aguilar
Derk-Jan Hartman
Henri Fallon
Ilkka Ollakka
Olivier Teulière
Rémi Duraffort
Jakob Leben
Jean-Baptiste Kempf
Jean-Paul Saman
Jean-Philippe Grimaldi
Jean-François Massol
Gaël Hendryckx
Jakob Leben
Jean-Marc Dressler
Jai Menon
Johan Bilien
Johann Ransay
Joris van Rooij
JP Dinger
Jean-Philippe André
Adrien Grand
Juha Jeronen
Juho Vähä-Herttua
Kaarlo Raiha
Kaarlo Raiha
Kamil Baldyga
Keary Griffin
Ken Self
KO Myung-Hun
Pierre Ynard
Filippo Carone
Loïc Minier
Luca Barbato
Lucas C. Villa Real
Lukas Durfina
Adrien Maglo
Marc Ariberti
Mark Lee
Mark Moriarty
Martin Storsjö
Christophe Massiot
Michel Kaempf
Marian Ďurkovič
Mirsal Ennaime
Carlo Calabrò
Damien Lucas
Naohiro Koriyama
Basos G
Pierre Baillet
Vincent Penquerc'h
Olivier Aubert
Pankaj Yadav
Paul Corke
Pierre d'Herbemont
Philippe Morin
Antoine Lejeune
Michael Ploujnikov
Jean-Marc Dressler
Michael Hanselmann
Rafaël Carré
Ramiro Polla
Rémi Denis-Courmont
Renaud Dartus
Richard Shepherd
Faustino Osuna
Arnaud Vallat
Rob Jonson
Robert Jedrzejczyk
Steve Lhomme
Rocky Bernstein
Romain Goyet
Rov Juvano
Sam Hocevar
Martin T. H. Sandsmark
Sebastian Birk
Sébastien Escudier
Vincent Seguin
Fabio Ritrovato
Sigmund Augdal Helberg
Casian Andrei
Srikanth Raju
Hannes Domani
Stéphane Borel
Stephan Krempel
Stephan Assmus
Tony Castley
Pavlov Konstantin
Eric Petit
Tanguy Krotoff
Dennis van Amerongen
Michel Lespinasse
Can Wu
Xavier Marchesini
Sébastien Toque
Christophe Mutricy
Yoann Peronneau
Yohann Martineau
Yuval Tze
Scott Caudle
Clément Stenac
It is possible, that some minor piece of code was badly tracked, for
some reasons (SVN, mainly) or that some small developers did not answer.
However, as an "œuvre collective", defined as in "CPI 113-2 alinéa 3",
and seeing "Cour. Cass. 17 Mai 1978", and seeing that the editor and
the very vast majority of developers have agreed (> 99.99% of the code,
> 99% of developers), we are fine here.
2011-11-27 21:44:15 +01:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
2004-01-05 15:10:58 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Preamble
|
|
|
|
*****************************************************************************/
|
2006-09-12 20:49:41 +02:00
|
|
|
|
2008-01-23 22:50:58 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2006-09-12 20:49:41 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2006-11-05 17:03:04 +01:00
|
|
|
#include <limits.h>
|
2004-10-04 15:34:42 +02:00
|
|
|
#include <errno.h>
|
2006-09-08 19:51:58 +02:00
|
|
|
#include <assert.h>
|
2004-01-05 15:10:58 +01:00
|
|
|
|
2013-12-29 15:15:46 +01:00
|
|
|
#include <unistd.h>
|
2018-11-09 21:49:12 +01:00
|
|
|
#ifdef HAVE_POLL
|
|
|
|
# include <poll.h>
|
|
|
|
#endif
|
2008-03-02 20:01:43 +01:00
|
|
|
#ifdef HAVE_LINUX_DCCP_H
|
|
|
|
/* TODO: use glibc instead of linux-kernel headers */
|
|
|
|
# include <linux/dccp.h>
|
|
|
|
# define SOL_DCCP 269
|
|
|
|
#endif
|
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
#include <vlc_common.h>
|
|
|
|
#include <vlc_network.h>
|
|
|
|
#include <vlc_interrupt.h>
|
2018-11-09 21:49:12 +01:00
|
|
|
#if defined (_WIN32)
|
|
|
|
# undef EINPROGRESS
|
|
|
|
# define EINPROGRESS WSAEWOULDBLOCK
|
|
|
|
# undef EWOULDBLOCK
|
|
|
|
# define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
# undef EAGAIN
|
|
|
|
# define EAGAIN WSAEWOULDBLOCK
|
|
|
|
#endif
|
2008-10-05 15:11:00 +02:00
|
|
|
|
2006-11-05 16:50:31 +01:00
|
|
|
extern int rootwrap_bind (int family, int socktype, int protocol,
|
|
|
|
const struct sockaddr *addr, size_t alen);
|
|
|
|
|
2006-10-19 17:05:35 +02:00
|
|
|
int net_Socket (vlc_object_t *p_this, int family, int socktype,
|
|
|
|
int protocol)
|
|
|
|
{
|
2010-03-31 18:08:15 +02:00
|
|
|
int fd = vlc_socket (family, socktype, protocol, true);
|
|
|
|
if (fd == -1)
|
2005-07-04 18:42:22 +02:00
|
|
|
{
|
2010-03-31 18:08:15 +02:00
|
|
|
if (net_errno != EAFNOSUPPORT)
|
2013-12-28 17:46:52 +01:00
|
|
|
msg_Err (p_this, "cannot create socket: %s",
|
|
|
|
vlc_strerror_c(net_errno));
|
2010-03-31 18:08:15 +02:00
|
|
|
return -1;
|
2005-07-04 18:42:22 +02:00
|
|
|
}
|
2006-10-19 17:05:35 +02:00
|
|
|
|
2009-11-08 11:38:41 +01:00
|
|
|
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
|
2005-07-04 18:42:22 +02:00
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
/*
|
2007-04-15 10:20:45 +02:00
|
|
|
* Accepts only IPv6 connections on IPv6 sockets.
|
2007-03-24 12:10:20 +01:00
|
|
|
* If possible, we should open two sockets, but it is not always possible.
|
2005-07-04 18:42:22 +02:00
|
|
|
*/
|
2006-10-19 17:05:35 +02:00
|
|
|
if (family == AF_INET6)
|
2007-04-15 10:20:45 +02:00
|
|
|
setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){ 1 }, sizeof (int));
|
2005-07-04 18:42:22 +02:00
|
|
|
#endif
|
|
|
|
|
2013-06-05 15:41:18 +02:00
|
|
|
#if defined (_WIN32)
|
2005-08-21 13:27:15 +02:00
|
|
|
# ifndef IPV6_PROTECTION_LEVEL
|
2006-11-11 17:14:37 +01:00
|
|
|
# warning Please update your C library headers.
|
2005-08-21 13:27:15 +02:00
|
|
|
# define IPV6_PROTECTION_LEVEL 23
|
2006-11-11 17:14:37 +01:00
|
|
|
# define PROTECTION_LEVEL_UNRESTRICTED 10
|
2005-08-21 13:27:15 +02:00
|
|
|
# endif
|
2006-10-19 17:05:35 +02:00
|
|
|
if (family == AF_INET6)
|
|
|
|
setsockopt (fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL,
|
|
|
|
&(int){ PROTECTION_LEVEL_UNRESTRICTED }, sizeof (int));
|
2005-07-04 18:42:22 +02:00
|
|
|
#endif
|
2006-10-19 17:05:35 +02:00
|
|
|
|
2008-03-02 20:01:43 +01:00
|
|
|
#ifdef DCCP_SOCKOPT_SERVICE
|
2008-03-04 19:56:33 +01:00
|
|
|
if (socktype == SOL_DCCP)
|
2008-03-02 20:01:43 +01:00
|
|
|
{
|
2010-11-10 17:41:38 +01:00
|
|
|
char *dccps = var_InheritString (p_this, "dccp-service");
|
2008-03-04 19:56:33 +01:00
|
|
|
if (dccps != NULL)
|
|
|
|
{
|
|
|
|
setsockopt (fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE, dccps,
|
|
|
|
(strlen (dccps) + 3) & ~3);
|
|
|
|
free (dccps);
|
|
|
|
}
|
2008-03-02 20:01:43 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-04 18:42:22 +02:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-11-09 21:49:12 +01:00
|
|
|
int (net_Connect)(vlc_object_t *obj, const char *host, int serv,
|
|
|
|
int type, int proto)
|
|
|
|
{
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_socktype = type,
|
|
|
|
.ai_protocol = proto,
|
|
|
|
.ai_flags = AI_NUMERICSERV | AI_IDN,
|
|
|
|
}, *res;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
int val = vlc_getaddrinfo_i11e(host, serv, &hints, &res);
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
msg_Err(obj, "cannot resolve %s port %d : %s", host, serv,
|
|
|
|
gai_strerror (val));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vlc_tick_t timeout = VLC_TICK_FROM_MS(var_InheritInteger(obj,
|
|
|
|
"ipv4-timeout"));
|
|
|
|
|
|
|
|
for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
|
|
|
|
{
|
|
|
|
int fd = net_Socket(obj, ptr->ai_family,
|
|
|
|
ptr->ai_socktype, ptr->ai_protocol);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
msg_Dbg(obj, "socket error: %s", vlc_strerror_c(net_errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(fd, ptr->ai_addr, ptr->ai_addrlen))
|
|
|
|
{
|
|
|
|
if (net_errno != EINPROGRESS && errno != EINTR)
|
|
|
|
{
|
|
|
|
msg_Err(obj, "connection failed: %s",
|
|
|
|
vlc_strerror_c(net_errno));
|
|
|
|
goto next_ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pollfd ufd;
|
|
|
|
vlc_tick_t deadline = VLC_TICK_INVALID;
|
|
|
|
|
|
|
|
ufd.fd = fd;
|
|
|
|
ufd.events = POLLOUT;
|
|
|
|
deadline = vlc_tick_now() + timeout;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
vlc_tick_t now = vlc_tick_now();
|
|
|
|
|
|
|
|
if (vlc_killed())
|
|
|
|
goto next_ai;
|
|
|
|
|
|
|
|
if (now > deadline)
|
|
|
|
now = deadline;
|
|
|
|
|
|
|
|
val = vlc_poll_i11e(&ufd, 1, MS_FROM_VLC_TICK(deadline - now));
|
|
|
|
}
|
|
|
|
while (val == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case -1: /* error */
|
|
|
|
msg_Err(obj, "polling error: %s",
|
|
|
|
vlc_strerror_c(net_errno));
|
|
|
|
goto next_ai;
|
|
|
|
|
|
|
|
case 0: /* timeout */
|
|
|
|
msg_Warn(obj, "connection timed out");
|
|
|
|
goto next_ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is NO WAY around checking SO_ERROR.
|
|
|
|
* Don't ifdef it out!!! */
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val,
|
|
|
|
&(socklen_t){ sizeof (val) }) || val)
|
|
|
|
{
|
|
|
|
msg_Err(obj, "connection failed: %s", vlc_strerror_c(val));
|
|
|
|
goto next_ai;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_Dbg(obj, "connection succeeded (socket = %d)", fd);
|
|
|
|
ret = fd; /* success! */
|
|
|
|
break;
|
|
|
|
|
|
|
|
next_ai: /* failure */
|
|
|
|
net_Close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-01-21 11:22:31 +01:00
|
|
|
|
2006-11-06 18:30:21 +01:00
|
|
|
int *net_Listen (vlc_object_t *p_this, const char *psz_host,
|
2018-07-11 14:48:56 +02:00
|
|
|
unsigned i_port, int type, int protocol)
|
2006-11-05 16:50:31 +01:00
|
|
|
{
|
2012-08-19 15:29:53 +02:00
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_socktype = type,
|
|
|
|
.ai_protocol = protocol,
|
2012-08-19 15:36:03 +02:00
|
|
|
.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_IDN,
|
2012-08-19 15:29:53 +02:00
|
|
|
}, *res;
|
2006-11-05 16:50:31 +01:00
|
|
|
|
2018-07-11 15:24:42 +02:00
|
|
|
msg_Dbg (p_this, "net: listening to %s port %u",
|
2011-08-02 18:09:43 +02:00
|
|
|
(psz_host != NULL) ? psz_host : "*", i_port);
|
2006-11-05 16:50:31 +01:00
|
|
|
|
2012-08-19 15:22:39 +02:00
|
|
|
int i_val = vlc_getaddrinfo (psz_host, i_port, &hints, &res);
|
2006-11-05 16:50:31 +01:00
|
|
|
if (i_val)
|
|
|
|
{
|
2018-07-11 15:24:42 +02:00
|
|
|
msg_Err (p_this, "Cannot resolve %s port %u : %s",
|
2011-08-02 18:09:43 +02:00
|
|
|
(psz_host != NULL) ? psz_host : "", i_port,
|
2010-04-28 22:48:30 +02:00
|
|
|
gai_strerror (i_val));
|
2006-11-05 16:50:31 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int *sockv = NULL;
|
|
|
|
unsigned sockc = 0;
|
|
|
|
|
|
|
|
for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
|
|
|
|
{
|
2010-02-01 22:13:26 +01:00
|
|
|
int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
|
|
|
|
ptr->ai_protocol);
|
2006-11-05 16:50:31 +01:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2013-12-28 17:46:52 +01:00
|
|
|
msg_Dbg (p_this, "socket error: %s", vlc_strerror_c(net_errno));
|
2006-11-05 16:50:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket */
|
|
|
|
if (bind (fd, ptr->ai_addr, ptr->ai_addrlen))
|
|
|
|
{
|
2017-09-16 15:14:49 +02:00
|
|
|
int err = net_errno;
|
2006-11-05 16:50:31 +01:00
|
|
|
net_Close (fd);
|
2013-06-05 15:41:18 +02:00
|
|
|
#if !defined(_WIN32)
|
2010-02-01 22:13:26 +01:00
|
|
|
fd = rootwrap_bind (ptr->ai_family, ptr->ai_socktype,
|
|
|
|
ptr->ai_protocol,
|
2009-05-17 10:16:03 +02:00
|
|
|
ptr->ai_addr, ptr->ai_addrlen);
|
2006-11-05 16:50:31 +01:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
msg_Dbg (p_this, "got socket %d from rootwrap", fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-09-16 15:14:49 +02:00
|
|
|
msg_Err (p_this, "socket bind error: %s", vlc_strerror_c(err));
|
2006-11-05 16:50:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen */
|
2018-11-10 10:41:30 +01:00
|
|
|
if (listen(fd, INT_MAX))
|
2006-11-05 16:50:31 +01:00
|
|
|
{
|
2018-11-10 10:41:30 +01:00
|
|
|
msg_Err(p_this, "socket listen error: %s",
|
|
|
|
vlc_strerror_c(net_errno));
|
|
|
|
net_Close(fd);
|
|
|
|
continue;
|
2006-11-05 16:50:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int *nsockv = (int *)realloc (sockv, (sockc + 2) * sizeof (int));
|
|
|
|
if (nsockv != NULL)
|
|
|
|
{
|
|
|
|
nsockv[sockc++] = fd;
|
|
|
|
sockv = nsockv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
net_Close (fd);
|
|
|
|
}
|
|
|
|
|
2010-04-28 22:50:08 +02:00
|
|
|
freeaddrinfo (res);
|
2006-11-05 16:50:31 +01:00
|
|
|
|
|
|
|
if (sockv != NULL)
|
|
|
|
sockv[sockc] = -1;
|
|
|
|
|
|
|
|
return sockv;
|
|
|
|
}
|
|
|
|
|
2018-11-09 21:49:12 +01:00
|
|
|
void net_ListenClose(int *fds)
|
|
|
|
{
|
|
|
|
if (fds != NULL)
|
|
|
|
{
|
|
|
|
for (int *p = fds; *p != -1; p++)
|
|
|
|
net_Close(*p);
|
|
|
|
|
|
|
|
free(fds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef net_Accept
|
|
|
|
int net_Accept(vlc_object_t *obj, int *fds)
|
|
|
|
{
|
|
|
|
assert(fds != NULL);
|
|
|
|
|
|
|
|
unsigned n = 0;
|
|
|
|
while (fds[n] != -1)
|
|
|
|
n++;
|
|
|
|
|
|
|
|
struct pollfd ufd[n];
|
|
|
|
/* Initialize file descriptor set */
|
|
|
|
for (unsigned i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
ufd[i].fd = fds[i];
|
|
|
|
ufd[i].events = POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while (poll(ufd, n, -1) == -1)
|
|
|
|
{
|
|
|
|
if (net_errno != EINTR)
|
|
|
|
{
|
|
|
|
msg_Err(obj, "poll error: %s", vlc_strerror_c(net_errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (ufd[i].revents == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int sfd = ufd[i].fd;
|
2018-11-09 21:52:57 +01:00
|
|
|
int fd = vlc_accept(sfd, NULL, NULL, true);
|
2018-11-09 21:49:12 +01:00
|
|
|
if (fd == -1)
|
2018-11-09 21:52:57 +01:00
|
|
|
{
|
|
|
|
if (net_errno != EAGAIN)
|
|
|
|
#if (EAGAIN != EWOULDBLOCK)
|
|
|
|
if (net_errno != EWOULDBLOCK)
|
|
|
|
#endif
|
|
|
|
msg_Err(obj, "accept failed (from socket %d): %s", sfd,
|
|
|
|
vlc_strerror_c(net_errno));
|
2018-11-09 21:49:12 +01:00
|
|
|
continue;
|
2018-11-09 21:52:57 +01:00
|
|
|
}
|
2018-11-09 21:49:12 +01:00
|
|
|
|
2018-11-09 21:52:57 +01:00
|
|
|
msg_Dbg(obj, "accepted socket %d (from socket %d)", fd, sfd);
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&(int){ 1 }, sizeof (int));
|
2018-11-09 21:49:12 +01:00
|
|
|
/*
|
|
|
|
* Move listening socket to the end to let the others in the
|
|
|
|
* set a chance next time.
|
|
|
|
*/
|
|
|
|
memmove(fds + i, fds + i + 1, n - (i + 1));
|
|
|
|
fds[n - 1] = sfd;
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
ssize_t (net_Read)(vlc_object_t *restrict obj, int fd,
|
|
|
|
void *restrict buf, size_t len)
|
2006-09-08 19:51:58 +02:00
|
|
|
{
|
2015-07-01 18:16:17 +02:00
|
|
|
size_t rd = 0;
|
2008-02-09 09:32:05 +01:00
|
|
|
|
2013-07-21 18:37:06 +02:00
|
|
|
do
|
2006-09-08 19:51:58 +02:00
|
|
|
{
|
2015-06-30 22:52:51 +02:00
|
|
|
if (vlc_killed())
|
2015-07-01 18:16:17 +02:00
|
|
|
{
|
|
|
|
vlc_testcancel();
|
|
|
|
errno = EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t val = vlc_recv_i11e(fd, buf, len, 0);
|
|
|
|
if (val < 0)
|
2006-09-08 19:51:58 +02:00
|
|
|
{
|
2015-07-01 18:16:17 +02:00
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
#ifdef _WIN32
|
|
|
|
else if (WSAGetLastError() == WSAEMSGSIZE) /* datagram too big */
|
2008-06-12 19:09:53 +02:00
|
|
|
{
|
2015-07-01 18:16:17 +02:00
|
|
|
msg_Warn(obj, "read truncated to %zu bytes", len);
|
|
|
|
val = len;
|
|
|
|
}
|
2013-07-21 18:37:06 +02:00
|
|
|
#endif
|
2015-07-01 18:16:17 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
msg_Err(obj, "read error: %s", vlc_strerror_c(errno));
|
|
|
|
return rd ? (ssize_t)rd : -1;
|
2008-06-12 19:09:53 +02:00
|
|
|
}
|
2013-07-21 18:37:06 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
rd += val;
|
2013-07-21 18:37:06 +02:00
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
if (val == 0)
|
|
|
|
break;
|
2006-09-08 19:51:58 +02:00
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
assert(len >= (size_t)val);
|
|
|
|
len -= val;
|
|
|
|
buf = ((char *)buf) + val;
|
2006-09-08 19:51:58 +02:00
|
|
|
}
|
2015-07-01 18:16:17 +02:00
|
|
|
while (len > 0);
|
2008-01-05 19:04:10 +01:00
|
|
|
|
2015-07-01 18:16:17 +02:00
|
|
|
return rd;
|
2006-09-08 19:51:58 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
ssize_t (net_Write)(vlc_object_t *obj, int fd, const void *buf, size_t len)
|
2004-01-05 15:10:58 +01:00
|
|
|
{
|
2015-06-30 22:52:51 +02:00
|
|
|
size_t written = 0;
|
2004-01-05 15:10:58 +01:00
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
do
|
2004-01-05 15:10:58 +01:00
|
|
|
{
|
2015-06-30 22:52:51 +02:00
|
|
|
if (vlc_killed())
|
2004-01-05 15:10:58 +01:00
|
|
|
{
|
2015-06-30 22:52:51 +02:00
|
|
|
vlc_testcancel();
|
|
|
|
errno = EINTR;
|
2008-06-12 19:09:53 +02:00
|
|
|
return -1;
|
2004-01-05 15:10:58 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
ssize_t val = vlc_send_i11e (fd, buf, len, MSG_NOSIGNAL);
|
2006-09-09 15:50:07 +02:00
|
|
|
if (val == -1)
|
2007-02-15 17:32:53 +01:00
|
|
|
{
|
2015-06-30 22:52:51 +02:00
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
2008-06-12 19:09:53 +02:00
|
|
|
continue;
|
2006-09-09 15:50:07 +02:00
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
msg_Err(obj, "write error: %s", vlc_strerror_c(errno));
|
|
|
|
return written ? (ssize_t)written : -1;
|
|
|
|
}
|
2006-09-09 15:50:07 +02:00
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
if (val == 0)
|
|
|
|
break;
|
2010-02-14 16:15:09 +01:00
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
written += val;
|
|
|
|
assert(len >= (size_t)val);
|
|
|
|
len -= val;
|
|
|
|
buf = ((const char *)buf) + val;
|
|
|
|
}
|
|
|
|
while (len > 0);
|
2007-02-15 17:32:53 +01:00
|
|
|
|
2015-06-30 22:52:51 +02:00
|
|
|
return written;
|
2004-01-05 15:10:58 +01:00
|
|
|
}
|