/*****************************************************************************
* io.c: network I/O functions
*****************************************************************************
- * Copyright (C) 2004-2005 the VideoLAN team
+ * Copyright (C) 2004-2005, 2007 the VideoLAN team
* Copyright © 2005-2006 Rémi Denis-Courmont
* $Id$
*
* Authors: Laurent Aimar <fenrir@videolan.org>
* Rémi Denis-Courmont <rem # videolan.org>
+ * Christophe Mutricy <xtophe at videolan dot org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
memset (&hints, 0, sizeof( hints ));
hints.ai_family = family;
hints.ai_socktype = socktype;
- hints.ai_protocol = protocol;
hints.ai_flags = AI_PASSIVE;
msg_Dbg (p_this, "net: listening to %s port %d", psz_host, i_port);
for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next)
{
int fd = net_Socket (p_this, ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
+ protocol ?: ptr->ai_protocol);
if (fd == -1)
{
msg_Dbg (p_this, "socket error: %s", net_strerror (net_errno));
net_Close (fd);
#if !defined(WIN32) && !defined(UNDER_CE)
fd = rootwrap_bind (ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol, ptr->ai_addr,
+ protocol ?: ptr->ai_protocol, ptr->ai_addr,
ptr->ai_addrlen);
if (fd != -1)
{
{
unsigned i;
ssize_t n;
-#ifdef HAVE_POLL
struct pollfd ufd[fdc];
-#else
- int maxfd = -1;
- fd_set set;
-#endif
int delay_ms = 500;
if ((wait_ms != -1) && (wait_ms < 500))
goto error;
}
-#ifdef HAVE_POLL
memset (ufd, 0, sizeof (ufd));
for( i = 0; i < fdc; i++ )
}
n = poll( ufd, fdc, delay_ms );
-#else
- FD_ZERO (&set);
-
- for( i = 0; i < fdc; i++ )
- {
-#if !defined(WIN32) && !defined(UNDER_CE)
- if( fdv[i] >= FD_SETSIZE )
- {
- /* We don't want to overflow select() fd_set */
- msg_Err( p_this, "select set overflow" );
- return -1;
- }
-#endif
- FD_SET( fdv[i], &set );
- if( fdv[i] > maxfd )
- maxfd = fdv[i];
- }
-
- n = select( maxfd + 1, &set, NULL, NULL,
- (wait_ms == -1) ? NULL
- : &(struct timeval){ 0, delay_ms * 1000 } );
-#endif
if( n == -1 )
goto error;
for (i = 0;; i++)
{
-#ifdef HAVE_POLL
if ((i_total > 0) && (ufd[i].revents & POLLERR))
return i_total; // error will be dequeued on next run
if ((ufd[i].revents & POLLIN) == 0)
-#else
- if (!FD_ISSET (fdv[i], &set))
continue;
-#endif
+
fdc = 1;
fdv += i;
vsv += i;
* If b_retry is true, then we repeat until we have read the right amount of
* data
*****************************************************************************/
-int __net_Read( vlc_object_t *restrict p_this, int fd,
- const v_socket_t *restrict p_vs,
- uint8_t *restrict p_data, int i_data, vlc_bool_t b_retry )
+ssize_t __net_Read( vlc_object_t *restrict p_this, int fd,
+ const v_socket_t *restrict p_vs,
+ uint8_t *restrict buf, size_t len, vlc_bool_t b_retry )
{
return net_ReadInner( p_this, 1, &(int){ fd },
- &(const v_socket_t *){ p_vs },
- p_data, i_data, -1, b_retry );
+ &(const v_socket_t *){ p_vs }, buf, len, -1,
+ b_retry );
}
*****************************************************************************
* Read from a network socket, non blocking mode (with timeout)
*****************************************************************************/
-int __net_ReadNonBlock( vlc_object_t *restrict p_this, int fd,
- const v_socket_t *restrict p_vs,
- uint8_t *restrict p_data, int i_data, mtime_t i_wait)
+ssize_t __net_ReadNonBlock( vlc_object_t *restrict p_this, int fd,
+ const v_socket_t *restrict p_vs,
+ uint8_t *restrict buf, size_t len, mtime_t i_wait)
{
return net_ReadInner (p_this, 1, &(int){ fd },
&(const v_socket_t *){ p_vs },
- p_data, i_data, i_wait / 1000, VLC_FALSE);
+ buf, len, i_wait / 1000, VLC_FALSE);
}
* Read from several sockets (with timeout). Takes data from the first socket
* that has some.
*****************************************************************************/
-int __net_Select( vlc_object_t *restrict p_this, const int *restrict pi_fd,
- const v_socket_t *const *restrict pp_vs,
- int i_fd, uint8_t *restrict p_data, int i_data,
- mtime_t i_wait )
+ssize_t __net_Select( vlc_object_t *restrict p_this,
+ const int *restrict fds, int nfd,
+ uint8_t *restrict buf, size_t len, mtime_t i_wait )
{
- if( pp_vs == NULL )
- {
- const v_socket_t *vsv[i_fd];
- memset( vsv, 0, sizeof (vsv) );
-
- return net_ReadInner( p_this, i_fd, pi_fd, vsv, p_data, i_data,
- i_wait / 1000, VLC_FALSE );
- }
+ const v_socket_t *vsv[nfd];
+ memset( vsv, 0, sizeof (vsv) );
- return net_ReadInner( p_this, i_fd, pi_fd, pp_vs, p_data, i_data,
+ return net_ReadInner( p_this, nfd, fds, vsv, buf, len,
i_wait / 1000, VLC_FALSE );
}
/* Write exact amount requested */
-int __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
- const uint8_t *p_data, int i_data )
+ssize_t __net_Write( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
+ const uint8_t *p_data, size_t i_data )
{
size_t i_total = 0;
while( i_data > 0 )
{
if( p_this->b_die )
- return i_total;
+ break;
-#ifdef HAVE_POLL
struct pollfd ufd[1];
memset (ufd, 0, sizeof (ufd));
ufd[0].fd = fd;
ufd[0].events = POLLOUT;
int val = poll (ufd, 1, 500);
- if ((val > 0) && (ufd[0].revents & POLLERR) && (i_total > 0))
- return i_total; // error will be dequeued separately on next call
-#else
- fd_set set;
- FD_ZERO (&set);
-
-#if !defined(WIN32) && !defined(UNDER_CE)
- if (fd >= FD_SETSIZE)
- {
- /* We don't want to overflow select() fd_set */
- msg_Err (p_this, "select set overflow");
- return -1;
- }
-#endif
- FD_SET (fd, &set);
-
- int val = select (fd + 1, NULL, &set, NULL,
- &(struct timeval){ 0, 500000 });
-#endif
switch (val)
{
case -1:
- if (errno != EINTR)
- {
- msg_Err (p_this, "Write error: %s",
- net_strerror (net_errno));
- return i_total ? (int)i_total : -1;
- }
+ msg_Err (p_this, "Write error: %s", net_strerror (net_errno));
+ goto out;
case 0:
continue;
}
+ if ((ufd[0].revents & POLLERR) && (i_total > 0))
+ return i_total; // error will be dequeued separately on next call
+
if (p_vs != NULL)
val = p_vs->pf_send (p_vs->p_sys, p_data, i_data);
else
#endif
if (val == -1)
- return i_total ? (int)i_total : -1;
+ {
+ msg_Err (p_this, "Write error: %s", net_strerror (net_errno));
+ break;
+ }
if (val == 0)
return i_total;
i_total += val;
}
- return i_total;
+out:
+ if ((i_total > 0) || (i_data == 0))
+ return i_total;
+
+ return -1;
}
char *__net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs )
return psz_line;
}
-int net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
- const char *psz_fmt, ... )
+ssize_t net_Printf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
+ const char *psz_fmt, ... )
{
int i_ret;
va_list args;
return i_ret;
}
-int __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
- const char *psz_fmt, va_list args )
+ssize_t __net_vaPrintf( vlc_object_t *p_this, int fd, const v_socket_t *p_vs,
+ const char *psz_fmt, va_list args )
{
char *psz;
int i_size, i_ret;
return 0;
}
#endif /* HAVE_INET_PTON */
+
+#ifndef HAVE_INET_NTOP
+#ifdef WIN32
+const char *inet_ntop(int af, const void * src,
+ char * dst, socklen_t cnt)
+{
+ switch( af )
+ {
+#ifdef AF_INET6
+ case AF_INET6:
+ {
+ struct sockaddr_in6 addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin6_family = AF_INET6;
+ addr.sin6_addr = *((struct in6_addr*)src);
+ if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,
+ sizeof(struct sockaddr_in6),
+ NULL, dst, &cnt) )
+ {
+ dst[cnt] = '\0';
+ return dst;
+ }
+ errno = WSAGetLastError();
+ return NULL;
+
+ }
+
+#endif
+ case AF_INET:
+ {
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr = *((struct in_addr*)src);
+ if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,
+ sizeof(struct sockaddr_in),
+ NULL, dst, &cnt) )
+ {
+ dst[cnt] = '\0';
+ return dst;
+ }
+ errno = WSAGetLastError();
+ return NULL;
+
+ }
+ }
+ errno = EAFNOSUPPORT;
+ return NULL;
+}
+#endif
+#endif