+
+/*****************************************************************************
+ * __net_ConnectUDP:
+ *****************************************************************************
+ * Open a UDP socket to send data to a defined destination, with an optional
+ * hop limit.
+ *****************************************************************************/
+int __net_ConnectUDP( vlc_object_t *p_this, const char *psz_host, int i_port,
+ int hlim )
+{
+ struct addrinfo hints, *res, *ptr;
+ int i_val, i_handle = -1;
+ vlc_bool_t b_unreach = VLC_FALSE;
+
+ if( i_port == 0 )
+ i_port = 1234; /* historical VLC thing */
+
+ memset( &hints, 0, sizeof( hints ) );
+ hints.ai_socktype = SOCK_DGRAM;
+
+ msg_Dbg( p_this, "net: connecting to %s port %d", psz_host, i_port );
+
+ i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
+ if( i_val )
+ {
+ msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
+ vlc_gai_strerror( i_val ) );
+ return -1;
+ }
+
+ for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
+ {
+ int fd;
+
+ fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
+ ptr->ai_protocol );
+ if( fd == -1 )
+ continue;
+#if !defined( SYS_BEOS )
+ else
+ {
+ int i_val;
+
+ /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
+ * packet loss caused by scheduling problems */
+ i_val = 0x80000;
+ setsockopt( i_handle, SOL_SOCKET, SO_RCVBUF, (void *)&i_val,
+ sizeof( i_val ) );
+ i_val = 0x80000;
+ setsockopt( i_handle, SOL_SOCKET, SO_SNDBUF, (void *)&i_val,
+ sizeof( i_val ) );
+ }
+#endif
+
+ if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) == 0 )
+ {
+ /* success */
+ i_handle = fd;
+ break;
+ }
+
+#if defined( WIN32 ) || defined( UNDER_CE )
+ if( WSAGetLastError () == WSAENETUNREACH )
+#else
+ if( errno == ENETUNREACH )
+#endif
+ b_unreach = VLC_TRUE;
+ else
+ {
+ msg_Warn( p_this, "%s port %d : %s", psz_host, i_port,
+ strerror( errno ) );
+ net_Close( fd );
+ continue;
+ }
+ }
+
+ vlc_freeaddrinfo( res );
+
+ if( i_handle == -1 )
+ {
+ if( b_unreach )
+ msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
+ i_port );
+ return -1;
+ }
+
+ return i_handle;
+}
+