📄 ipv4.c
字号:
} /* If there is no source address, we use IP_ADD_MEMBERSHIP */ else { struct ip_mreq imr; imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr; if( psz_if_addr != NULL && *psz_if_addr && inet_addr(psz_if_addr) != INADDR_NONE ) { imr.imr_interface.s_addr = inet_addr(psz_if_addr); } else { imr.imr_interface.s_addr = INADDR_ANY; } if( psz_if_addr != NULL ) free( psz_if_addr ); msg_Dbg( p_this, "IP_ADD_MEMBERSHIP multicast request" ); /* Join Multicast group without source filter */ if( setsockopt( i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&imr, sizeof(struct ip_mreq) ) == -1 ) {#if defined(WIN32) || defined(UNDER_CE) msg_Err( p_this, "failed to join IP multicast group (%i)", WSAGetLastError() );#else msg_Err( p_this, "failed to join IP multicast group (%s)", strerror(errno) );#endif close( i_handle ); return( -1 ); } } }#endif if( *psz_server_addr ) { /* Build socket for remote connection */ if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 ) { msg_Warn( p_this, "cannot build remote address" ); close( i_handle ); return( -1 ); } /* Connect the socket */ if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == (-1) ) {#if defined(WIN32) || defined(UNDER_CE) msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());#else msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );#endif close( i_handle ); return( -1 ); }#if !defined( SYS_BEOS ) if( IN_MULTICAST( ntohl(inet_addr(psz_server_addr) ) ) ) { /* set the time-to-live */ int i_ttl = p_socket->i_ttl; unsigned char ttl; /* set the multicast interface */ char * psz_mif_addr = config_GetPsz( p_this, "miface-addr" ); if( psz_mif_addr ) { struct in_addr intf; intf.s_addr = inet_addr(psz_mif_addr); free( psz_mif_addr ); if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_IF, &intf, sizeof( intf ) ) < 0 ) { msg_Dbg( p_this, "failed to set multicast interface (%s).", strerror(errno) ); close( i_handle ); return ( -1 ); } } if( i_ttl < 1 ) { if( var_Get( p_this, "ttl", &val ) != VLC_SUCCESS ) { var_Create( p_this, "ttl", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); var_Get( p_this, "ttl", &val ); } i_ttl = val.i_int; } if( i_ttl < 1 ) i_ttl = 1; ttl = (unsigned char) i_ttl; /* There is some confusion in the world whether IP_MULTICAST_TTL * takes a byte or an int as an argument. * BSD seems to indicate byte so we are going with that and use * int as a fallback to be safe */ if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof( ttl ) ) < 0 ) { msg_Dbg( p_this, "failed to set ttl (%s). Let's try it " "the integer way.", strerror(errno) ); if( setsockopt( i_handle, IPPROTO_IP, IP_MULTICAST_TTL, &i_ttl, sizeof( i_ttl ) ) <0 ) { msg_Err( p_this, "failed to set ttl (%s)", strerror(errno) ); close( i_handle ); return( -1 ); } } }#endif } p_socket->i_handle = i_handle; if( var_Get( p_this, "mtu", &val ) != VLC_SUCCESS ) { var_Create( p_this, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); var_Get( p_this, "mtu", &val ); } p_socket->i_mtu = val.i_int; return( 0 );}/***************************************************************************** * SocketTCP: create a TCP socket ***************************************************************************** * This function returns -1 in case of error. *****************************************************************************/static int SocketTCP( vlc_object_t * p_this ){ int i_handle; /* Open a SOCK_STREAM (TCP) socket, in the PF_INET domain, automatic (0) * protocol */ if( (i_handle = socket( PF_INET, SOCK_STREAM, 0 )) == -1 ) {#if defined(WIN32) || defined(UNDER_CE) msg_Warn( p_this, "cannot create socket (%i)", WSAGetLastError() );#else msg_Warn( p_this, "cannot create socket (%s)", strerror(errno) );#endif return -1; } /* Set to non-blocking */#if defined( WIN32 ) || defined( UNDER_CE ) { unsigned long i_dummy = 1; if( ioctlsocket( i_handle, FIONBIO, &i_dummy ) != 0 ) { msg_Err( p_this, "cannot set socket to non-blocking mode" ); } }#else { int i_flags; if( ( i_flags = fcntl( i_handle, F_GETFL, 0 ) ) < 0 || fcntl( i_handle, F_SETFL, i_flags | O_NONBLOCK ) < 0 ) { msg_Err( p_this, "cannot set socket to non-blocking mode" ); } }#endif return i_handle;}/***************************************************************************** * OpenTCP: open a TCP socket ***************************************************************************** * psz_server_addr, i_server_port : address and port used for the connect() * system call. If i_server_port == 0, 80 is used. * Other parameters are ignored. * This function returns -1 in case of error. *****************************************************************************/static int OpenTCP( vlc_object_t * p_this, network_socket_t * p_socket ){ char * psz_server_addr = p_socket->psz_server_addr; int i_server_port = p_socket->i_server_port; int i_handle; struct sockaddr_in sock; if( i_server_port == 0 ) { i_server_port = 80; } if( (i_handle = SocketTCP( p_this )) == -1 ) return VLC_EGENERIC; /* Build remote address */ if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 ) { msg_Dbg( p_this, "could not build local address" ); goto error; } /* Connect the socket */ if( connect( i_handle, (struct sockaddr *) &sock, sizeof( sock ) ) == -1 ) {#if defined( WIN32 ) || defined( UNDER_CE ) if( WSAGetLastError() == WSAEWOULDBLOCK )#else if( errno == EINPROGRESS )#endif { int i_ret, i_opt, i_opt_size = sizeof( i_opt ), i_max_count; struct timeval timeout; vlc_value_t val; fd_set fds; if( var_Get( p_this, "ipv4-timeout", &val ) != VLC_SUCCESS ) { var_Create( p_this, "ipv4-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); var_Get( p_this, "ipv4-timeout", &val ); } i_max_count = val.i_int * 1000 / 100000 /* timeout.tv_usec */; msg_Dbg( p_this, "connection in progress" ); do { if( p_this->b_die || i_max_count <= 0 ) { msg_Dbg( p_this, "connection aborted" ); goto error; } i_max_count--; /* Initialize file descriptor set */ FD_ZERO( &fds ); FD_SET( i_handle, &fds ); /* We'll wait 0.1 second if nothing happens */ timeout.tv_sec = 0; timeout.tv_usec = 100000; } while( ( i_ret = select( i_handle + 1, NULL, &fds, NULL, &timeout ) ) == 0 ||#if defined( WIN32 ) || defined( UNDER_CE ) ( i_ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK ) );#else ( i_ret < 0 && errno == EINTR ) );#endif if( i_ret < 0 ) { msg_Warn( p_this, "cannot connect socket (select failed)" ); goto error; }#if !defined( SYS_BEOS ) && !defined( UNDER_CE ) if( getsockopt( i_handle, SOL_SOCKET, SO_ERROR, (void*)&i_opt, &i_opt_size ) == -1 || i_opt != 0 ) { msg_Warn( p_this, "cannot connect socket (SO_ERROR)" ); goto error; }#endif } else {#if defined(WIN32) || defined(UNDER_CE) msg_Warn( p_this, "cannot connect socket (%i)", WSAGetLastError());#else msg_Warn( p_this, "cannot connect socket (%s)", strerror(errno) );#endif goto error; } } p_socket->i_handle = i_handle; p_socket->i_mtu = 0; /* There is no MTU notion in TCP */ return VLC_SUCCESS;error: close( i_handle ); return VLC_EGENERIC;}/***************************************************************************** * ListenTCP: open a TCP passive socket (server-side) ***************************************************************************** * psz_server_addr, i_server_port : address and port used for the bind() * system call. If i_server_port == 0, 80 is used. * Other parameters are ignored. * This function returns -1 in case of error. *****************************************************************************/static int ListenTCP( vlc_object_t * p_this, network_socket_t * p_socket ){ char * psz_server_addr = p_socket->psz_server_addr; int i_server_port = p_socket->i_server_port; int i_handle, i_dummy = 1; struct sockaddr_in sock; if( (i_handle = SocketTCP( p_this )) == -1 ) return VLC_EGENERIC; if ( setsockopt( i_handle, SOL_SOCKET, SO_REUSEADDR, (void *)&i_dummy, sizeof( i_dummy ) ) == -1 ) { msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" ); } /* Build remote address */ if ( BuildAddr( &sock, psz_server_addr, i_server_port ) == -1 ) { msg_Dbg( p_this, "could not build local address" ); return VLC_EGENERIC; } /* Bind the socket */ if( bind( i_handle, (struct sockaddr *) &sock, sizeof( sock )) == -1 ) {#if defined(WIN32) || defined(UNDER_CE) msg_Err( p_this, "cannot bind socket (%i)", WSAGetLastError());#else msg_Err( p_this, "cannot bind socket (%s)", strerror(errno) );#endif goto error; } /* Listen */ if( listen( i_handle, 100 ) == -1 ) {#if defined(WIN32) || defined(UNDER_CE) msg_Err( p_this, "cannot bring socket in listening mode (%i)", WSAGetLastError());#else msg_Err( p_this, "cannot bring the socket in listening mode (%s)", strerror(errno) );#endif goto error; } p_socket->i_handle = i_handle; p_socket->i_mtu = 0; /* There is no MTU notion in TCP */ return VLC_SUCCESS;error: close( i_handle ); return VLC_EGENERIC;}/***************************************************************************** * NetOpen: wrapper around OpenUDP, ListenTCP and OpenTCP *****************************************************************************/static int NetOpen( vlc_object_t * p_this ){ network_socket_t * p_socket = p_this->p_private; if( p_socket->i_type == NETWORK_UDP ) { return OpenUDP( p_this, p_socket ); } else if( p_socket->i_type == NETWORK_TCP_PASSIVE ) { return ListenTCP( p_this, p_socket ); } else { return OpenTCP( p_this, p_socket ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -